百度智能小程序 提前向用户发起授权请求

2020-09-05 14:43 更新

swan.authorize

解释: 提前向用户发起授权请求。

方法参数

Object object

object 参数说明

属性名类型必填默认值说明

scope

String

需要获取权限的 scope,详见 scope 列表

success

Function

接口调用成功的回调函数

fail

Function

接口调用失败的回调函数

complete

Function

接口调用结束的回调函数(调用成功、失败都会执行)

示例 

在开发者工具中打开


图片示例

代码示例

<view class="wrap">
    <view class="card-area">
        <button s-for="item in items" id="{{item.id}}" bind:tap="authorize" type="primary" hover-stop-propagation="true">{{item.name}}</button>
    </view>
</view>
Page({
    data: {
        items: [{
            name: '授权获取用户信息',
            id: 'scope.userInfo'
        }, {
            name: '授权获取位置信息',
            id: 'scope.userLocation'
        }, {
            name: '授权保存到相册',
            id: 'scope.writePhotosAlbum'
        }, {
            name: '授权获取收货地址',
            id: 'scope.address'
        }, {
            name: '授权获取发票抬头',
            id: 'scope.invoiceTitle'
        }, {
            name: '授权获取录音功能',
            id: 'scope.record'
        }, {
            name: '授权使用摄像头',
            id: 'scope.camera'
        }]
    },
    authorize(e) {
        let scope = e.currentTarget.id;
        swan.authorize({
            scope,
            success: res => {
                swan.showToast({
                    title: '已成功授权',
                    icon: 'none'
                });
            },
            fail: err => {
                if (err.errCode === 10003) {
                    swan.showToast({
                        title: '用户已拒绝授权申请,请自建提示,引导用户开启授权',
                        icon: 'none'
                    });
                }
            }
        });
    }
});

Bug & Tip

  • tip:授权操作为异步操作,完成前请不要多次调用。

错误码

通用错误码

错误码错误码显示信息说明

10001

internal error

服务器数据异常

10002

network error

网络异常、请求超时,请查看您的网络设置。

10003

user deny

用户拒绝授权

10004

user not logged in

用户未登录,建议提示用户登录。

10005

system deny

系统拒绝授权,可能原因:未申请接口授权;用户未开启百度 APP 相关权限。

Android

错误码说明

1001

执行失败

10001

内部错误

11001

未知错误

11003

用户取消授权

iOS

错误码说明

202

解析失败,请检查参数是否正确

10001

内部错误

10002

网络请求失败

10004

用户拒绝(user not login)


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号