123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- package com.hz.employmentsite.services.impl;
- import com.hz.employmentsite.AppConfig;
- import com.hz.employmentsite.mapper.WxMessagestatusMapper;
- import com.hz.employmentsite.mapper.WxMessagetempsettingFieldMapper;
- import com.hz.employmentsite.mapper.WxMessagetempsettingMapper;
- import com.hz.employmentsite.model.*;
- import com.hz.employmentsite.services.service.WechatService;
- import com.hz.employmentsite.util.StringUtils;
- import lombok.extern.slf4j.Slf4j;
- import me.chanjar.weixin.common.error.WxErrorException;
- import me.chanjar.weixin.mp.api.WxMpService;
- import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
- import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.*;
- @Service("WechatService")
- @Slf4j
- public class WechatServiceImpl implements WechatService {
- @Autowired
- private AppConfig appConfig;
- @Autowired
- private WxMpService wxMpService;
- @Autowired
- private WxMessagestatusMapper wxMessagestatusMapper;
- @Autowired
- private WxMessagetempsettingMapper wxMessagetempsettingMapper;
- @Autowired
- private WxMessagetempsettingFieldMapper wxMessagetempsettingFieldMapper;
- @Autowired
- private StringUtils stringUtils;
- private final Logger logger;
- public WechatServiceImpl() {
- logger = LoggerFactory.getLogger(this.getClass());
- }
- public String getOAuthUrl(String redirectUrl) {
- return wxMpService.oauth2buildAuthorizationUrl(redirectUrl, appConfig.wxAccessScope, "");
- }
- @Override
- public String getOpenId(String code) throws WxErrorException {
- String openId = wxMpService.oauth2getAccessToken(code).getOpenId();
- log.info("openId:" + openId);
- return openId;
- }
- private boolean sendMsg(String openId, String templateId, String url, Map<String, String> sentData) throws Exception {
- //数据
- List<WxMpTemplateData> data = new ArrayList<>();
- sentData.forEach((key, value) -> {
- data.add(new WxMpTemplateData(key, value));
- });
- //2,推送消息
- WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
- .toUser(openId)//要推送的用户openid
- .data(data) //数据
- .templateId(templateId)//模版id
- .url(url) // 点击详情跳转地址
- .build();
- //发起推送
- try {
- wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
- return true;
- } catch (Exception ex) {
- log.info("sendMsg2:微信消息推送失败" + ex.getMessage());
- ex.printStackTrace();
- throw new Exception("sendMsg2:微信消息推送失败" + ex.getMessage());
- }
- }
- @Override
- public boolean sendMsg(String openId, String sourceId, String url, String templateId, Map<String, String> sendData) throws Exception {
- if (stringUtils.IsNullOrEmpty(openId))
- throw new Exception("openId为空!");
- if (stringUtils.IsNullOrEmpty(sourceId))
- throw new Exception("sourceId为空!");
- if (stringUtils.IsNullOrEmpty(sourceId))
- throw new Exception("templateId为空!");
- if (sendData == null || sendData.values().size() == 0)
- throw new Exception("sendData为空!");
- WxMessagestatusExample exp = new WxMessagestatusExample();
- WxMessagestatusExample.Criteria cri = exp.or();
- cri.andOpenIdEqualTo(openId);
- cri.andSourceIdEqualTo(sourceId);
- WxMessagestatus wxMessagestatus = wxMessagestatusMapper.selectByExample(exp).stream().findFirst().orElse(null);
- if (wxMessagestatus != null && wxMessagestatus.getRecordStatus().equals(1))
- throw new Exception("该消息已有成功推送记录,请勿重复推送!");
- String sendUrl = "";
- if(!stringUtils.IsNullOrEmpty(url)){
- sendUrl = appConfig.wxMessageBaseUrl + url;
- }
- if (wxMessagestatus == null) {
- wxMessagestatus = new WxMessagestatus();
- wxMessagestatus.setMessageStatusId(UUID.randomUUID().toString());
- wxMessagestatus.setOpenId(openId);
- wxMessagestatus.setSourceId(sourceId);
- wxMessagestatus.setContent(sendData.toString());
- wxMessagestatus.setRecordStatus(0);
- wxMessagestatus.setCreateTime(new Date());
- wxMessagestatus.setUrl(sendUrl);
- wxMessagestatusMapper.insert(wxMessagestatus);
- } else {
- wxMessagestatus.setContent(sendData.toString());
- wxMessagestatus.setUrl(sendUrl);
- wxMessagestatus.setModifyTime(new Date());
- wxMessagestatusMapper.updateByPrimaryKey(wxMessagestatus);
- }
- try {
- sendMsg(openId, templateId, sendUrl, sendData);
- wxMessagestatus.setRecordStatus(1);
- wxMessagestatus.setSendTime(new Date());
- wxMessagestatusMapper.updateByPrimaryKey(wxMessagestatus);
- return true;
- } catch (Exception ex) {
- log.info("sendMsg1:微信消息推送失败" + ex.getMessage());
- ex.printStackTrace();
- return false;
- }
- }
- @Override
- public List<WxMessagetempsetting> getWxMessagetempsettingList() {
- List<WxMessagetempsetting> dataList = wxMessagetempsettingMapper.selectByExample(null);
- return dataList;
- }
- @Override
- public List<WxMessagetempsettingField> getWxMessagetempFieldList(String templateNo) {
- WxMessagetempsettingFieldExample exp = new WxMessagetempsettingFieldExample();
- WxMessagetempsettingFieldExample.Criteria cri = exp.or();
- cri.andTemplateNoEqualTo(templateNo);
- return wxMessagetempsettingFieldMapper.selectByExample(exp);
- }
- }
|