微信小程序服务通知模板的实现

本文介绍了微信小程序中服务通知模板消息的实现方法,重点是通过wxml和js代码示例展示开发流程。工程师需要根据官方文档进行开发,并注意formId的获取必须在真机上进行调试,因为开发工具中得到的formId是模拟值。
摘要由CSDN通过智能技术生成

小程序服务通知对应的技术实现是模板消息,是需要做技术开发的,对于工程师们来说,看一下官方文档就能上手。

 

下面直接上代码:

wxml:

<form name='pushMsgFm' report-submit bindsubmit='form'>
    <button form-type="submit">submit</button>
</form>
 

js:


// pages/index/index.js
Page({
data: {
},
/*
微信公众平台测试连接 https://mp.weixin.qq.com/debug/
全局返回码说明:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433747234

 

*/
onLoad: function (options) {
var that = this
wx.login({
success: function (data) {
console.log(data.code, data)
// 获取openid
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=你的自己的appid&secret=你自己的session_key&js_code=' + data.code ,
header:  {  "Content-Type":  "application/x-www-form-urlencoded"  },
method: "post",
success: function (res) {
console.log(res, "opind")
that.setData({
openid: res.data.openid,
session_key: res.data.session_key,
})
}
})
}
})
// 获取access_token
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的自己的appid&secret=你自己的session_key',
method: "GET",
success: function (res) {
console.log(res, "res")
console.log(res.data.access_token, "access_token")
that.setData({
access_token: res.data.access_token,
})
}
})
},
// 点击执行方法
form: function (e) {
var that = this;
var fId = e.detail.formId;
// 网络请求
var l = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.access_token;
// 需要传的参数
var d = {
touser: that.data.openid, //用户的openid
template_id: 'XX1hceIwI1XiQaUc5Z4qIrZnYQkYEHElSq5m6yIa0M8',//这个是申请的模板消息id,位置在微信公众平台/模板消息中添加并获取
page: '/pages/index/index', //点击通知跳转的页面
form_id: fId, //表单提交场景下,为 submit 事件带上的 formId
 
//此处必须为data,只有人说value也可以,可能官方已经修复这个bug
data: {
"keyword1": {
"value": "酒店",
"color": "#4a4a4a"
},
"keyword2": {
"value": "2018-03-22",
"color": "#9b9b9b",
},
"keyword3": {
"value": "$300",
"color": "#9b9b9b"
},
"keyword4": {
"value": "中国",
"color": "#9b9b9b"
},
 
},
color: '#ccc',
emphasis_keyword: 'keyword1.DATA'
}
wx.request({
url: l,
data: d,
method: 'POST', //此处不能有请求头
success: function (res) {
console.log(res, "push msg");
},
fail: function (err) {
console.log(err, "push err");
}
});
},
})
      

 

最后编译,点击提交发起请求,这里只能手机调试,我用开发工具打印出来的formId: "the formId is a mock one"并不是数字串,原因戳为什么formId没有数字串值。此处需要真机测试才能成功

转自https://www.hishop.com.cn/tags/15397.html 

要使用`RestTemplate`来实现微信小程序服务通知,你需要先获取access_token,然后使用该access_token发送模板消息。 下面是一个使用`RestTemplate`发送微信小程序服务通知的示例代码: ```java RestTemplate restTemplate = new RestTemplate(); // 获取access_token String appId = "YOUR_APP_ID"; String appSecret = "YOUR_APP_SECRET"; String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret; ResponseEntity<AccessTokenResponse> accessTokenResponse = restTemplate.getForEntity(accessTokenUrl, AccessTokenResponse.class); String accessToken = accessTokenResponse.getBody().getAccess_token(); // 发送模板消息 String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + accessToken; WxOpenTemplateMessageRequest request = new WxOpenTemplateMessageRequest(); request.setTouser("OPENID"); request.setTemplate_id("TEMPLATE_ID"); // 设置模板消息参数 // request.addData("keyword1", "VALUE1"); // request.addData("keyword2", "VALUE2"); // ... HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<WxOpenTemplateMessageRequest> entity = new HttpEntity<>(request, headers); ResponseEntity<SendTemplateMessageResponse> sendResponse = restTemplate.postForEntity(sendUrl, entity, SendTemplateMessageResponse.class); if (sendResponse.getStatusCode() == HttpStatus.OK) { SendTemplateMessageResponse sendResult = sendResponse.getBody(); if (sendResult.getErrcode() == 0) { // 发送成功 } else { // 发送失败 } } else { // 请求失败 } ``` 在上面的代码中,首先使用`RestTemplate`获取access_token,然后构建模板消息请求`WxOpenTemplateMessageRequest`,设置相应的参数,包括touser(接收用户的openid)、template_id(模板ID)和模板消息的具体参数。 然后,通过`RestTemplate`发送POST请求,将模板消息请求作为请求体发送到指定的URL。最后,根据响应结果判断发送是否成功。 请根据你的实际情况替换相应的参数,例如`YOUR_APP_ID`、`YOUR_APP_SECRET`、`OPENID`和`TEMPLATE_ID`。 希望对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值