消息模板
发送消息
地址:http://blog.csdn.net/eadio/article/details/53609193
需要form表单提交
<form name='pushMsgFm' report-submit bindsubmit='orderSign'>
<button form-type="submit">submit</button>
</form>
//获取openid
var user = wx.getStorageSync('user') || {};
if (!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) {//不要在30天后才更换openid-尽量提前10分钟更新
wx.login({
success: function (res) {
// success
var d = app.globalData;//这里存储了appid、secret、token串
var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appId + '&secret=' + d.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code';
wx.request({
url: l,
data: {},
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 设置请求的 header
success: function (res) {
var obj = {};
obj.openid = res.data.openid;
obj.expires_in = Date.now() + res.data.expires_in;
wx.setStorageSync('user', obj);//存储openid
}
});
}
});
} else {
console.log(user);
}
//发送消息
var fId = e.detail.formId;
var fObj = e.detail.value;
var l = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + app.globalData.token;
var d = {
touser: wx.getStorageSync('user').openid,
template_id: 'JkCe1JpKdM8ywduhkWNYTBl1suRzkTvXmTPh_b-rwB8',//这个是1、申请的模板消息id,
//page: '/pages/index/index',
form_id: fId,
data: {
"keyword1": {
"value": "adsf",
"color": "#4a4a4a"
},
"keyword2":{
"value":'123',
"color":'#4a4a4a'
},
"keyword3.": {
"value": new Date().getDate(),
"color": "#9b9b9b"
}
},
color: '#ccc',
//emphasis_keyword: 'keyword1.DATA'
}
wx.request({
url: l,
data: d,
method: 'POST',
success: function (res) {
console.log("push msg");
console.log(res);
},
fail: function (err) {
// fail
console.log("push err")
console.log(err);
}
});