小程序的授权

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

小程序的授权

Jeff的技术栈   2020-03-12 我要评论
[TOC] # 官方文档 <> # 小程序授权 ``` 1 因为部分功能需要用同意后才能使用。 2 wx.getSetting来判断该用户有没有对接口授权,我判断哪个接口,就必须给wx.getSetting传对应的scope值 - 一个scope值对应这个一个或多个接口 3 如果我们从wx.getSetting中发现scope值是false,标识没有授权,我们可以通过wx.authorize发起授权,对那个接口授权,就给wx.authorize传对应scope值就可以了。如果用用户同意授权,就可以直接使用对应的接口了。 4 但是scope.userInfo没有办法使用wx.authorize自动弹起弹框。必须要用户手动点击按钮唤起授权弹框。 代码格式: 我们可以再响应函数的参数中获取用户信息。e.detail,这个和直接调用wx.getUserInfo获取的内容一样。 ``` # 获取用户授权设置 开发者可以使用 [wx.getSetting](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/open-api/setting/wx.getSetting.html) 获取用户当前的授权状态。 # 提前发起授权请求 开发者可以使用 [wx.authorize](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/open-api/authorize/wx.authorize.html) 在调用需授权 API 之前,提前向用户发起授权请求。 # scope 列表 | scope | 对应接口 | 描述 | | ---------------------------- | ------------------------------------------------------------ | ------------ | | scope.userInfo | [wx.getUserInfo](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/open-api/user-info/wx.getUserInfo.html) | 用户信息 | | scope.userLocation | [wx.getLocation](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/location/wx.getLocation.html), [wx.chooseLocation](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/location/wx.chooseLocation.html) | 地理位置 | | scope.userLocationBackground | [wx.startLocationUpdateBackground](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/location/wx.startLocationUpdateBackground.html) | 后台定位 | | scope.address | [wx.chooseAddress](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/open-api/address/wx.chooseAddress.html) | 通讯地址 | | scope.invoiceTitle | [wx.chooseInvoiceTitle](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/open-api/invoice/wx.chooseInvoiceTitle.html) | 发票抬头 | | scope.invoice | [wx.chooseInvoice](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/open-api/invoice/wx.chooseInvoice.html) | 获取发票 | | scope.werun | [wx.getWeRunData](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/open-api/werun/wx.getWeRunData.html) | 微信运动步数 | | scope.record | [wx.startRecord](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/media/recorder/wx.startRecord.html) | 录音功能 | | scope.writePhotosAlbum | [wx.saveImageToPhotosAlbum](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/media/image/wx.saveImageToPhotosAlbum.html), [wx.saveVideoToPhotosAlbum](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/media/video/wx.saveVideoToPhotosAlbum.html) | 保存到相册 | | scope.camera | [camera](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/component/camera.html) 组件 | 摄像头 | # 授权有效期 一旦用户明确同意或拒绝过授权,其授权关系会记录在后台,直到用户主动删除小程序。 # 注意事项 ``` wx.authorize({scope: "scope.userInfo"}) 用户信息 的授权必须button用户手动触发弹窗,授权 其他录音等授权,可以直接写在生命周期中,自动弹窗,用户点击授权 ``` 1. `wx.authorize({scope: "scope.userInfo"})`,不会弹出授权窗口,请使用 [``](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/component/button.html) **open-type="getUserInfo"** ``` ``` 2. 需要授权 `scope.userLocation`、`scope.userLocationBackground` 时必须[配置地理位置用途说明](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/reference/configuration/app.html#permission)。 # 后台定位 与其它类型授权不同的是,scope.userLocationBackground 不会弹窗提醒用户。需要用户在设置页中,主动将“位置信息”选项设置为“使用小程序期间和离开小程序后”。开发者可以通过调用[wx.openSetting](https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/open-api/setting/wx.openSetting.html),打开设置页。 ![background-location](https://res.wx.qq.com/wxdochttps://img.qb5200.com/download-x/dist/assets/img/background-location.8290c764.png) # 案例 ```python # wxml文件: # js文件: lu:function(){ wx.getSetting({ success(res){ if (!res.authSetting['scope.record']) { wx.authorize({ scope: 'scope.record', // 授权的功能 success() { // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问 wx.startRecord() // 使用接口 }, fail() { // 用户不同意进入fail回调 console.log("你没有授权") } }) } else { wx.startRecord() //使用接口 } } }) }, user: function () { wx.getSetting({ success(res) { if (!res.authSetting['scope.userInfo']) { wx.authorize({ scope: 'scope.userinfo', // 授权的功能 success() { console.log('进来了') wx.startRecord() // 使用接口 } }) } else { console.log('已经授权了') wx.startRecord() //使用接口 } } }) }, user1:function(e){ console.log('e',e.detail) wx.getSetting({ success(res){ if (res.authSetting['scope.userInfo']){ wx.getUserInfo({ success:(res) => { console.log('res',res) } }) } } }) }, ```

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们