package com.doumee.config.wx; import com.alibaba.fastjson.JSONObject; import com.doumee.biz.system.SystemDictDataBiz; import com.doumee.core.constants.Constants; import com.doumee.core.utils.DateUtil; import com.doumee.dao.business.model.IdentityInfo; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * Created by IntelliJ IDEA. * * @Author : Rk * @create 2022/12/7 17:27 */ @Service @Slf4j public class SendWxMessage { private static String goodsOrderUrl = "pages/settlementDetails/settlementDetails?goodsOrderId="; private static String identityInfoUrl = "pages/mine/mine"; @Autowired private SystemDictDataBiz systemDictDataBiz; public void identityInfoMessage(String openid, IdentityInfo identityInfo) throws WxErrorException { RestTemplate restTemplate = new RestTemplate(); String accessToken = WxMiniConfig.wxMaService.getAccessToken(); log.info("微信小程序->微信消息通知 - 认证信息 -> accessToken:{}",accessToken); //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取) String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken; //拼接推送的模版 WxMsgVO wxMsgVO = new WxMsgVO(); //用户的openid(要发送给那个用户) wxMsgVO.setTouser(openid); //订阅消息模板id wxMsgVO.setTemplate_id("4H9ztG4E9BxJLwMuYSZ6oiM7yb4KX7u7bYhkhYJP0-Y"); Map m = new HashMap<>(4); m.put("thing11", new TemplateData(Constants.equalsInteger(identityInfo.getType(),Constants.ZERO)?"用工认证":Constants.equalsInteger(identityInfo.getType(),Constants.ONE)?"运货认证":"供餐认证")); m.put("phrase4", new TemplateData(Constants.equalsInteger(identityInfo.getAuditStatus(),Constants.TWO)?"认证通过":"认证未通过")); m.put("date3", new TemplateData(DateUtil.getFomartDate(identityInfo.getAuditTime(),"yyyy-MM-dd HH:mm:ss"))); m.put("thing5", new TemplateData(identityInfo.getAuditRemark())); wxMsgVO.setPage(identityInfoUrl); wxMsgVO.setData(m); ResponseEntity responseEntity = restTemplate.postForEntity(url, wxMsgVO, String.class); log.info("微信小程序->微信消息通知 认证信息:{}", JSONObject.toJSONString(responseEntity)); } }