Skip to content

Commit ad440b4

Browse files
committed
v0.2.0
1 parent 8149478 commit ad440b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+10614
-572
lines changed

README.md

Lines changed: 290 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# mpapi
22

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

77
[![Language][img-javascript]][url-github]
88
[![License][img-mit]][url-mit]
99

10-
**此项目解决的问题**在“多端小程序输出”需求下,调用小程序 API 时,同一个方法在各个小程序的传参、返回参等逻辑不一样。每个小程序需要单独编写,而此项目是寻找不同小程序 API 之间的差异,尽可能地通过**一套 API 兼容多个小程序使用**
10+
**此项目解决的问题**寻找不同小程序 API 之间的差异,尽可能地通过**一套 API 兼容多个小程序使用**
1111

1212

1313
## 特点
14-
- 一次编写,多端运行,支持: 微信小程序、支付宝小程序、百度智能小程序
14+
- 一次编写,多端运行,支持: 微信小程序、支付宝小程序、百度智能小程序、字节跳动小程序
1515
- 支持 Promise(包含success回调的才有)
1616
- 支持特殊 API 的事件处理,例如:`request``downloadFile`[详情查看](#特殊api的事件处理)
17-
- 支持不同端的判断,`api.isWechat``api.isAlipay``api.isSwan`
17+
- 支持不同端的判断,`api.isWechat``api.isAlipay``api.isSwan``api.isTt`
1818

1919

2020
## 安装
@@ -49,45 +49,307 @@ if(api.isSwan){
4949

5050

5151
## 快速查看
52-
- [兼容列表](#兼容列表)
52+
- [兼容API列表](#兼容api列表)
53+
- [其它包装成Promise的API](#其它包装成promise的api)
5354
- [API差异](#小程序之间的api差异)
5455
- [使用说明](#使用说明)
5556
- [特殊API的事件处理](#特殊api的事件处理)`request``downloadFile``uploadFile`
56-
- 官方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/)
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/)
5758

59+
## 兼容API列表
60+
> 所有小程序都可以使用的 API
5861
59-
## 兼容列表
60-
- 操作反馈
62+
- 交互
6163
- [x] `alert`
6264
- [x] `confirm`
63-
- [x] `showActionSheet`
64-
- [x] `showLoading`
6565
- [x] `showToast`
66-
- 文件相关
66+
- [x] `showLoading`
67+
- [x] `showActionSheet`
68+
69+
70+
- 导航栏
71+
- [x] `setNavigationBarTitle`
72+
- [x] `setNavigationBarColor`
73+
74+
75+
- 文件
76+
- [x] `saveFile`
6777
- [x] `getFileInfo`
6878
- [x] `getSavedFileInfo`
6979
- [x] `getSavedFileList`
7080
- [x] `removeSavedFile`
71-
- [x] `saveFile`
72-
- 图片相关
81+
82+
83+
- 图片
7384
- [x] `chooseImage`
74-
- [x] `getImageInfo`
7585
- [x] `previewImage`
76-
- 请求相关
77-
- [x] `downloadFile`
86+
- [x] `compressImage`
87+
- [x] `saveImageToPhotosAlbum`
88+
89+
90+
- 请求
7891
- [x] `request`
7992
- [x] `uploadFile`
93+
- [x] `downloadFile`
94+
95+
8096
- 数据缓存
81-
- [x] `clearStorage`
82-
- [x] `clearStorageSync`
83-
- [x] `getStorage`
97+
- [x] `setStorageSync`
8498
- [x] `getStorageSync`
85-
- [x] `getStorageInfo`
99+
- [x] `clearStorageSync`
86100
- [x] `getStorageInfoSync`
87-
- [x] `removeStorage`
88101
- [x] `removeStorageSync`
89-
- [x] `setStorage`
90-
- [x] `setStorageSync`
102+
103+
104+
- 系统设备
105+
- [x] `getSystemInfoSync`
106+
- [x] `setScreenBrightness`
107+
- [x] `getScreenBrightness`
108+
- [x] `makePhoneCall`
109+
- [x] `scanCode`
110+
- [x] `setClipboardData`
111+
- [x] `getClipboardData`
112+
113+
114+
115+
116+
## 其它包装成Promise的API
117+
> 只在特定小程序下才会支持,默认支持所有。
118+
119+
微信小程序![](./assets/wx.png)、支付宝小程序![](./assets/my.png)、百度智能小程序![](./assets/swan.png)、字节跳动小程序![](./assets/tt.png),有对应图标表示支持。
120+
121+
122+
- 交互
123+
- [x] `hideToast`
124+
- [x] `hideLoading`
125+
- [x] `showModal` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
126+
- [x] `prompt` ![](./assets/my.png)
127+
128+
129+
- 缓存
130+
- [x] `getStorage`
131+
- [x] `setStorage`
132+
- [x] `removeStorage`
133+
- [x] `getStorageInfo`
134+
135+
136+
- 路由
137+
- [x] `reLaunch`
138+
- [x] `switchTab`
139+
- [x] `redirectTo`
140+
- [x] `navigateTo`
141+
- [x] `navigateBack`
142+
143+
144+
- 位置
145+
- [x] `getLocation`
146+
- [x] `openLocation`
147+
- [x] `chooseLocation` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
148+
149+
150+
- 文件图片
151+
- [x] `saveImage` ![](./assets/my.png)
152+
- [x] `getImageInfo`
153+
- [x] `chooseVideo` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
154+
- [x] `chooseMessageFile` ![](./assets/wx.png)
155+
- [x] `saveVideoToPhotosAlbum` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
156+
- [x] `openDocument` ![](./assets/wx.png) ![](./assets/swan.png)
157+
158+
159+
- 音频
160+
- [x] `stopVoice` ![](./assets/wx.png)
161+
- [x] `playVoice` ![](./assets/wx.png)
162+
- [x] `getAvailableAudioSources` ![](./assets/wx.png)
163+
- [x] `stopBackgroundAudio` ![](./assets/wx.png)
164+
- [x] `playBackgroundAudio` ![](./assets/wx.png)
165+
- [x] `seekBackgroundAudio` ![](./assets/wx.png)
166+
- [x] `pauseBackgroundAudio` ![](./assets/wx.png)
167+
- [x] `getBackgroundAudioPlayerState` ![](./assets/wx.png)
168+
- [x] `setInnerAudioOption` ![](./assets/wx.png) ![](./assets/swan.png)
169+
- [x] `startRecord` ![](./assets/wx.png)
170+
- [x] `stopRecord` ![](./assets/wx.png)
171+
- [x] `stopRecord` ![](./assets/wx.png)
172+
173+
174+
- 导航栏
175+
- [x] `getTitleColor` ![](./assets/my.png)
176+
- [x] `setNavigationBar` ![](./assets/my.png)
177+
- [x] `showNavigationBarLoading` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
178+
- [x] `hideNavigationBarLoading` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
179+
180+
181+
- 背景
182+
- [x] `setBackgroundTextStyle` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
183+
- [x] `setBackgroundColor` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
184+
- [x] `showTabBar` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
185+
- [x] `hideTabBar` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
186+
- [x] `setTabBarItem` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
187+
- [x] `setTabBarStyle` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
188+
- [x] `showTabBarRedDot` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
189+
- [x] `hideTabBarRedDot` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
190+
- [x] `setTabBarBadge` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
191+
- [x] `removeTabBarBadge` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
192+
193+
194+
- 下拉刷新
195+
- [x] `startPullDownRefresh`
196+
- [x] `stopPullDownRefresh`
197+
198+
199+
- 滚动
200+
- [x] `pageScrollTo`
201+
- [x] `sendSocketMessage` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
202+
- [x] `connectSocket`
203+
- [x] `closeSocket` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
204+
- [x] `startLocalServiceDiscovery` ![](./assets/wx.png)
205+
- [x] `stopLocalServiceDiscovery` ![](./assets/wx.png)
206+
207+
208+
- 置顶
209+
- [x] `setTopBarText` ![](./assets/wx.png)
210+
211+
212+
- 画布
213+
- [x] `canvasGetImageData` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
214+
- [x] `canvasPutImageData` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
215+
- [x] `canvasToTempFilePath` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
216+
217+
218+
- 分享转发
219+
- [x] `getShareInfo` ![](./assets/wx.png)
220+
- [x] `updateShareMenu` ![](./assets/wx.png)
221+
- [x] `showShareMenu` ![](./assets/wx.png) ![](./assets/tt.png)
222+
- [x] `hideShareMenu` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/tt.png)
223+
- [x] `showFavoriteGuide` ![](./assets/swan.png)
224+
- [x] `openShare` ![](./assets/swan.png)
225+
226+
227+
- 登录、授权、用户信息
228+
- [x] `login` ![](./assets/wx.png) ![](./assets/swan.png)
229+
- [x] `checkSession` ![](./assets/wx.png) ![](./assets/swan.png)
230+
- [x] `getUserInfo` ![](./assets/wx.png) ![](./assets/swan.png)
231+
- [x] `getAuthCode` ![](./assets/my.png)
232+
- [x] `getAuthUserInfo` ![](./assets/my.png)
233+
- [x] `getPhoneNumber` ![](./assets/my.png)
234+
- [x] `authorize` ![](./assets/wx.png)
235+
236+
237+
- 支付
238+
- [x] `tradePay` ![](./assets/my.png)
239+
- [x] `requestPayment` ![](./assets/wx.png)
240+
- [x] `requestPolymerPayment` ![](./assets/swan.png)
241+
242+
243+
- 开放接口
244+
- [x] `reportAnalytics` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
245+
- [x] `getSetting` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
246+
- [x] `openSetting` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
247+
- [x] `chooseInvoiceTitle` ![](./assets/wx.png) ![](./assets/swan.png)
248+
- [x] `navigateToMiniProgram` ![](./assets/wx.png) ![](./assets/my.png)
249+
- [x] `navigateBackMiniProgram` ![](./assets/wx.png) ![](./assets/my.png)
250+
251+
252+
- 开放接口 - 微信小程序
253+
- [x] `addCard` ![](./assets/wx.png)
254+
- [x] `openCard` ![](./assets/wx.png)
255+
- [x] `chooseInvoice` ![](./assets/wx.png)
256+
- [x] `startSoterAuthentication` ![](./assets/wx.png)
257+
- [x] `checkIsSoterEnrolledInDevice` ![](./assets/wx.png)
258+
- [x] `checkIsSupportSoterAuthentication` ![](./assets/wx.png)
259+
- [x] `getWeRunData` ![](./assets/wx.png)
260+
261+
262+
- 开放接口 - 支付宝小程序
263+
- [x] `startZMVerify` ![](./assets/my.png)
264+
- [x] `textRiskIdentification` ![](./assets/my.png)
265+
- [x] `addCardAuth` ![](./assets/my.png)
266+
- [x] `getRunScene` ![](./assets/my.png)
267+
- [x] `chooseCity` ![](./assets/my.png)
268+
- [x] `datePicker` ![](./assets/my.png)
269+
- [x] `optionsSelect` ![](./assets/my.png)
270+
- [x] `multiLevelSelect` ![](./assets/my.png)
271+
- [x] `rsa` ![](./assets/my.png)
272+
273+
274+
- 开放接口 - 百度智能小程序
275+
- [x] `getSwanId` ![](./assets/swan.png)
276+
- [x] `navigateToSmartProgram` ![](./assets/swan.png)
277+
- [x] `navigateBackSmartProgram` ![](./assets/swan.png)
278+
- [x] `setPageInfo` ![](./assets/swan.png)
279+
- [x] `setMetaDescription` ![](./assets/swan.png)
280+
- [x] `setMetaKeywords` ![](./assets/swan.png)
281+
- [x] `setDocumentTitle` ![](./assets/swan.png)
282+
- [x] `loadSubPackage` ![](./assets/swan.png)
283+
284+
285+
- 联系人
286+
- [x] `chooseAddress` ![](./assets/wx.png) ![](./assets/swan.png)
287+
- [x] `chooseContact` ![](./assets/my.png)
288+
- [x] `choosePhoneContact` ![](./assets/my.png)
289+
- [x] `chooseAlipayContact` ![](./assets/my.png)
290+
- [x] `addPhoneContact` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
291+
292+
293+
- 字体加载
294+
- [x] `loadFontFace` ![](./assets/wx.png) ![](./assets/my.png)
295+
296+
297+
- 系统信息
298+
- [x] `getSystemInfo`
299+
- [x] `getBatteryInfo` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
300+
- [x] `getNetworkType`
301+
- [x] `setKeepScreenOn` ![](./assets/wx.png) ![](./assets/my.png) ![](./assets/swan.png)
302+
- [x] `startAccelerometer` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
303+
- [x] `stopAccelerometer` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
304+
- [x] `startCompass` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
305+
- [x] `stopCompass` ![](./assets/wx.png) ![](./assets/swan.png) ![](./assets/tt.png)
306+
- [x] `startDeviceMotionListening` ![](./assets/wx.png) ![](./assets/swan.png)
307+
- [x] `stopDeviceMotionListening` ![](./assets/wx.png) ![](./assets/swan.png)
308+
- [x] `startGyroscope` ![](./assets/wx.png) ![](./assets/swan.png)
309+
- [x] `stopGyroscope` ![](./assets/wx.png) ![](./assets/swan.png)
310+
- [x] `vibrate` ![](./assets/my.png)
311+
- [x] `vibrateShort`
312+
- [x] `vibrateLong`
313+
- [x] `watchShake` ![](./assets/my.png)
314+
- [x] `setEnableDebug` ![](./assets/wx.png) ![](./assets/swan.png)
315+
- [x] `getServerTime` ![](./assets/my.png)
316+
- [x] `scan` ![](./assets/my.png)
317+
318+
319+
- 蓝牙无线
320+
- [x] `getBeacons` ![](./assets/wx.png) ![](./assets/my.png)
321+
- [x] `startBeaconDiscovery` ![](./assets/wx.png) ![](./assets/my.png)
322+
- [x] `stopBeaconDiscovery` ![](./assets/wx.png) ![](./assets/my.png)
323+
- [x] `startWifi` ![](./assets/wx.png)
324+
- [x] `stopWifi` ![](./assets/wx.png)
325+
- [x] `setWifiList` ![](./assets/wx.png)
326+
- [x] `getWifiList` ![](./assets/wx.png)
327+
- [x] `connectWifi` ![](./assets/wx.png)
328+
- [x] `getConnectedWifi` ![](./assets/wx.png)
329+
- [x] `getBLEDeviceServices` ![](./assets/wx.png) ![](./assets/my.png)
330+
- [x] `getBLEDeviceCharacteristics` ![](./assets/wx.png) ![](./assets/my.png)
331+
- [x] `createBLEConnection` ![](./assets/wx.png) ![](./assets/my.png)
332+
- [x] `closeBLEConnection` ![](./assets/wx.png) ![](./assets/my.png)
333+
- [x] `writeBLECharacteristicValue` ![](./assets/wx.png) ![](./assets/my.png)
334+
- [x] `readBLECharacteristicValue` ![](./assets/wx.png) ![](./assets/my.png)
335+
- [x] `notifyBLECharacteristicValueChange` ![](./assets/wx.png) ![](./assets/my.png)
336+
- [x] `startBluetoothDevicesDiscovery` ![](./assets/wx.png) ![](./assets/my.png)
337+
- [x] `stopBluetoothDevicesDiscovery` ![](./assets/wx.png) ![](./assets/my.png)
338+
- [x] `openBluetoothAdapter` ![](./assets/wx.png) ![](./assets/my.png)
339+
- [x] `getConnectedBluetoothDevices` ![](./assets/wx.png) ![](./assets/my.png)
340+
- [x] `getBluetoothDevices` ![](./assets/wx.png) ![](./assets/my.png)
341+
- [x] `getBluetoothAdapterState` ![](./assets/wx.png) ![](./assets/my.png)
342+
- [x] `closeBluetoothAdapter` ![](./assets/wx.png) ![](./assets/my.png)
343+
- [x] `stopHCE` ![](./assets/wx.png)
344+
- [x] `startHCE` ![](./assets/wx.png)
345+
- [x] `getHCEState` ![](./assets/wx.png)
346+
- [x] `sendHCEMessage` ![](./assets/wx.png)
347+
348+
349+
- 第三方平台
350+
- [x] `getExtConfig` ![](./assets/wx.png) ![](./assets/swan.png)
351+
352+
91353

92354

93355
## 小程序之间的API差异
@@ -243,13 +505,13 @@ api.getLocation().then((res) => {})
243505

244506

245507
## 特殊API的事件处理
246-
某些 API 既要支持 Promise,又要监听它的事件回调,那么可以采用如下方式:
508+
某些 API 既要支持 Promise,又要调用它的事件,那么可以采用如下方式:
247509

248510
**以前:**
249511
```javascript
250512
const downloadTask = wx.downloadFile({
251513
url: 'https://example.com/audio/123', // 仅为示例,并非真实的资源
252-
success(res) {
514+
success(res){
253515
console.log(res)
254516
}
255517
})
@@ -265,16 +527,17 @@ downloadTask.abort() // 取消下载任务
265527
const api = require('mpapi')
266528

267529
const downloadTask = api.downloadFile({
268-
url: 'https://example.com/audio/123', // 仅为示例,并非真实的资源
530+
url: 'https://example.com/audio/123' // 仅为示例,并非真实的资源
269531
}).then((res) => {
532+
console.log('success')
270533
console.log(res)
271534
})
272535

273536
downloadTask.$event('onProgressUpdate', (res) => {
274537
console.log(res)
275538
})
276539

277-
downloadTask.$event('abort') // 取消下载任务
540+
// downloadTask.$event('abort') // 取消下载任务
278541
```
279542
其它 API 可以类似处理,例如:`request``uploadFile``connectSocket`
280543

0 commit comments

Comments
 (0)