巨焰小程序接入订阅消息

文章发布时间:

最后更新时间:

巨焰小程序接入订阅消息

页面 index.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
data: {
code: '',
templateid: '',
appid:'',
openid:''
},

onLoad(){
let that = this
//开始获取code
that.onCode()
//三秒后弹窗让用户订阅
setTimeout(() => {
wx.showModal({
title: '提示',
content: '是否订阅XXXX消息',
success(res) {
if (res.confirm) {
console.log('用户点击确定')
that.onTake()
} else if (res.cancel) {
console.log('用户点击取消')
that.onTake()
}
}
})
}, 3000)
}
//获取code
onCode() {
console.log('开始获取Code');
let that = this
wx.login({
timeout: 10000,
success: (res) => {
console.log(res);
that.setData({
code: res.code
})
},
})
},
// 发起订阅消息
onTake() {
console.log('开始订阅');
let that = this
let accountInfo = wx.getAccountInfoSync();
let appid = accountInfo.miniProgram.appId;
// 获取模板id
wx.request({
url: 'https://newsaas.guangzhouzhuangxiu01.cn/api/xcx/index/xcxsubtemplate',
data: {
wxappid: appid
},
success(res) {
console.log(res.data[0]);
that.setData({
templateid: res.data[0].templateid,
appid,
})
wx.requestSubscribeMessage({
tmplIds: [that.data.templateid],
success(res) {
if (res[that.data.templateid] === 'accept') {
console.log('用户同意了')
// 订阅小程序获取用户openid
wx.request({
url: 'https://newsaas.guangzhouzhuangxiu01.cn/api/xcx/index/getwxopenid',
data:{
name: '',
appId: appid,
secret: '132',
tmplIds: that.data.templateid,
code: that.data.code
},
success(res1){
console.log('订阅完成:', res1);
wx.showToast({
title: '订阅OK!',
duration: 1000,
})
that.setData({
openid: res1.data.openid
})
}
})
}
},
})
}
})
},