WechatServiceImpl.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.hz.employmentsite.services.impl;
  2. import com.hz.employmentsite.AppConfig;
  3. import com.hz.employmentsite.mapper.WxMessagestatusMapper;
  4. import com.hz.employmentsite.mapper.WxMessagetempsettingFieldMapper;
  5. import com.hz.employmentsite.mapper.WxMessagetempsettingMapper;
  6. import com.hz.employmentsite.model.*;
  7. import com.hz.employmentsite.services.service.WechatService;
  8. import com.hz.employmentsite.util.StringUtils;
  9. import lombok.extern.slf4j.Slf4j;
  10. import me.chanjar.weixin.common.error.WxErrorException;
  11. import me.chanjar.weixin.mp.api.WxMpService;
  12. import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
  13. import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.util.*;
  19. @Service("WechatService")
  20. @Slf4j
  21. public class WechatServiceImpl implements WechatService {
  22. @Autowired
  23. private AppConfig appConfig;
  24. @Autowired
  25. private WxMpService wxMpService;
  26. @Autowired
  27. private WxMessagestatusMapper wxMessagestatusMapper;
  28. @Autowired
  29. private WxMessagetempsettingMapper wxMessagetempsettingMapper;
  30. @Autowired
  31. private WxMessagetempsettingFieldMapper wxMessagetempsettingFieldMapper;
  32. @Autowired
  33. private StringUtils stringUtils;
  34. private final Logger logger;
  35. public WechatServiceImpl() {
  36. logger = LoggerFactory.getLogger(this.getClass());
  37. }
  38. public String getOAuthUrl(String redirectUrl) {
  39. return wxMpService.oauth2buildAuthorizationUrl(redirectUrl, appConfig.wxAccessScope, "");
  40. }
  41. @Override
  42. public String getOpenId(String code) throws WxErrorException {
  43. String openId = wxMpService.oauth2getAccessToken(code).getOpenId();
  44. log.info("openId:" + openId);
  45. return openId;
  46. }
  47. private boolean sendMsg(String openId, String templateId, String url, Map<String, String> sentData) throws Exception {
  48. //数据
  49. List<WxMpTemplateData> data = new ArrayList<>();
  50. sentData.forEach((key, value) -> {
  51. data.add(new WxMpTemplateData(key, value));
  52. });
  53. //2,推送消息
  54. WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
  55. .toUser(openId)//要推送的用户openid
  56. .data(data) //数据
  57. .templateId(templateId)//模版id
  58. .url(url) // 点击详情跳转地址
  59. .build();
  60. //发起推送
  61. try {
  62. wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
  63. return true;
  64. } catch (Exception ex) {
  65. log.info("sendMsg2:微信消息推送失败" + ex.getMessage());
  66. ex.printStackTrace();
  67. throw new Exception("sendMsg2:微信消息推送失败" + ex.getMessage());
  68. }
  69. }
  70. @Override
  71. public boolean sendMsg(String openId, String sourceId, String url, String templateId, Map<String, String> sendData) throws Exception {
  72. if (stringUtils.IsNullOrEmpty(openId))
  73. throw new Exception("openId为空!");
  74. if (stringUtils.IsNullOrEmpty(sourceId))
  75. throw new Exception("sourceId为空!");
  76. if (stringUtils.IsNullOrEmpty(sourceId))
  77. throw new Exception("templateId为空!");
  78. if (sendData == null || sendData.values().size() == 0)
  79. throw new Exception("sendData为空!");
  80. WxMessagestatusExample exp = new WxMessagestatusExample();
  81. WxMessagestatusExample.Criteria cri = exp.or();
  82. cri.andOpenIdEqualTo(openId);
  83. cri.andSourceIdEqualTo(sourceId);
  84. WxMessagestatus wxMessagestatus = wxMessagestatusMapper.selectByExample(exp).stream().findFirst().orElse(null);
  85. if (wxMessagestatus != null && wxMessagestatus.getRecordStatus().equals(1))
  86. throw new Exception("该消息已有成功推送记录,请勿重复推送!");
  87. String sendUrl = "";
  88. if(!stringUtils.IsNullOrEmpty(url)){
  89. sendUrl = appConfig.wxMessageBaseUrl + url;
  90. }
  91. if (wxMessagestatus == null) {
  92. wxMessagestatus = new WxMessagestatus();
  93. wxMessagestatus.setMessageStatusId(UUID.randomUUID().toString());
  94. wxMessagestatus.setOpenId(openId);
  95. wxMessagestatus.setSourceId(sourceId);
  96. wxMessagestatus.setContent(sendData.toString());
  97. wxMessagestatus.setRecordStatus(0);
  98. wxMessagestatus.setCreateTime(new Date());
  99. wxMessagestatus.setUrl(sendUrl);
  100. wxMessagestatusMapper.insert(wxMessagestatus);
  101. } else {
  102. wxMessagestatus.setContent(sendData.toString());
  103. wxMessagestatus.setUrl(sendUrl);
  104. wxMessagestatus.setModifyTime(new Date());
  105. wxMessagestatusMapper.updateByPrimaryKey(wxMessagestatus);
  106. }
  107. try {
  108. sendMsg(openId, templateId, sendUrl, sendData);
  109. wxMessagestatus.setRecordStatus(1);
  110. wxMessagestatus.setSendTime(new Date());
  111. wxMessagestatusMapper.updateByPrimaryKey(wxMessagestatus);
  112. return true;
  113. } catch (Exception ex) {
  114. log.info("sendMsg1:微信消息推送失败" + ex.getMessage());
  115. ex.printStackTrace();
  116. return false;
  117. }
  118. }
  119. @Override
  120. public List<WxMessagetempsetting> getWxMessagetempsettingList() {
  121. List<WxMessagetempsetting> dataList = wxMessagetempsettingMapper.selectByExample(null);
  122. return dataList;
  123. }
  124. @Override
  125. public List<WxMessagetempsettingField> getWxMessagetempFieldList(String templateNo) {
  126. WxMessagetempsettingFieldExample exp = new WxMessagetempsettingFieldExample();
  127. WxMessagetempsettingFieldExample.Criteria cri = exp.or();
  128. cri.andTemplateNoEqualTo(templateNo);
  129. return wxMessagetempsettingFieldMapper.selectByExample(exp);
  130. }
  131. }