uniapp隐私弹框权限说明(记录)

381 阅读1分钟

做uniapp 上架应用商城 获取权限需要权限说明 记录过程

使用蒙层方式

image.png


const popview = ref(null);

const createPop = (title, content) => {
  popview.value = new plus.nativeObj.View('pop', {
    top: '0px', // 视图固定在顶部
    left: '0px',
    width: '100%',
    height: 'auto', // 高度自适应内容
    backgroundColor: '#444',
  });

  // 标题 - 顶部显示
  popview.value.drawText(
    title || '相机权限',
    {
      top: '60px', // 距离视图顶部20px
      left: '20px',
      width: '90%',
      height: '30px',
    },
    {
      color: '#ffffff',
      size: '25px',
      textAlign: 'left',
    },
  );

  // 内容 - 在标题下方
  popview.value.drawText(
    content || '为了给您提供拍摄、上传图片、与客服沟通等场景中使用',
    {
      top: '100px', // 在标题下方40px (20+30+10)
      left: '20px',
      width: '90%',
      height: '50px',
    },
    {
      color: '#ffffff',
      whiteSpace: 'normal',
      textAlign: 'left',
    },
  );

  nextTick(() => {
    popview.value.show();
  });
};


// 发送图片消息
const handleSendImageMsg = () => {
  if (isTeamMute.value) return;
  stopAllAudio();

  // 在点击需要权限的方法前先获取是否有权限
  var Manifest = plus.android.importClass('android.Manifest');
  var MainActivity = plus.android.runtimeMainActivity();
  let camera = MainActivity.checkSelfPermission(Manifest.permission.CAMERA);
  let storage = MainActivity.checkSelfPermission(
    Manifest.permission.READ_EXTERNAL_STORAGE,
  );
  if (camera != -1 && storage != -1) {
    uni.chooseImage({
      count: 6,
      sizeType: ['compressed'],
      success: (res) => {
        const promises = res.tempFilePaths.map((item) => {
          return uni.$UIKitStore.msgStore
            .sendImageMsgActive({
              scene: props.scene as TMsgScene,
              to: props.to,
              filePath: item,
            })
            .then(() => {
              // 这里不需要在每个成功时都调用 scrollBottom(),因为我们会在所有完成后调用
            })
            .catch((error) => {
              // 可以在这里处理每个发送失败的错误,但不影响其他图片的发送
              uni.showToast({
                icon: 'error',
                title: t('sendImageFailedText'),
              });
            });
        });
        Promise.all(promises)
          .then(() => {
            // 所有图片发送完成后调用 scrollBottom()
            scrollBottom();
          })
          .catch((error) => {
            scrollBottom();
          });
      },
      //没有开启权限时,提示开启权限
      complete: handleNoPermission,
    });
  } else {
    createPop();
    plus.android.requestPermissions(
      ['android.permission.CAMERA', 'android.permission.READ_EXTERNAL_STORAGE'],
      (e) => {
        if (e.deniedPresent.length > 0 || e.deniedAlways.length > 0) {
          popview.value.hide();
          uni.showModal({
            title: '权限获取',
            content: '需要相机和存储权限才能给客服发送图片,是否去设置开启?',
            success(res) {
              if (res.confirm) {
                var Intent = plus.android.importClass('android.content.Intent');
                var Settings = plus.android.importClass(
                  'android.provider.Settings',
                );
                var intent = new Intent(
                  Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                );
                intent.setData(
                  plus.android.invoke(
                    'android.net.Uri',
                    'parse',
                    'package:' + MainActivity.getPackageName(),
                  ),
                );
                MainActivity.startActivity(intent);
              }
            },
          });
        } else if (e.granted.length > 0) {
        } else {
        }
        popview.value.hide(); // 别忘了关闭提示
      },
    );
  }
};

第二种方式 弹框方式


// 发送图片消息
const handleSendImageMsg = () => {
  if (isTeamMute.value) return;
  stopAllAudio();

  // 在点击需要权限的方法前先获取是否有权限
  var Manifest = plus.android.importClass('android.Manifest');
  var MainActivity = plus.android.runtimeMainActivity();
  let camera = MainActivity.checkSelfPermission(Manifest.permission.CAMERA);
  let storage = MainActivity.checkSelfPermission(
    Manifest.permission.READ_EXTERNAL_STORAGE,
  );
  if (camera != -1 && storage != -1) {
    uni.chooseImage({
      count: 6,
      sizeType: ['compressed'],
      success: (res) => {
        const promises = res.tempFilePaths.map((item) => {
          return uni.$UIKitStore.msgStore
            .sendImageMsgActive({
              scene: props.scene as TMsgScene,
              to: props.to,
              filePath: item,
            })
            .then(() => {
              // 这里不需要在每个成功时都调用 scrollBottom(),因为我们会在所有完成后调用
            })
            .catch((error) => {
              // 可以在这里处理每个发送失败的错误,但不影响其他图片的发送
              uni.showToast({
                icon: 'error',
                title: t('sendImageFailedText'),
              });
            });
        });
        Promise.all(promises)
          .then(() => {
            // 所有图片发送完成后调用 scrollBottom()
            scrollBottom();
          })
          .catch((error) => {
            scrollBottom();
          });
      },
      //没有开启权限时,提示开启权限
      complete: handleNoPermission,
    });
  } else {
    uni.showModal({
      title: '权限获取',
      content: '需要相机和存储权限才能给客服发送图片,是否去设置开启?',
      success(res) {
        if (res.confirm) {
          plus.android.requestPermissions(
            [
              'android.permission.CAMERA',
              'android.permission.READ_EXTERNAL_STORAGE',
            ],
            (e) => {
              if (e.deniedPresent.length > 0 || e.deniedAlways.length > 0) {
                uni.showToast({
                  icon: 'none',
                  title: '获取拍摄、相册权限失败,请在设置中打开',
                });
              } else if (e.granted.length > 0) {
              } else {
              }
            },
          );
        }
      },
    });
  }

};

参考文章链接:uniapp权限检测