Skip to content

Commit d9b6a74

Browse files
committed
developing
1 parent ad440b4 commit d9b6a74

17 files changed

Lines changed: 1411 additions & 905 deletions

README.md

Lines changed: 166 additions & 164 deletions
Large diffs are not rendered by default.

TEMPLATE.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# mpapi
22

3-
> mpapi(miniProgram API),优雅的小程序API兼容插件,一次编写,多端运行。支持:微信小程序、支付宝小程序、百度智能小程序、字节跳动小程序
3+
> mpapi(miniProgram API),小程序 API 兼容插件,一次编写,多端运行。
44
5-
[![NPM][img-npm]][url-npm]
5+
:alarm_clock: 更新日期: <%= obj.updateDate %>
66

7-
[![Language][img-javascript]][url-github]
8-
[![License][img-mit]][url-mit]
7+
[![NPM][img-npm-badge]][url-npm] [![Language][img-javascript]][url-github] [![License][img-mit]][url-mit]
98

109
**此项目解决的问题**:寻找不同小程序 API 之间的差异,尽可能地通过**一套 API 兼容多个小程序使用**
1110

1211

1312
## 特点
1413
- 一次编写,多端运行,支持: 微信小程序、支付宝小程序、百度智能小程序、字节跳动小程序
15-
- 支持 Promise(包含success回调的才有)
16-
- 支持特殊 API 的事件处理,例如:`request``downloadFile`[详情查看](#特殊api的事件处理)
14+
- 支持 Promise(包含 success 回调的才有)
15+
- 针对某些 API 使用做了优化,如:`api.showToast` 可以直接传 `string``api.setStorageSync` 无需调用 `try catch 等`[详情](#优化的api)
16+
- 支持特殊 API 的事件处理,例如:`request``downloadFile`[详情](#特殊api的事件处理)
1717
- 支持不同端的判断,`api.isWechat``api.isAlipay``api.isSwan``api.isTt`
1818

1919

2020
## 安装
21-
非npm安装方式,直接引入 `lib` 目录下的 `mpapi.js` 到项目即可
2221
```bash
2322
npm install mpapi --save
2423
```
25-
使用
24+
非npm安装方式,直接引入 `lib` 目录下的 `mpapi.js` 到项目即可
25+
26+
27+
## 使用
2628
```javascript
2729
const api = require('mpapi')
2830

@@ -49,12 +51,13 @@ if(api.isSwan){
4951

5052

5153
## 快速查看
52-
- [兼容API列表](#兼容api列表)
53-
- [其它包装成Promise的API](#其它包装成promise的api)
54-
- [API差异](#小程序之间的api差异)
54+
- [兼容 API 列表](#兼容api列表)
55+
- [其它包装成 Promise 的 API](#其它包装成promise的api)
56+
- [优化的 API](#优化的api)
57+
- [API 差异](#小程序之间的api差异)
5558
- [使用说明](#使用说明)
56-
- [特殊API的事件处理](#特殊api的事件处理)`request``downloadFile``uploadFile`
57-
- 官方API文档[微信小程序](https://developers.weixin.qq.com/miniprogram/dev/api/)[支付宝小程序](https://docs.alipay.com/mini/api/overview)[百度智能小程序](http://smartprogram.baidu.com/docs/develop/api/net_rule/)[字节跳动小程序](https://developer.toutiao.com/docs/framework/)
59+
- [特殊 API 的事件处理](#特殊api的事件处理)`request``downloadFile``uploadFile`
60+
- 官方 API 文档[微信小程序](https://developers.weixin.qq.com/miniprogram/dev/api/)[支付宝小程序](https://docs.alipay.com/mini/api/overview)[百度智能小程序](http://smartprogram.baidu.com/docs/develop/api/net_rule/)[字节跳动小程序](https://developer.toutiao.com/docs/framework/)
5861

5962
## 兼容API列表
6063
> 所有小程序都可以使用的 API
@@ -66,17 +69,21 @@ if(api.isSwan){
6669
6770

6871
## 其它包装成Promise的API
69-
> 只在特定小程序下才会支持,默认支持所有
72+
> 只在特定小程序下才会支持。
7073
71-
微信小程序![](./assets/wx.png)、支付宝小程序![](./assets/my.png)、百度智能小程序![](./assets/swan.png)、字节跳动小程序![](./assets/tt.png)有对应图标表示支持
74+
微信小程序![wx](./assets/wx.png)、支付宝小程序![my](./assets/my.png)、百度智能小程序![swan](./assets/swan.png)、字节跳动小程序![tt](./assets/tt.png)有图标表示只支持对应小程序,没有图标表示支持所有小程序
7275

73-
<% _.each(obj.apis, function(group){ %>
76+
<% _.each(obj.normalApi, function(group){ %>
7477
- <%= group.title %>
75-
<% _.each(group.items, function(item){ %> - [x] `<%= item.title %>` <% _.each(item.labels, function(lbl){ %> ![](./assets/<%= lbl %>.png) <% }) %>
78+
<% _.each(group.items, function(item){ %> - [x] `<%= item.title %>` <% _.each(item.labels, function(lbl){ %> ![%= lbl %>](./assets/<%= lbl %>.png) <% }) %>
7679
<% }) %>
7780
<% }) %>
7881

7982

83+
## 优化的API
84+
- `showToast`
85+
86+
8087
## 小程序之间的API差异
8188

8289
1、传参不一致
@@ -202,7 +209,7 @@ api.chooseImage({...}).then((res) => {})
202209
...
203210
```
204211

205-
2、兼容方法里的传参和返回参,**以微信小程序调用为准**。其它端不兼容的参数不处理,例如
212+
2、兼容方法里的传参和返回参,**以微信小程序调用为准**。其它端不兼容的参数不处理(某些参数也无法处理,特定小程序不支持)开发者需要留意,例如
206213
```javascript
207214
api.chooseImage({
208215
count: 1,
@@ -214,18 +221,12 @@ api.chooseImage({
214221
})
215222
```
216223

217-
3、不在兼容列表里面的方法,也可以通过 `api` 来调用,并且支持 `Promise` 语法(**`success` 回调的才有**
224+
3、深层级的 API,也可以通过 `api` 来调用,并且支持 `Promise` 语法(**`success` 回调的才有**
218225
```javascript
219-
// 微信小程序可用(检查登录态是否过期)
220-
api.checkSession().then((res) => {})
221-
222-
223-
// 支付宝小程序可用(获取授权码)
224-
api.getAuthCode().then((res) => {})
225-
226-
227-
// 小程序都可用(获取地理位置)
228-
api.getLocation().then((res) => {})
226+
// 支付宝小程序支持的
227+
api.ap.imgRisk({...}).then((res) => {})
228+
api.ap.navigateToAlipayPage({...}).then((res) => {})
229+
...
229230
```
230231

231232

@@ -276,6 +277,7 @@ downloadTask.$event('onProgressUpdate', (res) => {
276277
[url-mit]: https://opensource.org/licenses/mit-license.php
277278

278279
[img-npm]: https://nodei.co/npm/mpapi.png?compact=true
280+
[img-npm-badge]: https://img.shields.io/npm/v/mpapi.svg
279281
[img-javascript]: https://img.shields.io/badge/language-JavaScript-brightgreen.svg
280282
[img-mit]: https://img.shields.io/badge/license-MIT-blue.svg
281283

createMarkdown.js

Lines changed: 0 additions & 110 deletions
This file was deleted.

doc.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const path = require('path')
2+
const fs = require('fs-extra')
3+
const readline = require('linebyline')
4+
const _ = require('underscore')
5+
const moment = require('moment')
6+
7+
const TEMPLATE = path.join(__dirname, 'TEMPLATE.md')
8+
const README = path.join(__dirname, 'README.md')
9+
10+
const NORMALAPI_FILE = path.join(__dirname, 'src/api/normalApi.js')
11+
const POLYFILLS_FILE = path.join(__dirname, 'src/polyfills/index.js')
12+
13+
let NORMALAPI_DATA = []
14+
let POLYFILLS_DATA = []
15+
16+
// normalApi.js 转换成数组数据
17+
function handlerNormalApi(){
18+
let rl = readline(NORMALAPI_FILE, { retainBuffer: true })
19+
20+
let ready = false
21+
22+
return new Promise((resolve) => {
23+
rl.on('line', (data) => {
24+
let content = data.toString()
25+
26+
if(/module\.exports/.test(content)){
27+
ready = true
28+
}
29+
30+
if(ready){
31+
matchTitle(content, NORMALAPI_DATA)
32+
33+
let last = NORMALAPI_DATA[NORMALAPI_DATA.length - 1]
34+
35+
let matchApi = content.match(/'(\w+)',(\s+\/\/\s+([\w\s]+))?/)
36+
if(last && matchApi && matchApi[1]){
37+
let labels = []
38+
39+
if(matchApi[3]){
40+
labels = matchApi[3].split(/\s/)
41+
}
42+
43+
last.items.push({
44+
title: matchApi[1],
45+
labels: labels
46+
})
47+
}
48+
}
49+
});
50+
51+
rl.on('end', resolve)
52+
})
53+
}
54+
55+
56+
// polyfills/index.js 转换成数组数据
57+
function handlerPolyfills(){
58+
let rl = readline(POLYFILLS_FILE, { retainBuffer: true })
59+
60+
let ready = false
61+
62+
return new Promise((resolve) => {
63+
rl.on('line', (data) => {
64+
let content = data.toString()
65+
66+
if(/module\.exports/.test(content)){
67+
ready = true
68+
}
69+
70+
if(ready){
71+
matchTitle(content, POLYFILLS_DATA)
72+
73+
let last = POLYFILLS_DATA[POLYFILLS_DATA.length - 1]
74+
75+
let matchApi = content.match(/(\w+):/)
76+
if(last && matchApi && matchApi[1]){
77+
last.items.push({
78+
title: matchApi[1],
79+
})
80+
}
81+
}
82+
});
83+
84+
rl.on('end', resolve)
85+
})
86+
}
87+
88+
// 匹配一级标题
89+
function matchTitle(content, obj){
90+
let matchs = content.match(/\/\/\s([\u4e00-\u9fa5]+.*)/)
91+
if(matchs && matchs[1]){
92+
obj.push({
93+
title: matchs[1],
94+
items: []
95+
})
96+
}
97+
}
98+
99+
function writeMarkdown(){
100+
let tplContent = fs.readFileSync(TEMPLATE).toString()
101+
let compiled = _.template(tplContent);
102+
103+
tplContent = compiled({
104+
updateDate: moment().format('YYYY-MM-DD'),
105+
normalApi: NORMALAPI_DATA,
106+
polyfills: POLYFILLS_DATA
107+
});
108+
109+
fs.writeFileSync(README, tplContent, 'utf-8');
110+
}
111+
112+
async function init(){
113+
await handlerNormalApi()
114+
await handlerPolyfills()
115+
116+
writeMarkdown()
117+
}
118+
119+
// 开始转换
120+
init()

0 commit comments

Comments
 (0)