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>

附截圖:

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM