WeChatPushMessageTemplate.java 22 KB

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