UNPKG

9.03 kBJavaScriptView Raw
1/**
2 * Tencent is pleased to support the open source community by making WePY available.
3 * Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4 *
5 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6 * http://opensource.org/licenses/MIT
7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8 */
9
10let native = {};
11let RequestMQ = {
12 map: {},
13 mq: [],
14 running: [],
15 MAX_REQUEST: 5,
16 push (param) {
17 param.t = +new Date();
18 while ((this.mq.indexOf(param.t) > -1 || this.running.indexOf(param.t) > -1)) {
19 param.t += Math.random() * 10 >> 0;
20 }
21 this.mq.push(param.t);
22 this.map[param.t] = param;
23 },
24 next () {
25 let me = this;
26
27 if (this.mq.length === 0)
28 return;
29
30 if (this.running.length < this.MAX_REQUEST - 1) {
31 let newone = this.mq.shift();
32 let obj = this.map[newone];
33 let oldComplete = obj.complete;
34 obj.complete = (...args) => {
35 me.running.splice(me.running.indexOf(obj.t), 1);
36 delete me.map[obj.t];
37 oldComplete && oldComplete.apply(obj, args);
38 me.next();
39 }
40 this.running.push(obj.t);
41 return wx.request(obj);
42 }
43 },
44 request (obj) {
45
46 obj = obj || {};
47 obj = (typeof(obj) === 'string') ? {url: obj} : obj;
48
49
50 this.push(obj);
51
52 return this.next();
53 }
54};
55
56
57export default class CoreClass {
58
59
60 constructor () {
61 this.$addons = {};
62
63 this.$interceptors = {};
64
65 this.$pages = {};
66 }
67
68
69 $init (wepy, config = {}) {
70 this.$initAPI(wepy, config.noPromiseAPI);
71 this.$wxapp = getApp();
72 }
73
74
75 use (addon, ...args) {
76 if (typeof(addon) === 'string' && this[addon]) {
77 this.$addons[addon] = 1;
78 this[addon](args);
79 } else {
80 this.$addons[addon.name] = new addon(args);
81 }
82 }
83
84 intercept (api, provider) {
85 this.$interceptors[api] = provider;
86 }
87
88 promisify () {
89 console.log('promise已经启用')
90 }
91
92 requestfix () {
93 console.log('requestfix启用')
94 }
95
96 $initAPI (wepy, noPromiseAPI) {
97 const self = this;
98 let noPromiseMethods = {
99 // 媒体
100 stopRecord: true,
101 getRecorderManager: true,
102 pauseVoice: true,
103 stopVoice: true,
104 pauseBackgroundAudio: true,
105 stopBackgroundAudio: true,
106 getBackgroundAudioManager: true,
107 createAudioContext: true,
108 createInnerAudioContext: true,
109 createVideoContext: true,
110 createCameraContext: true,
111
112 // 位置
113 createMapContext: true,
114
115 // 设备
116 canIUse: true,
117 startAccelerometer: true,
118 stopAccelerometer: true,
119 startCompass: true,
120 stopCompass: true,
121 onBLECharacteristicValueChange: true,
122 onBLEConnectionStateChange: true,
123
124 // 界面
125 hideToast: true,
126 hideLoading: true,
127 showNavigationBarLoading: true,
128 hideNavigationBarLoading: true,
129 navigateBack: true,
130 createAnimation: true,
131 pageScrollTo: true,
132 createSelectorQuery: true,
133 createCanvasContext: true,
134 createContext: true,
135 drawCanvas: true,
136 hideKeyboard: true,
137 stopPullDownRefresh: true,
138
139 // 拓展接口
140 arrayBufferToBase64: true,
141 base64ToArrayBuffer: true
142 };
143 if (noPromiseAPI) {
144 if (Array.isArray(noPromiseAPI)) {
145 noPromiseAPI.forEach(v => noPromiseMethods[v] = true);
146 } else {
147 for (let k in noPromiseAPI) {
148 noPromiseMethods[k] = noPromiseAPI[k];
149 }
150 }
151 }
152 Object.keys(wx).forEach((key) => {
153 if (!noPromiseMethods[key] && key.substr(0, 2) !== 'on' && !(/\w+Sync$/.test(key))) {
154 Object.defineProperty(native, key, {
155 get () {
156 return (obj) => {
157 obj = obj || {};
158 if (self.$interceptors[key] && self.$interceptors[key].config) {
159 let rst = self.$interceptors[key].config.call(self, obj);
160 if (rst === false) {
161 if (self.$addons.promisify) {
162 return Promise.reject('aborted by interceptor');
163 } else {
164 obj.fail && obj.fail('aborted by interceptor');
165 return;
166 }
167 }
168 obj = rst;
169 }
170 if (key === 'request') {
171 obj = (typeof(obj) === 'string') ? {url: obj} : obj;
172 }
173 if (typeof obj === 'string') {
174 return wx[key](obj);
175 }
176 if (self.$addons.promisify) {
177 let task;
178 const p = new Promise((resolve, reject) => {
179 let bak = {};
180 ['fail', 'success', 'complete'].forEach((k) => {
181 bak[k] = obj[k];
182 obj[k] = (res) => {
183 if (self.$interceptors[key] && self.$interceptors[key][k]) {
184 res = self.$interceptors[key][k].call(self, res);
185 }
186 if (k === 'success')
187 resolve(res);
188 else if (k === 'fail')
189 reject(res);
190 };
191 });
192 if (self.$addons.requestfix && key === 'request') {
193 RequestMQ.request(obj);
194 } else {
195 task = wx[key](obj);
196 }
197 });
198 if (key === 'uploadFile' || key === 'downloadFile') {
199 p.progress = (cb) => {
200 task.onProgressUpdate(cb);
201 return p;
202 };
203 p.abort = (cb) => {
204 cb && cb();
205 task.abort();
206 return p;
207 }
208 }
209 return p;
210 } else {
211 let bak = {};
212 ['fail', 'success', 'complete'].forEach((k) => {
213 bak[k] = obj[k];
214 obj[k] = (res) => {
215 if (self.$interceptors[key] && self.$interceptors[key][k]) {
216 res = self.$interceptors[key][k].call(self, res);
217 }
218 bak[k] && bak[k].call(self, res);
219 };
220 });
221 if (self.$addons.requestfix && key === 'request') {
222 RequestMQ.request(obj);
223 } else {
224 return wx[key](obj);
225 }
226 }
227 };
228 }
229 });
230 wepy[key] = native[key];
231 } else {
232 Object.defineProperty(native, key, {
233 get () { return (...args) => wx[key].apply(wx, args) }
234 });
235 wepy[key] = native[key];
236 }
237 });
238
239 }
240}
\No newline at end of file