MiniProgramSubTask.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.fs.task;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.alibaba.fastjson.TypeReference;
  6. import com.fs.common.annotation.QuartzRunnable;
  7. import com.fs.store.domain.FsMiniprogramSubNotifyTask;
  8. import com.fs.store.dto.ClientCredGrantReqDTO;
  9. import com.fs.store.dto.MiniGramSubsMsgResultDTO;
  10. import com.fs.store.dto.TemplateMessageSendRequestDTO;
  11. import com.fs.store.dto.WeXinAccessTokenDTO;
  12. import com.fs.store.enums.MiniAppNotifyTaskStatusEnum;
  13. import com.fs.store.mapper.FsMiniprogramSubNotifyTaskMapper;
  14. import com.fs.store.service.IWechatMiniProgrService;
  15. import com.fs.wx.miniapp.config.WxMaProperties;
  16. import lombok.RequiredArgsConstructor;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.collections.CollectionUtils;
  19. import org.apache.commons.lang.exception.ExceptionUtils;
  20. import org.apache.tomcat.jni.Local;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import java.time.LocalDateTime;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * 小程序订阅通知定时任务
  28. */
  29. @Service("miniProgramSubTask")
  30. @Slf4j
  31. @RequiredArgsConstructor
  32. public class MiniProgramSubTask {
  33. private final IWechatMiniProgrService wechatMiniProgrService;
  34. private final FsMiniprogramSubNotifyTaskMapper notifyTaskMapper;
  35. private WxMaProperties.Config config = null;
  36. @Autowired
  37. public void setConfig(WxMaProperties properties) {
  38. if(ObjectUtil.isNotNull(properties)){
  39. this.config = properties.getConfigs().get(0);
  40. }
  41. }
  42. /**
  43. * 小程序订阅通知
  44. */
  45. @QuartzRunnable(name="小程序订阅通知")
  46. public void notifyMiniAppSub(){
  47. log.info("小程序订阅通知定时任务");
  48. // 先获取所有可用待处理任务
  49. List<FsMiniprogramSubNotifyTask> pendingData = notifyTaskMapper.selectPendingData();
  50. if(CollectionUtils.isEmpty(pendingData)){
  51. log.info("小程序订阅通知定时任务, 无待处理数据");
  52. return;
  53. }
  54. for (FsMiniprogramSubNotifyTask pendingDatum : pendingData) {
  55. pendingDatum.setUpdateTime(LocalDateTime.now());
  56. ClientCredGrantReqDTO clientCredGrantReqDTO = new ClientCredGrantReqDTO();
  57. clientCredGrantReqDTO.setAppid(config.getAppid());
  58. clientCredGrantReqDTO.setSecret(config.getSecret());
  59. clientCredGrantReqDTO.setGrant_type("client_credential");
  60. try{
  61. // 获取accessToken
  62. WeXinAccessTokenDTO stableToken = wechatMiniProgrService
  63. .getStableToken(clientCredGrantReqDTO);
  64. String accessToken = stableToken.getAccessToken();
  65. // 调用微信小程序订阅通知
  66. TemplateMessageSendRequestDTO sendRequestDTO = new TemplateMessageSendRequestDTO();
  67. sendRequestDTO.setTemplate_id(pendingDatum.getTemplateId());
  68. sendRequestDTO.setTouser(pendingDatum.getTouser());
  69. sendRequestDTO.setPage(pendingDatum.getPage());
  70. TypeReference<Map<String, TemplateMessageSendRequestDTO.TemplateDataValue>> typeReference = new TypeReference<Map<String, TemplateMessageSendRequestDTO.TemplateDataValue>>() {};
  71. sendRequestDTO.setData(JSON.parseObject(pendingDatum.getData(),typeReference));
  72. MiniGramSubsMsgResultDTO miniGramSubsMsgResultDTO = wechatMiniProgrService.sendSubscribeMsg(accessToken, sendRequestDTO);
  73. pendingDatum.setRequestBody(JSON.toJSONString(sendRequestDTO));
  74. pendingDatum.setResponseBody(JSON.toJSONString(miniGramSubsMsgResultDTO));
  75. // 如果推送消息成功
  76. if(miniGramSubsMsgResultDTO.getErrcode() == 0){
  77. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.SUCCESS.getValue());
  78. } else {
  79. // 更新任务状态为执行失败
  80. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.FAILED.getValue());
  81. pendingDatum.setErrorMessage(JSON.toJSONString(miniGramSubsMsgResultDTO));
  82. pendingDatum.setRetryCount(pendingDatum.getRetryCount() +1);
  83. }
  84. }catch (Throwable e){
  85. // 更新任务状态为执行失败
  86. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.FAILED.getValue());
  87. pendingDatum.setErrorMessage(ExceptionUtils.getStackTrace(e));
  88. pendingDatum.setRetryCount(pendingDatum.getRetryCount() +1);
  89. log.error("小程序订阅通知定时任务异常: {}", ExceptionUtils.getStackTrace(e));
  90. }
  91. }
  92. if(CollectionUtils.isNotEmpty(pendingData)){
  93. notifyTaskMapper.updateBatchById(pendingData);
  94. }
  95. }
  96. /**
  97. * 小程序订阅通知
  98. */
  99. @QuartzRunnable(name="小程序直播订阅通知")
  100. public void notifyMiniLiveAppSub(){
  101. log.info("小程序订阅通知定时任务");
  102. // 先获取所有可用待处理任务
  103. List<FsMiniprogramSubNotifyTask> pendingData = notifyTaskMapper.selectLivePendingData();
  104. if(CollectionUtils.isEmpty(pendingData)){
  105. log.info("小程序订阅通知定时任务, 无待处理数据");
  106. return;
  107. }
  108. LocalDateTime now = LocalDateTime.now();
  109. for (FsMiniprogramSubNotifyTask pendingDatum : pendingData) {
  110. if(pendingDatum.getUpdateTime().isAfter(now)) continue;
  111. pendingDatum.setUpdateTime(LocalDateTime.now());
  112. ClientCredGrantReqDTO clientCredGrantReqDTO = new ClientCredGrantReqDTO();
  113. clientCredGrantReqDTO.setAppid(config.getAppid());
  114. clientCredGrantReqDTO.setSecret(config.getSecret());
  115. clientCredGrantReqDTO.setGrant_type("client_credential");
  116. try{
  117. // 获取accessToken
  118. WeXinAccessTokenDTO stableToken = wechatMiniProgrService
  119. .getStableToken(clientCredGrantReqDTO);
  120. String accessToken = stableToken.getAccessToken();
  121. // 调用微信小程序订阅通知
  122. TemplateMessageSendRequestDTO sendRequestDTO = new TemplateMessageSendRequestDTO();
  123. sendRequestDTO.setTemplate_id(pendingDatum.getTemplateId());
  124. sendRequestDTO.setTouser(pendingDatum.getTouser());
  125. sendRequestDTO.setPage(pendingDatum.getPage());
  126. TypeReference<Map<String, TemplateMessageSendRequestDTO.TemplateDataValue>> typeReference = new TypeReference<Map<String, TemplateMessageSendRequestDTO.TemplateDataValue>>() {};
  127. sendRequestDTO.setData(JSON.parseObject(pendingDatum.getData(),typeReference));
  128. MiniGramSubsMsgResultDTO miniGramSubsMsgResultDTO = wechatMiniProgrService.sendSubscribeMsg(accessToken, sendRequestDTO);
  129. pendingDatum.setRequestBody(JSON.toJSONString(sendRequestDTO));
  130. pendingDatum.setResponseBody(JSON.toJSONString(miniGramSubsMsgResultDTO));
  131. // 如果推送消息成功
  132. if(miniGramSubsMsgResultDTO.getErrcode() == 0){
  133. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.SUCCESS.getValue());
  134. } else {
  135. // 更新任务状态为执行失败
  136. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.FAILED.getValue());
  137. pendingDatum.setErrorMessage(JSON.toJSONString(miniGramSubsMsgResultDTO));
  138. pendingDatum.setRetryCount(pendingDatum.getRetryCount() +1);
  139. }
  140. }catch (Throwable e){
  141. // 更新任务状态为执行失败
  142. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.FAILED.getValue());
  143. pendingDatum.setErrorMessage(ExceptionUtils.getStackTrace(e));
  144. pendingDatum.setRetryCount(pendingDatum.getRetryCount() +1);
  145. log.error("小程序订阅通知定时任务异常: {}", ExceptionUtils.getStackTrace(e));
  146. }
  147. }
  148. if(CollectionUtils.isNotEmpty(pendingData)){
  149. notifyTaskMapper.updateBatchById(pendingData);
  150. }
  151. }
  152. }