小程序调用设备相关的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));
});