美文网首页
简单的授权

简单的授权

作者: LUOTAOLUOTAO | 来源:发表于2020-08-10 15:47 被阅读0次
// 获取定位信息
function getLocation() {
  return new Promise((resolve, reject) => {
    uni.getLocation({
      type: "gcj02",
      success: (res = {}) => {
        const { longitude = "", latitude = "" } = res;
        console.log(longitude, latitude);
        const location = { longitude, latitude };
        resolve(location);
      },
      fail: err => {
        reject(new Error(err));
      }
    });
  });
}


// 授权
function getAuth(
  scopeType = "scope.userLocation",
  scopeText = "离你最近",
  callback = getLocation
) {
  uni.getSetting({
    success: res => {
      if (!res.authSetting[scopeType]) {
        // 接口调用询问
        uni.authorize({
          scope: scopeType,
          success: () => {
            if (callback) callback();
          },
          fail: () => {
            // 用户拒绝了授权
            uni.showModal({
              title: scopeText,
              content: scopeText + "需要您授权",
              showCancel: true,
              confirmText: "确定",
              success: res => {
                if (res.confirm) {
                  // 打开设置页面
                  uni.openSetting({
                    success: data => {
                      if (data.authSetting[scopeType]) {
                        // 授权成功
                        if (callback) callback();
                      } else {
                        uni.showToast({
                          title: "拒绝授权会导致此功能无法正常使用",
                          duration: 1500,
                          icon: "none"
                        });
                      }
                    }
                  });
                } else if (res.cancel) {
                  uni.showToast({
                    title: "拒绝授权会导致此功能无法正常使用",
                    duration: 1500,
                    icon: "none"
                  });
                }
              }
            });
          }
        });
      } else {
        if (callback) callback();
      }
    },
    fail(res) {
      console.log("getSetting: fail");
      console.log(res);
    }
  });
}

相关文章

网友评论

      本文标题:简单的授权

      本文链接:https://www.haomeiwen.com/subject/jnsjdktx.html