@dcloudio/uni-mp-harmony 2.0.2-alpha-4050720250316001
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/dist/index.js +2644 -0
- package/lib/assets/jsconfig.json +6 -0
- package/lib/uni.compiler.js +4 -0
- package/lib/uni.config.js +53 -0
- package/license.md +1 -0
- package/package.json +21 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,2644 @@
|
|
|
1
|
+
import { initVueI18n } from '@dcloudio/uni-i18n';
|
|
2
|
+
import Vue from 'vue';
|
|
3
|
+
|
|
4
|
+
let realAtob;
|
|
5
|
+
|
|
6
|
+
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
7
|
+
const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
|
|
8
|
+
|
|
9
|
+
if (typeof atob !== 'function') {
|
|
10
|
+
realAtob = function (str) {
|
|
11
|
+
str = String(str).replace(/[\t\n\f\r ]+/g, '');
|
|
12
|
+
if (!b64re.test(str)) { throw new Error("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.") }
|
|
13
|
+
|
|
14
|
+
// Adding the padding if missing, for semplicity
|
|
15
|
+
str += '=='.slice(2 - (str.length & 3));
|
|
16
|
+
var bitmap; var result = ''; var r1; var r2; var i = 0;
|
|
17
|
+
for (; i < str.length;) {
|
|
18
|
+
bitmap = b64.indexOf(str.charAt(i++)) << 18 | b64.indexOf(str.charAt(i++)) << 12 |
|
|
19
|
+
(r1 = b64.indexOf(str.charAt(i++))) << 6 | (r2 = b64.indexOf(str.charAt(i++)));
|
|
20
|
+
|
|
21
|
+
result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
|
|
22
|
+
: r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
|
|
23
|
+
: String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
|
|
24
|
+
}
|
|
25
|
+
return result
|
|
26
|
+
};
|
|
27
|
+
} else {
|
|
28
|
+
// 注意atob只能在全局对象上调用,例如:`const Base64 = {atob};Base64.atob('xxxx')`是错误的用法
|
|
29
|
+
realAtob = atob;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function b64DecodeUnicode (str) {
|
|
33
|
+
return decodeURIComponent(realAtob(str).split('').map(function (c) {
|
|
34
|
+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
|
|
35
|
+
}).join(''))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function getCurrentUserInfo () {
|
|
39
|
+
const token = ( has).getStorageSync('uni_id_token') || '';
|
|
40
|
+
const tokenArr = token.split('.');
|
|
41
|
+
if (!token || tokenArr.length !== 3) {
|
|
42
|
+
return {
|
|
43
|
+
uid: null,
|
|
44
|
+
role: [],
|
|
45
|
+
permission: [],
|
|
46
|
+
tokenExpired: 0
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
let userInfo;
|
|
50
|
+
try {
|
|
51
|
+
userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1]));
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message)
|
|
54
|
+
}
|
|
55
|
+
userInfo.tokenExpired = userInfo.exp * 1000;
|
|
56
|
+
delete userInfo.exp;
|
|
57
|
+
delete userInfo.iat;
|
|
58
|
+
return userInfo
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function uniIdMixin (Vue) {
|
|
62
|
+
Vue.prototype.uniIDHasRole = function (roleId) {
|
|
63
|
+
const {
|
|
64
|
+
role
|
|
65
|
+
} = getCurrentUserInfo();
|
|
66
|
+
return role.indexOf(roleId) > -1
|
|
67
|
+
};
|
|
68
|
+
Vue.prototype.uniIDHasPermission = function (permissionId) {
|
|
69
|
+
const {
|
|
70
|
+
permission
|
|
71
|
+
} = getCurrentUserInfo();
|
|
72
|
+
return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1
|
|
73
|
+
};
|
|
74
|
+
Vue.prototype.uniIDTokenValid = function () {
|
|
75
|
+
const {
|
|
76
|
+
tokenExpired
|
|
77
|
+
} = getCurrentUserInfo();
|
|
78
|
+
return tokenExpired > Date.now()
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const _toString = Object.prototype.toString;
|
|
83
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
84
|
+
|
|
85
|
+
function isFn (fn) {
|
|
86
|
+
return typeof fn === 'function'
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function isStr (str) {
|
|
90
|
+
return typeof str === 'string'
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isObject (obj) {
|
|
94
|
+
return obj !== null && typeof obj === 'object'
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function isPlainObject (obj) {
|
|
98
|
+
return _toString.call(obj) === '[object Object]'
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function hasOwn (obj, key) {
|
|
102
|
+
return hasOwnProperty.call(obj, key)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function noop () {}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Create a cached version of a pure function.
|
|
109
|
+
*/
|
|
110
|
+
function cached (fn) {
|
|
111
|
+
const cache = Object.create(null);
|
|
112
|
+
return function cachedFn (str) {
|
|
113
|
+
const hit = cache[str];
|
|
114
|
+
return hit || (cache[str] = fn(str))
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Camelize a hyphen-delimited string.
|
|
120
|
+
*/
|
|
121
|
+
const camelizeRE = /-(\w)/g;
|
|
122
|
+
const camelize = cached((str) => {
|
|
123
|
+
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const HOOKS = [
|
|
127
|
+
'invoke',
|
|
128
|
+
'success',
|
|
129
|
+
'fail',
|
|
130
|
+
'complete',
|
|
131
|
+
'returnValue'
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
const globalInterceptors = {};
|
|
135
|
+
const scopedInterceptors = {};
|
|
136
|
+
|
|
137
|
+
function mergeHook (parentVal, childVal) {
|
|
138
|
+
const res = childVal
|
|
139
|
+
? parentVal
|
|
140
|
+
? parentVal.concat(childVal)
|
|
141
|
+
: Array.isArray(childVal)
|
|
142
|
+
? childVal : [childVal]
|
|
143
|
+
: parentVal;
|
|
144
|
+
return res
|
|
145
|
+
? dedupeHooks(res)
|
|
146
|
+
: res
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function dedupeHooks (hooks) {
|
|
150
|
+
const res = [];
|
|
151
|
+
for (let i = 0; i < hooks.length; i++) {
|
|
152
|
+
if (res.indexOf(hooks[i]) === -1) {
|
|
153
|
+
res.push(hooks[i]);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return res
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function removeHook (hooks, hook) {
|
|
160
|
+
const index = hooks.indexOf(hook);
|
|
161
|
+
if (index !== -1) {
|
|
162
|
+
hooks.splice(index, 1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function mergeInterceptorHook (interceptor, option) {
|
|
167
|
+
Object.keys(option).forEach(hook => {
|
|
168
|
+
if (HOOKS.indexOf(hook) !== -1 && isFn(option[hook])) {
|
|
169
|
+
interceptor[hook] = mergeHook(interceptor[hook], option[hook]);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function removeInterceptorHook (interceptor, option) {
|
|
175
|
+
if (!interceptor || !option) {
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
Object.keys(option).forEach(hook => {
|
|
179
|
+
if (HOOKS.indexOf(hook) !== -1 && isFn(option[hook])) {
|
|
180
|
+
removeHook(interceptor[hook], option[hook]);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function addInterceptor (method, option) {
|
|
186
|
+
if (typeof method === 'string' && isPlainObject(option)) {
|
|
187
|
+
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), option);
|
|
188
|
+
} else if (isPlainObject(method)) {
|
|
189
|
+
mergeInterceptorHook(globalInterceptors, method);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function removeInterceptor (method, option) {
|
|
194
|
+
if (typeof method === 'string') {
|
|
195
|
+
if (isPlainObject(option)) {
|
|
196
|
+
removeInterceptorHook(scopedInterceptors[method], option);
|
|
197
|
+
} else {
|
|
198
|
+
delete scopedInterceptors[method];
|
|
199
|
+
}
|
|
200
|
+
} else if (isPlainObject(method)) {
|
|
201
|
+
removeInterceptorHook(globalInterceptors, method);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function wrapperHook (hook, params) {
|
|
206
|
+
return function (data) {
|
|
207
|
+
return hook(data, params) || data
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function isPromise (obj) {
|
|
212
|
+
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function queue (hooks, data, params) {
|
|
216
|
+
let promise = false;
|
|
217
|
+
for (let i = 0; i < hooks.length; i++) {
|
|
218
|
+
const hook = hooks[i];
|
|
219
|
+
if (promise) {
|
|
220
|
+
promise = Promise.resolve(wrapperHook(hook, params));
|
|
221
|
+
} else {
|
|
222
|
+
const res = hook(data, params);
|
|
223
|
+
if (isPromise(res)) {
|
|
224
|
+
promise = Promise.resolve(res);
|
|
225
|
+
}
|
|
226
|
+
if (res === false) {
|
|
227
|
+
return {
|
|
228
|
+
then () { }
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return promise || {
|
|
234
|
+
then (callback) {
|
|
235
|
+
return callback(data)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function wrapperOptions (interceptor, options = {}) {
|
|
241
|
+
['success', 'fail', 'complete'].forEach(name => {
|
|
242
|
+
if (Array.isArray(interceptor[name])) {
|
|
243
|
+
const oldCallback = options[name];
|
|
244
|
+
options[name] = function callbackInterceptor (res) {
|
|
245
|
+
queue(interceptor[name], res, options).then((res) => {
|
|
246
|
+
/* eslint-disable no-mixed-operators */
|
|
247
|
+
return isFn(oldCallback) && oldCallback(res) || res
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
return options
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function wrapperReturnValue (method, returnValue) {
|
|
256
|
+
const returnValueHooks = [];
|
|
257
|
+
if (Array.isArray(globalInterceptors.returnValue)) {
|
|
258
|
+
returnValueHooks.push(...globalInterceptors.returnValue);
|
|
259
|
+
}
|
|
260
|
+
const interceptor = scopedInterceptors[method];
|
|
261
|
+
if (interceptor && Array.isArray(interceptor.returnValue)) {
|
|
262
|
+
returnValueHooks.push(...interceptor.returnValue);
|
|
263
|
+
}
|
|
264
|
+
returnValueHooks.forEach(hook => {
|
|
265
|
+
returnValue = hook(returnValue) || returnValue;
|
|
266
|
+
});
|
|
267
|
+
return returnValue
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function getApiInterceptorHooks (method) {
|
|
271
|
+
const interceptor = Object.create(null);
|
|
272
|
+
Object.keys(globalInterceptors).forEach(hook => {
|
|
273
|
+
if (hook !== 'returnValue') {
|
|
274
|
+
interceptor[hook] = globalInterceptors[hook].slice();
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
const scopedInterceptor = scopedInterceptors[method];
|
|
278
|
+
if (scopedInterceptor) {
|
|
279
|
+
Object.keys(scopedInterceptor).forEach(hook => {
|
|
280
|
+
if (hook !== 'returnValue') {
|
|
281
|
+
interceptor[hook] = (interceptor[hook] || []).concat(scopedInterceptor[hook]);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return interceptor
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function invokeApi (method, api, options, ...params) {
|
|
289
|
+
const interceptor = getApiInterceptorHooks(method);
|
|
290
|
+
if (interceptor && Object.keys(interceptor).length) {
|
|
291
|
+
if (Array.isArray(interceptor.invoke)) {
|
|
292
|
+
const res = queue(interceptor.invoke, options);
|
|
293
|
+
return res.then((options) => {
|
|
294
|
+
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
|
|
295
|
+
return api(
|
|
296
|
+
wrapperOptions(getApiInterceptorHooks(method), options),
|
|
297
|
+
...params
|
|
298
|
+
)
|
|
299
|
+
})
|
|
300
|
+
} else {
|
|
301
|
+
return api(wrapperOptions(interceptor, options), ...params)
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return api(options, ...params)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const promiseInterceptor = {
|
|
308
|
+
returnValue (res) {
|
|
309
|
+
if (!isPromise(res)) {
|
|
310
|
+
return res
|
|
311
|
+
}
|
|
312
|
+
return new Promise((resolve, reject) => {
|
|
313
|
+
res.then(res => {
|
|
314
|
+
if (!res) {
|
|
315
|
+
resolve(res);
|
|
316
|
+
return
|
|
317
|
+
}
|
|
318
|
+
if (res[0]) {
|
|
319
|
+
reject(res[0]);
|
|
320
|
+
} else {
|
|
321
|
+
resolve(res[1]);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
})
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const SYNC_API_RE =
|
|
329
|
+
/^\$|Window$|WindowStyle$|sendHostEvent|sendNativeEvent|restoreGlobal|requireGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getLocale|setLocale|invokePushCallback|getWindowInfo|getDeviceInfo|getAppBaseInfo|getSystemSetting|getAppAuthorizeSetting|initUTS|requireUTS|registerUTS/;
|
|
330
|
+
|
|
331
|
+
const CONTEXT_API_RE = /^create|Manager$/;
|
|
332
|
+
|
|
333
|
+
// Context例外情况
|
|
334
|
+
const CONTEXT_API_RE_EXC = ['createBLEConnection'];
|
|
335
|
+
|
|
336
|
+
// 同步例外情况
|
|
337
|
+
const ASYNC_API = ['createBLEConnection', 'createPushMessage'];
|
|
338
|
+
|
|
339
|
+
const CALLBACK_API_RE = /^on|^off/;
|
|
340
|
+
|
|
341
|
+
function isContextApi (name) {
|
|
342
|
+
return CONTEXT_API_RE.test(name) && CONTEXT_API_RE_EXC.indexOf(name) === -1
|
|
343
|
+
}
|
|
344
|
+
function isSyncApi (name) {
|
|
345
|
+
return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function isCallbackApi (name) {
|
|
349
|
+
return CALLBACK_API_RE.test(name) && name !== 'onPush'
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function handlePromise (promise) {
|
|
353
|
+
return promise.then(data => {
|
|
354
|
+
return [null, data]
|
|
355
|
+
})
|
|
356
|
+
.catch(err => [err])
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function shouldPromise (name) {
|
|
360
|
+
if (
|
|
361
|
+
isContextApi(name) ||
|
|
362
|
+
isSyncApi(name) ||
|
|
363
|
+
isCallbackApi(name)
|
|
364
|
+
) {
|
|
365
|
+
return false
|
|
366
|
+
}
|
|
367
|
+
return true
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/* eslint-disable no-extend-native */
|
|
371
|
+
if (!Promise.prototype.finally) {
|
|
372
|
+
Promise.prototype.finally = function (callback) {
|
|
373
|
+
const promise = this.constructor;
|
|
374
|
+
return this.then(
|
|
375
|
+
value => promise.resolve(callback()).then(() => value),
|
|
376
|
+
reason => promise.resolve(callback()).then(() => {
|
|
377
|
+
throw reason
|
|
378
|
+
})
|
|
379
|
+
)
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function promisify (name, api) {
|
|
384
|
+
if (!shouldPromise(name) || !isFn(api)) {
|
|
385
|
+
return api
|
|
386
|
+
}
|
|
387
|
+
return function promiseApi (options = {}, ...params) {
|
|
388
|
+
if (isFn(options.success) || isFn(options.fail) || isFn(options.complete)) {
|
|
389
|
+
return wrapperReturnValue(name, invokeApi(name, api, options, ...params))
|
|
390
|
+
}
|
|
391
|
+
return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => {
|
|
392
|
+
invokeApi(name, api, Object.assign({}, options, {
|
|
393
|
+
success: resolve,
|
|
394
|
+
fail: reject
|
|
395
|
+
}), ...params);
|
|
396
|
+
})))
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const EPS = 1e-4;
|
|
401
|
+
const BASE_DEVICE_WIDTH = 750;
|
|
402
|
+
let isIOS = false;
|
|
403
|
+
let deviceWidth = 0;
|
|
404
|
+
let deviceDPR = 0;
|
|
405
|
+
|
|
406
|
+
function checkDeviceWidth () {
|
|
407
|
+
const {
|
|
408
|
+
platform,
|
|
409
|
+
pixelRatio,
|
|
410
|
+
windowWidth
|
|
411
|
+
} = has.getSystemInfoSync(); // uni=>has runtime 编译目标是 uni 对象,内部不允许直接使用 uni
|
|
412
|
+
|
|
413
|
+
deviceWidth = windowWidth;
|
|
414
|
+
deviceDPR = pixelRatio;
|
|
415
|
+
isIOS = platform === 'ios';
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function upx2px (number, newDeviceWidth) {
|
|
419
|
+
if (deviceWidth === 0) {
|
|
420
|
+
checkDeviceWidth();
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
number = Number(number);
|
|
424
|
+
if (number === 0) {
|
|
425
|
+
return 0
|
|
426
|
+
}
|
|
427
|
+
let result = (number / BASE_DEVICE_WIDTH) * (newDeviceWidth || deviceWidth);
|
|
428
|
+
if (result < 0) {
|
|
429
|
+
result = -result;
|
|
430
|
+
}
|
|
431
|
+
result = Math.floor(result + EPS);
|
|
432
|
+
if (result === 0) {
|
|
433
|
+
if (deviceDPR === 1 || !isIOS) {
|
|
434
|
+
result = 1;
|
|
435
|
+
} else {
|
|
436
|
+
result = 0.5;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return number < 0 ? -result : result
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const LOCALE_ZH_HANS = 'zh-Hans';
|
|
443
|
+
const LOCALE_ZH_HANT = 'zh-Hant';
|
|
444
|
+
const LOCALE_EN = 'en';
|
|
445
|
+
const LOCALE_FR = 'fr';
|
|
446
|
+
const LOCALE_ES = 'es';
|
|
447
|
+
|
|
448
|
+
const messages = {};
|
|
449
|
+
|
|
450
|
+
let locale;
|
|
451
|
+
|
|
452
|
+
{
|
|
453
|
+
locale = normalizeLocale(has.getSystemInfoSync().language) || LOCALE_EN;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function initI18nMessages () {
|
|
457
|
+
if (!isEnableLocale()) {
|
|
458
|
+
return
|
|
459
|
+
}
|
|
460
|
+
const localeKeys = Object.keys(__uniConfig.locales);
|
|
461
|
+
if (localeKeys.length) {
|
|
462
|
+
localeKeys.forEach((locale) => {
|
|
463
|
+
const curMessages = messages[locale];
|
|
464
|
+
const userMessages = __uniConfig.locales[locale];
|
|
465
|
+
if (curMessages) {
|
|
466
|
+
Object.assign(curMessages, userMessages);
|
|
467
|
+
} else {
|
|
468
|
+
messages[locale] = userMessages;
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
initI18nMessages();
|
|
475
|
+
|
|
476
|
+
const i18n = initVueI18n(
|
|
477
|
+
locale,
|
|
478
|
+
{}
|
|
479
|
+
);
|
|
480
|
+
const t = i18n.t;
|
|
481
|
+
const i18nMixin = (i18n.mixin = {
|
|
482
|
+
beforeCreate () {
|
|
483
|
+
const unwatch = i18n.i18n.watchLocale(() => {
|
|
484
|
+
this.$forceUpdate();
|
|
485
|
+
});
|
|
486
|
+
this.$once('hook:beforeDestroy', function () {
|
|
487
|
+
unwatch();
|
|
488
|
+
});
|
|
489
|
+
},
|
|
490
|
+
methods: {
|
|
491
|
+
$$t (key, values) {
|
|
492
|
+
return t(key, values)
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
const setLocale = i18n.setLocale;
|
|
497
|
+
const getLocale = i18n.getLocale;
|
|
498
|
+
|
|
499
|
+
function initAppLocale (Vue, appVm, locale) {
|
|
500
|
+
const state = Vue.observable({
|
|
501
|
+
locale: locale || i18n.getLocale()
|
|
502
|
+
});
|
|
503
|
+
const localeWatchers = [];
|
|
504
|
+
appVm.$watchLocale = fn => {
|
|
505
|
+
localeWatchers.push(fn);
|
|
506
|
+
};
|
|
507
|
+
Object.defineProperty(appVm, '$locale', {
|
|
508
|
+
get () {
|
|
509
|
+
return state.locale
|
|
510
|
+
},
|
|
511
|
+
set (v) {
|
|
512
|
+
state.locale = v;
|
|
513
|
+
localeWatchers.forEach(watch => watch(v));
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function isEnableLocale () {
|
|
519
|
+
return typeof __uniConfig !== 'undefined' && __uniConfig.locales && !!Object.keys(__uniConfig.locales).length
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function include (str, parts) {
|
|
523
|
+
return !!parts.find((part) => str.indexOf(part) !== -1)
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function startsWith (str, parts) {
|
|
527
|
+
return parts.find((part) => str.indexOf(part) === 0)
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function normalizeLocale (locale, messages) {
|
|
531
|
+
if (!locale) {
|
|
532
|
+
return
|
|
533
|
+
}
|
|
534
|
+
locale = locale.trim().replace(/_/g, '-');
|
|
535
|
+
if (messages && messages[locale]) {
|
|
536
|
+
return locale
|
|
537
|
+
}
|
|
538
|
+
locale = locale.toLowerCase();
|
|
539
|
+
if (locale === 'chinese') {
|
|
540
|
+
// 支付宝
|
|
541
|
+
return LOCALE_ZH_HANS
|
|
542
|
+
}
|
|
543
|
+
if (locale.indexOf('zh') === 0) {
|
|
544
|
+
if (locale.indexOf('-hans') > -1) {
|
|
545
|
+
return LOCALE_ZH_HANS
|
|
546
|
+
}
|
|
547
|
+
if (locale.indexOf('-hant') > -1) {
|
|
548
|
+
return LOCALE_ZH_HANT
|
|
549
|
+
}
|
|
550
|
+
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
|
|
551
|
+
return LOCALE_ZH_HANT
|
|
552
|
+
}
|
|
553
|
+
return LOCALE_ZH_HANS
|
|
554
|
+
}
|
|
555
|
+
const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES]);
|
|
556
|
+
if (lang) {
|
|
557
|
+
return lang
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
// export function initI18n() {
|
|
561
|
+
// const localeKeys = Object.keys(__uniConfig.locales || {})
|
|
562
|
+
// if (localeKeys.length) {
|
|
563
|
+
// localeKeys.forEach((locale) =>
|
|
564
|
+
// i18n.add(locale, __uniConfig.locales[locale])
|
|
565
|
+
// )
|
|
566
|
+
// }
|
|
567
|
+
// }
|
|
568
|
+
|
|
569
|
+
function getLocale$1 () {
|
|
570
|
+
// 优先使用 $locale
|
|
571
|
+
if (isFn(getApp)) {
|
|
572
|
+
const app = getApp({
|
|
573
|
+
allowDefault: true
|
|
574
|
+
});
|
|
575
|
+
if (app && app.$vm) {
|
|
576
|
+
return app.$vm.$locale
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return normalizeLocale(has.getSystemInfoSync().language) || LOCALE_EN
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function setLocale$1 (locale) {
|
|
583
|
+
const app = isFn(getApp) ? getApp() : false;
|
|
584
|
+
if (!app) {
|
|
585
|
+
return false
|
|
586
|
+
}
|
|
587
|
+
const oldLocale = app.$vm.$locale;
|
|
588
|
+
if (oldLocale !== locale) {
|
|
589
|
+
app.$vm.$locale = locale;
|
|
590
|
+
onLocaleChangeCallbacks.forEach((fn) => fn({
|
|
591
|
+
locale
|
|
592
|
+
}));
|
|
593
|
+
return true
|
|
594
|
+
}
|
|
595
|
+
return false
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const onLocaleChangeCallbacks = [];
|
|
599
|
+
function onLocaleChange (fn) {
|
|
600
|
+
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
|
|
601
|
+
onLocaleChangeCallbacks.push(fn);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (typeof global !== 'undefined') {
|
|
606
|
+
global.getLocale = getLocale$1;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
const interceptors = {
|
|
610
|
+
promiseInterceptor
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
var baseApi = /*#__PURE__*/Object.freeze({
|
|
614
|
+
__proto__: null,
|
|
615
|
+
upx2px: upx2px,
|
|
616
|
+
getLocale: getLocale$1,
|
|
617
|
+
setLocale: setLocale$1,
|
|
618
|
+
onLocaleChange: onLocaleChange,
|
|
619
|
+
addInterceptor: addInterceptor,
|
|
620
|
+
removeInterceptor: removeInterceptor,
|
|
621
|
+
interceptors: interceptors
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
class EventChannel {
|
|
625
|
+
constructor (id, events) {
|
|
626
|
+
this.id = id;
|
|
627
|
+
this.listener = {};
|
|
628
|
+
this.emitCache = {};
|
|
629
|
+
if (events) {
|
|
630
|
+
Object.keys(events).forEach(name => {
|
|
631
|
+
this.on(name, events[name]);
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
emit (eventName, ...args) {
|
|
637
|
+
const fns = this.listener[eventName];
|
|
638
|
+
if (!fns) {
|
|
639
|
+
return (this.emitCache[eventName] || (this.emitCache[eventName] = [])).push(args)
|
|
640
|
+
}
|
|
641
|
+
fns.forEach(opt => {
|
|
642
|
+
opt.fn.apply(opt.fn, args);
|
|
643
|
+
});
|
|
644
|
+
this.listener[eventName] = fns.filter(opt => opt.type !== 'once');
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
on (eventName, fn) {
|
|
648
|
+
this._addListener(eventName, 'on', fn);
|
|
649
|
+
this._clearCache(eventName);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
once (eventName, fn) {
|
|
653
|
+
this._addListener(eventName, 'once', fn);
|
|
654
|
+
this._clearCache(eventName);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
off (eventName, fn) {
|
|
658
|
+
const fns = this.listener[eventName];
|
|
659
|
+
if (!fns) {
|
|
660
|
+
return
|
|
661
|
+
}
|
|
662
|
+
if (fn) {
|
|
663
|
+
for (let i = 0; i < fns.length;) {
|
|
664
|
+
if (fns[i].fn === fn) {
|
|
665
|
+
fns.splice(i, 1);
|
|
666
|
+
i--;
|
|
667
|
+
}
|
|
668
|
+
i++;
|
|
669
|
+
}
|
|
670
|
+
} else {
|
|
671
|
+
delete this.listener[eventName];
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
_clearCache (eventName) {
|
|
676
|
+
const cacheArgs = this.emitCache[eventName];
|
|
677
|
+
if (cacheArgs) {
|
|
678
|
+
for (; cacheArgs.length > 0;) {
|
|
679
|
+
this.emit.apply(this, [eventName].concat(cacheArgs.shift()));
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
_addListener (eventName, type, fn) {
|
|
685
|
+
(this.listener[eventName] || (this.listener[eventName] = [])).push({
|
|
686
|
+
fn,
|
|
687
|
+
type
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const eventChannels = {};
|
|
693
|
+
|
|
694
|
+
let id = 0;
|
|
695
|
+
|
|
696
|
+
function initEventChannel (events, cache = true) {
|
|
697
|
+
id++;
|
|
698
|
+
const eventChannel = new EventChannel(id, events);
|
|
699
|
+
if (cache) {
|
|
700
|
+
eventChannels[id] = eventChannel;
|
|
701
|
+
}
|
|
702
|
+
return eventChannel
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function getEventChannel (id) {
|
|
706
|
+
const eventChannel = eventChannels[id];
|
|
707
|
+
delete eventChannels[id];
|
|
708
|
+
return eventChannel
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
function navigateTo () {
|
|
712
|
+
let eventChannel;
|
|
713
|
+
return {
|
|
714
|
+
args (fromArgs, toArgs) {
|
|
715
|
+
eventChannel = initEventChannel(fromArgs.events);
|
|
716
|
+
if (fromArgs.url) {
|
|
717
|
+
fromArgs.url = fromArgs.url + (fromArgs.url.indexOf('?') === -1 ? '?' : '&') + '__id__=' + eventChannel.id;
|
|
718
|
+
}
|
|
719
|
+
},
|
|
720
|
+
returnValue (fromRes, toRes) {
|
|
721
|
+
fromRes.eventChannel = eventChannel;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
function findExistsPageIndex (url) {
|
|
727
|
+
const pages = getCurrentPages();
|
|
728
|
+
let len = pages.length;
|
|
729
|
+
while (len--) {
|
|
730
|
+
const page = pages[len];
|
|
731
|
+
if (page.$page && page.$page.fullPath === url) {
|
|
732
|
+
return len
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
return -1
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
var redirectTo = {
|
|
739
|
+
name (fromArgs) {
|
|
740
|
+
if (fromArgs.exists === 'back' && fromArgs.delta) {
|
|
741
|
+
return 'navigateBack'
|
|
742
|
+
}
|
|
743
|
+
return 'redirectTo'
|
|
744
|
+
},
|
|
745
|
+
args (fromArgs) {
|
|
746
|
+
if (fromArgs.exists === 'back' && fromArgs.url) {
|
|
747
|
+
const existsPageIndex = findExistsPageIndex(fromArgs.url);
|
|
748
|
+
if (existsPageIndex !== -1) {
|
|
749
|
+
const delta = getCurrentPages().length - 1 - existsPageIndex;
|
|
750
|
+
if (delta > 0) {
|
|
751
|
+
fromArgs.delta = delta;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
var previewImage = {
|
|
759
|
+
args (fromArgs) {
|
|
760
|
+
let currentIndex = parseInt(fromArgs.current);
|
|
761
|
+
if (isNaN(currentIndex)) {
|
|
762
|
+
return
|
|
763
|
+
}
|
|
764
|
+
const urls = fromArgs.urls;
|
|
765
|
+
if (!Array.isArray(urls)) {
|
|
766
|
+
return
|
|
767
|
+
}
|
|
768
|
+
const len = urls.length;
|
|
769
|
+
if (!len) {
|
|
770
|
+
return
|
|
771
|
+
}
|
|
772
|
+
if (currentIndex < 0) {
|
|
773
|
+
currentIndex = 0;
|
|
774
|
+
} else if (currentIndex >= len) {
|
|
775
|
+
currentIndex = len - 1;
|
|
776
|
+
}
|
|
777
|
+
if (currentIndex > 0) {
|
|
778
|
+
fromArgs.current = urls[currentIndex];
|
|
779
|
+
fromArgs.urls = urls.filter(
|
|
780
|
+
(item, index) => index < currentIndex ? item !== urls[currentIndex] : true
|
|
781
|
+
);
|
|
782
|
+
} else {
|
|
783
|
+
fromArgs.current = urls[0];
|
|
784
|
+
}
|
|
785
|
+
return {
|
|
786
|
+
indicator: false,
|
|
787
|
+
loop: false
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
const UUID_KEY = '__DC_STAT_UUID';
|
|
793
|
+
let deviceId;
|
|
794
|
+
function useDeviceId (result) {
|
|
795
|
+
deviceId = deviceId || has.getStorageSync(UUID_KEY);
|
|
796
|
+
if (!deviceId) {
|
|
797
|
+
deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7);
|
|
798
|
+
has.setStorage({
|
|
799
|
+
key: UUID_KEY,
|
|
800
|
+
data: deviceId
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
result.deviceId = deviceId;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
function addSafeAreaInsets (result) {
|
|
807
|
+
if (result.safeArea) {
|
|
808
|
+
const safeArea = result.safeArea;
|
|
809
|
+
result.safeAreaInsets = {
|
|
810
|
+
top: safeArea.top,
|
|
811
|
+
left: safeArea.left,
|
|
812
|
+
right: result.windowWidth - safeArea.right,
|
|
813
|
+
bottom: result.screenHeight - safeArea.bottom
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
function populateParameters (result) {
|
|
819
|
+
const {
|
|
820
|
+
brand = '', model = '', system = '',
|
|
821
|
+
language = '', theme, version,
|
|
822
|
+
platform, fontSizeSetting,
|
|
823
|
+
SDKVersion, pixelRatio, deviceOrientation
|
|
824
|
+
} = result;
|
|
825
|
+
// const isQuickApp = "mp-harmony".indexOf('quickapp-webview') !== -1
|
|
826
|
+
|
|
827
|
+
const extraParam = {};
|
|
828
|
+
|
|
829
|
+
// osName osVersion
|
|
830
|
+
let osName = '';
|
|
831
|
+
let osVersion = '';
|
|
832
|
+
{
|
|
833
|
+
osName = system.split(' ')[0] || '';
|
|
834
|
+
osVersion = system.split(' ')[1] || '';
|
|
835
|
+
}
|
|
836
|
+
let hostVersion = version;
|
|
837
|
+
|
|
838
|
+
// deviceType
|
|
839
|
+
const deviceType = getGetDeviceType(result, model);
|
|
840
|
+
|
|
841
|
+
// deviceModel
|
|
842
|
+
const deviceBrand = getDeviceBrand(brand);
|
|
843
|
+
|
|
844
|
+
// hostName
|
|
845
|
+
const _hostName = getHostName(result);
|
|
846
|
+
|
|
847
|
+
// deviceOrientation
|
|
848
|
+
let _deviceOrientation = deviceOrientation; // 仅 微信 百度 支持
|
|
849
|
+
|
|
850
|
+
// devicePixelRatio
|
|
851
|
+
let _devicePixelRatio = pixelRatio;
|
|
852
|
+
|
|
853
|
+
// SDKVersion
|
|
854
|
+
let _SDKVersion = SDKVersion;
|
|
855
|
+
|
|
856
|
+
// hostLanguage
|
|
857
|
+
const hostLanguage = language.replace(/_/g, '-');
|
|
858
|
+
|
|
859
|
+
// wx.getAccountInfoSync
|
|
860
|
+
|
|
861
|
+
const parameters = {
|
|
862
|
+
appId: process.env.UNI_APP_ID,
|
|
863
|
+
appName: process.env.UNI_APP_NAME,
|
|
864
|
+
appVersion: process.env.UNI_APP_VERSION_NAME,
|
|
865
|
+
appVersionCode: process.env.UNI_APP_VERSION_CODE,
|
|
866
|
+
appLanguage: getAppLanguage(hostLanguage),
|
|
867
|
+
uniCompileVersion: process.env.UNI_COMPILER_VERSION,
|
|
868
|
+
uniCompilerVersion: process.env.UNI_COMPILER_VERSION,
|
|
869
|
+
uniRuntimeVersion: process.env.UNI_COMPILER_VERSION,
|
|
870
|
+
uniPlatform: process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM,
|
|
871
|
+
deviceBrand,
|
|
872
|
+
deviceModel: model,
|
|
873
|
+
deviceType,
|
|
874
|
+
devicePixelRatio: _devicePixelRatio,
|
|
875
|
+
deviceOrientation: _deviceOrientation,
|
|
876
|
+
osName: osName.toLocaleLowerCase(),
|
|
877
|
+
osVersion,
|
|
878
|
+
hostTheme: theme,
|
|
879
|
+
hostVersion,
|
|
880
|
+
hostLanguage,
|
|
881
|
+
hostName: _hostName,
|
|
882
|
+
hostSDKVersion: _SDKVersion,
|
|
883
|
+
hostFontSizeSetting: fontSizeSetting,
|
|
884
|
+
windowTop: 0,
|
|
885
|
+
windowBottom: 0,
|
|
886
|
+
// TODO
|
|
887
|
+
osLanguage: undefined,
|
|
888
|
+
osTheme: undefined,
|
|
889
|
+
ua: undefined,
|
|
890
|
+
hostPackageName: undefined,
|
|
891
|
+
browserName: undefined,
|
|
892
|
+
browserVersion: undefined,
|
|
893
|
+
isUniAppX: false
|
|
894
|
+
};
|
|
895
|
+
|
|
896
|
+
Object.assign(result, parameters, extraParam);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
function getGetDeviceType (result, model) {
|
|
900
|
+
let deviceType = result.deviceType || 'phone';
|
|
901
|
+
{
|
|
902
|
+
const deviceTypeMaps = {
|
|
903
|
+
ipad: 'pad',
|
|
904
|
+
windows: 'pc',
|
|
905
|
+
mac: 'pc'
|
|
906
|
+
};
|
|
907
|
+
const deviceTypeMapsKeys = Object.keys(deviceTypeMaps);
|
|
908
|
+
const _model = model.toLocaleLowerCase();
|
|
909
|
+
for (let index = 0; index < deviceTypeMapsKeys.length; index++) {
|
|
910
|
+
const _m = deviceTypeMapsKeys[index];
|
|
911
|
+
if (_model.indexOf(_m) !== -1) {
|
|
912
|
+
deviceType = deviceTypeMaps[_m];
|
|
913
|
+
break
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
return deviceType
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
function getDeviceBrand (brand) {
|
|
921
|
+
let deviceBrand = brand;
|
|
922
|
+
if (deviceBrand) {
|
|
923
|
+
deviceBrand = brand.toLocaleLowerCase();
|
|
924
|
+
}
|
|
925
|
+
return deviceBrand
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
function getAppLanguage (defaultLanguage) {
|
|
929
|
+
return getLocale$1
|
|
930
|
+
? getLocale$1()
|
|
931
|
+
: defaultLanguage
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
function getHostName (result) {
|
|
935
|
+
const _platform = "mp-harmony".split('-')[1];
|
|
936
|
+
let _hostName = result.hostName || _platform; // mp-jd
|
|
937
|
+
|
|
938
|
+
return _hostName
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
var getSystemInfo = {
|
|
942
|
+
returnValue: function (result) {
|
|
943
|
+
useDeviceId(result);
|
|
944
|
+
addSafeAreaInsets(result);
|
|
945
|
+
populateParameters(result);
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
|
|
949
|
+
const protocols = {
|
|
950
|
+
navigateTo: navigateTo(),
|
|
951
|
+
redirectTo,
|
|
952
|
+
previewImage,
|
|
953
|
+
getSystemInfo,
|
|
954
|
+
getSystemInfoSync: getSystemInfo
|
|
955
|
+
};
|
|
956
|
+
const todos = [
|
|
957
|
+
'preloadPage',
|
|
958
|
+
'unPreloadPage',
|
|
959
|
+
'loadSubPackage'
|
|
960
|
+
];
|
|
961
|
+
const canIUses = [];
|
|
962
|
+
|
|
963
|
+
const CALLBACKS = ['success', 'fail', 'cancel', 'complete'];
|
|
964
|
+
|
|
965
|
+
function processCallback (methodName, method, returnValue) {
|
|
966
|
+
return function (res) {
|
|
967
|
+
return method(processReturnValue(methodName, res, returnValue))
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}, keepFromArgs = false) {
|
|
972
|
+
if (isPlainObject(fromArgs)) { // 一般 api 的参数解析
|
|
973
|
+
const toArgs = keepFromArgs === true ? fromArgs : {}; // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值
|
|
974
|
+
if (isFn(argsOption)) {
|
|
975
|
+
argsOption = argsOption(fromArgs, toArgs) || {};
|
|
976
|
+
}
|
|
977
|
+
for (const key in fromArgs) {
|
|
978
|
+
if (hasOwn(argsOption, key)) {
|
|
979
|
+
let keyOption = argsOption[key];
|
|
980
|
+
if (isFn(keyOption)) {
|
|
981
|
+
keyOption = keyOption(fromArgs[key], fromArgs, toArgs);
|
|
982
|
+
}
|
|
983
|
+
if (!keyOption) { // 不支持的参数
|
|
984
|
+
console.warn(`The '${methodName}' method of platform '鸿蒙元服务' does not support option '${key}'`);
|
|
985
|
+
} else if (isStr(keyOption)) { // 重写参数 key
|
|
986
|
+
toArgs[keyOption] = fromArgs[key];
|
|
987
|
+
} else if (isPlainObject(keyOption)) { // {name:newName,value:value}可重新指定参数 key:value
|
|
988
|
+
toArgs[keyOption.name ? keyOption.name : key] = keyOption.value;
|
|
989
|
+
}
|
|
990
|
+
} else if (CALLBACKS.indexOf(key) !== -1) {
|
|
991
|
+
if (isFn(fromArgs[key])) {
|
|
992
|
+
toArgs[key] = processCallback(methodName, fromArgs[key], returnValue);
|
|
993
|
+
}
|
|
994
|
+
} else {
|
|
995
|
+
if (!keepFromArgs) {
|
|
996
|
+
toArgs[key] = fromArgs[key];
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
return toArgs
|
|
1001
|
+
} else if (isFn(fromArgs)) {
|
|
1002
|
+
fromArgs = processCallback(methodName, fromArgs, returnValue);
|
|
1003
|
+
}
|
|
1004
|
+
return fromArgs
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
function processReturnValue (methodName, res, returnValue, keepReturnValue = false) {
|
|
1008
|
+
if (isFn(protocols.returnValue)) { // 处理通用 returnValue
|
|
1009
|
+
res = protocols.returnValue(methodName, res);
|
|
1010
|
+
}
|
|
1011
|
+
return processArgs(methodName, res, returnValue, {}, keepReturnValue)
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
function wrapper (methodName, method) {
|
|
1015
|
+
if (hasOwn(protocols, methodName)) {
|
|
1016
|
+
const protocol = protocols[methodName];
|
|
1017
|
+
if (!protocol) { // 暂不支持的 api
|
|
1018
|
+
return function () {
|
|
1019
|
+
console.error(`Platform '鸿蒙元服务' does not support '${methodName}'.`);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return function (arg1, arg2) { // 目前 api 最多两个参数
|
|
1023
|
+
let options = protocol;
|
|
1024
|
+
if (isFn(protocol)) {
|
|
1025
|
+
options = protocol(arg1);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
arg1 = processArgs(methodName, arg1, options.args, options.returnValue);
|
|
1029
|
+
|
|
1030
|
+
const args = [arg1];
|
|
1031
|
+
if (typeof arg2 !== 'undefined') {
|
|
1032
|
+
args.push(arg2);
|
|
1033
|
+
}
|
|
1034
|
+
if (isFn(options.name)) {
|
|
1035
|
+
methodName = options.name(arg1);
|
|
1036
|
+
} else if (isStr(options.name)) {
|
|
1037
|
+
methodName = options.name;
|
|
1038
|
+
}
|
|
1039
|
+
const returnValue = has[methodName].apply(has, args);
|
|
1040
|
+
if (isSyncApi(methodName)) { // 同步 api
|
|
1041
|
+
return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName))
|
|
1042
|
+
}
|
|
1043
|
+
return returnValue
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
return method
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
const todoApis = Object.create(null);
|
|
1050
|
+
|
|
1051
|
+
const TODOS = [
|
|
1052
|
+
'onTabBarMidButtonTap',
|
|
1053
|
+
'subscribePush',
|
|
1054
|
+
'unsubscribePush',
|
|
1055
|
+
'onPush',
|
|
1056
|
+
'offPush',
|
|
1057
|
+
'share'
|
|
1058
|
+
];
|
|
1059
|
+
|
|
1060
|
+
function createTodoApi (name) {
|
|
1061
|
+
return function todoApi ({
|
|
1062
|
+
fail,
|
|
1063
|
+
complete
|
|
1064
|
+
}) {
|
|
1065
|
+
const res = {
|
|
1066
|
+
errMsg: `${name}:fail method '${name}' not supported`
|
|
1067
|
+
};
|
|
1068
|
+
isFn(fail) && fail(res);
|
|
1069
|
+
isFn(complete) && complete(res);
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
TODOS.forEach(function (name) {
|
|
1074
|
+
todoApis[name] = createTodoApi(name);
|
|
1075
|
+
});
|
|
1076
|
+
|
|
1077
|
+
const providers = {
|
|
1078
|
+
oauth: ['huawei'],
|
|
1079
|
+
share: ['huawei'],
|
|
1080
|
+
payment: ['huawei'],
|
|
1081
|
+
push: ['huawei']
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
if (has.canIUse('getAccountProvider')) {
|
|
1085
|
+
providers.oauth.push(has.getAccountProvider());
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
if (has.canIUse('getVendorPaymentProvider')) {
|
|
1089
|
+
providers.payment.push(has.getVendorPaymentProvider());
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
function getProvider ({
|
|
1093
|
+
service,
|
|
1094
|
+
success,
|
|
1095
|
+
fail,
|
|
1096
|
+
complete
|
|
1097
|
+
}) {
|
|
1098
|
+
let res = false;
|
|
1099
|
+
if (providers[service]) {
|
|
1100
|
+
res = {
|
|
1101
|
+
errMsg: 'getProvider:ok',
|
|
1102
|
+
service,
|
|
1103
|
+
provider: providers[service]
|
|
1104
|
+
};
|
|
1105
|
+
isFn(success) && success(res);
|
|
1106
|
+
} else {
|
|
1107
|
+
res = {
|
|
1108
|
+
errMsg: 'getProvider:fail service not found'
|
|
1109
|
+
};
|
|
1110
|
+
isFn(fail) && fail(res);
|
|
1111
|
+
}
|
|
1112
|
+
isFn(complete) && complete(res);
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
var extraApi = /*#__PURE__*/Object.freeze({
|
|
1116
|
+
__proto__: null,
|
|
1117
|
+
getProvider: getProvider
|
|
1118
|
+
});
|
|
1119
|
+
|
|
1120
|
+
const getEmitter = (function () {
|
|
1121
|
+
let Emitter;
|
|
1122
|
+
return function getUniEmitter () {
|
|
1123
|
+
if (!Emitter) {
|
|
1124
|
+
Emitter = new Vue();
|
|
1125
|
+
}
|
|
1126
|
+
return Emitter
|
|
1127
|
+
}
|
|
1128
|
+
})();
|
|
1129
|
+
|
|
1130
|
+
function apply (ctx, method, args) {
|
|
1131
|
+
return ctx[method].apply(ctx, args)
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
function $on () {
|
|
1135
|
+
return apply(getEmitter(), '$on', [...arguments])
|
|
1136
|
+
}
|
|
1137
|
+
function $off () {
|
|
1138
|
+
return apply(getEmitter(), '$off', [...arguments])
|
|
1139
|
+
}
|
|
1140
|
+
function $once () {
|
|
1141
|
+
return apply(getEmitter(), '$once', [...arguments])
|
|
1142
|
+
}
|
|
1143
|
+
function $emit () {
|
|
1144
|
+
return apply(getEmitter(), '$emit', [...arguments])
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
var eventApi = /*#__PURE__*/Object.freeze({
|
|
1148
|
+
__proto__: null,
|
|
1149
|
+
$on: $on,
|
|
1150
|
+
$off: $off,
|
|
1151
|
+
$once: $once,
|
|
1152
|
+
$emit: $emit
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1155
|
+
/**
|
|
1156
|
+
* 框架内 try-catch
|
|
1157
|
+
*/
|
|
1158
|
+
/**
|
|
1159
|
+
* 开发者 try-catch
|
|
1160
|
+
*/
|
|
1161
|
+
function tryCatch (fn) {
|
|
1162
|
+
return function () {
|
|
1163
|
+
try {
|
|
1164
|
+
return fn.apply(fn, arguments)
|
|
1165
|
+
} catch (e) {
|
|
1166
|
+
// TODO
|
|
1167
|
+
console.error(e);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
function getApiCallbacks (params) {
|
|
1173
|
+
const apiCallbacks = {};
|
|
1174
|
+
for (const name in params) {
|
|
1175
|
+
const param = params[name];
|
|
1176
|
+
if (isFn(param)) {
|
|
1177
|
+
apiCallbacks[name] = tryCatch(param);
|
|
1178
|
+
delete params[name];
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
return apiCallbacks
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
let cid;
|
|
1185
|
+
let cidErrMsg;
|
|
1186
|
+
let enabled;
|
|
1187
|
+
|
|
1188
|
+
function normalizePushMessage (message) {
|
|
1189
|
+
try {
|
|
1190
|
+
return JSON.parse(message)
|
|
1191
|
+
} catch (e) {}
|
|
1192
|
+
return message
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
function invokePushCallback (
|
|
1196
|
+
args
|
|
1197
|
+
) {
|
|
1198
|
+
if (args.type === 'enabled') {
|
|
1199
|
+
enabled = true;
|
|
1200
|
+
} else if (args.type === 'clientId') {
|
|
1201
|
+
cid = args.cid;
|
|
1202
|
+
cidErrMsg = args.errMsg;
|
|
1203
|
+
invokeGetPushCidCallbacks(cid, args.errMsg);
|
|
1204
|
+
} else if (args.type === 'pushMsg') {
|
|
1205
|
+
const message = {
|
|
1206
|
+
type: 'receive',
|
|
1207
|
+
data: normalizePushMessage(args.message)
|
|
1208
|
+
};
|
|
1209
|
+
for (let i = 0; i < onPushMessageCallbacks.length; i++) {
|
|
1210
|
+
const callback = onPushMessageCallbacks[i];
|
|
1211
|
+
callback(message);
|
|
1212
|
+
// 该消息已被阻止
|
|
1213
|
+
if (message.stopped) {
|
|
1214
|
+
break
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
} else if (args.type === 'click') {
|
|
1218
|
+
onPushMessageCallbacks.forEach((callback) => {
|
|
1219
|
+
callback({
|
|
1220
|
+
type: 'click',
|
|
1221
|
+
data: normalizePushMessage(args.message)
|
|
1222
|
+
});
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
const getPushCidCallbacks = [];
|
|
1228
|
+
|
|
1229
|
+
function invokeGetPushCidCallbacks (cid, errMsg) {
|
|
1230
|
+
getPushCidCallbacks.forEach((callback) => {
|
|
1231
|
+
callback(cid, errMsg);
|
|
1232
|
+
});
|
|
1233
|
+
getPushCidCallbacks.length = 0;
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
function getPushClientId (args) {
|
|
1237
|
+
if (!isPlainObject(args)) {
|
|
1238
|
+
args = {};
|
|
1239
|
+
}
|
|
1240
|
+
const {
|
|
1241
|
+
success,
|
|
1242
|
+
fail,
|
|
1243
|
+
complete
|
|
1244
|
+
} = getApiCallbacks(args);
|
|
1245
|
+
const hasSuccess = isFn(success);
|
|
1246
|
+
const hasFail = isFn(fail);
|
|
1247
|
+
const hasComplete = isFn(complete);
|
|
1248
|
+
|
|
1249
|
+
Promise.resolve().then(() => {
|
|
1250
|
+
if (typeof enabled === 'undefined') {
|
|
1251
|
+
enabled = false;
|
|
1252
|
+
cid = '';
|
|
1253
|
+
cidErrMsg = 'uniPush is not enabled';
|
|
1254
|
+
}
|
|
1255
|
+
getPushCidCallbacks.push((cid, errMsg) => {
|
|
1256
|
+
let res;
|
|
1257
|
+
if (cid) {
|
|
1258
|
+
res = {
|
|
1259
|
+
errMsg: 'getPushClientId:ok',
|
|
1260
|
+
cid
|
|
1261
|
+
};
|
|
1262
|
+
hasSuccess && success(res);
|
|
1263
|
+
} else {
|
|
1264
|
+
res = {
|
|
1265
|
+
errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '')
|
|
1266
|
+
};
|
|
1267
|
+
hasFail && fail(res);
|
|
1268
|
+
}
|
|
1269
|
+
hasComplete && complete(res);
|
|
1270
|
+
});
|
|
1271
|
+
if (typeof cid !== 'undefined') {
|
|
1272
|
+
invokeGetPushCidCallbacks(cid, cidErrMsg);
|
|
1273
|
+
}
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
const onPushMessageCallbacks = [];
|
|
1278
|
+
// 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现
|
|
1279
|
+
const onPushMessage = (fn) => {
|
|
1280
|
+
if (onPushMessageCallbacks.indexOf(fn) === -1) {
|
|
1281
|
+
onPushMessageCallbacks.push(fn);
|
|
1282
|
+
}
|
|
1283
|
+
};
|
|
1284
|
+
|
|
1285
|
+
const offPushMessage = (fn) => {
|
|
1286
|
+
if (!fn) {
|
|
1287
|
+
onPushMessageCallbacks.length = 0;
|
|
1288
|
+
} else {
|
|
1289
|
+
const index = onPushMessageCallbacks.indexOf(fn);
|
|
1290
|
+
if (index > -1) {
|
|
1291
|
+
onPushMessageCallbacks.splice(index, 1);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
};
|
|
1295
|
+
|
|
1296
|
+
var api = /*#__PURE__*/Object.freeze({
|
|
1297
|
+
__proto__: null,
|
|
1298
|
+
getPushClientId: getPushClientId,
|
|
1299
|
+
onPushMessage: onPushMessage,
|
|
1300
|
+
offPushMessage: offPushMessage,
|
|
1301
|
+
invokePushCallback: invokePushCallback
|
|
1302
|
+
});
|
|
1303
|
+
|
|
1304
|
+
function findVmByVueId (vm, vuePid) {
|
|
1305
|
+
const $children = vm.$children;
|
|
1306
|
+
// 优先查找直属(反向查找:https://github.com/dcloudio/uni-app/issues/1200)
|
|
1307
|
+
for (let i = $children.length - 1; i >= 0; i--) {
|
|
1308
|
+
const childVm = $children[i];
|
|
1309
|
+
if (childVm.$scope._$vueId === vuePid) {
|
|
1310
|
+
return childVm
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
// 反向递归查找
|
|
1314
|
+
let parentVm;
|
|
1315
|
+
for (let i = $children.length - 1; i >= 0; i--) {
|
|
1316
|
+
parentVm = findVmByVueId($children[i], vuePid);
|
|
1317
|
+
if (parentVm) {
|
|
1318
|
+
return parentVm
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
function initBehavior (options) {
|
|
1324
|
+
return Behavior(options)
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
function selectAllComponents (mpInstance, selector, $refs) {
|
|
1328
|
+
const components = mpInstance.selectAllComponents(selector) || [];
|
|
1329
|
+
components.forEach(component => {
|
|
1330
|
+
const ref = component.dataset.ref;
|
|
1331
|
+
$refs[ref] = component.$vm || toSkip(component);
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
function syncRefs (refs, newRefs) {
|
|
1336
|
+
const oldKeys = new Set(...Object.keys(refs));
|
|
1337
|
+
const newKeys = Object.keys(newRefs);
|
|
1338
|
+
newKeys.forEach(key => {
|
|
1339
|
+
const oldValue = refs[key];
|
|
1340
|
+
const newValue = newRefs[key];
|
|
1341
|
+
if (Array.isArray(oldValue) && Array.isArray(newValue) && oldValue.length === newValue.length && newValue.every(value => oldValue.includes(value))) {
|
|
1342
|
+
return
|
|
1343
|
+
}
|
|
1344
|
+
refs[key] = newValue;
|
|
1345
|
+
oldKeys.delete(key);
|
|
1346
|
+
});
|
|
1347
|
+
oldKeys.forEach(key => {
|
|
1348
|
+
delete refs[key];
|
|
1349
|
+
});
|
|
1350
|
+
return refs
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
function initRefs (vm) {
|
|
1354
|
+
const mpInstance = vm.$scope;
|
|
1355
|
+
const refs = {};
|
|
1356
|
+
Object.defineProperty(vm, '$refs', {
|
|
1357
|
+
get () {
|
|
1358
|
+
const $refs = {};
|
|
1359
|
+
selectAllComponents(mpInstance, '.vue-ref', $refs);
|
|
1360
|
+
// TODO 暂不考虑 for 中的 scoped
|
|
1361
|
+
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for') || [];
|
|
1362
|
+
forComponents.forEach(component => {
|
|
1363
|
+
const ref = component.dataset.ref;
|
|
1364
|
+
if (!$refs[ref]) {
|
|
1365
|
+
$refs[ref] = [];
|
|
1366
|
+
}
|
|
1367
|
+
$refs[ref].push(component.$vm || toSkip(component));
|
|
1368
|
+
});
|
|
1369
|
+
return syncRefs(refs, $refs)
|
|
1370
|
+
}
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
function handleLink (event) {
|
|
1375
|
+
const {
|
|
1376
|
+
vuePid,
|
|
1377
|
+
vueOptions
|
|
1378
|
+
} = event.detail || event.value; // detail 是微信,value 是百度(dipatch)
|
|
1379
|
+
|
|
1380
|
+
let parentVm;
|
|
1381
|
+
|
|
1382
|
+
if (vuePid) {
|
|
1383
|
+
parentVm = findVmByVueId(this.$vm, vuePid);
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
if (!parentVm) {
|
|
1387
|
+
parentVm = this.$vm;
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
vueOptions.parent = parentVm;
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
function markMPComponent (component) {
|
|
1394
|
+
// 在 Vue 中标记为小程序组件
|
|
1395
|
+
const IS_MP = '__v_isMPComponent';
|
|
1396
|
+
Object.defineProperty(component, IS_MP, {
|
|
1397
|
+
configurable: true,
|
|
1398
|
+
enumerable: false,
|
|
1399
|
+
value: true
|
|
1400
|
+
});
|
|
1401
|
+
return component
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
function toSkip (obj) {
|
|
1405
|
+
const OB = '__ob__';
|
|
1406
|
+
const SKIP = '__v_skip';
|
|
1407
|
+
if (isObject(obj) && Object.isExtensible(obj)) {
|
|
1408
|
+
// 避免被 @vue/composition-api 观测
|
|
1409
|
+
Object.defineProperty(obj, OB, {
|
|
1410
|
+
configurable: true,
|
|
1411
|
+
enumerable: false,
|
|
1412
|
+
value: {
|
|
1413
|
+
[SKIP]: true
|
|
1414
|
+
}
|
|
1415
|
+
});
|
|
1416
|
+
}
|
|
1417
|
+
return obj
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
const MPPage = Page;
|
|
1421
|
+
const MPComponent = Component;
|
|
1422
|
+
|
|
1423
|
+
const customizeRE = /:/g;
|
|
1424
|
+
|
|
1425
|
+
const customize = cached((str) => {
|
|
1426
|
+
return camelize(str.replace(customizeRE, '-'))
|
|
1427
|
+
});
|
|
1428
|
+
|
|
1429
|
+
function initTriggerEvent (mpInstance) {
|
|
1430
|
+
const oldTriggerEvent = mpInstance.triggerEvent;
|
|
1431
|
+
const newTriggerEvent = function (event, ...args) {
|
|
1432
|
+
// 事件名统一转驼峰格式,仅处理:当前组件为 vue 组件、当前组件为 vue 组件子组件
|
|
1433
|
+
if (this.$vm || (this.dataset && this.dataset.comType)) {
|
|
1434
|
+
event = customize(event);
|
|
1435
|
+
}
|
|
1436
|
+
return oldTriggerEvent.apply(this, [event, ...args])
|
|
1437
|
+
};
|
|
1438
|
+
try {
|
|
1439
|
+
// 京东小程序 triggerEvent 为只读
|
|
1440
|
+
mpInstance.triggerEvent = newTriggerEvent;
|
|
1441
|
+
} catch (error) {
|
|
1442
|
+
mpInstance._triggerEvent = newTriggerEvent;
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
function initHook (name, options, isComponent) {
|
|
1447
|
+
const oldHook = options[name];
|
|
1448
|
+
options[name] = function (...args) {
|
|
1449
|
+
markMPComponent(this);
|
|
1450
|
+
initTriggerEvent(this);
|
|
1451
|
+
if (oldHook) {
|
|
1452
|
+
return oldHook.apply(this, args)
|
|
1453
|
+
}
|
|
1454
|
+
};
|
|
1455
|
+
}
|
|
1456
|
+
if (!MPPage.__$wrappered) {
|
|
1457
|
+
MPPage.__$wrappered = true;
|
|
1458
|
+
Page = function (options = {}) {
|
|
1459
|
+
initHook('onLoad', options);
|
|
1460
|
+
return MPPage(options)
|
|
1461
|
+
};
|
|
1462
|
+
Page.after = MPPage.after;
|
|
1463
|
+
|
|
1464
|
+
Component = function (options = {}) {
|
|
1465
|
+
initHook('created', options);
|
|
1466
|
+
return MPComponent(options)
|
|
1467
|
+
};
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
const PAGE_EVENT_HOOKS = [
|
|
1471
|
+
'onPullDownRefresh',
|
|
1472
|
+
'onReachBottom',
|
|
1473
|
+
'onAddToFavorites',
|
|
1474
|
+
'onShareTimeline',
|
|
1475
|
+
'onShareAppMessage',
|
|
1476
|
+
'onPageScroll',
|
|
1477
|
+
'onResize',
|
|
1478
|
+
'onTabItemTap'
|
|
1479
|
+
];
|
|
1480
|
+
|
|
1481
|
+
function initMocks (vm, mocks) {
|
|
1482
|
+
const mpInstance = vm.$mp[vm.mpType];
|
|
1483
|
+
mocks.forEach(mock => {
|
|
1484
|
+
if (hasOwn(mpInstance, mock)) {
|
|
1485
|
+
vm[mock] = mpInstance[mock];
|
|
1486
|
+
}
|
|
1487
|
+
});
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
function hasHook (hook, vueOptions) {
|
|
1491
|
+
if (!vueOptions) {
|
|
1492
|
+
return true
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
if (Vue.options && Array.isArray(Vue.options[hook])) {
|
|
1496
|
+
return true
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
vueOptions = vueOptions.default || vueOptions;
|
|
1500
|
+
|
|
1501
|
+
if (isFn(vueOptions)) {
|
|
1502
|
+
if (isFn(vueOptions.extendOptions[hook])) {
|
|
1503
|
+
return true
|
|
1504
|
+
}
|
|
1505
|
+
if (vueOptions.super &&
|
|
1506
|
+
vueOptions.super.options &&
|
|
1507
|
+
Array.isArray(vueOptions.super.options[hook])) {
|
|
1508
|
+
return true
|
|
1509
|
+
}
|
|
1510
|
+
return false
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
if (isFn(vueOptions[hook]) || Array.isArray(vueOptions[hook])) {
|
|
1514
|
+
return true
|
|
1515
|
+
}
|
|
1516
|
+
const mixins = vueOptions.mixins;
|
|
1517
|
+
if (Array.isArray(mixins)) {
|
|
1518
|
+
return !!mixins.find(mixin => hasHook(hook, mixin))
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
function initHooks (mpOptions, hooks, vueOptions) {
|
|
1523
|
+
hooks.forEach(hook => {
|
|
1524
|
+
if (hasHook(hook, vueOptions)) {
|
|
1525
|
+
mpOptions[hook] = function (args) {
|
|
1526
|
+
return this.$vm && this.$vm.__call_hook(hook, args)
|
|
1527
|
+
};
|
|
1528
|
+
}
|
|
1529
|
+
});
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
function initUnknownHooks (mpOptions, vueOptions, excludes = []) {
|
|
1533
|
+
findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes));
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
function findHooks (vueOptions, hooks = []) {
|
|
1537
|
+
if (vueOptions) {
|
|
1538
|
+
Object.keys(vueOptions).forEach((name) => {
|
|
1539
|
+
if (name.indexOf('on') === 0 && isFn(vueOptions[name])) {
|
|
1540
|
+
hooks.push(name);
|
|
1541
|
+
}
|
|
1542
|
+
});
|
|
1543
|
+
}
|
|
1544
|
+
return hooks
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
function initHook$1 (mpOptions, hook, excludes) {
|
|
1548
|
+
if (excludes.indexOf(hook) === -1 && !hasOwn(mpOptions, hook)) {
|
|
1549
|
+
mpOptions[hook] = function (args) {
|
|
1550
|
+
return this.$vm && this.$vm.__call_hook(hook, args)
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
function initVueComponent (Vue, vueOptions) {
|
|
1556
|
+
vueOptions = vueOptions.default || vueOptions;
|
|
1557
|
+
let VueComponent;
|
|
1558
|
+
if (isFn(vueOptions)) {
|
|
1559
|
+
VueComponent = vueOptions;
|
|
1560
|
+
} else {
|
|
1561
|
+
VueComponent = Vue.extend(vueOptions);
|
|
1562
|
+
}
|
|
1563
|
+
vueOptions = VueComponent.options;
|
|
1564
|
+
return [VueComponent, vueOptions]
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
function initSlots (vm, vueSlots) {
|
|
1568
|
+
if (Array.isArray(vueSlots) && vueSlots.length) {
|
|
1569
|
+
const $slots = Object.create(null);
|
|
1570
|
+
vueSlots.forEach(slotName => {
|
|
1571
|
+
$slots[slotName] = true;
|
|
1572
|
+
});
|
|
1573
|
+
vm.$scopedSlots = vm.$slots = $slots;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
function initVueIds (vueIds, mpInstance) {
|
|
1578
|
+
vueIds = (vueIds || '').split(',');
|
|
1579
|
+
const len = vueIds.length;
|
|
1580
|
+
|
|
1581
|
+
if (len === 1) {
|
|
1582
|
+
mpInstance._$vueId = vueIds[0];
|
|
1583
|
+
} else if (len === 2) {
|
|
1584
|
+
mpInstance._$vueId = vueIds[0];
|
|
1585
|
+
mpInstance._$vuePid = vueIds[1];
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
function initData (vueOptions, context) {
|
|
1590
|
+
let data = vueOptions.data || {};
|
|
1591
|
+
const methods = vueOptions.methods || {};
|
|
1592
|
+
|
|
1593
|
+
if (typeof data === 'function') {
|
|
1594
|
+
try {
|
|
1595
|
+
data = data.call(context); // 支持 Vue.prototype 上挂的数据
|
|
1596
|
+
} catch (e) {
|
|
1597
|
+
if (process.env.VUE_APP_DEBUG) {
|
|
1598
|
+
console.warn('根据 Vue 的 data 函数初始化小程序 data 失败,请尽量确保 data 函数中不访问 vm 对象,否则可能影响首次数据渲染速度。', data);
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
} else {
|
|
1602
|
+
try {
|
|
1603
|
+
// 对 data 格式化
|
|
1604
|
+
data = JSON.parse(JSON.stringify(data));
|
|
1605
|
+
} catch (e) { }
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
if (!isPlainObject(data)) {
|
|
1609
|
+
data = {};
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
Object.keys(methods).forEach(methodName => {
|
|
1613
|
+
if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) {
|
|
1614
|
+
data[methodName] = methods[methodName];
|
|
1615
|
+
}
|
|
1616
|
+
});
|
|
1617
|
+
|
|
1618
|
+
return data
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
|
|
1622
|
+
|
|
1623
|
+
function createObserver (name) {
|
|
1624
|
+
return function observer (newVal, oldVal) {
|
|
1625
|
+
if (this.$vm) {
|
|
1626
|
+
this.$vm[name] = newVal; // 为了触发其他非 render watcher
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
function initBehaviors (vueOptions, initBehavior) {
|
|
1632
|
+
const vueBehaviors = vueOptions.behaviors;
|
|
1633
|
+
const vueExtends = vueOptions.extends;
|
|
1634
|
+
const vueMixins = vueOptions.mixins;
|
|
1635
|
+
|
|
1636
|
+
let vueProps = vueOptions.props;
|
|
1637
|
+
|
|
1638
|
+
if (!vueProps) {
|
|
1639
|
+
vueOptions.props = vueProps = [];
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
const behaviors = [];
|
|
1643
|
+
if (Array.isArray(vueBehaviors)) {
|
|
1644
|
+
vueBehaviors.forEach(behavior => {
|
|
1645
|
+
behaviors.push(behavior.replace('uni://', `${"has"}://`));
|
|
1646
|
+
if (behavior === 'uni://form-field') {
|
|
1647
|
+
if (Array.isArray(vueProps)) {
|
|
1648
|
+
vueProps.push('name');
|
|
1649
|
+
vueProps.push('value');
|
|
1650
|
+
} else {
|
|
1651
|
+
vueProps.name = {
|
|
1652
|
+
type: String,
|
|
1653
|
+
default: ''
|
|
1654
|
+
};
|
|
1655
|
+
vueProps.value = {
|
|
1656
|
+
type: [String, Number, Boolean, Array, Object, Date],
|
|
1657
|
+
default: ''
|
|
1658
|
+
};
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
});
|
|
1662
|
+
}
|
|
1663
|
+
if (isPlainObject(vueExtends) && vueExtends.props) {
|
|
1664
|
+
behaviors.push(
|
|
1665
|
+
initBehavior({
|
|
1666
|
+
properties: initProperties(vueExtends.props, true)
|
|
1667
|
+
})
|
|
1668
|
+
);
|
|
1669
|
+
}
|
|
1670
|
+
if (Array.isArray(vueMixins)) {
|
|
1671
|
+
vueMixins.forEach(vueMixin => {
|
|
1672
|
+
if (isPlainObject(vueMixin) && vueMixin.props) {
|
|
1673
|
+
behaviors.push(
|
|
1674
|
+
initBehavior({
|
|
1675
|
+
properties: initProperties(vueMixin.props, true)
|
|
1676
|
+
})
|
|
1677
|
+
);
|
|
1678
|
+
}
|
|
1679
|
+
});
|
|
1680
|
+
}
|
|
1681
|
+
return behaviors
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1684
|
+
function parsePropType (key, type, defaultValue, file) {
|
|
1685
|
+
// [String]=>String
|
|
1686
|
+
if (Array.isArray(type) && type.length === 1) {
|
|
1687
|
+
return type[0]
|
|
1688
|
+
}
|
|
1689
|
+
return type
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
function initProperties (props, isBehavior = false, file = '', options) {
|
|
1693
|
+
const properties = {};
|
|
1694
|
+
if (!isBehavior) {
|
|
1695
|
+
properties.vueId = {
|
|
1696
|
+
type: String,
|
|
1697
|
+
value: ''
|
|
1698
|
+
};
|
|
1699
|
+
// scopedSlotsCompiler auto
|
|
1700
|
+
properties.scopedSlotsCompiler = {
|
|
1701
|
+
type: String,
|
|
1702
|
+
value: ''
|
|
1703
|
+
};
|
|
1704
|
+
properties.vueSlots = { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
|
|
1705
|
+
type: null,
|
|
1706
|
+
value: [],
|
|
1707
|
+
observer: function (newVal, oldVal) {
|
|
1708
|
+
const $slots = Object.create(null);
|
|
1709
|
+
newVal.forEach(slotName => {
|
|
1710
|
+
$slots[slotName] = true;
|
|
1711
|
+
});
|
|
1712
|
+
this.setData({
|
|
1713
|
+
$slots
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
1718
|
+
if (Array.isArray(props)) { // ['title']
|
|
1719
|
+
props.forEach(key => {
|
|
1720
|
+
properties[key] = {
|
|
1721
|
+
type: null,
|
|
1722
|
+
observer: createObserver(key)
|
|
1723
|
+
};
|
|
1724
|
+
});
|
|
1725
|
+
} else if (isPlainObject(props)) { // {title:{type:String,default:''},content:String}
|
|
1726
|
+
Object.keys(props).forEach(key => {
|
|
1727
|
+
const opts = props[key];
|
|
1728
|
+
if (isPlainObject(opts)) { // title:{type:String,default:''}
|
|
1729
|
+
let value = opts.default;
|
|
1730
|
+
if (isFn(value)) {
|
|
1731
|
+
value = value();
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
opts.type = parsePropType(key, opts.type);
|
|
1735
|
+
|
|
1736
|
+
properties[key] = {
|
|
1737
|
+
type: PROP_TYPES.indexOf(opts.type) !== -1 ? opts.type : null,
|
|
1738
|
+
value,
|
|
1739
|
+
observer: createObserver(key)
|
|
1740
|
+
};
|
|
1741
|
+
} else { // content:String
|
|
1742
|
+
const type = parsePropType(key, opts);
|
|
1743
|
+
properties[key] = {
|
|
1744
|
+
type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
|
|
1745
|
+
observer: createObserver(key)
|
|
1746
|
+
};
|
|
1747
|
+
}
|
|
1748
|
+
});
|
|
1749
|
+
}
|
|
1750
|
+
return properties
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
function wrapper$1 (event) {
|
|
1754
|
+
// TODO 又得兼容 mpvue 的 mp 对象
|
|
1755
|
+
try {
|
|
1756
|
+
event.mp = JSON.parse(JSON.stringify(event));
|
|
1757
|
+
} catch (e) { }
|
|
1758
|
+
|
|
1759
|
+
event.stopPropagation = noop;
|
|
1760
|
+
event.preventDefault = noop;
|
|
1761
|
+
|
|
1762
|
+
event.target = event.target || {};
|
|
1763
|
+
|
|
1764
|
+
if (!hasOwn(event, 'detail')) {
|
|
1765
|
+
event.detail = {};
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
if (hasOwn(event, 'markerId')) {
|
|
1769
|
+
event.detail = typeof event.detail === 'object' ? event.detail : {};
|
|
1770
|
+
event.detail.markerId = event.markerId;
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
if (isPlainObject(event.detail)) {
|
|
1774
|
+
event.target = Object.assign({}, event.target, event.detail);
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
return event
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
function getExtraValue (vm, dataPathsArray) {
|
|
1781
|
+
let context = vm;
|
|
1782
|
+
dataPathsArray.forEach(dataPathArray => {
|
|
1783
|
+
const dataPath = dataPathArray[0];
|
|
1784
|
+
const value = dataPathArray[2];
|
|
1785
|
+
if (dataPath || typeof value !== 'undefined') { // ['','',index,'disable']
|
|
1786
|
+
const propPath = dataPathArray[1];
|
|
1787
|
+
const valuePath = dataPathArray[3];
|
|
1788
|
+
|
|
1789
|
+
let vFor;
|
|
1790
|
+
if (Number.isInteger(dataPath)) {
|
|
1791
|
+
vFor = dataPath;
|
|
1792
|
+
} else if (!dataPath) {
|
|
1793
|
+
vFor = context;
|
|
1794
|
+
} else if (typeof dataPath === 'string' && dataPath) {
|
|
1795
|
+
if (dataPath.indexOf('#s#') === 0) {
|
|
1796
|
+
vFor = dataPath.substr(3);
|
|
1797
|
+
} else {
|
|
1798
|
+
vFor = vm.__get_value(dataPath, context);
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
if (Number.isInteger(vFor)) {
|
|
1803
|
+
context = value;
|
|
1804
|
+
} else if (!propPath) {
|
|
1805
|
+
context = vFor[value];
|
|
1806
|
+
} else {
|
|
1807
|
+
if (Array.isArray(vFor)) {
|
|
1808
|
+
context = vFor.find(vForItem => {
|
|
1809
|
+
return vm.__get_value(propPath, vForItem) === value
|
|
1810
|
+
});
|
|
1811
|
+
} else if (isPlainObject(vFor)) {
|
|
1812
|
+
context = Object.keys(vFor).find(vForKey => {
|
|
1813
|
+
return vm.__get_value(propPath, vFor[vForKey]) === value
|
|
1814
|
+
});
|
|
1815
|
+
} else {
|
|
1816
|
+
console.error('v-for 暂不支持循环数据:', vFor);
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
if (valuePath) {
|
|
1821
|
+
context = vm.__get_value(valuePath, context);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
});
|
|
1825
|
+
return context
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
function processEventExtra (vm, extra, event, __args__) {
|
|
1829
|
+
const extraObj = {};
|
|
1830
|
+
|
|
1831
|
+
if (Array.isArray(extra) && extra.length) {
|
|
1832
|
+
/**
|
|
1833
|
+
*[
|
|
1834
|
+
* ['data.items', 'data.id', item.data.id],
|
|
1835
|
+
* ['metas', 'id', meta.id]
|
|
1836
|
+
*],
|
|
1837
|
+
*[
|
|
1838
|
+
* ['data.items', 'data.id', item.data.id],
|
|
1839
|
+
* ['metas', 'id', meta.id]
|
|
1840
|
+
*],
|
|
1841
|
+
*'test'
|
|
1842
|
+
*/
|
|
1843
|
+
extra.forEach((dataPath, index) => {
|
|
1844
|
+
if (typeof dataPath === 'string') {
|
|
1845
|
+
if (!dataPath) { // model,prop.sync
|
|
1846
|
+
extraObj['$' + index] = vm;
|
|
1847
|
+
} else {
|
|
1848
|
+
if (dataPath === '$event') { // $event
|
|
1849
|
+
extraObj['$' + index] = event;
|
|
1850
|
+
} else if (dataPath === 'arguments') {
|
|
1851
|
+
extraObj['$' + index] = event.detail ? event.detail.__args__ || __args__ : __args__;
|
|
1852
|
+
} else if (dataPath.indexOf('$event.') === 0) { // $event.target.value
|
|
1853
|
+
extraObj['$' + index] = vm.__get_value(dataPath.replace('$event.', ''), event);
|
|
1854
|
+
} else {
|
|
1855
|
+
extraObj['$' + index] = vm.__get_value(dataPath);
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
} else {
|
|
1859
|
+
extraObj['$' + index] = getExtraValue(vm, dataPath);
|
|
1860
|
+
}
|
|
1861
|
+
});
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
return extraObj
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
function getObjByArray (arr) {
|
|
1868
|
+
const obj = {};
|
|
1869
|
+
for (let i = 1; i < arr.length; i++) {
|
|
1870
|
+
const element = arr[i];
|
|
1871
|
+
obj[element[0]] = element[1];
|
|
1872
|
+
}
|
|
1873
|
+
return obj
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
function processEventArgs (vm, event, args = [], extra = [], isCustom, methodName) {
|
|
1877
|
+
let isCustomMPEvent = false; // wxcomponent 组件,传递原始 event 对象
|
|
1878
|
+
|
|
1879
|
+
// fixed 用户直接触发 mpInstance.triggerEvent
|
|
1880
|
+
const __args__ = isPlainObject(event.detail)
|
|
1881
|
+
? event.detail.__args__ || [event.detail]
|
|
1882
|
+
: [event.detail];
|
|
1883
|
+
|
|
1884
|
+
if (isCustom) { // 自定义事件
|
|
1885
|
+
isCustomMPEvent = event.currentTarget &&
|
|
1886
|
+
event.currentTarget.dataset &&
|
|
1887
|
+
event.currentTarget.dataset.comType === 'wx';
|
|
1888
|
+
if (!args.length) { // 无参数,直接传入 event 或 detail 数组
|
|
1889
|
+
if (isCustomMPEvent) {
|
|
1890
|
+
return [event]
|
|
1891
|
+
}
|
|
1892
|
+
return __args__
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
const extraObj = processEventExtra(vm, extra, event, __args__);
|
|
1897
|
+
|
|
1898
|
+
const ret = [];
|
|
1899
|
+
args.forEach(arg => {
|
|
1900
|
+
if (arg === '$event') {
|
|
1901
|
+
if (methodName === '__set_model' && !isCustom) { // input v-model value
|
|
1902
|
+
ret.push(event.target.value);
|
|
1903
|
+
} else {
|
|
1904
|
+
if (isCustom && !isCustomMPEvent) {
|
|
1905
|
+
ret.push(__args__[0]);
|
|
1906
|
+
} else { // wxcomponent 组件或内置组件
|
|
1907
|
+
ret.push(event);
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
} else {
|
|
1911
|
+
if (Array.isArray(arg) && arg[0] === 'o') {
|
|
1912
|
+
ret.push(getObjByArray(arg));
|
|
1913
|
+
} else if (typeof arg === 'string' && hasOwn(extraObj, arg)) {
|
|
1914
|
+
ret.push(extraObj[arg]);
|
|
1915
|
+
} else {
|
|
1916
|
+
ret.push(arg);
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
});
|
|
1920
|
+
|
|
1921
|
+
return ret
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
const ONCE = '~';
|
|
1925
|
+
const CUSTOM = '^';
|
|
1926
|
+
|
|
1927
|
+
function isMatchEventType (eventType, optType) {
|
|
1928
|
+
return (eventType === optType) ||
|
|
1929
|
+
(
|
|
1930
|
+
optType === 'regionchange' &&
|
|
1931
|
+
(
|
|
1932
|
+
eventType === 'begin' ||
|
|
1933
|
+
eventType === 'end'
|
|
1934
|
+
)
|
|
1935
|
+
)
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
function getContextVm (vm) {
|
|
1939
|
+
let $parent = vm.$parent;
|
|
1940
|
+
// 父组件是 scoped slots 或者其他自定义组件时继续查找
|
|
1941
|
+
while ($parent && $parent.$parent && ($parent.$options.generic || $parent.$parent.$options.generic || $parent.$scope._$vuePid)) {
|
|
1942
|
+
$parent = $parent.$parent;
|
|
1943
|
+
}
|
|
1944
|
+
return $parent && $parent.$parent
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
function handleEvent (event) {
|
|
1948
|
+
event = wrapper$1(event);
|
|
1949
|
+
|
|
1950
|
+
// [['tap',[['handle',[1,2,a]],['handle1',[1,2,a]]]]]
|
|
1951
|
+
const dataset = (event.currentTarget || event.target).dataset;
|
|
1952
|
+
if (!dataset) {
|
|
1953
|
+
return console.warn('事件信息不存在')
|
|
1954
|
+
}
|
|
1955
|
+
const eventOpts = dataset.eventOpts || dataset['event-opts']; // 支付宝 web-view 组件 dataset 非驼峰
|
|
1956
|
+
if (!eventOpts) {
|
|
1957
|
+
return console.warn('事件信息不存在')
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
// [['handle',[1,2,a]],['handle1',[1,2,a]]]
|
|
1961
|
+
const eventType = event.type;
|
|
1962
|
+
|
|
1963
|
+
const ret = [];
|
|
1964
|
+
|
|
1965
|
+
eventOpts.forEach(eventOpt => {
|
|
1966
|
+
let type = eventOpt[0];
|
|
1967
|
+
const eventsArray = eventOpt[1];
|
|
1968
|
+
|
|
1969
|
+
const isCustom = type.charAt(0) === CUSTOM;
|
|
1970
|
+
type = isCustom ? type.slice(1) : type;
|
|
1971
|
+
const isOnce = type.charAt(0) === ONCE;
|
|
1972
|
+
type = isOnce ? type.slice(1) : type;
|
|
1973
|
+
|
|
1974
|
+
if (eventsArray && isMatchEventType(eventType, type)) {
|
|
1975
|
+
eventsArray.forEach(eventArray => {
|
|
1976
|
+
const methodName = eventArray[0];
|
|
1977
|
+
if (methodName) {
|
|
1978
|
+
let handlerCtx = this.$vm;
|
|
1979
|
+
if (handlerCtx.$options.generic) { // mp-weixin,mp-toutiao 抽象节点模拟 scoped slots
|
|
1980
|
+
handlerCtx = getContextVm(handlerCtx) || handlerCtx;
|
|
1981
|
+
}
|
|
1982
|
+
if (methodName === '$emit') {
|
|
1983
|
+
handlerCtx.$emit.apply(handlerCtx,
|
|
1984
|
+
processEventArgs(
|
|
1985
|
+
this.$vm,
|
|
1986
|
+
event,
|
|
1987
|
+
eventArray[1],
|
|
1988
|
+
eventArray[2],
|
|
1989
|
+
isCustom,
|
|
1990
|
+
methodName
|
|
1991
|
+
));
|
|
1992
|
+
return
|
|
1993
|
+
}
|
|
1994
|
+
const handler = handlerCtx[methodName];
|
|
1995
|
+
if (!isFn(handler)) {
|
|
1996
|
+
const type = this.$vm.mpType === 'page' ? 'Page' : 'Component';
|
|
1997
|
+
const path = this.route || this.is;
|
|
1998
|
+
throw new Error(`${type} "${path}" does not have a method "${methodName}"`)
|
|
1999
|
+
}
|
|
2000
|
+
if (isOnce) {
|
|
2001
|
+
if (handler.once) {
|
|
2002
|
+
return
|
|
2003
|
+
}
|
|
2004
|
+
handler.once = true;
|
|
2005
|
+
}
|
|
2006
|
+
let params = processEventArgs(
|
|
2007
|
+
this.$vm,
|
|
2008
|
+
event,
|
|
2009
|
+
eventArray[1],
|
|
2010
|
+
eventArray[2],
|
|
2011
|
+
isCustom,
|
|
2012
|
+
methodName
|
|
2013
|
+
);
|
|
2014
|
+
params = Array.isArray(params) ? params : [];
|
|
2015
|
+
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
|
|
2016
|
+
if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
|
|
2017
|
+
// eslint-disable-next-line no-sparse-arrays
|
|
2018
|
+
params = params.concat([, , , , , , , , , , event]);
|
|
2019
|
+
}
|
|
2020
|
+
ret.push(handler.apply(handlerCtx, params));
|
|
2021
|
+
}
|
|
2022
|
+
});
|
|
2023
|
+
}
|
|
2024
|
+
});
|
|
2025
|
+
|
|
2026
|
+
if (
|
|
2027
|
+
eventType === 'input' &&
|
|
2028
|
+
ret.length === 1 &&
|
|
2029
|
+
typeof ret[0] !== 'undefined'
|
|
2030
|
+
) {
|
|
2031
|
+
return ret[0]
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
const hooks = [
|
|
2036
|
+
'onShow',
|
|
2037
|
+
'onHide',
|
|
2038
|
+
'onError',
|
|
2039
|
+
'onPageNotFound',
|
|
2040
|
+
'onThemeChange',
|
|
2041
|
+
'onUnhandledRejection'
|
|
2042
|
+
];
|
|
2043
|
+
|
|
2044
|
+
function initEventChannel$1 () {
|
|
2045
|
+
Vue.prototype.getOpenerEventChannel = function () {
|
|
2046
|
+
if (!this.__eventChannel__) {
|
|
2047
|
+
this.__eventChannel__ = new EventChannel();
|
|
2048
|
+
}
|
|
2049
|
+
return this.__eventChannel__
|
|
2050
|
+
};
|
|
2051
|
+
const callHook = Vue.prototype.__call_hook;
|
|
2052
|
+
Vue.prototype.__call_hook = function (hook, args) {
|
|
2053
|
+
if (hook === 'onLoad' && args && args.__id__) {
|
|
2054
|
+
this.__eventChannel__ = getEventChannel(args.__id__);
|
|
2055
|
+
delete args.__id__;
|
|
2056
|
+
}
|
|
2057
|
+
return callHook.call(this, hook, args)
|
|
2058
|
+
};
|
|
2059
|
+
}
|
|
2060
|
+
|
|
2061
|
+
function parseBaseApp (vm, {
|
|
2062
|
+
mocks,
|
|
2063
|
+
initRefs
|
|
2064
|
+
}) {
|
|
2065
|
+
initEventChannel$1();
|
|
2066
|
+
if (vm.$options.store) {
|
|
2067
|
+
Vue.prototype.$store = vm.$options.store;
|
|
2068
|
+
}
|
|
2069
|
+
uniIdMixin(Vue);
|
|
2070
|
+
|
|
2071
|
+
Vue.prototype.mpHost = "mp-harmony";
|
|
2072
|
+
|
|
2073
|
+
Vue.mixin({
|
|
2074
|
+
beforeCreate () {
|
|
2075
|
+
if (!this.$options.mpType) {
|
|
2076
|
+
return
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
this.mpType = this.$options.mpType;
|
|
2080
|
+
|
|
2081
|
+
this.$mp = {
|
|
2082
|
+
data: {},
|
|
2083
|
+
[this.mpType]: this.$options.mpInstance
|
|
2084
|
+
};
|
|
2085
|
+
|
|
2086
|
+
this.$scope = this.$options.mpInstance;
|
|
2087
|
+
|
|
2088
|
+
delete this.$options.mpType;
|
|
2089
|
+
delete this.$options.mpInstance;
|
|
2090
|
+
if (
|
|
2091
|
+
( this.mpType === 'page') &&
|
|
2092
|
+
typeof getApp === 'function'
|
|
2093
|
+
) { // hack vue-i18n
|
|
2094
|
+
const app = getApp();
|
|
2095
|
+
if (app.$vm && app.$vm.$i18n) {
|
|
2096
|
+
this._i18n = app.$vm.$i18n;
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
if (this.mpType !== 'app') {
|
|
2100
|
+
initRefs(this);
|
|
2101
|
+
initMocks(this, mocks);
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
});
|
|
2105
|
+
|
|
2106
|
+
const appOptions = {
|
|
2107
|
+
onLaunch (args) {
|
|
2108
|
+
if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
|
|
2109
|
+
return
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
this.$vm = vm;
|
|
2113
|
+
|
|
2114
|
+
this.$vm.$mp = {
|
|
2115
|
+
app: this
|
|
2116
|
+
};
|
|
2117
|
+
|
|
2118
|
+
this.$vm.$scope = this;
|
|
2119
|
+
// vm 上也挂载 globalData
|
|
2120
|
+
this.$vm.globalData = this.globalData;
|
|
2121
|
+
|
|
2122
|
+
this.$vm._isMounted = true;
|
|
2123
|
+
this.$vm.__call_hook('mounted', args);
|
|
2124
|
+
|
|
2125
|
+
this.$vm.__call_hook('onLaunch', args);
|
|
2126
|
+
}
|
|
2127
|
+
};
|
|
2128
|
+
|
|
2129
|
+
// 兼容旧版本 globalData
|
|
2130
|
+
appOptions.globalData = vm.$options.globalData || {};
|
|
2131
|
+
// 将 methods 中的方法挂在 getApp() 中
|
|
2132
|
+
const methods = vm.$options.methods;
|
|
2133
|
+
if (methods) {
|
|
2134
|
+
Object.keys(methods).forEach(name => {
|
|
2135
|
+
appOptions[name] = methods[name];
|
|
2136
|
+
});
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
initAppLocale(Vue, vm, normalizeLocale(has.getSystemInfoSync().language) || LOCALE_EN);
|
|
2140
|
+
|
|
2141
|
+
initHooks(appOptions, hooks);
|
|
2142
|
+
initUnknownHooks(appOptions, vm.$options);
|
|
2143
|
+
|
|
2144
|
+
return appOptions
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
const mocks = ['nodeId', 'componentName', '_componentId', 'uniquePrefix'];
|
|
2148
|
+
|
|
2149
|
+
function isPage () {
|
|
2150
|
+
// 百度小程序组件的id,某些情况下可能是number类型的0,不能直接return !this.ownerId 判断当前组件是否是Page
|
|
2151
|
+
// 否则会导致mounted不执行
|
|
2152
|
+
// 基础库 3.290.33 及以上 ownerId 为 null
|
|
2153
|
+
return typeof this.ownerId === 'undefined' || this.ownerId === null
|
|
2154
|
+
}
|
|
2155
|
+
|
|
2156
|
+
const instances = Object.create(null);
|
|
2157
|
+
|
|
2158
|
+
function initRelation ({
|
|
2159
|
+
vuePid,
|
|
2160
|
+
mpInstance
|
|
2161
|
+
}) {
|
|
2162
|
+
// triggerEvent 后,接收事件时机特别晚,已经到了 ready 之后
|
|
2163
|
+
const nodeId = mpInstance.nodeId + '';
|
|
2164
|
+
const webviewId = mpInstance.pageinstance.__pageId__ + '';
|
|
2165
|
+
|
|
2166
|
+
instances[webviewId + '_' + nodeId] = mpInstance.$vm;
|
|
2167
|
+
|
|
2168
|
+
this.triggerEvent('__l', {
|
|
2169
|
+
vuePid,
|
|
2170
|
+
nodeId,
|
|
2171
|
+
webviewId
|
|
2172
|
+
});
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
function handleLink$1 ({
|
|
2176
|
+
detail: {
|
|
2177
|
+
nodeId,
|
|
2178
|
+
webviewId
|
|
2179
|
+
}
|
|
2180
|
+
}) {
|
|
2181
|
+
const vm = instances[webviewId + '_' + nodeId];
|
|
2182
|
+
if (!vm) {
|
|
2183
|
+
return
|
|
2184
|
+
}
|
|
2185
|
+
let parentVm = instances[webviewId + '_' + vm.$scope.ownerId];
|
|
2186
|
+
if (!parentVm) {
|
|
2187
|
+
parentVm = this.$vm;
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
vm.$parent = parentVm;
|
|
2191
|
+
vm.$root = parentVm.$root;
|
|
2192
|
+
parentVm.$children.push(vm);
|
|
2193
|
+
|
|
2194
|
+
const createdVm = function () {
|
|
2195
|
+
vm.__call_hook('created');
|
|
2196
|
+
};
|
|
2197
|
+
const mountedVm = function () {
|
|
2198
|
+
// 处理当前 vm 子
|
|
2199
|
+
if (vm._$childVues) {
|
|
2200
|
+
vm._$childVues.forEach(([createdVm]) => createdVm());
|
|
2201
|
+
vm._$childVues.forEach(([, mountedVm]) => mountedVm());
|
|
2202
|
+
delete vm._$childVues;
|
|
2203
|
+
}
|
|
2204
|
+
vm.__call_hook('beforeMount');
|
|
2205
|
+
vm._isMounted = true;
|
|
2206
|
+
vm.__call_hook('mounted');
|
|
2207
|
+
vm.__call_hook('onReady');
|
|
2208
|
+
};
|
|
2209
|
+
// 当 parentVm 已经 mounted 时,直接触发,否则延迟
|
|
2210
|
+
if (!parentVm || parentVm._isMounted) {
|
|
2211
|
+
createdVm();
|
|
2212
|
+
mountedVm();
|
|
2213
|
+
} else {
|
|
2214
|
+
(parentVm._$childVues || (parentVm._$childVues = [])).push([createdVm, mountedVm]);
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
function parseApp (vm) {
|
|
2219
|
+
Vue.prototype._$fallback = true; // 降级(调整原 vue 的部分生命周期,如 created,beforeMount,inject,provide)
|
|
2220
|
+
|
|
2221
|
+
Vue.mixin({
|
|
2222
|
+
created () { // 处理 injections, triggerEvent 是异步,且触发时机很慢,故延迟 relation 设置
|
|
2223
|
+
if (this.mpType !== 'app') {
|
|
2224
|
+
initRefs(this);
|
|
2225
|
+
this.__init_injections(this);
|
|
2226
|
+
this.__init_provide(this);
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
});
|
|
2230
|
+
|
|
2231
|
+
return parseBaseApp(vm, {
|
|
2232
|
+
mocks,
|
|
2233
|
+
initRefs: function () {} // attached 时,可能查询不到
|
|
2234
|
+
})
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
function createApp (vm) {
|
|
2238
|
+
App(parseApp(vm));
|
|
2239
|
+
return vm
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2242
|
+
const encodeReserveRE = /[!'()*]/g;
|
|
2243
|
+
const encodeReserveReplacer = c => '%' + c.charCodeAt(0).toString(16);
|
|
2244
|
+
const commaRE = /%2C/g;
|
|
2245
|
+
|
|
2246
|
+
// fixed encodeURIComponent which is more conformant to RFC3986:
|
|
2247
|
+
// - escapes [!'()*]
|
|
2248
|
+
// - preserve commas
|
|
2249
|
+
const encode = str => encodeURIComponent(str)
|
|
2250
|
+
.replace(encodeReserveRE, encodeReserveReplacer)
|
|
2251
|
+
.replace(commaRE, ',');
|
|
2252
|
+
|
|
2253
|
+
function stringifyQuery (obj, encodeStr = encode) {
|
|
2254
|
+
const res = obj ? Object.keys(obj).map(key => {
|
|
2255
|
+
const val = obj[key];
|
|
2256
|
+
|
|
2257
|
+
if (val === undefined) {
|
|
2258
|
+
return ''
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
if (val === null) {
|
|
2262
|
+
return encodeStr(key)
|
|
2263
|
+
}
|
|
2264
|
+
|
|
2265
|
+
if (Array.isArray(val)) {
|
|
2266
|
+
const result = [];
|
|
2267
|
+
val.forEach(val2 => {
|
|
2268
|
+
if (val2 === undefined) {
|
|
2269
|
+
return
|
|
2270
|
+
}
|
|
2271
|
+
if (val2 === null) {
|
|
2272
|
+
result.push(encodeStr(key));
|
|
2273
|
+
} else {
|
|
2274
|
+
result.push(encodeStr(key) + '=' + encodeStr(val2));
|
|
2275
|
+
}
|
|
2276
|
+
});
|
|
2277
|
+
return result.join('&')
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
return encodeStr(key) + '=' + encodeStr(val)
|
|
2281
|
+
}).filter(x => x.length > 0).join('&') : null;
|
|
2282
|
+
return res ? `?${res}` : ''
|
|
2283
|
+
}
|
|
2284
|
+
|
|
2285
|
+
function parseBaseComponent (vueComponentOptions, {
|
|
2286
|
+
isPage,
|
|
2287
|
+
initRelation
|
|
2288
|
+
} = {}, needVueOptions) {
|
|
2289
|
+
const [VueComponent, vueOptions] = initVueComponent(Vue, vueComponentOptions);
|
|
2290
|
+
|
|
2291
|
+
const options = {
|
|
2292
|
+
multipleSlots: true,
|
|
2293
|
+
// styleIsolation: 'apply-shared',
|
|
2294
|
+
addGlobalClass: true,
|
|
2295
|
+
...(vueOptions.options || {})
|
|
2296
|
+
};
|
|
2297
|
+
|
|
2298
|
+
const componentOptions = {
|
|
2299
|
+
options,
|
|
2300
|
+
data: initData(vueOptions, Vue.prototype),
|
|
2301
|
+
behaviors: initBehaviors(vueOptions, initBehavior),
|
|
2302
|
+
properties: initProperties(vueOptions.props, false, vueOptions.__file),
|
|
2303
|
+
lifetimes: {
|
|
2304
|
+
attached () {
|
|
2305
|
+
const properties = this.properties;
|
|
2306
|
+
|
|
2307
|
+
const options = {
|
|
2308
|
+
mpType: isPage.call(this) ? 'page' : 'component',
|
|
2309
|
+
mpInstance: this,
|
|
2310
|
+
propsData: properties
|
|
2311
|
+
};
|
|
2312
|
+
|
|
2313
|
+
initVueIds(properties.vueId, this);
|
|
2314
|
+
|
|
2315
|
+
// 处理父子关系
|
|
2316
|
+
initRelation.call(this, {
|
|
2317
|
+
vuePid: this._$vuePid,
|
|
2318
|
+
vueOptions: options
|
|
2319
|
+
});
|
|
2320
|
+
|
|
2321
|
+
// 初始化 vue 实例
|
|
2322
|
+
this.$vm = new VueComponent(options);
|
|
2323
|
+
|
|
2324
|
+
// 处理$slots,$scopedSlots(暂不支持动态变化$slots)
|
|
2325
|
+
initSlots(this.$vm, properties.vueSlots);
|
|
2326
|
+
|
|
2327
|
+
// 触发首次 setData
|
|
2328
|
+
this.$vm.$mount();
|
|
2329
|
+
},
|
|
2330
|
+
ready () {
|
|
2331
|
+
// 当组件 props 默认值为 true,初始化时传入 false 会导致 created,ready 触发, 但 attached 不触发
|
|
2332
|
+
// https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
|
|
2333
|
+
if (this.$vm) {
|
|
2334
|
+
this.$vm._isMounted = true;
|
|
2335
|
+
this.$vm.__call_hook('mounted');
|
|
2336
|
+
this.$vm.__call_hook('onReady');
|
|
2337
|
+
}
|
|
2338
|
+
},
|
|
2339
|
+
detached () {
|
|
2340
|
+
this.$vm && this.$vm.$destroy();
|
|
2341
|
+
}
|
|
2342
|
+
},
|
|
2343
|
+
pageLifetimes: {
|
|
2344
|
+
show (args) {
|
|
2345
|
+
this.$vm && this.$vm.__call_hook('onPageShow', args);
|
|
2346
|
+
},
|
|
2347
|
+
hide () {
|
|
2348
|
+
this.$vm && this.$vm.__call_hook('onPageHide');
|
|
2349
|
+
},
|
|
2350
|
+
resize (size) {
|
|
2351
|
+
this.$vm && this.$vm.__call_hook('onPageResize', size);
|
|
2352
|
+
}
|
|
2353
|
+
},
|
|
2354
|
+
methods: {
|
|
2355
|
+
__l: handleLink,
|
|
2356
|
+
__e: handleEvent
|
|
2357
|
+
}
|
|
2358
|
+
};
|
|
2359
|
+
// externalClasses
|
|
2360
|
+
if (vueOptions.externalClasses) {
|
|
2361
|
+
componentOptions.externalClasses = vueOptions.externalClasses;
|
|
2362
|
+
}
|
|
2363
|
+
|
|
2364
|
+
if (Array.isArray(vueOptions.wxsCallMethods)) {
|
|
2365
|
+
vueOptions.wxsCallMethods.forEach(callMethod => {
|
|
2366
|
+
componentOptions.methods[callMethod] = function (args) {
|
|
2367
|
+
return this.$vm[callMethod](args)
|
|
2368
|
+
};
|
|
2369
|
+
});
|
|
2370
|
+
}
|
|
2371
|
+
|
|
2372
|
+
if (needVueOptions) {
|
|
2373
|
+
return [componentOptions, vueOptions, VueComponent]
|
|
2374
|
+
}
|
|
2375
|
+
if (isPage) {
|
|
2376
|
+
return componentOptions
|
|
2377
|
+
}
|
|
2378
|
+
return [componentOptions, VueComponent]
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
function parseComponent (vueComponentOptions, needVueOptions) {
|
|
2382
|
+
const [componentOptions, vueOptions, VueComponent] = parseBaseComponent(vueComponentOptions, {
|
|
2383
|
+
isPage,
|
|
2384
|
+
initRelation
|
|
2385
|
+
}, true);
|
|
2386
|
+
|
|
2387
|
+
const properties = componentOptions.properties;
|
|
2388
|
+
if (properties) {
|
|
2389
|
+
const observers = {};
|
|
2390
|
+
Object.keys(properties).forEach(name => {
|
|
2391
|
+
const options = properties[name];
|
|
2392
|
+
const observer = options.observer;
|
|
2393
|
+
if (observer) {
|
|
2394
|
+
observers[name] = observer;
|
|
2395
|
+
delete options.observer;
|
|
2396
|
+
}
|
|
2397
|
+
});
|
|
2398
|
+
componentOptions.observers = observers;
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
componentOptions.lifetimes.attached = function attached () {
|
|
2402
|
+
const properties = this.properties;
|
|
2403
|
+
|
|
2404
|
+
const options = {
|
|
2405
|
+
mpType: isPage.call(this) ? 'page' : 'component',
|
|
2406
|
+
mpInstance: this,
|
|
2407
|
+
propsData: properties
|
|
2408
|
+
};
|
|
2409
|
+
|
|
2410
|
+
initVueIds(properties.vueId, this);
|
|
2411
|
+
|
|
2412
|
+
// 初始化 vue 实例
|
|
2413
|
+
this.$vm = new VueComponent(options);
|
|
2414
|
+
|
|
2415
|
+
// 处理$slots,$scopedSlots(暂不支持动态变化$slots)
|
|
2416
|
+
initSlots(this.$vm, properties.vueSlots);
|
|
2417
|
+
|
|
2418
|
+
// 处理父子关系
|
|
2419
|
+
initRelation.call(this, {
|
|
2420
|
+
vuePid: this._$vuePid,
|
|
2421
|
+
mpInstance: this
|
|
2422
|
+
});
|
|
2423
|
+
|
|
2424
|
+
// 触发首次 setData
|
|
2425
|
+
this.$vm.$mount();
|
|
2426
|
+
};
|
|
2427
|
+
|
|
2428
|
+
// ready 比 handleLink 还早,初始化逻辑放到 handleLink 中
|
|
2429
|
+
delete componentOptions.lifetimes.ready;
|
|
2430
|
+
|
|
2431
|
+
componentOptions.methods.__l = handleLink$1;
|
|
2432
|
+
|
|
2433
|
+
return needVueOptions ? [componentOptions, vueOptions] : componentOptions
|
|
2434
|
+
}
|
|
2435
|
+
|
|
2436
|
+
const hooks$1 = [
|
|
2437
|
+
'onShow',
|
|
2438
|
+
'onHide',
|
|
2439
|
+
'onUnload'
|
|
2440
|
+
];
|
|
2441
|
+
|
|
2442
|
+
hooks$1.push(...PAGE_EVENT_HOOKS);
|
|
2443
|
+
|
|
2444
|
+
function parseBasePage (vuePageOptions) {
|
|
2445
|
+
const [pageOptions, vueOptions] = parseComponent(vuePageOptions, true);
|
|
2446
|
+
|
|
2447
|
+
initHooks(pageOptions.methods, hooks$1, vueOptions);
|
|
2448
|
+
|
|
2449
|
+
pageOptions.methods.onLoad = function (query) {
|
|
2450
|
+
this.options = query;
|
|
2451
|
+
const copyQuery = Object.assign({}, query);
|
|
2452
|
+
delete copyQuery.__id__;
|
|
2453
|
+
this.$page = {
|
|
2454
|
+
fullPath: '/' + (this.route || this.is) + stringifyQuery(copyQuery)
|
|
2455
|
+
};
|
|
2456
|
+
this.$vm.$mp.query = query; // 兼容 mpvue
|
|
2457
|
+
this.$vm.__call_hook('onLoad', query);
|
|
2458
|
+
};
|
|
2459
|
+
{
|
|
2460
|
+
initUnknownHooks(pageOptions.methods, vuePageOptions, ['onReady']);
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
return pageOptions
|
|
2464
|
+
}
|
|
2465
|
+
|
|
2466
|
+
function parsePage (vuePageOptions) {
|
|
2467
|
+
const pageOptions = parseBasePage(vuePageOptions);
|
|
2468
|
+
// 页面需要在 ready 中触发,其他组件是在 handleLink 中触发
|
|
2469
|
+
pageOptions.lifetimes.ready = function ready () {
|
|
2470
|
+
if (this.$vm && this.$vm.mpType === 'page') {
|
|
2471
|
+
this.$vm.__call_hook('created');
|
|
2472
|
+
this.$vm.__call_hook('beforeMount');
|
|
2473
|
+
this.$vm._isMounted = true;
|
|
2474
|
+
this.$vm.__call_hook('mounted');
|
|
2475
|
+
this.$vm.__call_hook('onReady');
|
|
2476
|
+
} else {
|
|
2477
|
+
this.is && console.warn(this.is + ' is not ready');
|
|
2478
|
+
}
|
|
2479
|
+
};
|
|
2480
|
+
|
|
2481
|
+
pageOptions.lifetimes.detached = function detached () {
|
|
2482
|
+
this.$vm && this.$vm.$destroy();
|
|
2483
|
+
// 清理
|
|
2484
|
+
const pageId = this.pageinstance.__pageId__;
|
|
2485
|
+
Object.keys(instances).forEach(key => {
|
|
2486
|
+
if (key.indexOf(pageId + '_') === 0) {
|
|
2487
|
+
delete instances[key];
|
|
2488
|
+
}
|
|
2489
|
+
});
|
|
2490
|
+
};
|
|
2491
|
+
|
|
2492
|
+
return pageOptions
|
|
2493
|
+
}
|
|
2494
|
+
|
|
2495
|
+
function createPage (vuePageOptions) {
|
|
2496
|
+
{
|
|
2497
|
+
return Component(parsePage(vuePageOptions))
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
function createComponent (vueOptions) {
|
|
2502
|
+
{
|
|
2503
|
+
return Component(parseComponent(vueOptions))
|
|
2504
|
+
}
|
|
2505
|
+
}
|
|
2506
|
+
|
|
2507
|
+
function createSubpackageApp (vm) {
|
|
2508
|
+
const appOptions = parseApp(vm);
|
|
2509
|
+
const app = getApp({
|
|
2510
|
+
allowDefault: true
|
|
2511
|
+
});
|
|
2512
|
+
vm.$scope = app;
|
|
2513
|
+
const globalData = app.globalData;
|
|
2514
|
+
if (globalData) {
|
|
2515
|
+
Object.keys(appOptions.globalData).forEach(name => {
|
|
2516
|
+
if (!hasOwn(globalData, name)) {
|
|
2517
|
+
globalData[name] = appOptions.globalData[name];
|
|
2518
|
+
}
|
|
2519
|
+
});
|
|
2520
|
+
}
|
|
2521
|
+
Object.keys(appOptions).forEach(name => {
|
|
2522
|
+
if (!hasOwn(app, name)) {
|
|
2523
|
+
app[name] = appOptions[name];
|
|
2524
|
+
}
|
|
2525
|
+
});
|
|
2526
|
+
if (isFn(appOptions.onShow) && has.onAppShow) {
|
|
2527
|
+
has.onAppShow((...args) => {
|
|
2528
|
+
vm.__call_hook('onShow', args);
|
|
2529
|
+
});
|
|
2530
|
+
}
|
|
2531
|
+
if (isFn(appOptions.onHide) && has.onAppHide) {
|
|
2532
|
+
has.onAppHide((...args) => {
|
|
2533
|
+
vm.__call_hook('onHide', args);
|
|
2534
|
+
});
|
|
2535
|
+
}
|
|
2536
|
+
if (isFn(appOptions.onLaunch)) {
|
|
2537
|
+
const args = has.getLaunchOptionsSync && has.getLaunchOptionsSync();
|
|
2538
|
+
vm.__call_hook('onLaunch', args);
|
|
2539
|
+
}
|
|
2540
|
+
return vm
|
|
2541
|
+
}
|
|
2542
|
+
|
|
2543
|
+
function createPlugin (vm) {
|
|
2544
|
+
const appOptions = parseApp(vm);
|
|
2545
|
+
if (isFn(appOptions.onShow) && has.onAppShow) {
|
|
2546
|
+
has.onAppShow((...args) => {
|
|
2547
|
+
vm.__call_hook('onShow', args);
|
|
2548
|
+
});
|
|
2549
|
+
}
|
|
2550
|
+
if (isFn(appOptions.onHide) && has.onAppHide) {
|
|
2551
|
+
has.onAppHide((...args) => {
|
|
2552
|
+
vm.__call_hook('onHide', args);
|
|
2553
|
+
});
|
|
2554
|
+
}
|
|
2555
|
+
if (isFn(appOptions.onLaunch)) {
|
|
2556
|
+
const args = has.getLaunchOptionsSync && has.getLaunchOptionsSync();
|
|
2557
|
+
vm.__call_hook('onLaunch', args);
|
|
2558
|
+
}
|
|
2559
|
+
return vm
|
|
2560
|
+
}
|
|
2561
|
+
|
|
2562
|
+
todos.forEach(todoApi => {
|
|
2563
|
+
protocols[todoApi] = false;
|
|
2564
|
+
});
|
|
2565
|
+
|
|
2566
|
+
canIUses.forEach(canIUseApi => {
|
|
2567
|
+
const apiName = protocols[canIUseApi] && protocols[canIUseApi].name ? protocols[canIUseApi].name
|
|
2568
|
+
: canIUseApi;
|
|
2569
|
+
if (!has.canIUse(apiName)) {
|
|
2570
|
+
protocols[canIUseApi] = false;
|
|
2571
|
+
}
|
|
2572
|
+
});
|
|
2573
|
+
|
|
2574
|
+
let uni = {};
|
|
2575
|
+
|
|
2576
|
+
if (typeof Proxy !== 'undefined' && "mp-harmony" !== 'app-plus') {
|
|
2577
|
+
uni = new Proxy({}, {
|
|
2578
|
+
get (target, name) {
|
|
2579
|
+
if (hasOwn(target, name)) {
|
|
2580
|
+
return target[name]
|
|
2581
|
+
}
|
|
2582
|
+
if (baseApi[name]) {
|
|
2583
|
+
return baseApi[name]
|
|
2584
|
+
}
|
|
2585
|
+
if (api[name]) {
|
|
2586
|
+
return promisify(name, api[name])
|
|
2587
|
+
}
|
|
2588
|
+
{
|
|
2589
|
+
if (extraApi[name]) {
|
|
2590
|
+
return promisify(name, extraApi[name])
|
|
2591
|
+
}
|
|
2592
|
+
if (todoApis[name]) {
|
|
2593
|
+
return promisify(name, todoApis[name])
|
|
2594
|
+
}
|
|
2595
|
+
}
|
|
2596
|
+
if (eventApi[name]) {
|
|
2597
|
+
return eventApi[name]
|
|
2598
|
+
}
|
|
2599
|
+
return promisify(name, wrapper(name, has[name]))
|
|
2600
|
+
},
|
|
2601
|
+
set (target, name, value) {
|
|
2602
|
+
target[name] = value;
|
|
2603
|
+
return true
|
|
2604
|
+
}
|
|
2605
|
+
});
|
|
2606
|
+
} else {
|
|
2607
|
+
Object.keys(baseApi).forEach(name => {
|
|
2608
|
+
uni[name] = baseApi[name];
|
|
2609
|
+
});
|
|
2610
|
+
|
|
2611
|
+
{
|
|
2612
|
+
Object.keys(todoApis).forEach(name => {
|
|
2613
|
+
uni[name] = promisify(name, todoApis[name]);
|
|
2614
|
+
});
|
|
2615
|
+
Object.keys(extraApi).forEach(name => {
|
|
2616
|
+
uni[name] = promisify(name, extraApi[name]);
|
|
2617
|
+
});
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
Object.keys(eventApi).forEach(name => {
|
|
2621
|
+
uni[name] = eventApi[name];
|
|
2622
|
+
});
|
|
2623
|
+
|
|
2624
|
+
Object.keys(api).forEach(name => {
|
|
2625
|
+
uni[name] = promisify(name, api[name]);
|
|
2626
|
+
});
|
|
2627
|
+
|
|
2628
|
+
Object.keys(has).forEach(name => {
|
|
2629
|
+
if (hasOwn(has, name) || hasOwn(protocols, name)) {
|
|
2630
|
+
uni[name] = promisify(name, wrapper(name, has[name]));
|
|
2631
|
+
}
|
|
2632
|
+
});
|
|
2633
|
+
}
|
|
2634
|
+
|
|
2635
|
+
has.createApp = createApp;
|
|
2636
|
+
has.createPage = createPage;
|
|
2637
|
+
has.createComponent = createComponent;
|
|
2638
|
+
has.createSubpackageApp = createSubpackageApp;
|
|
2639
|
+
has.createPlugin = createPlugin;
|
|
2640
|
+
|
|
2641
|
+
var uni$1 = uni;
|
|
2642
|
+
|
|
2643
|
+
export default uni$1;
|
|
2644
|
+
export { createApp, createComponent, createPage, createPlugin, createSubpackageApp };
|