小程序订阅消息

 小程序订阅消息

 /**
     * 订阅消息
     */
    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() {}
        })
    },

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值