# zls-wxa-run

> 微信小程序发起平滑调用API接口,发起登录授权,支持要求强制开启API权限

Latest version **1.0.8** (published 2018-02-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install zls-wxa-run
pnpm add zls-wxa-run
yarn add zls-wxa-run
bun add zls-wxa-run
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2018-02-18 |
| First published | 2018-02-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | 影浅 |
| Maintainers | seekwe |
| Keywords | wepy, 小程序, 微信授权, API权限 |

## Links

- npm: https://www.npmjs.com/package/zls-wxa-run
- Repository: https://blog.73zls.com/project/
- npm.io page: https://npm.io/package/zls-wxa-run

## Recent versions

- 1.0.8 (latest) — 2018-02-18
- 1.0.6 — 2018-02-14
- 1.0.5 — 2018-02-11
- 1.0.3 — 2018-02-11
- 1.0.2 — 2018-02-09
- 1.0.1 — 2018-02-09
- 1.0.0 — 2018-02-09
- 0.0.1 — 2018-02-09

## README

# 小程序API接口平滑调用

## 需求

因小程序迭代太快了，很多接口都要兼容处理。

## 依赖

需要 async-await 支持

如不熟悉如何小程序使用，建议直使用wepy。

[wepy](https://github.com/Tencent/wepy)

[async-await](https://github.com/Tencent/wepy/wiki/wepy项目中使用async-await)

## 示例脚手架

请先安装构建工具 - [zls-cli](https://docs.73zls.com/zls-cli/)

```bash
npm install -g zls-cli

zls new wepy-template ProjectFolder

cd ProjectFolder

npm run dev

```

## 初始化

**重要: 引入之后要记得执行初始化**

`interfaces.init(wepy)`

参说明数

wepy:    wepy对象/支持Promise的微信接口对象（看底下说明）

ps. 那天微信接口直接支持 Promise 了，就直接把 wx 实例直接丢进入就好了。

***


## 执行API接口

`interfaces.run(name, args = [])`

参说明数

name:    接口名

args:    接口传参（数组）

```js
let location
try {
    location = WxaRun.run(wepy, 'getLocation', 'scope.userLocation', 1, { content: '请允许地理位置', showCancel: true }, '请允许地理位置')
}catch ( type, msg ) {
    //type == 'warning',版本过低
    //type == 'fail',执行失败
}
```

***

## 执行需要权限判断的API接口

`interfaces.authRun(name, args = [], scope = null, must = 1, tip = {}, errorText = {})`

参说明数

name:    接口名

args:    接口传参（数组）

scope:    [scope](https://mp.weixin.qq.com/debug/wxadoc/dev/api/authorize-index.html)(包含scope.)/false(执行方式只支持1,0)

must:    执行方式  -1:必须开启权限执行(必须设置scope)/0:静默执行没有权限不提示/1:没权限下提并跳去开启

tip:    没有权限时候的提示弹窗/false不显示弹窗

errorText:    开启权限失败的提示弹窗/false不显示弹窗

返回值 Promise

***

## 登录授权

`interfaces.login(must = 1, tip = {}, errorText = {})`

参说明数

must:    执行方式  -1:必须开启权限执行(必须设置scope)/0:静默执行没有权限不提示/1:没权限下提并跳去开启

tip:    没有权限时候的提示弹窗/false不显示弹窗

errorText:    开启权限失败的提示弹窗/false不显示弹窗

返回值 Promise

***

## 示例

### 获地理位置

```js


import wepy from 'wepy'
import 'wepy-async-function'
import WxaRun from './components/login_relogin.js'

WxaRun.init(wepy)


//...methods
let location
try {
    location = WxaRun.run(wepy, 'getLocation', 'scope.userLocation', 1, { content: '请允许地理位置', showCancel: true }, '请允许地理位置')
}catch ( type, msg ) {
    //type == 'warning',版本过低
    //type == 'fail',执行失败
}
```

## Promise化微信接口

可能有些人更喜欢原生小程序的写法，可以自己把微信接口Promise化

话说 ，现在微信好像移除了 promise ， 建议自己加上 `es6-promise`

**promise.util.js**
```js

module.exports = {
  promisify: (api) => {
    return (options, ...params) => {
      return new Promise((resolve, reject) => {
        const extras = {
          success: resolve,
          fail: reject
        }
        api({ ...options, ...extras }, ...params)
      })
    }
  }
}
```

**page.js**

```js
import promiseUtil from './promise.util'

var getLocationPromisified = promiseUtil(wx.getLocation)

getLocationPromisified({
  type: 'wgs84'
}).then(function (res) {
 console.log(res)
}).catch(function () {
  console.error("get location failed")
})
```

---
_Source: https://npm.io/package/zls-wxa-run · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
