@dcloudio/uni-quickapp-webview 0.0.1-nvue3.3030820220125001
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/build.json +42 -0
- package/dist/uni.api.esm.js +902 -0
- package/dist/uni.compiler.js +128 -0
- package/dist/uni.mp.esm.js +1129 -0
- package/package.json +34 -0
- package/src/api/index.ts +4 -0
- package/src/api/protocols.ts +7 -0
- package/src/api/shims.ts +18 -0
- package/src/compiler/index.ts +21 -0
- package/src/compiler/jsconfig.json +6 -0
- package/src/compiler/options.ts +74 -0
- package/src/compiler/project.config.json +35 -0
- package/src/compiler/utils.ts +59 -0
- package/src/platform/index.ts +3 -0
- package/src/runtime/index.ts +22 -0
- package/src/runtime/parseComponentOptions.ts +91 -0
- package/src/runtime/parsePageOptions.ts +10 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,902 @@
|
|
|
1
|
+
import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isPromise, isFunction, extend } from '@vue/shared';
|
|
2
|
+
import { injectHook } from 'vue';
|
|
3
|
+
|
|
4
|
+
//App
|
|
5
|
+
const ON_LAUNCH = 'onLaunch';
|
|
6
|
+
|
|
7
|
+
const eventChannels = {};
|
|
8
|
+
const eventChannelStack = [];
|
|
9
|
+
let id = 0;
|
|
10
|
+
function initEventChannel(events, cache = true) {
|
|
11
|
+
id++;
|
|
12
|
+
const eventChannel = new qa.EventChannel(id, events);
|
|
13
|
+
if (cache) {
|
|
14
|
+
eventChannels[id] = eventChannel;
|
|
15
|
+
eventChannelStack.push(eventChannel);
|
|
16
|
+
}
|
|
17
|
+
return eventChannel;
|
|
18
|
+
}
|
|
19
|
+
function getEventChannel(id) {
|
|
20
|
+
if (id) {
|
|
21
|
+
const eventChannel = eventChannels[id];
|
|
22
|
+
delete eventChannels[id];
|
|
23
|
+
return eventChannel;
|
|
24
|
+
}
|
|
25
|
+
return eventChannelStack.shift();
|
|
26
|
+
}
|
|
27
|
+
const navigateTo = {
|
|
28
|
+
args(fromArgs) {
|
|
29
|
+
const id = initEventChannel(fromArgs.events).id;
|
|
30
|
+
if (fromArgs.url) {
|
|
31
|
+
fromArgs.url =
|
|
32
|
+
fromArgs.url +
|
|
33
|
+
(fromArgs.url.indexOf('?') === -1 ? '?' : '&') +
|
|
34
|
+
'__id__=' +
|
|
35
|
+
id;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
returnValue(fromRes) {
|
|
39
|
+
fromRes.eventChannel = getEventChannel();
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
qa.appLaunchHooks = [];
|
|
44
|
+
function onAppLaunch(hook) {
|
|
45
|
+
const app = getApp({ allowDefault: true });
|
|
46
|
+
if (app && app.$vm) {
|
|
47
|
+
return injectHook(ON_LAUNCH, hook, app.$vm.$);
|
|
48
|
+
}
|
|
49
|
+
qa.appLaunchHooks.push(hook);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function getBaseSystemInfo() {
|
|
53
|
+
return qa.getSystemInfoSync()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function validateProtocolFail(name, msg) {
|
|
57
|
+
console.warn(`${name}: ${msg}`);
|
|
58
|
+
}
|
|
59
|
+
function validateProtocol(name, data, protocol, onFail) {
|
|
60
|
+
if (!onFail) {
|
|
61
|
+
onFail = validateProtocolFail;
|
|
62
|
+
}
|
|
63
|
+
for (const key in protocol) {
|
|
64
|
+
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
|
|
65
|
+
if (isString(errMsg)) {
|
|
66
|
+
onFail(name, errMsg);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function validateProtocols(name, args, protocol, onFail) {
|
|
71
|
+
if (!protocol) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (!isArray(protocol)) {
|
|
75
|
+
return validateProtocol(name, args[0] || Object.create(null), protocol, onFail);
|
|
76
|
+
}
|
|
77
|
+
const len = protocol.length;
|
|
78
|
+
const argsLen = args.length;
|
|
79
|
+
for (let i = 0; i < len; i++) {
|
|
80
|
+
const opts = protocol[i];
|
|
81
|
+
const data = Object.create(null);
|
|
82
|
+
if (argsLen > i) {
|
|
83
|
+
data[opts.name] = args[i];
|
|
84
|
+
}
|
|
85
|
+
validateProtocol(name, data, { [opts.name]: opts }, onFail);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function validateProp(name, value, prop, isAbsent) {
|
|
89
|
+
if (!isPlainObject(prop)) {
|
|
90
|
+
prop = { type: prop };
|
|
91
|
+
}
|
|
92
|
+
const { type, required, validator } = prop;
|
|
93
|
+
// required!
|
|
94
|
+
if (required && isAbsent) {
|
|
95
|
+
return 'Missing required args: "' + name + '"';
|
|
96
|
+
}
|
|
97
|
+
// missing but optional
|
|
98
|
+
if (value == null && !required) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
// type check
|
|
102
|
+
if (type != null) {
|
|
103
|
+
let isValid = false;
|
|
104
|
+
const types = isArray(type) ? type : [type];
|
|
105
|
+
const expectedTypes = [];
|
|
106
|
+
// value is valid as long as one of the specified types match
|
|
107
|
+
for (let i = 0; i < types.length && !isValid; i++) {
|
|
108
|
+
const { valid, expectedType } = assertType(value, types[i]);
|
|
109
|
+
expectedTypes.push(expectedType || '');
|
|
110
|
+
isValid = valid;
|
|
111
|
+
}
|
|
112
|
+
if (!isValid) {
|
|
113
|
+
return getInvalidTypeMessage(name, value, expectedTypes);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// custom validator
|
|
117
|
+
if (validator) {
|
|
118
|
+
return validator(value);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const isSimpleType = /*#__PURE__*/ makeMap('String,Number,Boolean,Function,Symbol');
|
|
122
|
+
function assertType(value, type) {
|
|
123
|
+
let valid;
|
|
124
|
+
const expectedType = getType(type);
|
|
125
|
+
if (isSimpleType(expectedType)) {
|
|
126
|
+
const t = typeof value;
|
|
127
|
+
valid = t === expectedType.toLowerCase();
|
|
128
|
+
// for primitive wrapper objects
|
|
129
|
+
if (!valid && t === 'object') {
|
|
130
|
+
valid = value instanceof type;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (expectedType === 'Object') {
|
|
134
|
+
valid = isObject(value);
|
|
135
|
+
}
|
|
136
|
+
else if (expectedType === 'Array') {
|
|
137
|
+
valid = isArray(value);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
{
|
|
141
|
+
valid = value instanceof type;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
valid,
|
|
146
|
+
expectedType,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function getInvalidTypeMessage(name, value, expectedTypes) {
|
|
150
|
+
let message = `Invalid args: type check failed for args "${name}".` +
|
|
151
|
+
` Expected ${expectedTypes.map(capitalize).join(', ')}`;
|
|
152
|
+
const expectedType = expectedTypes[0];
|
|
153
|
+
const receivedType = toRawType(value);
|
|
154
|
+
const expectedValue = styleValue(value, expectedType);
|
|
155
|
+
const receivedValue = styleValue(value, receivedType);
|
|
156
|
+
// check if we need to specify expected value
|
|
157
|
+
if (expectedTypes.length === 1 &&
|
|
158
|
+
isExplicable(expectedType) &&
|
|
159
|
+
!isBoolean(expectedType, receivedType)) {
|
|
160
|
+
message += ` with value ${expectedValue}`;
|
|
161
|
+
}
|
|
162
|
+
message += `, got ${receivedType} `;
|
|
163
|
+
// check if we need to specify received value
|
|
164
|
+
if (isExplicable(receivedType)) {
|
|
165
|
+
message += `with value ${receivedValue}.`;
|
|
166
|
+
}
|
|
167
|
+
return message;
|
|
168
|
+
}
|
|
169
|
+
function getType(ctor) {
|
|
170
|
+
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
|
|
171
|
+
return match ? match[1] : '';
|
|
172
|
+
}
|
|
173
|
+
function styleValue(value, type) {
|
|
174
|
+
if (type === 'String') {
|
|
175
|
+
return `"${value}"`;
|
|
176
|
+
}
|
|
177
|
+
else if (type === 'Number') {
|
|
178
|
+
return `${Number(value)}`;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
return `${value}`;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function isExplicable(type) {
|
|
185
|
+
const explicitTypes = ['string', 'number', 'boolean'];
|
|
186
|
+
return explicitTypes.some((elem) => type.toLowerCase() === elem);
|
|
187
|
+
}
|
|
188
|
+
function isBoolean(...args) {
|
|
189
|
+
return args.some((elem) => elem.toLowerCase() === 'boolean');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const HOOK_SUCCESS = 'success';
|
|
193
|
+
const HOOK_FAIL = 'fail';
|
|
194
|
+
const HOOK_COMPLETE = 'complete';
|
|
195
|
+
const globalInterceptors = {};
|
|
196
|
+
const scopedInterceptors = {};
|
|
197
|
+
function wrapperHook(hook) {
|
|
198
|
+
return function (data) {
|
|
199
|
+
return hook(data) || data;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function queue(hooks, data) {
|
|
203
|
+
let promise = false;
|
|
204
|
+
for (let i = 0; i < hooks.length; i++) {
|
|
205
|
+
const hook = hooks[i];
|
|
206
|
+
if (promise) {
|
|
207
|
+
promise = Promise.resolve(wrapperHook(hook));
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
const res = hook(data);
|
|
211
|
+
if (isPromise(res)) {
|
|
212
|
+
promise = Promise.resolve(res);
|
|
213
|
+
}
|
|
214
|
+
if (res === false) {
|
|
215
|
+
return {
|
|
216
|
+
then() { },
|
|
217
|
+
catch() { },
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return (promise || {
|
|
223
|
+
then(callback) {
|
|
224
|
+
return callback(data);
|
|
225
|
+
},
|
|
226
|
+
catch() { },
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
function wrapperOptions(interceptors, options = {}) {
|
|
230
|
+
[HOOK_SUCCESS, HOOK_FAIL, HOOK_COMPLETE].forEach((name) => {
|
|
231
|
+
const hooks = interceptors[name];
|
|
232
|
+
if (!isArray(hooks)) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const oldCallback = options[name];
|
|
236
|
+
options[name] = function callbackInterceptor(res) {
|
|
237
|
+
queue(hooks, res).then((res) => {
|
|
238
|
+
return (isFunction(oldCallback) && oldCallback(res)) || res;
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
});
|
|
242
|
+
return options;
|
|
243
|
+
}
|
|
244
|
+
function wrapperReturnValue(method, returnValue) {
|
|
245
|
+
const returnValueHooks = [];
|
|
246
|
+
if (isArray(globalInterceptors.returnValue)) {
|
|
247
|
+
returnValueHooks.push(...globalInterceptors.returnValue);
|
|
248
|
+
}
|
|
249
|
+
const interceptor = scopedInterceptors[method];
|
|
250
|
+
if (interceptor && isArray(interceptor.returnValue)) {
|
|
251
|
+
returnValueHooks.push(...interceptor.returnValue);
|
|
252
|
+
}
|
|
253
|
+
returnValueHooks.forEach((hook) => {
|
|
254
|
+
returnValue = hook(returnValue) || returnValue;
|
|
255
|
+
});
|
|
256
|
+
return returnValue;
|
|
257
|
+
}
|
|
258
|
+
function getApiInterceptorHooks(method) {
|
|
259
|
+
const interceptor = Object.create(null);
|
|
260
|
+
Object.keys(globalInterceptors).forEach((hook) => {
|
|
261
|
+
if (hook !== 'returnValue') {
|
|
262
|
+
interceptor[hook] = globalInterceptors[hook].slice();
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
const scopedInterceptor = scopedInterceptors[method];
|
|
266
|
+
if (scopedInterceptor) {
|
|
267
|
+
Object.keys(scopedInterceptor).forEach((hook) => {
|
|
268
|
+
if (hook !== 'returnValue') {
|
|
269
|
+
interceptor[hook] = (interceptor[hook] || []).concat(scopedInterceptor[hook]);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return interceptor;
|
|
274
|
+
}
|
|
275
|
+
function invokeApi(method, api, options, params) {
|
|
276
|
+
const interceptor = getApiInterceptorHooks(method);
|
|
277
|
+
if (interceptor && Object.keys(interceptor).length) {
|
|
278
|
+
if (isArray(interceptor.invoke)) {
|
|
279
|
+
const res = queue(interceptor.invoke, options);
|
|
280
|
+
return res.then((options) => {
|
|
281
|
+
return api(wrapperOptions(interceptor, options), ...params);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
return api(wrapperOptions(interceptor, options), ...params);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return api(options, ...params);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function handlePromise(promise) {
|
|
292
|
+
// if (__UNI_FEATURE_PROMISE__) {
|
|
293
|
+
// return promise
|
|
294
|
+
// .then((data) => {
|
|
295
|
+
// return [null, data]
|
|
296
|
+
// })
|
|
297
|
+
// .catch((err) => [err])
|
|
298
|
+
// }
|
|
299
|
+
return promise;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function formatApiArgs(args, options) {
|
|
303
|
+
const params = args[0];
|
|
304
|
+
if (!options ||
|
|
305
|
+
(!isPlainObject(options.formatArgs) && isPlainObject(params))) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const formatArgs = options.formatArgs;
|
|
309
|
+
const keys = Object.keys(formatArgs);
|
|
310
|
+
for (let i = 0; i < keys.length; i++) {
|
|
311
|
+
const name = keys[i];
|
|
312
|
+
const formatterOrDefaultValue = formatArgs[name];
|
|
313
|
+
if (isFunction(formatterOrDefaultValue)) {
|
|
314
|
+
const errMsg = formatterOrDefaultValue(args[0][name], params);
|
|
315
|
+
if (isString(errMsg)) {
|
|
316
|
+
return errMsg;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
// defaultValue
|
|
321
|
+
if (!hasOwn(params, name)) {
|
|
322
|
+
params[name] = formatterOrDefaultValue;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
function beforeInvokeApi(name, args, protocol, options) {
|
|
328
|
+
if ((process.env.NODE_ENV !== 'production')) {
|
|
329
|
+
validateProtocols(name, args, protocol);
|
|
330
|
+
}
|
|
331
|
+
if (options && options.beforeInvoke) {
|
|
332
|
+
const errMsg = options.beforeInvoke(args);
|
|
333
|
+
if (isString(errMsg)) {
|
|
334
|
+
return errMsg;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const errMsg = formatApiArgs(args, options);
|
|
338
|
+
if (errMsg) {
|
|
339
|
+
return errMsg;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function wrapperSyncApi(name, fn, protocol, options) {
|
|
343
|
+
return (...args) => {
|
|
344
|
+
const errMsg = beforeInvokeApi(name, args, protocol, options);
|
|
345
|
+
if (errMsg) {
|
|
346
|
+
throw new Error(errMsg);
|
|
347
|
+
}
|
|
348
|
+
return fn.apply(null, args);
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
function defineSyncApi(name, fn, protocol, options) {
|
|
352
|
+
return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const API_UPX2PX = 'upx2px';
|
|
356
|
+
const Upx2pxProtocol = [
|
|
357
|
+
{
|
|
358
|
+
name: 'upx',
|
|
359
|
+
type: [Number, String],
|
|
360
|
+
required: true,
|
|
361
|
+
},
|
|
362
|
+
];
|
|
363
|
+
|
|
364
|
+
const EPS = 1e-4;
|
|
365
|
+
const BASE_DEVICE_WIDTH = 750;
|
|
366
|
+
let isIOS = false;
|
|
367
|
+
let deviceWidth = 0;
|
|
368
|
+
let deviceDPR = 0;
|
|
369
|
+
function checkDeviceWidth() {
|
|
370
|
+
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo();
|
|
371
|
+
deviceWidth = windowWidth;
|
|
372
|
+
deviceDPR = pixelRatio;
|
|
373
|
+
isIOS = platform === 'ios';
|
|
374
|
+
}
|
|
375
|
+
const upx2px = defineSyncApi(API_UPX2PX, (number, newDeviceWidth) => {
|
|
376
|
+
if (deviceWidth === 0) {
|
|
377
|
+
checkDeviceWidth();
|
|
378
|
+
}
|
|
379
|
+
number = Number(number);
|
|
380
|
+
if (number === 0) {
|
|
381
|
+
return 0;
|
|
382
|
+
}
|
|
383
|
+
let width = newDeviceWidth || deviceWidth;
|
|
384
|
+
let result = (number / BASE_DEVICE_WIDTH) * width;
|
|
385
|
+
if (result < 0) {
|
|
386
|
+
result = -result;
|
|
387
|
+
}
|
|
388
|
+
result = Math.floor(result + EPS);
|
|
389
|
+
if (result === 0) {
|
|
390
|
+
if (deviceDPR === 1 || !isIOS) {
|
|
391
|
+
result = 1;
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
result = 0.5;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return number < 0 ? -result : result;
|
|
398
|
+
}, Upx2pxProtocol);
|
|
399
|
+
|
|
400
|
+
const API_ADD_INTERCEPTOR = 'addInterceptor';
|
|
401
|
+
const API_REMOVE_INTERCEPTOR = 'removeInterceptor';
|
|
402
|
+
const AddInterceptorProtocol = [
|
|
403
|
+
{
|
|
404
|
+
name: 'method',
|
|
405
|
+
type: [String, Object],
|
|
406
|
+
required: true,
|
|
407
|
+
},
|
|
408
|
+
];
|
|
409
|
+
const RemoveInterceptorProtocol = AddInterceptorProtocol;
|
|
410
|
+
|
|
411
|
+
function mergeInterceptorHook(interceptors, interceptor) {
|
|
412
|
+
Object.keys(interceptor).forEach((hook) => {
|
|
413
|
+
if (isFunction(interceptor[hook])) {
|
|
414
|
+
interceptors[hook] = mergeHook(interceptors[hook], interceptor[hook]);
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
function removeInterceptorHook(interceptors, interceptor) {
|
|
419
|
+
if (!interceptors || !interceptor) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
Object.keys(interceptor).forEach((hook) => {
|
|
423
|
+
if (isFunction(interceptor[hook])) {
|
|
424
|
+
removeHook(interceptors[hook], interceptor[hook]);
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
function mergeHook(parentVal, childVal) {
|
|
429
|
+
const res = childVal
|
|
430
|
+
? parentVal
|
|
431
|
+
? parentVal.concat(childVal)
|
|
432
|
+
: isArray(childVal)
|
|
433
|
+
? childVal
|
|
434
|
+
: [childVal]
|
|
435
|
+
: parentVal;
|
|
436
|
+
return res ? dedupeHooks(res) : res;
|
|
437
|
+
}
|
|
438
|
+
function dedupeHooks(hooks) {
|
|
439
|
+
const res = [];
|
|
440
|
+
for (let i = 0; i < hooks.length; i++) {
|
|
441
|
+
if (res.indexOf(hooks[i]) === -1) {
|
|
442
|
+
res.push(hooks[i]);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return res;
|
|
446
|
+
}
|
|
447
|
+
function removeHook(hooks, hook) {
|
|
448
|
+
if (!hooks) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const index = hooks.indexOf(hook);
|
|
452
|
+
if (index !== -1) {
|
|
453
|
+
hooks.splice(index, 1);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
const addInterceptor = defineSyncApi(API_ADD_INTERCEPTOR, (method, interceptor) => {
|
|
457
|
+
if (typeof method === 'string' && isPlainObject(interceptor)) {
|
|
458
|
+
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
|
|
459
|
+
}
|
|
460
|
+
else if (isPlainObject(method)) {
|
|
461
|
+
mergeInterceptorHook(globalInterceptors, method);
|
|
462
|
+
}
|
|
463
|
+
}, AddInterceptorProtocol);
|
|
464
|
+
const removeInterceptor = defineSyncApi(API_REMOVE_INTERCEPTOR, (method, interceptor) => {
|
|
465
|
+
if (typeof method === 'string') {
|
|
466
|
+
if (isPlainObject(interceptor)) {
|
|
467
|
+
removeInterceptorHook(scopedInterceptors[method], interceptor);
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
delete scopedInterceptors[method];
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
else if (isPlainObject(method)) {
|
|
474
|
+
removeInterceptorHook(globalInterceptors, method);
|
|
475
|
+
}
|
|
476
|
+
}, RemoveInterceptorProtocol);
|
|
477
|
+
const interceptors = {};
|
|
478
|
+
|
|
479
|
+
const API_ON = '$on';
|
|
480
|
+
const OnProtocol = [
|
|
481
|
+
{
|
|
482
|
+
name: 'event',
|
|
483
|
+
type: String,
|
|
484
|
+
required: true,
|
|
485
|
+
},
|
|
486
|
+
{
|
|
487
|
+
name: 'callback',
|
|
488
|
+
type: Function,
|
|
489
|
+
required: true,
|
|
490
|
+
},
|
|
491
|
+
];
|
|
492
|
+
const API_ONCE = '$once';
|
|
493
|
+
const OnceProtocol = OnProtocol;
|
|
494
|
+
const API_OFF = '$off';
|
|
495
|
+
const OffProtocol = [
|
|
496
|
+
{
|
|
497
|
+
name: 'event',
|
|
498
|
+
type: [String, Array],
|
|
499
|
+
},
|
|
500
|
+
{
|
|
501
|
+
name: 'callback',
|
|
502
|
+
type: Function,
|
|
503
|
+
},
|
|
504
|
+
];
|
|
505
|
+
const API_EMIT = '$emit';
|
|
506
|
+
const EmitProtocol = [
|
|
507
|
+
{
|
|
508
|
+
name: 'event',
|
|
509
|
+
type: String,
|
|
510
|
+
required: true,
|
|
511
|
+
},
|
|
512
|
+
];
|
|
513
|
+
|
|
514
|
+
const E = function () {
|
|
515
|
+
// Keep this empty so it's easier to inherit from
|
|
516
|
+
// (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
|
|
517
|
+
};
|
|
518
|
+
E.prototype = {
|
|
519
|
+
on: function (name, callback, ctx) {
|
|
520
|
+
var e = this.e || (this.e = {});
|
|
521
|
+
(e[name] || (e[name] = [])).push({
|
|
522
|
+
fn: callback,
|
|
523
|
+
ctx: ctx,
|
|
524
|
+
});
|
|
525
|
+
return this;
|
|
526
|
+
},
|
|
527
|
+
once: function (name, callback, ctx) {
|
|
528
|
+
var self = this;
|
|
529
|
+
function listener() {
|
|
530
|
+
self.off(name, listener);
|
|
531
|
+
callback.apply(ctx, arguments);
|
|
532
|
+
}
|
|
533
|
+
listener._ = callback;
|
|
534
|
+
return this.on(name, listener, ctx);
|
|
535
|
+
},
|
|
536
|
+
emit: function (name) {
|
|
537
|
+
var data = [].slice.call(arguments, 1);
|
|
538
|
+
var evtArr = ((this.e || (this.e = {}))[name] || []).slice();
|
|
539
|
+
var i = 0;
|
|
540
|
+
var len = evtArr.length;
|
|
541
|
+
for (i; i < len; i++) {
|
|
542
|
+
evtArr[i].fn.apply(evtArr[i].ctx, data);
|
|
543
|
+
}
|
|
544
|
+
return this;
|
|
545
|
+
},
|
|
546
|
+
off: function (name, callback) {
|
|
547
|
+
var e = this.e || (this.e = {});
|
|
548
|
+
var evts = e[name];
|
|
549
|
+
var liveEvents = [];
|
|
550
|
+
if (evts && callback) {
|
|
551
|
+
for (var i = 0, len = evts.length; i < len; i++) {
|
|
552
|
+
if (evts[i].fn !== callback && evts[i].fn._ !== callback)
|
|
553
|
+
liveEvents.push(evts[i]);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
// Remove event from queue to prevent memory leak
|
|
557
|
+
// Suggested by https://github.com/lazd
|
|
558
|
+
// Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910
|
|
559
|
+
liveEvents.length ? (e[name] = liveEvents) : delete e[name];
|
|
560
|
+
return this;
|
|
561
|
+
},
|
|
562
|
+
};
|
|
563
|
+
var Emitter = E;
|
|
564
|
+
|
|
565
|
+
const emitter = new Emitter();
|
|
566
|
+
const $on = defineSyncApi(API_ON, (name, callback) => {
|
|
567
|
+
emitter.on(name, callback);
|
|
568
|
+
return () => emitter.off(name, callback);
|
|
569
|
+
}, OnProtocol);
|
|
570
|
+
const $once = defineSyncApi(API_ONCE, (name, callback) => {
|
|
571
|
+
emitter.once(name, callback);
|
|
572
|
+
return () => emitter.off(name, callback);
|
|
573
|
+
}, OnceProtocol);
|
|
574
|
+
const $off = defineSyncApi(API_OFF, (name, callback) => {
|
|
575
|
+
if (!name) {
|
|
576
|
+
emitter.e = {};
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
if (!Array.isArray(name))
|
|
580
|
+
name = [name];
|
|
581
|
+
name.forEach((n) => emitter.off(n, callback));
|
|
582
|
+
}, OffProtocol);
|
|
583
|
+
const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
|
|
584
|
+
emitter.emit(name, ...args);
|
|
585
|
+
}, EmitProtocol);
|
|
586
|
+
|
|
587
|
+
const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
|
|
588
|
+
const CONTEXT_API_RE = /^create|Manager$/;
|
|
589
|
+
// Context例外情况
|
|
590
|
+
const CONTEXT_API_RE_EXC = ['createBLEConnection'];
|
|
591
|
+
// 同步例外情况
|
|
592
|
+
const ASYNC_API = ['createBLEConnection'];
|
|
593
|
+
const CALLBACK_API_RE = /^on|^off/;
|
|
594
|
+
function isContextApi(name) {
|
|
595
|
+
return CONTEXT_API_RE.test(name) && CONTEXT_API_RE_EXC.indexOf(name) === -1;
|
|
596
|
+
}
|
|
597
|
+
function isSyncApi(name) {
|
|
598
|
+
return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1;
|
|
599
|
+
}
|
|
600
|
+
function isCallbackApi(name) {
|
|
601
|
+
return CALLBACK_API_RE.test(name) && name !== 'onPush';
|
|
602
|
+
}
|
|
603
|
+
function shouldPromise(name) {
|
|
604
|
+
if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) {
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
/* eslint-disable no-extend-native */
|
|
610
|
+
if (!Promise.prototype.finally) {
|
|
611
|
+
Promise.prototype.finally = function (onfinally) {
|
|
612
|
+
const promise = this.constructor;
|
|
613
|
+
return this.then((value) => promise.resolve(onfinally && onfinally()).then(() => value), (reason) => promise.resolve(onfinally && onfinally()).then(() => {
|
|
614
|
+
throw reason;
|
|
615
|
+
}));
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
function promisify(name, api) {
|
|
619
|
+
if (!shouldPromise(name)) {
|
|
620
|
+
return api;
|
|
621
|
+
}
|
|
622
|
+
if (!isFunction(api)) {
|
|
623
|
+
return api;
|
|
624
|
+
}
|
|
625
|
+
return function promiseApi(options = {}, ...rest) {
|
|
626
|
+
if (isFunction(options.success) ||
|
|
627
|
+
isFunction(options.fail) ||
|
|
628
|
+
isFunction(options.complete)) {
|
|
629
|
+
return wrapperReturnValue(name, invokeApi(name, api, options, rest));
|
|
630
|
+
}
|
|
631
|
+
return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => {
|
|
632
|
+
invokeApi(name, api, extend({}, options, {
|
|
633
|
+
success: resolve,
|
|
634
|
+
fail: reject,
|
|
635
|
+
}), rest);
|
|
636
|
+
})));
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
const CALLBACKS = ['success', 'fail', 'cancel', 'complete'];
|
|
641
|
+
function initWrapper(protocols) {
|
|
642
|
+
function processCallback(methodName, method, returnValue) {
|
|
643
|
+
return function (res) {
|
|
644
|
+
return method(processReturnValue(methodName, res, returnValue));
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
function processArgs(methodName, fromArgs, argsOption = {}, returnValue = {}, keepFromArgs = false) {
|
|
648
|
+
if (isPlainObject(fromArgs)) {
|
|
649
|
+
// 一般 api 的参数解析
|
|
650
|
+
const toArgs = (keepFromArgs === true ? fromArgs : {}); // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值
|
|
651
|
+
if (isFunction(argsOption)) {
|
|
652
|
+
argsOption = argsOption(fromArgs, toArgs) || {};
|
|
653
|
+
}
|
|
654
|
+
for (const key in fromArgs) {
|
|
655
|
+
if (hasOwn(argsOption, key)) {
|
|
656
|
+
let keyOption = argsOption[key];
|
|
657
|
+
if (isFunction(keyOption)) {
|
|
658
|
+
keyOption = keyOption(fromArgs[key], fromArgs, toArgs);
|
|
659
|
+
}
|
|
660
|
+
if (!keyOption) {
|
|
661
|
+
// 不支持的参数
|
|
662
|
+
console.warn(`快应用(Webview)版 ${methodName} 暂不支持 ${key}`);
|
|
663
|
+
}
|
|
664
|
+
else if (isString(keyOption)) {
|
|
665
|
+
// 重写参数 key
|
|
666
|
+
toArgs[keyOption] = fromArgs[key];
|
|
667
|
+
}
|
|
668
|
+
else if (isPlainObject(keyOption)) {
|
|
669
|
+
// {name:newName,value:value}可重新指定参数 key:value
|
|
670
|
+
toArgs[keyOption.name ? keyOption.name : key] = keyOption.value;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
else if (CALLBACKS.indexOf(key) !== -1) {
|
|
674
|
+
const callback = fromArgs[key];
|
|
675
|
+
if (isFunction(callback)) {
|
|
676
|
+
toArgs[key] = processCallback(methodName, callback, returnValue);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
else {
|
|
680
|
+
if (!keepFromArgs && !hasOwn(toArgs, key)) {
|
|
681
|
+
toArgs[key] = fromArgs[key];
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
return toArgs;
|
|
686
|
+
}
|
|
687
|
+
else if (isFunction(fromArgs)) {
|
|
688
|
+
fromArgs = processCallback(methodName, fromArgs, returnValue);
|
|
689
|
+
}
|
|
690
|
+
return fromArgs;
|
|
691
|
+
}
|
|
692
|
+
function processReturnValue(methodName, res, returnValue, keepReturnValue = false) {
|
|
693
|
+
if (isFunction(protocols.returnValue)) {
|
|
694
|
+
// 处理通用 returnValue
|
|
695
|
+
res = protocols.returnValue(methodName, res);
|
|
696
|
+
}
|
|
697
|
+
return processArgs(methodName, res, returnValue, {}, keepReturnValue);
|
|
698
|
+
}
|
|
699
|
+
return function wrapper(methodName, method) {
|
|
700
|
+
if (!hasOwn(protocols, methodName)) {
|
|
701
|
+
return method;
|
|
702
|
+
}
|
|
703
|
+
const protocol = protocols[methodName];
|
|
704
|
+
if (!protocol) {
|
|
705
|
+
// 暂不支持的 api
|
|
706
|
+
return function () {
|
|
707
|
+
console.error(`快应用(Webview)版 暂不支持${methodName}`);
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
return function (arg1, arg2) {
|
|
711
|
+
// 目前 api 最多两个参数
|
|
712
|
+
let options = protocol;
|
|
713
|
+
if (isFunction(protocol)) {
|
|
714
|
+
options = protocol(arg1);
|
|
715
|
+
}
|
|
716
|
+
arg1 = processArgs(methodName, arg1, options.args, options.returnValue);
|
|
717
|
+
const args = [arg1];
|
|
718
|
+
if (typeof arg2 !== 'undefined') {
|
|
719
|
+
args.push(arg2);
|
|
720
|
+
}
|
|
721
|
+
const returnValue = qa[options.name || methodName].apply(qa, args);
|
|
722
|
+
if (isSyncApi(methodName)) {
|
|
723
|
+
// 同步 api
|
|
724
|
+
return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName));
|
|
725
|
+
}
|
|
726
|
+
return returnValue;
|
|
727
|
+
};
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const getLocale = () => {
|
|
732
|
+
// 优先使用 $locale
|
|
733
|
+
const app = getApp({ allowDefault: true });
|
|
734
|
+
if (app && app.$vm) {
|
|
735
|
+
return app.$vm.$locale;
|
|
736
|
+
}
|
|
737
|
+
return qa.getSystemInfoSync().language || 'zh-Hans';
|
|
738
|
+
};
|
|
739
|
+
const setLocale = (locale) => {
|
|
740
|
+
const app = getApp();
|
|
741
|
+
if (!app) {
|
|
742
|
+
return false;
|
|
743
|
+
}
|
|
744
|
+
const oldLocale = app.$vm.$locale;
|
|
745
|
+
if (oldLocale !== locale) {
|
|
746
|
+
app.$vm.$locale = locale;
|
|
747
|
+
onLocaleChangeCallbacks.forEach((fn) => fn({ locale }));
|
|
748
|
+
return true;
|
|
749
|
+
}
|
|
750
|
+
return false;
|
|
751
|
+
};
|
|
752
|
+
const onLocaleChangeCallbacks = [];
|
|
753
|
+
const onLocaleChange = (fn) => {
|
|
754
|
+
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
|
|
755
|
+
onLocaleChangeCallbacks.push(fn);
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
if (typeof global !== 'undefined') {
|
|
759
|
+
global.getLocale = getLocale;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const baseApis = {
|
|
763
|
+
$on,
|
|
764
|
+
$off,
|
|
765
|
+
$once,
|
|
766
|
+
$emit,
|
|
767
|
+
upx2px,
|
|
768
|
+
interceptors,
|
|
769
|
+
addInterceptor,
|
|
770
|
+
removeInterceptor,
|
|
771
|
+
onAppLaunch,
|
|
772
|
+
getLocale,
|
|
773
|
+
setLocale,
|
|
774
|
+
onLocaleChange,
|
|
775
|
+
};
|
|
776
|
+
function initUni(api, protocols) {
|
|
777
|
+
const wrapper = initWrapper(protocols);
|
|
778
|
+
const UniProxyHandlers = {
|
|
779
|
+
get(target, key) {
|
|
780
|
+
if (hasOwn(target, key)) {
|
|
781
|
+
return target[key];
|
|
782
|
+
}
|
|
783
|
+
if (hasOwn(api, key)) {
|
|
784
|
+
return promisify(key, api[key]);
|
|
785
|
+
}
|
|
786
|
+
if (hasOwn(baseApis, key)) {
|
|
787
|
+
return promisify(key, baseApis[key]);
|
|
788
|
+
}
|
|
789
|
+
// event-api
|
|
790
|
+
// provider-api?
|
|
791
|
+
return promisify(key, wrapper(key, qa[key]));
|
|
792
|
+
},
|
|
793
|
+
};
|
|
794
|
+
return new Proxy({}, UniProxyHandlers);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
function initGetProvider(providers) {
|
|
798
|
+
return function getProvider({ service, success, fail, complete, }) {
|
|
799
|
+
let res;
|
|
800
|
+
if (providers[service]) {
|
|
801
|
+
res = {
|
|
802
|
+
errMsg: 'getProvider:ok',
|
|
803
|
+
service,
|
|
804
|
+
provider: providers[service],
|
|
805
|
+
};
|
|
806
|
+
isFunction(success) && success(res);
|
|
807
|
+
}
|
|
808
|
+
else {
|
|
809
|
+
res = {
|
|
810
|
+
errMsg: 'getProvider:fail:服务[' + service + ']不存在',
|
|
811
|
+
};
|
|
812
|
+
isFunction(fail) && fail(res);
|
|
813
|
+
}
|
|
814
|
+
isFunction(complete) && complete(res);
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
function addSafeAreaInsets(fromRes, toRes) {
|
|
819
|
+
if (fromRes.safeArea) {
|
|
820
|
+
const safeArea = fromRes.safeArea;
|
|
821
|
+
toRes.safeAreaInsets = {
|
|
822
|
+
top: safeArea.top,
|
|
823
|
+
left: safeArea.left,
|
|
824
|
+
right: fromRes.windowWidth - safeArea.right,
|
|
825
|
+
bottom: fromRes.windowHeight - safeArea.bottom,
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const getSystemInfo = {
|
|
831
|
+
returnValue: addSafeAreaInsets,
|
|
832
|
+
};
|
|
833
|
+
|
|
834
|
+
const getSystemInfoSync = getSystemInfo;
|
|
835
|
+
|
|
836
|
+
const redirectTo = {};
|
|
837
|
+
|
|
838
|
+
const previewImage = {
|
|
839
|
+
args(fromArgs, toArgs) {
|
|
840
|
+
let currentIndex = parseInt(fromArgs.current);
|
|
841
|
+
if (isNaN(currentIndex)) {
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
const urls = fromArgs.urls;
|
|
845
|
+
if (!isArray(urls)) {
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
const len = urls.length;
|
|
849
|
+
if (!len) {
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
if (currentIndex < 0) {
|
|
853
|
+
currentIndex = 0;
|
|
854
|
+
}
|
|
855
|
+
else if (currentIndex >= len) {
|
|
856
|
+
currentIndex = len - 1;
|
|
857
|
+
}
|
|
858
|
+
if (currentIndex > 0) {
|
|
859
|
+
toArgs.current = urls[currentIndex];
|
|
860
|
+
toArgs.urls = urls.filter((item, index) => index < currentIndex ? item !== urls[currentIndex] : true);
|
|
861
|
+
}
|
|
862
|
+
else {
|
|
863
|
+
toArgs.current = urls[0];
|
|
864
|
+
}
|
|
865
|
+
return {
|
|
866
|
+
indicator: false,
|
|
867
|
+
loop: false,
|
|
868
|
+
};
|
|
869
|
+
},
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
const providers = {
|
|
873
|
+
oauth: [],
|
|
874
|
+
share: [],
|
|
875
|
+
payment: [],
|
|
876
|
+
push: [],
|
|
877
|
+
};
|
|
878
|
+
if (qa.canIUse('getAccountProvider')) {
|
|
879
|
+
providers.oauth.push(qa.getAccountProvider());
|
|
880
|
+
}
|
|
881
|
+
if (qa.canIUse('getVendorPaymentProvider')) {
|
|
882
|
+
providers.payment.push(qa.getVendorPaymentProvider());
|
|
883
|
+
}
|
|
884
|
+
const getProvider = initGetProvider(providers);
|
|
885
|
+
|
|
886
|
+
var shims = /*#__PURE__*/Object.freeze({
|
|
887
|
+
__proto__: null,
|
|
888
|
+
getProvider: getProvider
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
var protocols = /*#__PURE__*/Object.freeze({
|
|
892
|
+
__proto__: null,
|
|
893
|
+
redirectTo: redirectTo,
|
|
894
|
+
navigateTo: navigateTo,
|
|
895
|
+
previewImage: previewImage,
|
|
896
|
+
getSystemInfo: getSystemInfo,
|
|
897
|
+
getSystemInfoSync: getSystemInfoSync
|
|
898
|
+
});
|
|
899
|
+
|
|
900
|
+
var index = initUni(shims, protocols);
|
|
901
|
+
|
|
902
|
+
export { index as default };
|