-
Notifications
You must be signed in to change notification settings - Fork 240
/
index.js
98 lines (89 loc) · 2.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @copyright Maichong Software Ltd. 2016 http://maichong.it
* @date 2016-09-26
* @author Liang <liang@maichong.it>
*/
'use strict';
import Component from './component';
import PropTypes from './prop-types';
import _createPage from './create-page';
// 特别指定的wx对象中不进行Promise封装的方法
const noPromiseMethods = {
clearStorage: 1,
hideToast: 1,
showNavigationBarLoading: 1,
hideNavigationBarLoading: 1,
drawCanvas: 1,
canvasToTempFilePath: 1,
hideKeyboard: 1,
};
const labrador = {
// 原始wx对象
wx,
// getApp() 优雅的封装
get app() {
return getApp();
},
// getCurrentPages() 优雅的封装
get currentPages() {
return getCurrentPages();
}
};
if (__DEV__) {
Object.defineProperty(labrador, 'Component', {
get(){
console.error('labrador 0.6版本之后废弃了 wx.Component,请使用 ' +
'"import wx, { Component, PropsTypes } from \'labrador\'" 代替 ' +
'"import wx from \'labrador\'"');
}
});
Object.defineProperty(labrador, 'PropsTypes', {
get(){
console.error('labrador 0.6版本之后废弃了 wx.PropsTypes,请使用 ' +
'"import wx, { Component, PropsTypes } from \'labrador\'" 代替 ' +
'"import wx from \'labrador\'"');
}
});
}
Object.keys(wx).forEach((key) => {
if (
noPromiseMethods[key] // 特别指定的方法
|| /^(on|create|stop|pause|close)/.test(key) // 以on* create* stop* pause* close* 开头的方法
|| /\w+Sync$/.test(key) // 以Sync结尾的方法
) {
// 不进行Promise封装
labrador[key] = function () {
if (__DEV__) {
let res = wx[key].apply(wx, arguments);
if (!res) {
res = {};
}
if (res && typeof res === 'object') {
res.then = () => {
console.warn('wx.' + key + ' is not a async function, you should not use await ');
};
}
return res;
}
return wx[key].apply(wx, arguments);
};
return;
}
// 其余方法自动Promise化
labrador[key] = function (obj) {
obj = obj || {};
return new Promise((resolve, reject) => {
obj.success = resolve;
obj.fail = (res) => {
if (res && res.errMsg) {
reject(new Error(res.errMsg));
} else {
reject(res);
}
};
wx[key](obj);
});
};
});
export default labrador;
export { Component, PropTypes, _createPage };