小程序权限设置(位置)

3,147 阅读1分钟

小程序的权限问题

小程序开发中不免要获取用户的许多信息,而这些信息又必须获得用户的同意,但是这些信息一般只会请求一次(打开小程序-小程序销毁),之后会默认授权或默认拒绝授权,所以务必妥善处理。解决方案是可以通过用户手动设置重新改写权限。以下将以获取用户位置权限为例,其他权限也可作为参考


目录结构

|-----util

|---------util.js

|-----page

|---------location.js

|---------location.json

|---------location.wxml

|---------location.wxss


util.js代码

//获取位置设置信息
const GetSetting = (init,refused)=>{  
    init = typeof (init) === 'function' ? init : function (res) { };
    refused = typeof (refused) === 'function' ? refused : function (res) { };    
    wx.getSetting({      
        success: (res) => {        
            console.log(res);        
            console.log(res.authSetting['scope.userLocation']);        
            if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {          
                //非初始化进入该页面,且未授权         
                wx.showModal({            
                    title: '是否授权当前位置',            
                    content: '需要获取您的地理位置,请确认授权,否则无法获取地址',            
                    success: function (res) {              
                        if (res.cancel) {                
                            console.info("1授权失败返回数据");
                            refused();                          
                        } else if (res.confirm) {                                          
                            wx.openSetting({                  
                                success: function (data) {                    
                                    console.log(data);                    
                                    if (data.authSetting["scope.userLocation"] == true) {                      
                                        wx.showToast({                        
                                            title: '授权成功',                        
                                            icon: 'success',                        
                                            duration: 2000                      
                                        })           
                                    } else {                      
                                        wx.showToast({                        
                                            title: '授权失败',                        
                                            icon: 'none',                        
                                            duration: 2000                      
                                        });                     
                                    }                  
                                }                
                            })              
                        }            
                    }          
                })        
            } else if (res.authSetting['scope.userLocation'] == true){          
                init();        
            } else if (res.authSetting['scope.userLocation'] == undefined) {          
                //初始化进入          
                init();           
            }     
        }   
    })
}
const GetLocation = (success,fail)=>{  
    success = typeof (success) === 'function' ? success : function (res) { };  
    fail = typeof (fail) === 'function' ? fail : function (res) { };  
    wx.getLocation({    
        type: 'wgs84',    
        success:success,    
        fail:fail  
    })
}
module.exports = {  GetSetting,  GetLocation}

location.js

// pages/Quality/index.js
const util = require('../../utils/util');
Page({  
    /**   * 页面的初始数据   */  
    data: {      },    
    setLocation(lat,lon){     },   
    /**   * 生命周期函数--监听页面显示   */  
    onShow: function () {    
        util.GetSetting(      
            () => {        
                console.log("进入获取位置");        
                util.GetLocation((res) => {
                    //获取用户位置信息成功          
                    console.log('GetLocationSuccess:', res);         
                    //此处获取具体位置信息this.setLocation(res.latitude, res.longitude)        
                }, 
                (err) => {
                    //用户拒绝提供位置权限操作           
                    console.log('GetLocationFial:', err);          
                    //wx.switchTab({ url: '../newindex/index'})       
                })      
            } ,
            ()=>{
                //用户拒绝提供位置权限操作
                 console.log('GetLocationFial:', err);
                // wx.switchTab({url: '../newindex/index'})

            }
        ) 
    }
})


可以设置权限的列表

都是一些个人开发总结,不喜勿喷,谢谢