第一步,html 内容,添加按钮点击事件

<text>{{textcontent}}</text>
<button bindtap="subTap" hover-class="other-button-hover">允许订阅消息</button>


第二步,js 内容

data: {
    textcontent: '提示:未开启'
},
 
// 检测是否开启  更新提示
  testingTap: function() {
    let that = this;
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        // console.log(res)
        if (res.subscriptionsSetting.mainSwitch) {
          if (res.subscriptionsSetting.itemSettings != null) {
            let item = res.subscriptionsSetting.itemSettings.P2wDrfX0CYmme5mgnmyxVdbXMZFp1mjByg9dE4_YrdQ
            if (item == "reject") {
              that.setData({
                textcontent: '提示:您已经拒绝订阅消息'
              })
            } else if (item == "accept") {
              that.setData({
                textcontent: '提示:您已经开启订阅消息'
              })
            } else if (item == "ban") {
              that.setData({
                textcontent: '提示:您已经被后台封禁'
              })
            }
          }
        } else {
          that.setData({
            textcontent: '提示:订阅消息未开启'
          })
        }
      }
    })
  },
 
//授权
subTap: function() {
    let that = this;
    wx.requestSubscribeMessage({
      tmplIds: ['P2wDrfX0CYmme5mgnmyxVdbXMZFp1mjByg9dE4_YrdQ'],
      success(res) {
        that.setData({
          textcontent: '提示:授权成功'
        })
      },
      fail(res) {
        that.setData({
          textcontent: '提示:授权失败'
        })
      }
    })
  },
 
 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐