用时授权处理
发布于 5 年前 作者 qiang62 2030 次浏览 来自 分享

忽略logger,我自己的另一个打印数据的类

/**
 * 判断第一次授权还是再次设置授权
 */
function settingScopePromise(scope, notice) {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success(res) {
        if (res.authSetting[scope] == undefined) {
          wx.authorize({
            scope: scope,
            success(res) {
              //授权成功
              resolve("authorize success");
            },
            fail(error) {
              //授权才能使用
              const reject_obj = {
                scope: true,
                data: "授权失败"
              };
              reject(reject_obj);
              logger.error(reject_obj, error);
            }
          });
        } else if (res.authSetting[scope] == false) {
          ///打开设置开始授权【打开授权设置】
          wx.openSetting({
            success(res) {
              if (!res.authSetting[scope]) {
                //请打开授权
                resolve("openSetting success cancel");
              }
              if (res.authSetting[scope]) {
                //授权成功
                resolve("openSetting success ok");
              }
            },
            fail(error) {
              //授权失败
              const reject_obj = {
                scope: true,
                data: "打开设置授权失败"
              };
              reject(reject_obj);
              logger.error(reject_obj, error);
            }
          });
          ///
        } else if (res.authSetting[scope] == true) {
          resolve("authorize success already");
        }
      },
      fail(error) {
        const reject_obj = {
          scope: true,
          data: "getSetting error"
        };
        reject(reject_obj);
        logger.error(reject_obj, error);
      }
    });
  });
}
/**
 * 授权过了的状态
 */
function scopeOKBool(result) {
  if (result == "openSetting success ok" || result == "authorize success" || result == "authorize success already") {
    return true;
  } else {
    return false;
  }
}

回到顶部