微信端订阅
util.alert(\'温馨提示\', \'提交成功,请等待审核!\', function(res){
var tmplId = "29hTTFKavuho2ifDjoEkCD3cCM4PsTGHKqN7fq7npI4";
//订阅消息
wx.requestSubscribeMessage({
tmplIds: [
tmplId
],
success: (res2) => {
if (res2[tmplId] === \'accept\') {
wx.showToast({
title: \'订阅成功!\',
duration: 1000,
success(data) {
//成功
console.log("订阅成功");
})
}else {
console.log("订阅失败");
},
fail: function() {
console.log("订阅失败");
})
});
});
微信小程序模板
后台代码
/**
* 机构端向客户发送服务通知
* @param zxwqTsId
*/
public void sendMessageFromJiGou(String zxwqTsId) {
String wxOpenid = "";
try{
log.info("开始发送服务消息");
ZxwqTs zxwqTs = zxwqTsDao.getById(zxwqTsId);
String userId = zxwqTs.getUserId();
if(!StringUtils.isEmpty(userId)) {
SysUser sysUser = sysUserDao.getById(userId);
if (sysUser != null) {
wxOpenid = sysUser.getWxOpenid();
sendMessageForOneUser(zxwqTs, wxOpenid, "toGeren");
}
}
}catch (Exception e){
log.error("向["+wxOpenid+"]推送消息标失败", e);
}
}
public void sendMessageForOneUser(ZxwqTs zxwqTs, String wxOpenid, String toUserType){
String res = "";
int i = 0;
boolean flag = false;
boolean isInokeHttp = false;
while (!flag) {
String wx_isNeedTransfer = EnvUtils.getValue("wx.isNeedTransfer");
String wx_transterSendMessageUrl = EnvUtils.getValue("wx.transterSendMessageUrl");
String bizType = "weiquan";
//构建模板消息
Template template = assembeTemplateAndReturnIt(zxwqTs, wxOpenid, bizType, toUserType);
if (template == null)
break;
//发送模板消息
if (template != null) {
if (!"true".equals(wx_isNeedTransfer)) {
String wxAccessToken = WxUtil.getToken(isInokeHttp);
if (!StringUtils.isEmpty(wxAccessToken))
res = WxUtil.send(template, wxAccessToken);
} else {
if (!StringUtils.isEmpty(wx_transterSendMessageUrl)) {
try {
String templateJson = JSONUtilsExt.toJson(template);
String returnStr = HttpClientUtil.post(wx_transterSendMessageUrl, new MapWapperExt().add("templateJson", templateJson).toMap());
RestResponse<String> restResponse = JSONUtilsExt.fromJson(returnStr, RestResponse.class);
if (restResponse.getError() != null)
throw new ServiceException(ExceptionUtilsExt.getFinalMessage(restResponse));
res = restResponse.getResult();
} catch (Exception e) {
log.error("调取远程接口错误", e);
continue;
}
}
}
if (!StringUtils.isEmpty(res)) {
if (res.indexOf("{\"errcode\":42001,") >= 0 || res.indexOf("{\"errcode\":40001,") >= 0) {
log.info("redis里面保存的wxAccessToken 已经过期了 !!!需要往redis里面保存 新的wxAccessToken");
flag = false;
isInokeHttp = true;
} else if (res.indexOf("{\"errcode\":47001,") >= 0) {
log.error(res + " : " + template, "有可能是 企业名称(错误地)带了 英文双引号(\") , 破坏了 json结构");
flag = true;
} else if (res.indexOf("{\"errcode\":47001,") >= 0) {
log.error(res + " : " + template, "有可能是 企业名称(错误地)带了 英文双引号(\") , 破坏了 json结构");
flag = true;
} else if (res.indexOf("{\"errcode\":47003,") >= 0) {
log.error(res + " : " + template, "有可能是 企业名称字符多 , 超过了 微信小程序中的thing类型最多20个中英文的限制");
flag = true;
} else if (res.indexOf("{\"errcode\":40003,") >= 0) {
log.error(res + "相应申请人的wxOpenid不可用");
flag = true;
} else if (res.indexOf("{\"errcode\":43101,") >= 0) {
log.error(res + "有可能是 申请人在提交申请之后 , 没有订阅“一次性消息”");
flag = true;
} else if (res.indexOf("{\"errcode\":0,") >= 0) {
log.info("微信小程序成功推送了一条消息");
flag = true;
} else {
log.info("推送消息发生了其他情况 : " + res);
flag = true;
}
}
}
i++;
if (!flag) {//线程休眠3秒钟后重试3次
try {
Thread.sleep(3000);
} catch (Exception e) {
}
}
}
}
// 组装模板数据
public Template assembeTemplateAndReturnIt(ZxwqTs zxwqTs, String wxOpenid, String bizType, String toUserType) {
String templateId = EnvUtils.getValue("wxxcx.message." + bizType + ".template_id");
String page = "";
if("toGeren".equals(toUserType))
page = "pages/zhengxinweiquanGerenList/zhengxinweiquanGerenList?isAutoLogin=true";
else if("toJigou".equals(toUserType)){
page = "pages/zhengxinweiquanJigouList/zhengxinweiquanJigouList?isAutoLogin=true";
}
Template template = new Template();
template.setTemplate_id(templateId);
template.setTouser(wxOpenid);
template.setPage(page);
ArrayList<TemplateParam> params = new ArrayList<>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String dateStr = format.format(new Date());
params.add(new TemplateParam("time1", dateStr));
params.add(new TemplateParam("thing2", "您有待办的维权消息!"));
template.setTemplateParamList(params);
return template;
}
请发表评论