function requestMsgNotice(templateIds) {

  return new Promise((resolve) => {

    let ids = [];

    wx.getSetting({

      withSubscriptions: true,

      success(res) {

        console.log(res)

        if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关

          if (res.subscriptionsSetting

            .itemSettings) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权

            let itemSettings = res.subscriptionsSetting.itemSettings; // 用户同意的消息模板id

            let status = true;

            //判断模板是否全部授权

            for (let i = 0; i < templateIds.length; i++) {

              let key = templateIds[i];

              if (itemSettings[key] != 'accept') {

                ids.push(key);

                status = false;

                break;

              }

            }

            if (!status) {

              wx.requestSubscribeMessage({

                tmplIds: ids,

                success(res) { },

              })

            } else {

              wx.requestSubscribeMessage({

                tmplIds: templateIds,

                success(res) { },

              })

              wx.showToast({

                title: '您已经订阅通知'

              })

            }

          } else {

            wx.requestSubscribeMessage({

              tmplIds: templateIds,

              success(res) { },

            })

          }

        } else {

          wx.openSetting({

            withSubscriptions: true,

          })
        }
      }
    })
  });

}

由于项目紧急,没有留太多批注,有问题的朋友可留言讨论

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐