帖子
帖子
用户
博客
课程
显示全部楼层
26
帖子
0
勋章
221
Y币

APP授权的那点事

[复制链接]
发表于 2021-5-14 16:28:15
1、在根目录下res里新建一个 manifest.xml ;内容如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <manifest>
  3.     <application name="targetSdkVersion" value="28"/>
  4. </manifest>
复制代码
2、在所需要授权的地方加上授权代码,这里以储存空间为例,操作相册
  1. <div class="update-voucher-wrap" onclick="updateVoucher()">
  2.                         <div class="update-voucher">+</div>
  3.                         <span class="update-voucher-text">添加照片</span>
  4.                     </div>
复制代码
2-1、行为事件
  1. function updateVoucher() {
  2.     // 判断是否开启储存权限
  3.     var photos = api.hasPermission({list:['photos','photos-w']})
  4.     var result = photos.every(function(v){
  5.         return v.granted === true
  6.     })
  7.     if(result){
  8.         // 通过调用系统默认相机或者图库应用,获取图片以及视频媒体文件
  9.         api.getPicture({
  10.             sourceType: 'album',
  11.             encodingType: 'jpg',
  12.             mediaValue: 'pic',
  13.             destinationType: 'base64',
  14.             allowEdit: true,
  15.             quality: 50,
  16.             saveToPhotoAlbum: false
  17.         }, function (ret, err) {
  18.             if (ret) {
  19.                 let pic = ret.base64Data;
  20.                 console.log(pic);
  21.                 // 这里进行ajax请求
  22.             } else {
  23.                 alert(JSON.stringify(err));
  24.             }
  25.         });
  26.     }else{
  27.         var permission = ['photos','photos-w'];
  28.         api.confirm({
  29.             msg: '应用需要您的授权才能访问相册',
  30.             buttons: ['取消', '去设置']
  31.         }, function(ret) {
  32.             if (ret.buttonIndex == 2) {
  33.                 api.requestPermission({
  34.                     list: permission,
  35.                 }, function(res) {
  36.                     var item = res.list.every(function (v) {
  37.                         return v.granted === true
  38.                     })
  39.                     if (item) {
  40.                         // 已授权,可以继续下一步操作
  41.                         api.alert({ msg: '已授权' });
  42.                     }
  43.                 });
  44.             }
  45.         });
  46.     }
  47. }
复制代码

Tips:其他的授权原理同样;最好在第一次进入app 的时候加一个授权提示,这样之后的就不需要了,单独增加的 也只是为了用户把授权关掉,导致我们没法操作所家的提示;

不想复制我的,可以下载常山赵子云版主发的链接  https://community.apicloud.com/bbs/thread-110959-1-1.html
380
帖子
4
勋章
6
Y币
感谢分享
11
帖子
0
勋章
292
Y币
您需要登录后才可以回帖 登录

本版积分规则