swan.authorize

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

方法参数

Object object

object参数说明

scope 有效值列表

示例

在开发者工具中预览效果

扫码体验

请使用百度APP扫码

图片示例

代码示例

-

  1. <view class="container"> <view class="card-area"> <button s-for="item in items" id="{{item.id}}" bind:tap="btnClick" type="primary" hover-stop-propagation="true">{{item.name}}</button> </view></view>

-

  1. Page({
  2. data: {
  3. title: 'authorize',
  4. items: [{
  5. name: '授权获取用户信息',
  6. id: 'scope.userInfo'
  7. }, {
  8. name: '授权获取位置信息',
  9. id: 'scope.userLocation'
  10. }, {
  11. name: '授权保存到相册',
  12. id: 'scope.writePhotosAlbum'
  13. }, {
  14. name: '授权获取收货地址',
  15. id: 'scope.address'
  16. }, {
  17. name: '授权获取发票抬头',
  18. id: 'scope.invoiceTitle'
  19. }, {
  20. name: '授权获取录音功能',
  21. id: 'scope.record'
  22. }, {
  23. name: '授权使用摄像头',
  24. id: 'scope.camera'
  25. }]
  26. },
  27. btnClick(e) {
  28. let scope = e.currentTarget.id;
  29. swan.authorize({
  30. scope,
  31. success: res => {
  32. swan.showToast({
  33. title: '已成功授权',
  34. icon: 'none'
  35. });
  36. },
  37. fail: err => {
  38. if (err.errCode === 10003) {
  39. swan.showToast({
  40. title: '你已拒绝授权申请',
  41. icon: 'none'
  42. });
  43. }
  44. }
  45. });
  46. }
  47. });

错误码

授权接口通用错误码

Android

iOS

Bug & Tip

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