@uni/authorize
v1.0.1
Published
[![npm](https://img.shields.io/npm/v/@uni/authorize.svg)](https://www.npmjs.com/package/@uni/authorize)
Downloads
1
Readme
authorize
Some of the APIs need users’ authorization before they can be called. We have divided these APIs into multiple scope according to the scope of usage. The users can select scope to authorize. After a scope is authorized, all of its APIs can be used directly.
When such an API is called:
- If the user has not accepted or rejected this authorization, a pop-up window will appear to ask the user if he/she wants to accept. The API can be called only after the user clicks to accept;
- If the user has accepted authorization, the API can be called directly;
- If the user has rejected authorization, no pop-up appears. Instead, API fail callback will be accessed directly. Developers should make the scenario compatible where the user has rejected to authorization.
Supported
Install
$ npm install @uni/authorize --save
or
$ npm install @uni/apis --save
Usage
import authorize from '@uni/authorize';
authorize({
scope: 'scope.userLocation',
success: () => {
console.log('success');
},
fail: (res) => {
console.log('fail', res);
},
complete: (res) => {
console.log('complete', res);
}
});
authorize({
scope: 'scope.userLocation',
}).then((res?) => {
console.log('success');
});
你也可以从大包引入:
import { authorize } from '@uni/apis';
Parameters
| Property | Type | Description | Required | Default |
| --- | --- | --- | --- | --- |
| options | object
| | 是 | - |
| options.scope | string
| The scope to be authorized. For details, see scope list | Yes | - |
| options.success | Function
| The callback function for a successful API call | No | - |
| options.fail | Function
| The callback function for a failed API call | No | - |
| options.complete | Function
| The callback function used when the API call completed (always executed whether the call succeeds or fails) | No | - |
Scope List
| scope | Description | Corresponding APIs | Supported | | ------ | -------- | ------ | ---- | | scope.userInfo | User information | getUserInfo, getOpenUserInfo | | | scope.userLocation | Geographic location | getLocation, chooseLocation | | | scope.userLocationBackground | Background positioning | startLocationUpdateBackground | | | scope.werun | WeRun step counts | getWeRunData | | | scope.record | Recording feature | startRecord | | | scope.writePhotosAlbum | Saves to album | saveImage, saveVideo | | | scope.camera | Camera | scanCode,chooseImage,chooseVideo | | | scope.calendar | calendar | addEventOnCalendar,deleteEventOnCalendar | | | auth_base | Silent authorization will not initiate authorization floating window. Get auth_ from Alipay client Code, the incoming server invokes the alipay.system.oauth.token interface to get Alipay membership identifier (user_). id)| addEventOnCalendar,deleteEventOnCalendar | | | auth_zhima | User sesame information | zhima.credit.score.brief.get | |
Response<Response>
(Not general)
Note: only Alipay applet returns.
| Property | Type | Description |
| --- | --- | --- |
| response | object
| - |
| response.authCode | string
| Authorization code |
| response.authErrorScopes | object
| Failed authorization type. Key is the scope of failed authorization, and value is the corresponding error code |
| response.authSuccessScopes | Array
| Successful authorization scope |