說明
個人使用環境說明
- 設備環境:win10 64bit
- 編譯環境:HBuilder X
- 運行環境 :微信開發者工具、真機
其他說明
- 在微信小程序中,在調試模式、開發者模式中,調用用戶信息默認失敗。發布后,可以根據實際用戶授權返回授權的結果。
- 由於如果用戶之前拒絕過授權,此接口會直接調用師表回調。一般搭配
uni.getSetting(獲取當前用戶授權)和uni.openSetting(打開授權)使用。 - APP的授權判斷方式見https://ext.dcloud.net.cn/plugin?id=594。
- 地理位置權限需要在manifest.json 配置 permission,詳見https://uniapp.dcloud.io/collocation/manifest。
- 本人在測試該接口時,引用了ColorUI的樣式
調用
調用方法
uni.authorize(OBJECT)
功能
提前向用戶發起授權請求。調用后會立刻彈窗詢問用戶是否同意授權小程序使用某項功能或獲取用戶的某些數據,但不會實際調用對應接口。如果用戶之前已經同意授權,則不會出現彈窗,直接返回成功。如果用戶之前拒絕了授權,此接口會直接進入失敗回調,一般搭配uni.getSetting和uni.openSetting使用。
平台差異
| App |
H5 |
微信小程序 |
支付寶小程序 |
百度小程序 |
字節跳動小程序 |
QQ小程序 |
| × |
× |
√ |
× |
√ |
√ |
√ |
Object參數
| 參數 |
類型 |
必填 |
說明 |
| scope |
String |
是 |
需要獲取權限的 scope,詳見 scope 列表。 |
| success |
Function |
否 |
接口調用成功的回調函數 |
| fail |
Function |
否 |
接口調用失敗的回調函數 |
| complete |
Function |
否 |
接口調用結束的回調函數(調用成功、失敗都會執行) |
scope列表
| scope |
對應接口 |
描述 |
地理位置 |
| scope.userInfo |
uni.getUserInfo |
用戶信息 |
|
| scope.userLocation |
uni.getLocation, uni.chooseLocation |
地理位置 |
|
| scope.userLocationBackground |
wx.userLocationBackground |
后台定位 |
微信小程序 |
| scope.address |
uni.chooseAddress |
通信地址 |
|
| scope.record |
uni.getRecorderManager |
錄音功能 |
|
| scope.writePhotosAlbum |
uni.saveImageToPhotosAlbum, uni.saveVideoToPhotosAlbum |
保存到相冊 |
字節跳動小程序的返回值是scope.album |
| scope.camera |
<camera /> 組件,頭條下的掃碼、拍照、選擇相冊 |
攝像頭 |
|
| scope.invoice |
wx.chooseInvoice |
獲取發票 |
微信小程序、QQ小程序 |
| scope.invoiceTitle |
uni.chooseInvoiceTitle |
發票抬頭 |
微信小程序、百度小程序、QQ小程序 |
| scope.werun |
wx.getWeRunData |
微信運動步數 |
微信小程序 |
示例代碼
1 uni.authorize({ 2 scope: 'scope.userLocation', 3 success() { 4 uni.getLocation() 5 } 6 })
個人代碼
打開授權設置頁
授權獲取失敗時,根據用戶選擇,打開授權設置頁。
1 // 打開授權設置頁 : authouName: 授權名稱 2 openSetting(authouName) { 3 uni.showModal({ 4 title: '授權', 5 content: '獲取授權' + authouName + '失敗,是否前往授權設置?', 6 success: function(result) { 7 if (result.confirm) { 8 uni.openSetting(); 9 } 10 }, 11 fail: function() { 12 uni.showToast({ 13 title: '系統錯誤!', 14 icon: 'none' 15 }); 16 } 17 }) 18 }
配置授權
由於申請位置接口,需要在maifest.json在小程序特有相關中配置權限。以微信小程序為列,使用HBuilder X 打開項目的配置文件。點擊【微信小程序配置】→微信小程序權限的【位置】接口,並填寫權限申請原因。
1 /* 小程序特有相關 */ 2 "mp-weixin": { 3 "appid": "小程序appid", 4 "setting": { 5 "urlCheck": false 6 }, 7 "usingComponents": true, 8 "permission": { 9 "scope.userLocation": { 10 "desc": "小程序位置接口的效果展示" 11 } 12 } 13 },
頁面代碼
authorize.vue頁面代碼如下:
1 <template> 2 <view class="padding flex flex-direction bg-white"> 3 <!-- 授權設置頁 --> 4 <!-- #ifdef MP-WEIXIN || MP-BAIDU --> 5 <button class="cu-btn bg-orange margin-xs" open-type="openSetting">授權設置頁</button> 6 <!-- #endif --> 7 8 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aUserInfo">用戶信息</button> 9 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aLocation">地理位置</button> 10 <!-- #ifdef MP-WEIXIN --> 11 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aLocationBackground">后台定位</button> 12 <!-- #endif --> 13 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aAddress">通信地址</button> 14 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aRecord">錄音功能</button> 15 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aWritePhotosAlbum">保存到相冊</button> 16 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aCamera">攝像頭</button> 17 <!-- #ifdef MP-WEIXIN || MP-QQ --> 18 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aInvoice">獲取發票</button> 19 <!-- #endif --> 20 <!-- #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ --> 21 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aInvoiceTitle">發票抬頭</button> 22 <!-- #endif --> 23 <!-- #ifdef MP-WEIXIN --> 24 <button class="cu-btn bg-orange light margin-bottom-xs" @click="aWerun">微信步數</button> 25 <!-- #endif --> 26 </view> 27 </template> 28 29 <script> 30 var _self; 31 32 export default { 33 data() { 34 return { 35 36 } 37 }, 38 onLoad:function(){ 39 _self = this; 40 }, 41 methods: { 42 // ============================== 授權 ========================================== 43 // 用戶信息 44 aUserInfo(e){ 45 uni.authorize({ 46 scope:'scope.userInfo', 47 success:function(){ 48 console.log("獲取用戶信息:成功"); 49 }, 50 fail:function(){ 51 console.log("授權用戶信息:失敗"); 52 _self.openSetting("用戶信息"); 53 } 54 }); 55 }, 56 // 地理位置 57 aLocation(e){ 58 uni.authorize({ 59 scope:'scope.userLocation', 60 success:function(){ 61 console.log("授權地理位置:成功"); 62 }, 63 fail:function(){ 64 console.log("授權地理位置:失敗"); 65 _self.openSetting("地理位置"); 66 } 67 }); 68 }, 69 // 后台定位 (微信小程序) 70 // #ifdef MP-WEIXIN 71 aLocationBackground(){ 72 uni.authorize({ 73 scope:'scope.userLocationBackground', 74 success:function(){ 75 console.log("授權后台定位:成功"); 76 }, 77 fail:function(){ 78 console.log("授權后台定位:失敗"); 79 _self.openSetting("后台定位"); 80 } 81 }); 82 }, 83 // #endif 84 // 通訊地址 85 aAddress(){ 86 uni.authorize({ 87 scope:'scope.address', 88 success:function(){ 89 console.log("授權通訊地址:成功"); 90 }, 91 fail:function(){ 92 console.log("授權通訊地址:失敗"); 93 _self.openSetting("通訊地址"); 94 } 95 }); 96 }, 97 // 錄音功能 98 aRecord(){ 99 uni.authorize({ 100 scope:'scope.record', 101 success:function(){ 102 console.log("授權錄音功能:成功"); 103 }, 104 fail:function(){ 105 console.log("授權錄音功能:失敗"); 106 _self.openSetting("錄音功能"); 107 } 108 }); 109 }, 110 // 保存到相冊(字節跳動返回值是scope.album) 111 aWritePhotosAlbum(){ 112 uni.authorize({ 113 scope:'scope.writePhotosAlbum', 114 success:function(){ 115 console.log("保存到相冊:成功"); 116 }, 117 fail:function(e){ 118 console.log("保存到相冊:失敗"); 119 _self.openSetting("保存到相冊"); 120 } 121 }); 122 }, 123 // 攝像頭 124 aCamera(){ 125 uni.authorize({ 126 scope:'scope.camera', 127 success:function(){ 128 console.log("授權攝像頭:成功"); 129 }, 130 fail:function(){ 131 console.log("授權攝像頭:失敗"); 132 _self.openSetting("攝像頭"); 133 } 134 }); 135 }, 136 // 獲取發票(微信小程序、QQ小程序) 137 // #ifdef MP-WEIXIN || MP-QQ 138 aInvoice(){ 139 uni.authorize({ 140 scope:'scope.invoice', 141 success:function(){ 142 console.log("授權獲取發票:成功"); 143 }, 144 fail:function(){ 145 console.log("授權獲取發票:失敗"); 146 _self.openSetting("獲取發票"); 147 } 148 }); 149 }, 150 // #endif 151 // 發票抬頭(微信小程序、百度小程序、QQ小程序) 152 // #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ 153 aInvoiceTitle(){ 154 uni.authorize({ 155 scope:'scope.invoiceTitle', 156 success:function(){ 157 console.log("授權獲取發票抬頭:成功"); 158 }, 159 fail:function(){ 160 console.log("授權獲取發票抬頭:失敗"); 161 _self.openSetting("發票抬頭"); 162 } 163 }); 164 }, 165 // #endif 166 // 微信步數(微信小程序) 167 // #ifdef MP-WEIXIN 168 aWerun(){ 169 uni.authorize({ 170 scope:'scope.werun', 171 success:function(){ 172 console.log("授權微信步數:成功"); 173 }, 174 fail:function(){ 175 console.log("授權微信步數:失敗"); 176 _self.openSetting("微信步數"); 177 } 178 }); 179 }, 180 // #endif 181 // ===================================== 方法 =========================== 182 // 打開授權設置頁 : authouName: 授權名稱 183 openSetting(authouName){ 184 uni.showModal({ 185 title:'授權', 186 content:'獲取授權'+ authouName + '失敗,是否前往授權設置?', 187 success:function(result){ 188 if(result.confirm){ 189 uni.openSetting(); 190 } 191 }, 192 fail:function(){ 193 uni.showToast({ 194 title:'系統錯誤!', 195 icon:'none' 196 }); 197 } 198 }) 199 } 200 } 201 } 202 </script> 203 204 <style> 205 206 </style>
參考網址
uni-app官網授權:https://uniapp.dcloud.io/api/other/authorize
uni-app 官網配置:https://uniapp.dcloud.io/collocation/manifest?id=mp-weixin
微信小程序權限配置:https://developers.weixin.qq.com/miniprogram/dev/reference/configuration/app.html#permission
