WeChatPushMessageTemplate.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. package com.java110.job.task.wechat;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.java110.core.annotation.Java110Synchronized;
  6. import com.java110.core.factory.GenerateCodeFactory;
  7. import com.java110.core.factory.Java110ThreadPoolFactory;
  8. import com.java110.core.factory.WechatFactory;
  9. import com.java110.core.log.LoggerFactory;
  10. import com.java110.core.smo.ISaveTransactionLogSMO;
  11. import com.java110.dto.RoomDto;
  12. import com.java110.dto.app.AppDto;
  13. import com.java110.dto.community.CommunityDto;
  14. import com.java110.dto.logSystemError.LogSystemErrorDto;
  15. import com.java110.dto.notice.NoticeDto;
  16. import com.java110.dto.owner.OwnerAppUserDto;
  17. import com.java110.dto.owner.OwnerRoomRelDto;
  18. import com.java110.dto.smallWeChat.SmallWeChatDto;
  19. import com.java110.dto.smallWechatAttr.SmallWechatAttrDto;
  20. import com.java110.dto.task.TaskDto;
  21. import com.java110.dto.wechatSubscribe.WechatSubscribeDto;
  22. import com.java110.entity.wechat.Content;
  23. import com.java110.entity.wechat.Data;
  24. import com.java110.entity.wechat.Miniprogram;
  25. import com.java110.entity.wechat.PropertyFeeTemplateMessage;
  26. import com.java110.intf.community.INoticeInnerServiceSMO;
  27. import com.java110.intf.community.IRoomInnerServiceSMO;
  28. import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
  29. import com.java110.intf.store.ISmallWechatAttrInnerServiceSMO;
  30. import com.java110.intf.user.IOwnerAppUserInnerServiceSMO;
  31. import com.java110.intf.user.IOwnerRoomRelInnerServiceSMO;
  32. import com.java110.intf.user.IWechatSubscribeV1InnerServiceSMO;
  33. import com.java110.job.quartz.TaskSystemQuartz;
  34. import com.java110.po.logSystemError.LogSystemErrorPo;
  35. import com.java110.po.transactionLog.TransactionLogPo;
  36. import com.java110.po.wechatSubscribe.WechatSubscribePo;
  37. import com.java110.service.smo.ISaveSystemErrorSMO;
  38. import com.java110.utils.cache.MappingCache;
  39. import com.java110.utils.constant.WechatConstant;
  40. import com.java110.utils.util.DateUtil;
  41. import com.java110.utils.util.ExceptionUtil;
  42. import com.java110.utils.util.StringUtil;
  43. import org.slf4j.Logger;
  44. import org.springframework.beans.factory.annotation.Autowired;
  45. import org.springframework.http.HttpStatus;
  46. import org.springframework.http.ResponseEntity;
  47. import org.springframework.stereotype.Component;
  48. import org.springframework.web.client.RestTemplate;
  49. import java.util.ArrayList;
  50. import java.util.Date;
  51. import java.util.List;
  52. /**
  53. * @program: MicroCommunity
  54. * @description: 微信公众号主动推送信息
  55. * @author: wuxw
  56. * @create: 2020-06-15 13:35
  57. **/
  58. @Component
  59. public class WeChatPushMessageTemplate extends TaskSystemQuartz {
  60. private static Logger logger = LoggerFactory.getLogger(WeChatPushMessageTemplate.class);
  61. public static final int DEFAULT_THREAD_NUM = 20;
  62. public static final int DEFAULT_SUBSCRIBE_PERSON = 100;
  63. @Autowired
  64. private INoticeInnerServiceSMO noticeInnerServiceSMOImpl;
  65. @Autowired
  66. private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
  67. @Autowired
  68. private ISmallWechatAttrInnerServiceSMO smallWechatAttrInnerServiceSMOImpl;
  69. @Autowired
  70. private IOwnerAppUserInnerServiceSMO ownerAppUserInnerServiceSMOImpl;
  71. @Autowired
  72. private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
  73. @Autowired
  74. private IOwnerRoomRelInnerServiceSMO ownerRoomRelInnerServiceSMOImpl;
  75. @Autowired
  76. private ISaveTransactionLogSMO saveTransactionLogSMOImpl;
  77. @Autowired
  78. private RestTemplate outRestTemplate;
  79. @Autowired
  80. private ISaveSystemErrorSMO saveSystemErrorSMOImpl;
  81. @Autowired
  82. private IWechatSubscribeV1InnerServiceSMO wechatSubscribeV1InnerServiceSMOImpl;
  83. //模板信息推送地址
  84. private static String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
  85. private static String getUser = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN";
  86. private Java110ThreadPoolFactory<PublicWeChatPushMessageTemplate> publicWeChatPushMessageTemplateJava110ThreadPoolFactory = null;
  87. @Override
  88. protected void process(TaskDto taskDto) {
  89. logger.debug("开始执行微信模板信息推送" + taskDto.toString());
  90. //创建连接池
  91. publicWeChatPushMessageTemplateJava110ThreadPoolFactory = Java110ThreadPoolFactory.getInstance().createThreadPool(DEFAULT_THREAD_NUM);
  92. // 获取小区
  93. List<CommunityDto> communityDtos = getAllCommunity();
  94. for (CommunityDto communityDto : communityDtos) {
  95. try {
  96. publishMsg(taskDto, communityDto);
  97. } catch (Exception e) {
  98. LogSystemErrorPo logSystemErrorPo = new LogSystemErrorPo();
  99. logSystemErrorPo.setErrId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_errId));
  100. logSystemErrorPo.setErrType(LogSystemErrorDto.ERR_TYPE_NOTICE);
  101. logSystemErrorPo.setMsg(ExceptionUtil.getStackTrace(e));
  102. saveSystemErrorSMOImpl.saveLog(logSystemErrorPo);
  103. logger.error("推送消息失败", e);
  104. }
  105. }
  106. publicWeChatPushMessageTemplateJava110ThreadPoolFactory.stop();
  107. }
  108. private void publishMsg(TaskDto taskDto, CommunityDto communityDto) throws Exception {
  109. //查询公众号配置
  110. SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
  111. smallWeChatDto.setWeChatType("1100");
  112. smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
  113. smallWeChatDto.setObjId(communityDto.getCommunityId());
  114. List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
  115. if (smallWeChatDto == null || smallWeChatDtos.size() <= 0) {
  116. logger.info("未配置微信公众号信息,定时任务执行结束");
  117. return;
  118. }
  119. SmallWeChatDto weChatDto = smallWeChatDtos.get(0);
  120. SmallWechatAttrDto smallWechatAttrDto = new SmallWechatAttrDto();
  121. smallWechatAttrDto.setCommunityId(communityDto.getCommunityId());
  122. smallWechatAttrDto.setWechatId(weChatDto.getWeChatId());
  123. smallWechatAttrDto.setSpecCd(SmallWechatAttrDto.SPEC_CD_WECHAT_TEMPLATE);
  124. List<SmallWechatAttrDto> smallWechatAttrDtos = smallWechatAttrInnerServiceSMOImpl.querySmallWechatAttrs(smallWechatAttrDto);
  125. if (smallWechatAttrDtos == null || smallWechatAttrDtos.size() <= 0) {
  126. logger.info("未配置微信公众号消息模板");
  127. return;
  128. }
  129. String templateId = smallWechatAttrDtos.get(0).getValue();
  130. String accessToken = WechatFactory.getAccessToken(weChatDto.getAppId(), weChatDto.getAppSecret());
  131. if (accessToken == null || accessToken == "") {
  132. logger.info("推送微信模板,获取accessToken失败:{}", accessToken);
  133. return;
  134. }
  135. //
  136. List<NoticeDto> noticeDtos = getNotices(communityDto.getCommunityId());
  137. if (noticeDtos == null || noticeDtos.size() < 1) {
  138. return;
  139. }
  140. for (NoticeDto tmpNotice : noticeDtos) {
  141. try {
  142. doSentWechat(tmpNotice, templateId, accessToken, weChatDto);
  143. } catch (Exception e) {
  144. LogSystemErrorPo logSystemErrorPo = new LogSystemErrorPo();
  145. logSystemErrorPo.setErrId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_errId));
  146. logSystemErrorPo.setErrType(LogSystemErrorDto.ERR_TYPE_NOTICE);
  147. logSystemErrorPo.setMsg(ExceptionUtil.getStackTrace(e));
  148. saveSystemErrorSMOImpl.saveLog(logSystemErrorPo);
  149. logger.error("通知异常", e);
  150. }
  151. }
  152. }
  153. @Java110Synchronized(value = "communityId")
  154. private List<NoticeDto> getNotices(String communityId) {
  155. NoticeDto noticeDto = new NoticeDto();
  156. noticeDto.setCommunityId(communityId);
  157. noticeDto.setPage(1);
  158. noticeDto.setRow(50);
  159. noticeDto.setState(NoticeDto.STATE_WAIT);
  160. noticeDto.setNoticeTypeCd(NoticeDto.NOTICE_TYPE_OWNER_WECHAT);
  161. List<NoticeDto> noticeDtos = noticeInnerServiceSMOImpl.queryNotices(noticeDto);
  162. //更新为发送中
  163. for (NoticeDto noticeDto1 : noticeDtos) {
  164. noticeDto = new NoticeDto();
  165. noticeDto.setNoticeId(noticeDto1.getNoticeId());
  166. noticeDto.setState(NoticeDto.STATE_DOING);
  167. noticeDto.setCommunityId(communityId);
  168. noticeInnerServiceSMOImpl.updateNotice(noticeDto);
  169. }
  170. return noticeDtos;
  171. }
  172. private void doSentWechat(NoticeDto noticeDto, String templateId, String accessToken, SmallWeChatDto weChatDto) throws Exception {
  173. // Date startTime = DateUtil.getDateFromString(noticeDto.getStartTime(), DateUtil.DATE_FORMATE_STRING_A);
  174. // Date nowTime = DateUtil.getCurrentDate();
  175. // if (startTime.getTime() > nowTime.getTime()) { //还没有到时间
  176. // return;
  177. // }
  178. String objType = noticeDto.getObjType();
  179. switch (objType) {
  180. case NoticeDto.OBJ_TYPE_ALL:
  181. sendAllOwner(noticeDto, templateId, accessToken, weChatDto);
  182. break;
  183. case NoticeDto.OBJ_TYPE_FLOOR:
  184. sendFloorOwner(noticeDto, templateId, accessToken, weChatDto);
  185. break;
  186. case NoticeDto.OBJ_TYPE_UNIT:
  187. sendUnitOwner(noticeDto, templateId, accessToken, weChatDto);
  188. break;
  189. case NoticeDto.OBJ_TYPE_ROOM:
  190. sendRoomOwner(noticeDto, templateId, accessToken, weChatDto);
  191. break;
  192. case NoticeDto.OBJ_TYPE_COMMUNITY:
  193. sendCommunityOwner(noticeDto, templateId, accessToken, weChatDto);
  194. break;
  195. }
  196. NoticeDto tmpNoticeDto = new NoticeDto();
  197. tmpNoticeDto.setNoticeId(noticeDto.getNoticeId());
  198. tmpNoticeDto.setState(NoticeDto.STATE_FINISH);
  199. noticeInnerServiceSMOImpl.updateNotice(tmpNoticeDto);
  200. }
  201. private void sendCommunityOwner(NoticeDto noticeDto, String templateId, String accessToken, SmallWeChatDto weChatDto) {
  202. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  203. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  204. ownerAppUserDto.setCommunityId(noticeDto.getCommunityId());
  205. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  206. doSend(ownerAppUserDtos, noticeDto, templateId, accessToken, weChatDto);
  207. }
  208. private void sendFloorOwner(NoticeDto noticeDto, String templateId, String accessToken, SmallWeChatDto weChatDto) {
  209. RoomDto roomDto = new RoomDto();
  210. roomDto.setCommunityId(noticeDto.getCommunityId());
  211. roomDto.setFloorId(noticeDto.getObjId());
  212. List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
  213. for (RoomDto tmpRoomDto : roomDtos) {
  214. if (!RoomDto.STATE_SELL.equals(tmpRoomDto.getState())) {
  215. continue;
  216. }
  217. OwnerRoomRelDto ownerRoomRelDto = new OwnerRoomRelDto();
  218. ownerRoomRelDto.setRoomId(tmpRoomDto.getRoomId());
  219. List<OwnerRoomRelDto> ownerRoomRelDtos = ownerRoomRelInnerServiceSMOImpl.queryOwnerRoomRels(ownerRoomRelDto);
  220. if (ownerRoomRelDtos == null || ownerRoomRelDtos.size() < 1) {
  221. continue;
  222. }
  223. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  224. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  225. ownerAppUserDto.setMemberId(ownerRoomRelDtos.get(0).getOwnerId());
  226. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  227. doSend(ownerAppUserDtos, noticeDto, templateId, accessToken, weChatDto);
  228. }
  229. }
  230. private void sendUnitOwner(NoticeDto noticeDto, String templateId, String accessToken, SmallWeChatDto weChatDto) {
  231. RoomDto roomDto = new RoomDto();
  232. roomDto.setCommunityId(noticeDto.getCommunityId());
  233. roomDto.setUnitId(noticeDto.getObjId());
  234. List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
  235. for (RoomDto tmpRoomDto : roomDtos) {
  236. if (!RoomDto.STATE_SELL.equals(tmpRoomDto.getState())) {
  237. continue;
  238. }
  239. OwnerRoomRelDto ownerRoomRelDto = new OwnerRoomRelDto();
  240. ownerRoomRelDto.setRoomId(tmpRoomDto.getRoomId());
  241. List<OwnerRoomRelDto> ownerRoomRelDtos = ownerRoomRelInnerServiceSMOImpl.queryOwnerRoomRels(ownerRoomRelDto);
  242. if (ownerRoomRelDtos == null || ownerRoomRelDtos.size() < 1) {
  243. continue;
  244. }
  245. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  246. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  247. ownerAppUserDto.setMemberId(ownerRoomRelDtos.get(0).getOwnerId());
  248. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  249. doSend(ownerAppUserDtos, noticeDto, templateId, accessToken, weChatDto);
  250. }
  251. }
  252. private void sendRoomOwner(NoticeDto noticeDto, String templateId, String accessToken, SmallWeChatDto weChatDto) {
  253. RoomDto roomDto = new RoomDto();
  254. roomDto.setCommunityId(noticeDto.getCommunityId());
  255. roomDto.setRoomId(noticeDto.getObjId());
  256. List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
  257. for (RoomDto tmpRoomDto : roomDtos) {
  258. if (!RoomDto.STATE_SELL.equals(tmpRoomDto.getState())) {
  259. continue;
  260. }
  261. OwnerRoomRelDto ownerRoomRelDto = new OwnerRoomRelDto();
  262. ownerRoomRelDto.setRoomId(tmpRoomDto.getRoomId());
  263. List<OwnerRoomRelDto> ownerRoomRelDtos = ownerRoomRelInnerServiceSMOImpl.queryOwnerRoomRels(ownerRoomRelDto);
  264. if (ownerRoomRelDtos == null || ownerRoomRelDtos.size() < 1) {
  265. continue;
  266. }
  267. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  268. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  269. ownerAppUserDto.setMemberId(ownerRoomRelDtos.get(0).getOwnerId());
  270. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  271. doSend(ownerAppUserDtos, noticeDto, templateId, accessToken, weChatDto);
  272. }
  273. }
  274. private void sendAllOwner(NoticeDto noticeDto, String templateId, String accessToken, SmallWeChatDto weChatDto) {
  275. //判断关注表的用户量 是否大于100人
  276. WechatSubscribeDto wechatSubscribeDto = new WechatSubscribeDto();
  277. wechatSubscribeDto.setAppId(weChatDto.getAppId());
  278. wechatSubscribeDto.setOpenType(WechatSubscribeDto.OPEN_TYPE_WECHAT);
  279. int count = wechatSubscribeV1InnerServiceSMOImpl.queryWechatSubscribesCount(wechatSubscribeDto);
  280. //可能公众号 已经 使用了好久 但是 程序可能刚开始用
  281. if (count < DEFAULT_SUBSCRIBE_PERSON) {
  282. //doSendToOpenId(noticeDto, templateId, accessToken, "", weChatDto);
  283. WechatSubscribePo tmpWechatSubscribePo = new WechatSubscribePo();
  284. tmpWechatSubscribePo.setAppId(weChatDto.getAppId());
  285. wechatSubscribeV1InnerServiceSMOImpl.deleteWechatSubscribe(tmpWechatSubscribePo);
  286. getAllOpenId(accessToken, "", weChatDto);
  287. }
  288. List<WechatSubscribeDto> wechatSubscribeDtos = wechatSubscribeV1InnerServiceSMOImpl.queryWechatSubscribes(wechatSubscribeDto);
  289. if (wechatSubscribeDtos == null || wechatSubscribeDtos.size() < 1) {
  290. return;
  291. }
  292. String wechatUrl = MappingCache.getValue("OWNER_WECHAT_URL");
  293. Miniprogram miniprogram = null;
  294. if (wechatUrl.startsWith("https://") || wechatUrl.startsWith("http://")) {
  295. } else {
  296. miniprogram = new Miniprogram();
  297. miniprogram.setAppid(wechatUrl);
  298. }
  299. String sendTemplate = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.SEND_TEMPLATE_URL);
  300. if (StringUtil.isEmpty(sendTemplate)) {
  301. sendTemplate = sendMsgUrl;
  302. }
  303. ResponseEntity<String> responseEntity = null;
  304. for (WechatSubscribeDto wechatSubscribeDto1 : wechatSubscribeDtos) {
  305. Date startTime = DateUtil.getCurrentDate();
  306. Data data = new Data();
  307. PropertyFeeTemplateMessage templateMessage = new PropertyFeeTemplateMessage();
  308. try {
  309. templateMessage.setTemplate_id(templateId);
  310. templateMessage.setTouser(wechatSubscribeDto1.getOpenId());
  311. data.setFirst(new Content(noticeDto.getTitle()));
  312. data.setKeyword1(new Content(noticeDto.getTitle()));
  313. data.setKeyword2(new Content(noticeDto.getStartTime()));
  314. data.setKeyword3(new Content(StringUtil.delHtmlTag(noticeDto.getContext())));
  315. data.setRemark(new Content("如有疑问请联系相关物业人员"));
  316. templateMessage.setData(data);
  317. if (!StringUtil.isEmpty(wechatUrl)) {
  318. if (miniprogram == null) {
  319. templateMessage.setUrl(wechatUrl + "/#/pages/notice/detail/detail?noticeId=" + noticeDto.getNoticeId() + "&wAppId=" + weChatDto.getAppId() + "&communityId=" + noticeDto.getCommunityId());
  320. } else {
  321. miniprogram.setPagepath("/pages/notice/detail/detail?noticeId=" + noticeDto.getNoticeId() + "&wAppId=" + weChatDto.getAppId() + "&communityId=" + noticeDto.getCommunityId());
  322. templateMessage.setMiniprogram(miniprogram);
  323. }
  324. }
  325. //并发处理
  326. PushWechatTemplateMessageThread pushWechatTemplateMessageThread = new PushWechatTemplateMessageThread(outRestTemplate, sendTemplate + accessToken, JSON.toJSONString(templateMessage));
  327. publicWeChatPushMessageTemplateJava110ThreadPoolFactory.submit(pushWechatTemplateMessageThread);
  328. //responseEntity = outRestTemplate.postForEntity(sendTemplate + accessToken, JSON.toJSONString(templateMessage), String.class);
  329. } catch (Exception e) {
  330. LogSystemErrorPo logSystemErrorPo = new LogSystemErrorPo();
  331. logSystemErrorPo.setErrId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_errId));
  332. logSystemErrorPo.setErrType(LogSystemErrorDto.ERR_TYPE_NOTICE);
  333. logSystemErrorPo.setMsg(ExceptionUtil.getStackTrace(e));
  334. saveSystemErrorSMOImpl.saveLog(logSystemErrorPo);
  335. logger.error("发送失败", e);
  336. } finally {
  337. doSaveLog(startTime, DateUtil.getCurrentDate(), "/pages/notice/detail/detail", JSON.toJSONString(templateMessage), responseEntity, wechatSubscribeDto1.getOpenId());
  338. }
  339. }
  340. }
  341. private void getAllOpenId(String accessToken, String nextOpenid, SmallWeChatDto weChatDto) {
  342. String url = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.GET_USER_URL);
  343. if (StringUtil.isEmpty(url)) {
  344. url = getUser;
  345. }
  346. url = url.replace("ACCESS_TOKEN", accessToken);
  347. if (!StringUtil.isEmpty(nextOpenid)) {
  348. url += ("&next_openid=" + nextOpenid);
  349. }
  350. ResponseEntity<String> paramOut = outRestTemplate.getForEntity(url, String.class);
  351. logger.info("获取用户返回:{}", paramOut);
  352. if (paramOut.getStatusCode() != HttpStatus.OK) {
  353. throw new IllegalArgumentException(paramOut.getBody());
  354. }
  355. JSONObject paramOutObj = JSONObject.parseObject(paramOut.getBody());
  356. if (paramOutObj.containsKey("errcode")) {
  357. throw new IllegalArgumentException(paramOut.getBody());
  358. }
  359. if (!paramOutObj.containsKey("data")) {
  360. return;
  361. }
  362. JSONObject dataObj = paramOutObj.getJSONObject("data");
  363. JSONArray openids = dataObj.getJSONArray("openid");
  364. nextOpenid = paramOutObj.getString("next_openid");
  365. List<WechatSubscribePo> wechatSubscribePos = new ArrayList<>();
  366. for (int openIndex = 0; openIndex < openids.size(); openIndex++) {
  367. String openId = openids.getString(openIndex);
  368. WechatSubscribePo wechatSubscribePo = new WechatSubscribePo();
  369. wechatSubscribePo.setAppId(weChatDto.getAppId());
  370. wechatSubscribePo.setOpenId(openId);
  371. wechatSubscribePo.setSubId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_file_id));
  372. wechatSubscribePo.setOpenType(WechatSubscribeDto.OPEN_TYPE_WECHAT);
  373. wechatSubscribePos.add(wechatSubscribePo);
  374. }
  375. if (wechatSubscribePos.size() > 0) {
  376. wechatSubscribeV1InnerServiceSMOImpl.saveWechatSubscribes(wechatSubscribePos);
  377. }
  378. //(关注者列表已返回完时,返回next_openid为空)
  379. if (!StringUtil.isEmpty(nextOpenid)) {
  380. getAllOpenId(accessToken, nextOpenid, weChatDto);
  381. }
  382. }
  383. private void doSend(List<OwnerAppUserDto> ownerAppUserDtos, NoticeDto noticeDto, String templateId, String accessToken, SmallWeChatDto weChatDto) {
  384. String wechatUrl = MappingCache.getValue("OWNER_WECHAT_URL") + "/#/pages/notice/detail/detail?noticeId=";
  385. ResponseEntity<String> responseEntity = null;
  386. String sendTemplate = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.SEND_TEMPLATE_URL);
  387. if (StringUtil.isEmpty(sendTemplate)) {
  388. sendTemplate = sendMsgUrl;
  389. }
  390. for (OwnerAppUserDto appUserDto : ownerAppUserDtos) {
  391. Date startTime = DateUtil.getCurrentDate();
  392. PropertyFeeTemplateMessage templateMessage = new PropertyFeeTemplateMessage();
  393. try {
  394. Data data = new Data();
  395. templateMessage.setTemplate_id(templateId);
  396. templateMessage.setTouser(appUserDto.getOpenId());
  397. data.setFirst(new Content(noticeDto.getTitle()));
  398. data.setKeyword1(new Content(noticeDto.getTitle()));
  399. data.setKeyword2(new Content(noticeDto.getStartTime()));
  400. data.setKeyword3(new Content(StringUtil.delHtmlTag(noticeDto.getContext())));
  401. data.setRemark(new Content("如有疑问请联系相关物业人员"));
  402. templateMessage.setData(data);
  403. templateMessage.setUrl(wechatUrl + noticeDto.getNoticeId() + "&wAppId=" + weChatDto.getAppId() + "&communityId=" + noticeDto.getCommunityId());
  404. logger.info("发送模板消息内容:{}", JSON.toJSONString(templateMessage));
  405. //responseEntity = outRestTemplate.postForEntity(sendTemplate + accessToken, JSON.toJSONString(templateMessage), String.class);
  406. PushWechatTemplateMessageThread pushWechatTemplateMessageThread = new PushWechatTemplateMessageThread(outRestTemplate, sendTemplate + accessToken, JSON.toJSONString(templateMessage));
  407. publicWeChatPushMessageTemplateJava110ThreadPoolFactory.submit(pushWechatTemplateMessageThread);
  408. logger.info("微信模板返回内容:{}", responseEntity);
  409. } catch (Exception e) {
  410. LogSystemErrorPo logSystemErrorPo = new LogSystemErrorPo();
  411. logSystemErrorPo.setErrId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_errId));
  412. logSystemErrorPo.setErrType(LogSystemErrorDto.ERR_TYPE_NOTICE);
  413. logSystemErrorPo.setMsg(ExceptionUtil.getStackTrace(e));
  414. saveSystemErrorSMOImpl.saveLog(logSystemErrorPo);
  415. logger.error("发送失败", e);
  416. } finally {
  417. doSaveLog(startTime, DateUtil.getCurrentDate(), "/pages/notice/detail/detail", JSON.toJSONString(templateMessage), responseEntity, appUserDto.getOpenId());
  418. }
  419. }
  420. }
  421. private void doSaveLog(Date startDate, Date endDate, String serviceCode, String reqJson, ResponseEntity<String> responseEntity, String userId) {
  422. try {
  423. TransactionLogPo transactionLogPo = new TransactionLogPo();
  424. transactionLogPo.setAppId(AppDto.OWNER_WECHAT_PAY);
  425. transactionLogPo.setCostTime((endDate.getTime() - startDate.getTime()) + "");
  426. transactionLogPo.setIp("");
  427. transactionLogPo.setServiceCode(serviceCode);
  428. transactionLogPo.setSrcIp("127.0.0.1");
  429. transactionLogPo.setState(responseEntity.getStatusCode() != HttpStatus.OK ? "F" : "S");
  430. transactionLogPo.setTimestamp(DateUtil.getCurrentDate().getTime() + "");
  431. transactionLogPo.setUserId(userId);
  432. transactionLogPo.setTransactionId(userId);
  433. transactionLogPo.setRequestHeader("");
  434. transactionLogPo.setResponseHeader(responseEntity.getHeaders().toSingleValueMap().toString());
  435. transactionLogPo.setRequestMessage(reqJson);
  436. transactionLogPo.setResponseMessage(responseEntity.getBody());
  437. saveTransactionLogSMOImpl.saveLog(transactionLogPo);
  438. } catch (Exception e) {
  439. logger.error("存日志失败", e);
  440. }
  441. }
  442. }