MiniProgramSubTask.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.fs.task;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.TypeReference;
  5. import com.fs.store.domain.FsMiniprogramSubNotifyTask;
  6. import com.fs.store.dto.ClientCredGrantReqDTO;
  7. import com.fs.store.dto.MiniGramSubsMsgResultDTO;
  8. import com.fs.store.dto.TemplateMessageSendRequestDTO;
  9. import com.fs.store.dto.WeXinAccessTokenDTO;
  10. import com.fs.store.enums.MiniAppNotifyTaskStatusEnum;
  11. import com.fs.store.mapper.FsMiniprogramSubNotifyTaskMapper;
  12. import com.fs.store.service.IWechatMiniProgrService;
  13. import com.fs.wx.miniapp.config.WxMaProperties;
  14. import lombok.RequiredArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.collections.CollectionUtils;
  17. import org.apache.commons.lang.exception.ExceptionUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.time.LocalDateTime;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * 小程序订阅通知定时任务
  25. */
  26. @Service("miniProgramSubTask")
  27. @Slf4j
  28. @RequiredArgsConstructor
  29. public class MiniProgramSubTask {
  30. private final IWechatMiniProgrService wechatMiniProgrService;
  31. private final FsMiniprogramSubNotifyTaskMapper notifyTaskMapper;
  32. private WxMaProperties.Config config = null;
  33. @Autowired
  34. public void setConfig(WxMaProperties properties) {
  35. if(ObjectUtil.isNotNull(properties)){
  36. this.config = properties.getConfigs().get(0);
  37. }
  38. }
  39. /**
  40. * 小程序订阅通知
  41. */
  42. public void notifyMiniAppSub(){
  43. log.info("小程序订阅通知定时任务");
  44. // 先获取所有可用待处理任务
  45. List<FsMiniprogramSubNotifyTask> pendingData = notifyTaskMapper.selectPendingData();
  46. if(CollectionUtils.isEmpty(pendingData)){
  47. log.info("小程序订阅通知定时任务, 无待处理数据");
  48. return;
  49. }
  50. for (FsMiniprogramSubNotifyTask pendingDatum : pendingData) {
  51. pendingDatum.setUpdateTime(LocalDateTime.now());
  52. ClientCredGrantReqDTO clientCredGrantReqDTO = new ClientCredGrantReqDTO();
  53. clientCredGrantReqDTO.setAppid(config.getAppid());
  54. clientCredGrantReqDTO.setSecret(config.getSecret());
  55. clientCredGrantReqDTO.setGrant_type("client_credential");
  56. try{
  57. // 获取accessToken
  58. WeXinAccessTokenDTO stableToken = wechatMiniProgrService
  59. .getStableToken(clientCredGrantReqDTO);
  60. String accessToken = stableToken.getAccessToken();
  61. // 调用微信小程序订阅通知
  62. TemplateMessageSendRequestDTO sendRequestDTO = new TemplateMessageSendRequestDTO();
  63. sendRequestDTO.setTemplate_id(pendingDatum.getTemplateId());
  64. sendRequestDTO.setTouser(pendingDatum.getTouser());
  65. sendRequestDTO.setPage(pendingDatum.getPage());
  66. TypeReference<Map<String, TemplateMessageSendRequestDTO.TemplateDataValue>> typeReference = new TypeReference<Map<String, TemplateMessageSendRequestDTO.TemplateDataValue>>() {};
  67. sendRequestDTO.setData(JSONObject.parseObject(pendingDatum.getData(),typeReference));
  68. MiniGramSubsMsgResultDTO miniGramSubsMsgResultDTO = wechatMiniProgrService.sendSubscribeMsg(accessToken, sendRequestDTO);
  69. pendingDatum.setRequestBody(JSONObject.toJSONString(sendRequestDTO));
  70. pendingDatum.setResponseBody(JSONObject.toJSONString(miniGramSubsMsgResultDTO));
  71. // 如果推送消息成功
  72. if(miniGramSubsMsgResultDTO.getErrcode() == 0){
  73. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.SUCCESS.getValue());
  74. } else {
  75. // 更新任务状态为执行失败
  76. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.FAILED.getValue());
  77. pendingDatum.setErrorMessage(JSONObject.toJSONString(miniGramSubsMsgResultDTO));
  78. pendingDatum.setRetryCount(pendingDatum.getRetryCount() +1);
  79. }
  80. }catch (Throwable e){
  81. // 更新任务状态为执行失败
  82. pendingDatum.setStatus(MiniAppNotifyTaskStatusEnum.FAILED.getValue());
  83. pendingDatum.setErrorMessage(ExceptionUtils.getStackTrace(e));
  84. pendingDatum.setRetryCount(pendingDatum.getRetryCount() +1);
  85. log.error("小程序订阅通知定时任务异常: {}", ExceptionUtils.getStackTrace(e));
  86. }
  87. }
  88. if(CollectionUtils.isNotEmpty(pendingData)){
  89. notifyTaskMapper.updateBatchById(pendingData);
  90. }
  91. }
  92. }