├── .gitignore ├── .npmignore ├── README.md ├── examples ├── app.js ├── app.json ├── app.wxss ├── index │ ├── index.js │ ├── index.json │ ├── index.wxml │ └── index.wxss ├── package-lock.json ├── package.json ├── project.config.json └── sitemap.json ├── package.json └── src ├── index.js ├── method.js └── promise.js /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wechat-miniprogram/miniprogram-api-promise/ed5e17deefe548cd6a8ed15554ba8c8a84f14b4b/.gitignore -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | examples 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # miniprogram-api-promise 2 | 3 | [![](https://img.shields.io/npm/v/miniprogram-api-promise.svg?style=flat)](https://www.npmjs.com/package/miniprogram-api-promise) 4 | [![](https://img.shields.io/github/license/wechat-miniprogram/api-typings.svg)](https://github.com/wechat-miniprogram/miniprogram-api-promise) 5 | 6 | Extend WeChat miniprogram's api to surport promise. 7 | 8 | # Installation 9 | 10 | ``` 11 | npm install --save miniprogram-api-promise 12 | ``` 13 | 14 | # Getting started 15 | Call the method promisifyAll at the program entry (app.js), It only needs to be called once. 16 | 17 | 💨example: 18 | ``` 19 | import { promisifyAll, promisify } from 'miniprogram-api-promise'; 20 | 21 | const wxp = {} 22 | // promisify all wx's api 23 | promisifyAll(wx, wxp) 24 | console.log(wxp.getSystemInfoSync()) 25 | wxp.getSystemInfo().then(console.log) 26 | wxp.showModal().then(wxp.openSetting()) 27 | 28 | // compatible usage 29 | wxp.getSystemInfo({success(res) {console.log(res)}}) 30 | 31 | // promisify single api 32 | promisify(wx.getSystemInfo)().then(console.log) 33 | ``` 34 | -------------------------------------------------------------------------------- /examples/app.js: -------------------------------------------------------------------------------- 1 | App({ 2 | onLaunch: function () { 3 | 4 | } 5 | }) 6 | -------------------------------------------------------------------------------- /examples/app.json: -------------------------------------------------------------------------------- 1 | { 2 | "pages": [ 3 | "index/index" 4 | ], 5 | "window": { 6 | "backgroundTextStyle": "light", 7 | "navigationBarBackgroundColor": "#fff", 8 | "navigationBarTitleText": "WeChat", 9 | "navigationBarTextStyle": "black" 10 | }, 11 | "sitemapLocation": "sitemap.json" 12 | } -------------------------------------------------------------------------------- /examples/app.wxss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wechat-miniprogram/miniprogram-api-promise/ed5e17deefe548cd6a8ed15554ba8c8a84f14b4b/examples/app.wxss -------------------------------------------------------------------------------- /examples/index/index.js: -------------------------------------------------------------------------------- 1 | const { promisifyAll, promisify } = require('miniprogram-api-promise') 2 | const app = getApp() 3 | const wxp = {} 4 | 5 | Page({ 6 | data: { 7 | 8 | }, 9 | onLoad: function () { 10 | console.log('代码片段是一种迷你、可分享的小程序或小游戏项目,可用于分享小程序和小游戏的开发经验、展示组件和 API 的使用、复现开发问题和 Bug 等。可点击以下链接查看代码片段的详细文档:') 11 | console.log('https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html') 12 | promisifyAll(wx, wxp) 13 | promisify(wx.getSystemInfo)().then(console.log) 14 | }, 15 | tap() { 16 | wxp.showModal({ 17 | title: '打开 Setting' 18 | }).then(() => { 19 | wxp.openSetting() 20 | }) 21 | 22 | wxp.getSystemInfo().then(res => { 23 | console.log('Async: getSystemInfo ', res) 24 | }) 25 | console.log('Sycn getSystemInfoSync', wxp.getSystemInfoSync()) 26 | console.log('wx.env', wxp.env) 27 | wxp.getSystemInfo({ 28 | success(res) { 29 | console.log('Callback getSystemInfo', res) 30 | } 31 | }) 32 | } 33 | }) 34 | -------------------------------------------------------------------------------- /examples/index/index.json: -------------------------------------------------------------------------------- 1 | { 2 | "usingComponents": {} 3 | } -------------------------------------------------------------------------------- /examples/index/index.wxml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /examples/index/index.wxss: -------------------------------------------------------------------------------- 1 | .intro { 2 | margin: 30px; 3 | text-align: center; 4 | } -------------------------------------------------------------------------------- /examples/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "minicode-10", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "miniprogram-api-promise": { 8 | "version": "1.0.2", 9 | "resolved": "https://registry.npmjs.org/miniprogram-api-promise/-/miniprogram-api-promise-1.0.2.tgz", 10 | "integrity": "sha512-yNvcOmey/STj2hdtZ1sKVDdHMklf06Y05blD1zZD+3m/dY/CrernHLYyAYgacqvhN4HHfkiyFwTtqci8ZX957A==" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /examples/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "minicode-10", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "miniprogram-api-promise": "^1.0.2" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /examples/project.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "项目配置文件", 3 | "packOptions": { 4 | "ignore": [] 5 | }, 6 | "setting": { 7 | "urlCheck": true, 8 | "es6": true, 9 | "postcss": true, 10 | "minified": true, 11 | "newFeature": true, 12 | "coverView": true, 13 | "autoAudits": false, 14 | "checkInvalidKey": true, 15 | "checkSiteMap": true, 16 | "uploadWithSourceMap": true, 17 | "babelSetting": { 18 | "ignore": [], 19 | "disablePlugins": [], 20 | "outputPath": "" 21 | }, 22 | "nodeModules": true 23 | }, 24 | "compileType": "miniprogram", 25 | "libVersion": "2.7.4", 26 | "appid": "wx407d0fba58912be6", 27 | "projectname": "test-promise", 28 | "debugOptions": { 29 | "hidedInDevtools": [] 30 | }, 31 | "isGameTourist": false, 32 | "simulatorType": "wechat", 33 | "simulatorPluginLibVersion": {}, 34 | "condition": { 35 | "search": { 36 | "current": -1, 37 | "list": [] 38 | }, 39 | "conversation": { 40 | "current": -1, 41 | "list": [] 42 | }, 43 | "game": { 44 | "currentL": -1, 45 | "list": [] 46 | }, 47 | "miniprogram": { 48 | "current": -1, 49 | "list": [] 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /examples/sitemap.json: -------------------------------------------------------------------------------- 1 | { 2 | "desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html", 3 | "rules": [{ 4 | "action": "allow", 5 | "page": "*" 6 | }] 7 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "miniprogram-api-promise", 3 | "version": "1.0.4", 4 | "description": "Extend WeChat miniprogram's api to surport promise", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/wechat-miniprogram-admin/miniprogram-api-promise.git" 12 | }, 13 | "keywords": [ 14 | "miniprogram", 15 | "api", 16 | "promise" 17 | ], 18 | "author": "sanfordsun", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "https://github.com/wechat-miniprogram-admin/miniprogram-api-promise/issues" 22 | }, 23 | "homepage": "https://github.com/wechat-miniprogram-admin/miniprogram-api-promise#readme" 24 | } 25 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export {promisify, promisifyAll} from './promise' 2 | -------------------------------------------------------------------------------- /src/method.js: -------------------------------------------------------------------------------- 1 | export const asyncMethods = [ 2 | 'canvasGetImageData', 3 | 'canvasPutImageData', 4 | 'canvasToTempFilePath', 5 | 'setEnableDebug', 6 | 'startAccelerometer', 7 | 'stopAccelerometer', 8 | 'getBatteryInfo', 9 | 'getClipboardData', 10 | 'setClipboardData', 11 | 'startCompass', 12 | 'stopCompass', 13 | 'addPhoneContact', 14 | 'startGyroscope', 15 | 'stopGyroscope', 16 | 'startBeaconDiscovery', 17 | 'stopBeaconDiscovery', 18 | 'getBeacons', 19 | 'startLocalServiceDiscovery', 20 | 'stopLocalServiceDiscovery', 21 | 'startDeviceMotionListening', 22 | 'stopDeviceMotionListening', 23 | 'getNetworkType', 24 | 'makePhoneCall', 25 | 'scanCode', 26 | 'getSystemInfo', 27 | 'vibrateShort', 28 | 'vibrateLong', 29 | 'getExtConfig', 30 | 'chooseLocation', 31 | 'getLocation', 32 | 'openLocation', 33 | 'chooseMessageFile', 34 | 'loadFontFace', 35 | 'chooseImage', 36 | 'previewImage', 37 | 'getImageInfo', 38 | 'saveImageToPhotosAlbum', 39 | 'compressImage', 40 | 'chooseVideo', 41 | 'saveVideoToPhotosAlbum', 42 | 'downloadFile', 43 | 'request', 44 | 'connectSocket', 45 | 'closeSocket', 46 | 'sendSocketMessage', 47 | 'uploadFile', 48 | 'login', 49 | 'checkSession', 50 | 'chooseAddress', 51 | 'authorize', 52 | 'addCard', 53 | 'openCard', 54 | 'chooseInvoice', 55 | 'chooseInvoiceTitle', 56 | 'getUserInfo', 57 | 'requestPayment', 58 | 'getWeRunData', 59 | 'showModal', 60 | 'showToast', 61 | 'hideToast', 62 | 'showLoading', 63 | 'hideLoading', 64 | 'showActionSheet', 65 | 'pageScrollTo', 66 | 'startPullDownRefresh', 67 | 'stopPullDownRefresh', 68 | 'setBackgroundColor', 69 | 'setBackgroundTextStyle', 70 | 'setTabBarBadge', 71 | 'removeTabBarBadge', 72 | 'showTabBarRedDot', 73 | 'hideTabBarRedDot', 74 | 'showTabBar', 75 | 'hideTabBar', 76 | 'setTabBarStyle', 77 | 'setTabBarItem', 78 | 'setTopBarText', 79 | 'saveFile', 80 | 'openDocument', 81 | 'getSavedFileList', 82 | 'getSavedFileInfo', 83 | 'removeSavedFile', 84 | 'getFileInfo', 85 | 'getStorage', 86 | 'setStorage', 87 | 'removeStorage', 88 | 'clearStorage', 89 | 'getStorageInfo', 90 | 'closeBLEConnection', 91 | 'closeBluetoothAdapter', 92 | 'createBLEConnection', 93 | 'getBLEDeviceCharacteristics', 94 | 'getBLEDeviceServices', 95 | 'getBluetoothAdapterState', 96 | 'getBluetoothDevices', 97 | 'getConnectedBluetoothDevices', 98 | 'notifyBLECharacteristicValueChange', 99 | 'openBluetoothAdapter', 100 | 'readBLECharacteristicValue', 101 | 'startBluetoothDevicesDiscovery', 102 | 'stopBluetoothDevicesDiscovery', 103 | 'writeBLECharacteristicValue', 104 | 'getHCEState', 105 | 'sendHCEMessage', 106 | 'startHCE', 107 | 'stopHCE', 108 | 'getScreenBrightness', 109 | 'setKeepScreenOn', 110 | 'setScreenBrightness', 111 | 'connectWifi', 112 | 'getConnectedWifi', 113 | 'getWifiList', 114 | 'setWifiList', 115 | 'startWifi', 116 | 'stopWifi', 117 | 'getBackgroundAudioPlayerState', 118 | 'playBackgroundAudio', 119 | 'pauseBackgroundAudio', 120 | 'seekBackgroundAudio', 121 | 'stopBackgroundAudio', 122 | 'getAvailableAudioSources', 123 | 'startRecord', 124 | 'stopRecord', 125 | 'setInnerAudioOption', 126 | 'playVoice', 127 | 'pauseVoice', 128 | 'stopVoice', 129 | 'getSetting', 130 | 'openSetting', 131 | 'getShareInfo', 132 | 'hideShareMenu', 133 | 'showShareMenu', 134 | 'updateShareMenu', 135 | 'checkIsSoterEnrolledInDevice', 136 | 'checkIsSupportSoterAuthentication', 137 | 'startSoterAuthentication', 138 | 'navigateBackMiniProgram', 139 | 'navigateToMiniProgram', 140 | 'setNavigationBarTitle', 141 | 'showNavigationBarLoading', 142 | 'hideNavigationBarLoading', 143 | 'setNavigationBarColor', 144 | 'redirectTo', 145 | 'reLaunch', 146 | 'navigateTo', 147 | 'switchTab', 148 | 'navigateBack' 149 | ] -------------------------------------------------------------------------------- /src/promise.js: -------------------------------------------------------------------------------- 1 | import { asyncMethods } from './method' 2 | 3 | function hasCallback(args) { 4 | if (!args || typeof args !== 'object') return false 5 | 6 | const callback = ['success', 'fail', 'complete'] 7 | for (const m of callback) { 8 | if (typeof args[m] === 'function') return true 9 | } 10 | return false 11 | } 12 | 13 | function _promisify(func) { 14 | if (typeof func !== 'function') return fn 15 | return (args = {}) => 16 | new Promise((resolve, reject) => { 17 | func( 18 | Object.assign(args, { 19 | success: resolve, 20 | fail: reject 21 | }) 22 | ) 23 | }) 24 | } 25 | 26 | export function promisifyAll(wx = {}, wxp = {}) { 27 | Object.keys(wx).forEach(key => { 28 | const fn = wx[key] 29 | if (typeof fn === 'function' && asyncMethods.indexOf(key) >= 0) { 30 | wxp[key] = args => { 31 | if (hasCallback(args)) { 32 | fn(args) 33 | } else { 34 | return _promisify(fn)(args) 35 | } 36 | } 37 | } else { 38 | wxp[key] = fn 39 | } 40 | }) 41 | } 42 | 43 | export const promisify = _promisify --------------------------------------------------------------------------------