微信消息模板使用

<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-mp -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
</dependency>

@GetMapping("/pushMsg")
public String pushWXMsg(String openId){
sealOrderService.queryOrderPayStatus(openId);// 测试支付结果查询接口
Map<String,String> dataMap = new HashMap<>();
dataMap.put("first","创建时间:"+new SimpleDateFormat("yyyyMMdd").format(new Date()));
dataMap.put("keyword1","232323");
dataMap.put("keyword2","长沙起飞刻章店");
dataMap.put("keyword3","微信支付");
dataMap.put("keyword4","666");
dataMap.put("keyword5","无");
dataMap.put("remark","印章1-印章2-印章3");
boolean flag = weiXinService.pushWeiXinMsg(openId,dataMap);
return (flag?"OK":"NO");
}


package com.dhht.wechat.service;

import com.dhht.wechat.util.SendMsgUtil;
import com.dhht.wechat.wxpay.config.WxPayProperties;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
* @Author: sh
* @Description: WeiXinService 微信工具
* @Date: 10:12 2019/9/17
*/
@Slf4j
@Service
public class WeiXinService {

@Value("${appSecret}")
private String appSecret;

@Value("${wechat_msg_tem_id}")
private String WECHAT_MSG_TEM_ID;

@Resource
private WxPayProperties wxPayProperties;// 第三方

/**
* 获取微信openId
*
* @param code
* @return
*/
public String getOpenId(String code) {
try {
String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wxPayProperties.getAppId() + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code";
String openId = SendMsgUtil.httpRequest(openUrl, "GET", "{}").getString("openid");// 获取openid
return openId;
} catch (Exception e) {
return null;
} finally {
}
}

/**
* 微信消息推送
*
* @param openId
* @return
*/
public boolean pushWeiXinMsg(String openId, Map<String, String> dataMap) {
try {
if (StringUtils.isEmpty(openId)) {
return false;
}
//1,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId(wxPayProperties.getAppId());
wxStorage.setSecret(appSecret);
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);

//2,推送消息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openId)//要推送的用户openid
.templateId(WECHAT_MSG_TEM_ID)//模版id
//.url("https://30paotui.com/")//点击模版消息要访问的网址
.build();
//3,如果是正式版发送模版消息,这里需要配置你的信息
dataMap.forEach((k, v) -> {
templateMessage.addData(new WxMpTemplateData(k, v, "#FF00FF"));
});
// templateMessage.addData(new WxMpTemplateData("first","认为热", "#FF00FF"));
// templateMessage.addData(new WxMpTemplateData("keyword1","dfdsfd", "#FF00FF"));
// templateMessage.addData(new WxMpTemplateData("keyword2","门店", "#FF00FF"));
// templateMessage.addData(new WxMpTemplateData("keyword3","支付房贷首法定付", "#FF00FF"));
// templateMessage.addData(new WxMpTemplateData("keyword4","888", "#FF00FF"));
// templateMessage.addData(new WxMpTemplateData("keyword5","meiyou地方", "#FF00FF"));
// templateMessage.addData(new WxMpTemplateData("remark","请及时查看", "#FF00FF"));
wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
return true;
} catch (Exception e) {
log.info("推送失败:" + e.getMessage());
return false;
}


}
}

猜你喜欢

转载自www.cnblogs.com/sung1024/p/11700611.html