uniapp 统一获取授权提示和48小时间隔授权

应用商店审核要求 获取权限前需要给提示,拒绝之后48小时不能给弹窗授权
项目用的是uniapp

 getImagePermission(v?: string, tag?: any, source?: any, proj?: any) {
    // proj = proj || vueSelf.$proj(tag, source);
    let data = {
      state: false,//是否原生授权
      denied: true,//是否提示
      permissionList: [
        {
          permissionName: "相机",
          tips: "允许应用打开摄像头",
          androidPermission: "android.permission.CAMERA",
        },
        {
          permissionName: "相册",
          tips: "允许应用读取存储卡上的照片、媒体内容和文件",
          androidPermission: "android.permission.READ_EXTERNAL_STORAGE",
        },
      ],
    };
   userPermissions(
      JSON.stringify(data), null, null, null,
      function (res) {
        console.log("getImagePermission res", res);
        // console.log("getImagePermission back", back);
        // back(res === 1 ? true : false)
        vueSelf.$callback('abAct.getImagePermission', null, res == 1 ? true : false);
      }
    );
	// 这里是因为异步问题,必须写这个
    return true
  },
  _androidPermissions: <any>undefined,

  userPermissions(v?: string, back?: any) {
    proj = proj || vueSelf.$proj(tag, source);
    // #ifdef APP
    try {
      if (axCc.info.platform == "android") {
        let permissionDs: any[] = JSON.parse(v);
        let permissionState = false;
        let permissionDenied = "";
        if (!Array.isArray(permissionDs)) {
          // @ts-ignore
          if (Array.isArray(permissionDs.permissionList)) {
            // @ts-ignore
            permissionState = permissionDs.state;
            // @ts-ignore
            permissionDenied = permissionDs.denied;
            // @ts-ignore
            permissionDs = permissionDs.permissionList;
          } else {
            permissionDs = [permissionDs];
          }
        }

        let androidPermissionsDirty = false;
        let androidPermissions = SdkActs._androidPermissions;
        if (!androidPermissions) {
          androidPermissions = axCc.getStorage("_androidPermissions");
          if (typeof androidPermissions !== "object") {
            androidPermissions = {};
          }

          SdkActs._androidPermissions = androidPermissions;
        }

        let mainActivity: any;
        let nowTime: any;
        let deniedTime: any;
        let deniedPermissions;
        let requestPermissions;
        let nonePermissions = false;
        let tips = "";
        for (let i = permissionDs.length - 1; i >= 0; i--) {
          let permissionD = permissionDs[i];
          let permission = permissionD.androidPermission;
          let permissionV = androidPermissions[permission];
          if (permissionV === 1) {
            permissionDs.splice(i, 1);
          } else {
            mainActivity || (mainActivity = plus.android.runtimeMainActivity());
            if (mainActivity.checkSelfPermission(permission) === 0) {
              permissionDs.splice(i, 1);
              androidPermissionsDirty = true;
              androidPermissions[permission] = 1;
            } else {
              // 暂停二次授权间隔48小时
              if (!deniedTime) {
                nowTime = new Date().getTime();
                deniedTime = nowTime - 48 * 24 * 3600;
              }
              if (permissionV > deniedTime) {
                // 暂停二次授权
                (deniedPermissions || (deniedPermissions = [])).push(
                  permissionD
                );
              } else {
                if (permissionD.tips && !permissionState) {
                  // 可授权提示
                  tips = permissionD.tips + tips;
                  (requestPermissions || (requestPermissions = [])).push(
                    permission
                  );
                } else {
                  nonePermissions = true;
                }
              }
            }
          }
        }

        if (androidPermissionsDirty) {
          // 权限记录
          androidPermissionsDirty = false;
          axCc.saveStorage("_androidPermissions", androidPermissions);
        }
        if (nonePermissions) {
          // 没有授权
          back
            ? back(-1)
            : vueSelf.$callback("abAct.userPermissions", proj, -1);
          return;
        } else if (deniedPermissions) {
          // 暂停二次授权
          if (permissionDenied) {
            uni.showModal({
              title: "提示",
              content: "没有权限",
              showCancel: false,
              complete(result) {
                back
                  ? back(-2)
                  : vueSelf.$callback("abAct.userPermissions", proj, -2);
              },
            });

            return true;
          }

          back
            ? back(-2)
            : vueSelf.$callback("abAct.userPermissions", proj, -2);
          return;
        } else if (tips) {
          uni.showModal({
            title: "权限申请",
            content: tips,
            success(res) {
              if (res.confirm) {
                // https://www.html5plus.org/doc/zh_cn/android.html#plus.android.requestPermissions
                plus.android.requestPermissions(requestPermissions, (e) => {

                  for (let i = e.deniedAlways.length - 1; i >= 0; i--) {
                    androidPermissions[e.deniedAlways[i]] = nowTime;
                  }

                  for (let i = e.deniedPresent.length - 1; i >= 0; i--) {
                    androidPermissions[e.deniedPresent[i]] = nowTime;
                  }

                  for (let i = e.granted.length - 1; i >= 0; i--) {
                    androidPermissions[e.granted[i]] = 1;
                  }

                  axCc.saveStorage("_androidPermissions", androidPermissions);

                  if (e.deniedAlways.length > 0) {
                    //权限被永久拒绝
                    // 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启
                    back
                      ? back(-2)
                      : vueSelf.$callback("abAct.userPermissions", proj, -2);
                  } else if (
                    e.deniedPresent.length > 0 ||
                    e.granted.length <= 0
                  ) {
                    //权限被临时拒绝
                    // 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限
                    back
                      ? back(-1)
                      : vueSelf.$callback("abAct.userPermissions", proj, -1);
                  } else {
                    //权限被允许
                    //调用依赖获取定位权限的代码
                    back
                      ? back(1)
                      : vueSelf.$callback("abAct.userPermissions", proj, 1);
                  }
                });
              } else if (res.cancel) {
                // 拒接授权
                back
                  ? back(0)
                  : vueSelf.$callback("abAct.userPermissions", proj, 0);
              }
            },
          });

          return true;
        }

        back ? back(1) : vueSelf.$callback("abAct.userPermissions", proj, 1);
        return;
      }
    } catch (e) {
      console.error(e);
    }
    // #endif
    back ? back(1) : vueSelf.$callback("abAct.userPermissions", proj, 1);
  },

你可能感兴趣的:(uni-app)