温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序服务通知功能如何实现

发布时间:2022-03-16 09:27:33 来源:亿速云 阅读:973 作者:iii 栏目:开发技术

这篇文章主要介绍“微信小程序服务通知功能如何实现”,在日常操作中,相信很多人在微信小程序服务通知功能如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”微信小程序服务通知功能如何实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

下面直接上代码:

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"并不是数字串。此处需要真机测试才能成功

到此,关于“微信小程序服务通知功能如何实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI