zoukankan      html  css  js  c++  java
  • uniapp中小程序的授权操作

    使用uniapp开发小程序时,大致获取授权及权限的操作如下:

    这里以获取位置信息为例,需要注意的是获取位置信息时,需要在manifest.json文件中进行相应配置

    manifest.json:

    "permission": {
                "scope.userLocation": {
                    "desc": "提示文字"
                }
            }

    vue文件内部写法:

    <template>
        <view>
            <button @click="handleLocation">手动授权</button>
        </view>
    </template>
    
    <script>
        export default {
            data() {
                return {}
            },
            onLoad() {
                // 初始化页面,执行查看已授权信息
                this.getSettingMess();
            },
            methods: {
                // 执行函数
                getSettingMess() {
                    // 获取已授权类别
                    uni.getSetting({
                        success(res) {
                            if (res.authSetting['scope.userLocation']) {
                                console.log("userLocation位置功能已授权")
                                // 如果已授权,直接获取对应参数
                                uni.getLocation({
                                    success(res) {
                                        console.log(res)
                                    }
                                })
                            } else if (!res.authSetting['scope.userLocation']) {
                                // 说明此时要获取的位置功能尚未授权,
                                // 则设置进入页面时主动弹出,直接授权
                                uni.authorize({
                                    scope: 'scope.userLocation',
                                    success(res) {
                                        // 授权成功
                                        console.log(res)
                                        // 成功后获取对应的位置参数
                                        uni.getLocation({
                                            success(res) {
                                                console.log(res)
                                            }
                                        })
                                    },
                                    fail() {
                                        console.log("位置授权失败")
                                    }
                                })
                            }
                        },
                        fail() {
                            console.log("获取授权信息授权失败")
                        }
                    })
                },
                // 如果初始进入页面时点击了拒绝授权,如需要该权限,需要手动授权
                // 手动授权的触发方法
                handleLocation() {
                    uni.getSetting({
                        success(res) {
                            if (res.authSetting['scope.userLocation']) {
                                // 再次判断所需权限是否已授权,如已授权则直接使用
                                uni.getLocation({
                                    success(res) {
                                        console.log(res)
                                    }
                                })
                            } else if (!res.authSetting['scope.userLocation']) {
                                // 如未授权则重新打开设置页面,进行授权
                                uni.showModal({
                                    //弹出提示框
                                    title: '是否打开设置页?',
                                    content: '需要在设置中获取xx信息和xx权限',
                                    success(res) {
                                        if (res.confirm) {
                                            // 用户点击确定按钮
                                            uni.openSetting({
                                                // 确认后打开设置页面
                                                success(res) {
                                                    if(res.authSetting['scope.userLocation']){
                                                        console.log("手动授权成功")
                                                    }else{
                                                        console.log("手动授权失败")
                                                    }
                                                },
                                                fail(){
                                                    console.log("打开设置页面失败")
                                                }
                                            })
                                        } else if (res.cancel) {
                                            // 用户点击取消按钮
                                            console.log('用户点击取消');
                                        }
                                    },
                                    fail(){
                                        console.log("确认取消弹出未弹出")
                                    }
                                });
                            }
                        },
                        fail(){
                            console.log("获取已授权信息失败")
                        }
                    })
                }
            }
        }
    </script>

    附截图:

     

  • 相关阅读:
    python脚本 – 删除指定天数前的文件
    java 获取屏幕的分辩率
    解决Multi input/output stream coders are not yet supported(org.apache.commons.compress)
    解决tomcat at org.apache.tomcat.util.buf.CharChunk.append(CharChunk.java:355)
    org.quartz.jobStore.misfireThreshold = 60000
    python list 自定义排序
    利用pycron在windows上实现cron定时任务
    [Selenium+Java] Scroll UP or Down a page in Selenium Webdriver
    Python获取硬件信息(硬盘序列号,CPU序列号)
    ChromeDriver自动更新、FirefoxDriver自动更新、InternetExplorerDriver自动更新(Java+Python)
  • 原文地址:https://www.cnblogs.com/Alex-Song/p/12162802.html
Copyright © 2011-2022 走看看