Commit 3775a6c4 authored by 张大伟's avatar 张大伟

增加发送模版消息代码

parent 326fb6fc
......@@ -3,9 +3,10 @@ package org.rcisoft.wechat;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.rcisoft.core.constant.MessageConstant;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.rcisoft.core.result.Result;
import org.rcisoft.core.result.ResultCode;
import org.rcisoft.wechat.service.WxPortalService;
......@@ -66,24 +67,39 @@ public class WechatRedirectController {
return null;
}
/**
* 发送模版消息(此方法参数需要修改,需要替换几个参数在此处需要传入几个参数,但是openId是必须的)
* @param message
* @param openId
* @return
*/
@PostMapping(value = {"/sendMessage"})
@ResponseBody
public Result sendMessage(String message, String openId) {
Result result = new Result();
WxMpKefuMessage wxMpKefuMessage = WxMpKefuMessage
.TEXT()
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openId)
.content(message)
.templateId("此处填写模版消息ID")
.url(" ")
.build();
// 设置消息的内容等信息
boolean flag;
// 模版中有几个需要替换的参数templateMessage就添加几个WxMpTemplateData
templateMessage
.addData(new WxMpTemplateData("first", "替换对应first的内容", "#FF00FF"))
.addData(new WxMpTemplateData("remark", "替换对应remark的内容", "#FF00FF"));
// 消息发送完之后返回的消息id,用于判断是否发送成功
String msgId;
try {
flag = service.getKefuService().sendKefuMessage(wxMpKefuMessage);
// 发送消息
msgId = this.wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
} catch (WxErrorException e) {
// 此处暂时抛出异常,正式需要注释掉,否则前台不会接到返回值
e.printStackTrace();
flag = false;
msgId = "";
}
if(flag) {
// 判断msgId是否为空,来判断是否发送成功
if(StringUtils.isNotEmpty(msgId)) {
result.setCode(ResultCode.SUCCESS);
} else {
result.setCode(ResultCode.FAIL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment