1、模版消息功能概述基于微信的通知渠道,为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验。模板推送位置:服务通知模板下发条件:用户本人在微 ...
基于微信的通知渠道,为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验。
<!-- ih_request.js -->
const request = require('request');
var ih_request = {};
module.exports = ih_request;
ih_request.get = async function(option){
var res = await req({
url: option.url,
method: 'get'
});
res.result?option.success(res.msg):option.error(res.msg);
}
const request = require('../script/ih_request');
await request.get({
url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET',
success: function(res){
console.log(res.access_token)
},
error: function(err){
console.log(err)
}
});
//app.js
App({
onLaunch: function() {
wx.login({
success: function(res) {
if (res.code) {
//发起网络请求 将code传给服务器
wx.request({
url: 'https://test.com/onLogin',
data: {
code: res.code
}
})
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
});
}
})
<!-- ih_request.js -->
const request = require('request');
var ih_request = {};
module.exports = ih_request;
ih_request.get = async function(option){
var res = await req({
url: option.url,
method: 'get'
});
res.result?option.success(res.msg):option.error(res.msg);
}
const request = require('../script/ih_request');
request.get({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code',
success: function(res){
console.log(res)
},
error: function(err){
console.log(err)
}
});
<!--index.wxml-->
<view class="container">
<view bindtap="bindViewTap" class="userinfo">
<image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</view>
<form bindsubmit="formSubmit" report-submit="true">
<input name="input" class="input" placeholder="please input here" />
<button formType="submit">Submit</button>
<button formType="reset">Reset</button>
</form>
</view>
<!--index.js-->
//实现绑定的formSubmit 将formId传给服务器
formSubmit: function (e) {
var that = this
wx.request({
url: 'https://ihealth-wx.s1.natapp.cc/template',
data: {
'input': e.detail.value.input,
'formId': e.detail.formId,
'code': that.data.login_res.code
},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 设置请求的 header
success: function(res){
// success
console.log('成功' + res);
// console.log(e.detail.formId);
},
fail: function(err) {
// fail
console.log('失败' + err);
},
complete: function() {
// complete
}
})
}
<!-- ih_request.js -->
const request = require('request');
var ih_request = {};
module.exports = ih_request;
ih_request.postJson = async function(option){
var res = await req({
url: option.url,
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(option.body)
});
res.result?option.success(res.msg):option.error(res.msg);
}
const request = require('../script/ih_request');
await request.postJson({
url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='+access_token,
body: {
touser: '触发帐号的opened',
template_id: '模版id',
page: '点击模版卡片的跳转页面',
form_id: 'form_id或者prepay_id',
data: {
keyword1:{
value: '小程序测试模版',
color: '#173177'
},
keyword2:{
value: '2017年3月24日',
color: '#173177'
},
keyword3:{
value: 'iHleath',
color: '#173177'
}
},
//需要放大的关键字
emphasis_keyword: 'keyword1.DATA'
},
success: function(res){
console.log(res);
},
error: function(err){
console.log(err);
}
});