文章目录

js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Page({

data: {
openid:"",
session_key:"",
access_token:"",
},

onLoad: function (options) {
var that = this
//第一步获取openid
wx.login({
success: function (data) {
console.log(data.code, data)
console.log("12345");
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data:{
appid: "你的appid",
secret: "小程序秘钥",
js_code: data.code//wx.login获取到的code
},
method: "post",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(res);
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=你的小程序秘钥',
method: "GET",
success: function (res) {

console.log("xxx");
console.log(res);
that.setData({
access_token: res.data.access_token,//获取到的access_token
})
}
})
},
form: function (e) {
console.log(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: '模板消息ID',
page: '/pages/index/index',
form_id: fId,
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: JSON.stringify(d),
method: 'POST',
success: function (res) {
console.log(res);
},
fail: function (err) {
console.log(err);
}
});
},
})

wxml

1
2
3
4
5
<form bindsubmit="form" report-submit='true' >
<button form-type="submit" class='xx'>
<text>123</text>
</button>
</form>

在这里插入图片描述

成功!!!