PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > HBuilderX教程 > 正文

    uniapp中小程序的授权操作

    作者:admin来源:网络浏览:时间:2020-10-06 11:02:34我要评论
    导读:使用uniapp开发小程序时,大致获取授权及权限的操作如下:这里以获取位置信息为例,需要注意的是获取位置信息时,需要在manifest.json文件...
    使用uniapp开发小程序时,大致获取授权及权限的操作如下:
    这里以获取位置信息为例,需要注意的是获取位置信息时,需要在manifest.json文件中进行相应配置

    1. manifest.json: 
    2.  
    3. "permission": { 
    4.             "scope.userLocation": { 
    5.                 "desc""提示文字" 
    6.             } 
    7.         } 


    vue文件内部写法:


    1. <template> 
    2.     <view> 
    3.         <button @click="handleLocation">手动授权</button> 
    4.     </view> 
    5. </template> 
    6.  
    7. <script> 
    8.     export default { 
    9.         data() { 
    10.             return {} 
    11.         }, 
    12.         onLoad() { 
    13.             // 初始化页面,执行查看已授权信息 
    14.             this.getSettingMess(); 
    15.         }, 
    16.         methods: { 
    17.             // 执行函数 
    18.             getSettingMess() { 
    19.                 // 获取已授权类别 
    20.                 uni.getSetting({ 
    21.                     success(res) { 
    22.                         if (res.authSetting['scope.userLocation']) { 
    23.                             console.log("userLocation位置功能已授权"
    24.                             // 如果已授权,直接获取对应参数 
    25.                             uni.getLocation({ 
    26.                                 success(res) { 
    27.                                     console.log(res) 
    28.                                 } 
    29.                             }) 
    30.                         } else if (!res.authSetting['scope.userLocation']) { 
    31.                             // 说明此时要获取的位置功能尚未授权, 
    32.                             // 则设置进入页面时主动弹出,直接授权 
    33.                             uni.authorize({ 
    34.                                 scope: 'scope.userLocation'
    35.                                 success(res) { 
    36.                                     // 授权成功 
    37.                                     console.log(res) 
    38.                                     // 成功后获取对应的位置参数 
    39.                                     uni.getLocation({ 
    40.                                         success(res) { 
    41.                                             console.log(res) 
    42.                                         } 
    43.                                     }) 
    44.                                 }, 
    45.                                 fail() { 
    46.                                     console.log("位置授权失败"
    47.                                 } 
    48.                             }) 
    49.                         } 
    50.                     }, 
    51.                     fail() { 
    52.                         console.log("获取授权信息授权失败"
    53.                     } 
    54.                 }) 
    55.             }, 
    56.             // 如果初始进入页面时点击了拒绝授权,如需要该权限,需要手动授权 
    57.             // 手动授权的触发方法 
    58.             handleLocation() { 
    59.                 uni.getSetting({ 
    60.                     success(res) { 
    61.                         if (res.authSetting['scope.userLocation']) { 
    62.                             // 再次判断所需权限是否已授权,如已授权则直接使用 
    63.                             uni.getLocation({ 
    64.                                 success(res) { 
    65.                                     console.log(res) 
    66.                                 } 
    67.                             }) 
    68.                         } else if (!res.authSetting['scope.userLocation']) { 
    69.                             // 如未授权则重新打开设置页面,进行授权 
    70.                             uni.showModal({ 
    71.                                 //弹出提示框 
    72.                                 title: '是否打开设置页?'
    73.                                 content: '需要在设置中获取xx信息和xx权限'
    74.                                 success(res) { 
    75.                                     if (res.confirm) { 
    76.                                         // 用户点击确定按钮 
    77.                                         uni.openSetting({ 
    78.                                             // 确认后打开设置页面 
    79.                                             success(res) { 
    80.                                                 if(res.authSetting['scope.userLocation']){ 
    81.                                                     console.log("手动授权成功"
    82.                                                 }else
    83.                                                     console.log("手动授权失败"
    84.                                                 } 
    85.                                             }, 
    86.                                             fail(){ 
    87.                                                 console.log("打开设置页面失败"
    88.                                             } 
    89.                                         }) 
    90.                                     } else if (res.cancel) { 
    91.                                         // 用户点击取消按钮 
    92.                                         console.log('用户点击取消'); 
    93.                                     } 
    94.                                 }, 
    95.                                 fail(){ 
    96.                                     console.log("确认取消弹出未弹出"
    97.                                 } 
    98.                             }); 
    99.                         } 
    100.                     }, 
    101.                     fail(){ 
    102.                         console.log("获取已授权信息失败"
    103.                     } 
    104.                 }) 
    105.             } 
    106.         } 
    107.     } 
    108. </script> 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-146-6348-1.html
    相关热词搜索: uniapp uniapp教程