小程序订阅消息
/**
* 订阅消息
*/
subscriptionNotice: function() {
console.log('subscriptionNotice')
let that = this;
return new Promise((resolve, reject) => {
let obj = that.data.need_subscript_template;
let tmplIds = Object.keys(obj).map(key => obj[key]); // 订阅消息模版id
if (wx.requestSubscribeMessage) {
tmplIds.length && wx.requestSubscribeMessage({
tmplIds: tmplIds,
success(res) {
let is_need_subscript = 1;
let acceptId = [];
Object.keys(obj).forEach(item => {
if (res[obj[item]] == 'accept') {
//用户同意了订阅,添加进数据库
acceptId.push(item);
} else {
//用户拒绝了订阅或当前游戏被禁用订阅消息
is_need_subscript = 0;
}
})
if (acceptId.length) {
that.addAccept(acceptId);
}
that.setData({ is_need_subscript })
resolve();
},
fail() {
reject();
}
})
} else {
// 兼容处理
reject();
}
})
},
// 用户点击订阅添加到数据库
addAccept: function(acceptId) {
let token = wx.getStorageSync('token');
let type = acceptId.join(',');
app.util.request({
url: 'entry/wxapp/user',
data: {
controller: 'user.collect_subscriptmsg',
token,
type
},
dataType: 'json',
method: 'POST',
success: function() {}
})
},