小程式調用裝置相關的JavaScript API前(參見scope列表對應介面),需要提前向使用者發起授權請求。調用後會立刻彈窗詢問使用者是否同意授權小程式使用某項功能或擷取使用者的某些資料,但不會實際調用對應的JavaScript API。如果使用者之前已經同意授權,則不會出現彈窗,直接返回成功。
入參
參數 | 類型 | 必填 | 說明 |
scope | String | 是 | 需要擷取許可權的scope。 |
success | Function | 否 | 授權成功的回呼函數。 |
fail | Function | 否 | 授權失敗的回呼函數。 |
scope列表
scope | 說明 | 對應介面 |
location | 地理位置。 | |
camera | 網路攝影機。 | |
bluetooth | 藍芽。 | WVBluetooth涉及到的所有方法 |
album | 相簿。 | |
contacts | 通訊錄 | WVContacts涉及到的所有方法 |
microphone | 麥克風。 | |
file | 檔案。 | WVFile 涉及到的所有方法| WVImage.saveImage| WVVideo.saveVideoToPhotosAlbum |
call | 電話。 | |
vibrate | 震動。 | |
screen | 截屏。 |
出參
success 回呼函數會收到一個Object 類型的對象,其屬性如下:
屬性 | 類型 | 描述 |
successScope | JSONObject | 成功的授權scope。 |
msg | String | 失敗的錯誤資訊 |
fail回呼函數會收到一個Object類型的對象,msg為錯誤資訊:
錯誤資訊 | 錯誤說明 | 解決方案 |
You need to configure the authorization switch to apply for authorization | 需要開啟授權開關 | 檢查容器初始化是否配置授權開關。 |
Parameter parsing exception | 資料解析異常 | 檢查傳入的資料格式。 |
Authorization scope cannot be empty | 傳入的scope為null | 檢查傳入的scope。 |
User not bound | 容器註冊使用者資訊UserId未註冊 | 檢查容器註冊使用者資訊,userId是否傳入。 |
User refuses authorization request | 使用者拒絕了授權 | 可詳細說明功能的用途,以擷取授權。 |
The authorization you requested does not exist. Please check the parameters | 傳入的scope不在範圍之內 | scope入參包含無效值,請檢查scope參數。 |
範例程式碼
window.WindVane.call('wv', 'getSetting', {}, function(res) {
if (!res.authSetting['location']) {
window.WindVane.call('wv', 'authorize', {scope: 'location'}, function(res) {
if (res.successScope['location']) {
alert('success authorize location');
//調用需要使用的jsapi
}
}, function(e) {
alert('failure:' + JSON.stringify(e));
})
} else {
alert('success authorize location');
}
}, function(e) {
alert('failure:' + JSON.stringify(e));
});