@dcloudio/uni-mp-toutiao 3.0.0-alpha-4030320241109001 → 3.0.0-alpha-4030320241117001
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/dist/uni.api.esm.js +1260 -0
- package/dist/uni.compiler.js +11 -10
- package/dist/uni.mp.esm.js +1064 -0
- package/dist-x/uni.api.esm.js +1347 -0
- package/dist-x/uni.mp.esm.js +1780 -0
- package/package.json +8 -7
|
@@ -0,0 +1,1347 @@
|
|
|
1
|
+
import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared';
|
|
2
|
+
import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n';
|
|
3
|
+
import { findUniElement } from 'vue';
|
|
4
|
+
import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared';
|
|
5
|
+
|
|
6
|
+
function validateProtocolFail(name, msg) {
|
|
7
|
+
console.warn(`${name}: ${msg}`);
|
|
8
|
+
}
|
|
9
|
+
function validateProtocol(name, data, protocol, onFail) {
|
|
10
|
+
if (!onFail) {
|
|
11
|
+
onFail = validateProtocolFail;
|
|
12
|
+
}
|
|
13
|
+
for (const key in protocol) {
|
|
14
|
+
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
|
|
15
|
+
if (isString(errMsg)) {
|
|
16
|
+
onFail(name, errMsg);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function validateProtocols(name, args, protocol, onFail) {
|
|
21
|
+
if (!protocol) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!isArray(protocol)) {
|
|
25
|
+
return validateProtocol(name, args[0] || Object.create(null), protocol, onFail);
|
|
26
|
+
}
|
|
27
|
+
const len = protocol.length;
|
|
28
|
+
const argsLen = args.length;
|
|
29
|
+
for (let i = 0; i < len; i++) {
|
|
30
|
+
const opts = protocol[i];
|
|
31
|
+
const data = Object.create(null);
|
|
32
|
+
if (argsLen > i) {
|
|
33
|
+
data[opts.name] = args[i];
|
|
34
|
+
}
|
|
35
|
+
validateProtocol(name, data, { [opts.name]: opts }, onFail);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function validateProp(name, value, prop, isAbsent) {
|
|
39
|
+
if (!isPlainObject(prop)) {
|
|
40
|
+
prop = { type: prop };
|
|
41
|
+
}
|
|
42
|
+
const { type, required, validator } = prop;
|
|
43
|
+
// required!
|
|
44
|
+
if (required && isAbsent) {
|
|
45
|
+
return 'Missing required args: "' + name + '"';
|
|
46
|
+
}
|
|
47
|
+
// missing but optional
|
|
48
|
+
if (value == null && !required) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// type check
|
|
52
|
+
if (type != null) {
|
|
53
|
+
let isValid = false;
|
|
54
|
+
const types = isArray(type) ? type : [type];
|
|
55
|
+
const expectedTypes = [];
|
|
56
|
+
// value is valid as long as one of the specified types match
|
|
57
|
+
for (let i = 0; i < types.length && !isValid; i++) {
|
|
58
|
+
const { valid, expectedType } = assertType(value, types[i]);
|
|
59
|
+
expectedTypes.push(expectedType || '');
|
|
60
|
+
isValid = valid;
|
|
61
|
+
}
|
|
62
|
+
if (!isValid) {
|
|
63
|
+
return getInvalidTypeMessage(name, value, expectedTypes);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// custom validator
|
|
67
|
+
if (validator) {
|
|
68
|
+
return validator(value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const isSimpleType = /*#__PURE__*/ makeMap('String,Number,Boolean,Function,Symbol');
|
|
72
|
+
function assertType(value, type) {
|
|
73
|
+
let valid;
|
|
74
|
+
const expectedType = getType(type);
|
|
75
|
+
if (isSimpleType(expectedType)) {
|
|
76
|
+
const t = typeof value;
|
|
77
|
+
valid = t === expectedType.toLowerCase();
|
|
78
|
+
// for primitive wrapper objects
|
|
79
|
+
if (!valid && t === 'object') {
|
|
80
|
+
valid = value instanceof type;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else if (expectedType === 'Object') {
|
|
84
|
+
valid = isObject(value);
|
|
85
|
+
}
|
|
86
|
+
else if (expectedType === 'Array') {
|
|
87
|
+
valid = isArray(value);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
{
|
|
91
|
+
valid = value instanceof type;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
valid,
|
|
96
|
+
expectedType,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function getInvalidTypeMessage(name, value, expectedTypes) {
|
|
100
|
+
let message = `Invalid args: type check failed for args "${name}".` +
|
|
101
|
+
` Expected ${expectedTypes.map(capitalize).join(', ')}`;
|
|
102
|
+
const expectedType = expectedTypes[0];
|
|
103
|
+
const receivedType = toRawType(value);
|
|
104
|
+
const expectedValue = styleValue(value, expectedType);
|
|
105
|
+
const receivedValue = styleValue(value, receivedType);
|
|
106
|
+
// check if we need to specify expected value
|
|
107
|
+
if (expectedTypes.length === 1 &&
|
|
108
|
+
isExplicable(expectedType) &&
|
|
109
|
+
!isBoolean(expectedType, receivedType)) {
|
|
110
|
+
message += ` with value ${expectedValue}`;
|
|
111
|
+
}
|
|
112
|
+
message += `, got ${receivedType} `;
|
|
113
|
+
// check if we need to specify received value
|
|
114
|
+
if (isExplicable(receivedType)) {
|
|
115
|
+
message += `with value ${receivedValue}.`;
|
|
116
|
+
}
|
|
117
|
+
return message;
|
|
118
|
+
}
|
|
119
|
+
function getType(ctor) {
|
|
120
|
+
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
|
|
121
|
+
return match ? match[1] : '';
|
|
122
|
+
}
|
|
123
|
+
function styleValue(value, type) {
|
|
124
|
+
if (type === 'String') {
|
|
125
|
+
return `"${value}"`;
|
|
126
|
+
}
|
|
127
|
+
else if (type === 'Number') {
|
|
128
|
+
return `${Number(value)}`;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
return `${value}`;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function isExplicable(type) {
|
|
135
|
+
const explicitTypes = ['string', 'number', 'boolean'];
|
|
136
|
+
return explicitTypes.some((elem) => type.toLowerCase() === elem);
|
|
137
|
+
}
|
|
138
|
+
function isBoolean(...args) {
|
|
139
|
+
return args.some((elem) => elem.toLowerCase() === 'boolean');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function tryCatch(fn) {
|
|
143
|
+
return function () {
|
|
144
|
+
try {
|
|
145
|
+
return fn.apply(fn, arguments);
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
// TODO
|
|
149
|
+
console.error(e);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
let invokeCallbackId = 1;
|
|
155
|
+
const invokeCallbacks = {};
|
|
156
|
+
function addInvokeCallback(id, name, callback, keepAlive = false) {
|
|
157
|
+
invokeCallbacks[id] = {
|
|
158
|
+
name,
|
|
159
|
+
keepAlive,
|
|
160
|
+
callback,
|
|
161
|
+
};
|
|
162
|
+
return id;
|
|
163
|
+
}
|
|
164
|
+
// onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数
|
|
165
|
+
function invokeCallback(id, res, extras) {
|
|
166
|
+
if (typeof id === 'number') {
|
|
167
|
+
const opts = invokeCallbacks[id];
|
|
168
|
+
if (opts) {
|
|
169
|
+
if (!opts.keepAlive) {
|
|
170
|
+
delete invokeCallbacks[id];
|
|
171
|
+
}
|
|
172
|
+
return opts.callback(res, extras);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return res;
|
|
176
|
+
}
|
|
177
|
+
const API_SUCCESS = 'success';
|
|
178
|
+
const API_FAIL = 'fail';
|
|
179
|
+
const API_COMPLETE = 'complete';
|
|
180
|
+
function getApiCallbacks(args) {
|
|
181
|
+
const apiCallbacks = {};
|
|
182
|
+
for (const name in args) {
|
|
183
|
+
const fn = args[name];
|
|
184
|
+
if (isFunction(fn)) {
|
|
185
|
+
apiCallbacks[name] = tryCatch(fn);
|
|
186
|
+
delete args[name];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return apiCallbacks;
|
|
190
|
+
}
|
|
191
|
+
function normalizeErrMsg(errMsg, name) {
|
|
192
|
+
if (!errMsg || errMsg.indexOf(':fail') === -1) {
|
|
193
|
+
return name + ':ok';
|
|
194
|
+
}
|
|
195
|
+
return name + errMsg.substring(errMsg.indexOf(':fail'));
|
|
196
|
+
}
|
|
197
|
+
function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) {
|
|
198
|
+
if (!isPlainObject(args)) {
|
|
199
|
+
args = {};
|
|
200
|
+
}
|
|
201
|
+
const { success, fail, complete } = getApiCallbacks(args);
|
|
202
|
+
const hasSuccess = isFunction(success);
|
|
203
|
+
const hasFail = isFunction(fail);
|
|
204
|
+
const hasComplete = isFunction(complete);
|
|
205
|
+
const callbackId = invokeCallbackId++;
|
|
206
|
+
addInvokeCallback(callbackId, name, (res) => {
|
|
207
|
+
res = res || {};
|
|
208
|
+
res.errMsg = normalizeErrMsg(res.errMsg, name);
|
|
209
|
+
isFunction(beforeAll) && beforeAll(res);
|
|
210
|
+
if (res.errMsg === name + ':ok') {
|
|
211
|
+
isFunction(beforeSuccess) && beforeSuccess(res, args);
|
|
212
|
+
hasSuccess && success(res);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
hasFail && fail(res);
|
|
216
|
+
}
|
|
217
|
+
hasComplete && complete(res);
|
|
218
|
+
});
|
|
219
|
+
return callbackId;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const HOOK_SUCCESS = 'success';
|
|
223
|
+
const HOOK_FAIL = 'fail';
|
|
224
|
+
const HOOK_COMPLETE = 'complete';
|
|
225
|
+
const globalInterceptors = {};
|
|
226
|
+
const scopedInterceptors = {};
|
|
227
|
+
function wrapperHook(hook, params) {
|
|
228
|
+
return function (data) {
|
|
229
|
+
return hook(data, params) || data;
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function queue(hooks, data, params) {
|
|
233
|
+
let promise = false;
|
|
234
|
+
for (let i = 0; i < hooks.length; i++) {
|
|
235
|
+
const hook = hooks[i];
|
|
236
|
+
if (promise) {
|
|
237
|
+
promise = Promise.resolve(wrapperHook(hook, params));
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
const res = hook(data, params);
|
|
241
|
+
if (isPromise(res)) {
|
|
242
|
+
promise = Promise.resolve(res);
|
|
243
|
+
}
|
|
244
|
+
if (res === false) {
|
|
245
|
+
return {
|
|
246
|
+
then() { },
|
|
247
|
+
catch() { },
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return (promise || {
|
|
253
|
+
then(callback) {
|
|
254
|
+
return callback(data);
|
|
255
|
+
},
|
|
256
|
+
catch() { },
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
function wrapperOptions(interceptors, options = {}) {
|
|
260
|
+
[HOOK_SUCCESS, HOOK_FAIL, HOOK_COMPLETE].forEach((name) => {
|
|
261
|
+
const hooks = interceptors[name];
|
|
262
|
+
if (!isArray(hooks)) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const oldCallback = options[name];
|
|
266
|
+
options[name] = function callbackInterceptor(res) {
|
|
267
|
+
queue(hooks, res, options).then((res) => {
|
|
268
|
+
return (isFunction(oldCallback) && oldCallback(res)) || res;
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
});
|
|
272
|
+
return options;
|
|
273
|
+
}
|
|
274
|
+
function wrapperReturnValue(method, returnValue) {
|
|
275
|
+
const returnValueHooks = [];
|
|
276
|
+
if (isArray(globalInterceptors.returnValue)) {
|
|
277
|
+
returnValueHooks.push(...globalInterceptors.returnValue);
|
|
278
|
+
}
|
|
279
|
+
const interceptor = scopedInterceptors[method];
|
|
280
|
+
if (interceptor && isArray(interceptor.returnValue)) {
|
|
281
|
+
returnValueHooks.push(...interceptor.returnValue);
|
|
282
|
+
}
|
|
283
|
+
returnValueHooks.forEach((hook) => {
|
|
284
|
+
returnValue = hook(returnValue) || returnValue;
|
|
285
|
+
});
|
|
286
|
+
return returnValue;
|
|
287
|
+
}
|
|
288
|
+
function getApiInterceptorHooks(method) {
|
|
289
|
+
const interceptor = Object.create(null);
|
|
290
|
+
Object.keys(globalInterceptors).forEach((hook) => {
|
|
291
|
+
if (hook !== 'returnValue') {
|
|
292
|
+
interceptor[hook] = globalInterceptors[hook].slice();
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
const scopedInterceptor = scopedInterceptors[method];
|
|
296
|
+
if (scopedInterceptor) {
|
|
297
|
+
Object.keys(scopedInterceptor).forEach((hook) => {
|
|
298
|
+
if (hook !== 'returnValue') {
|
|
299
|
+
interceptor[hook] = (interceptor[hook] || []).concat(scopedInterceptor[hook]);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
return interceptor;
|
|
304
|
+
}
|
|
305
|
+
function invokeApi(method, api, options, params) {
|
|
306
|
+
const interceptor = getApiInterceptorHooks(method);
|
|
307
|
+
if (interceptor && Object.keys(interceptor).length) {
|
|
308
|
+
if (isArray(interceptor.invoke)) {
|
|
309
|
+
const res = queue(interceptor.invoke, options);
|
|
310
|
+
return res.then((options) => {
|
|
311
|
+
// 重新访问 getApiInterceptorHooks, 允许 invoke 中再次调用 addInterceptor,removeInterceptor
|
|
312
|
+
return api(wrapperOptions(getApiInterceptorHooks(method), options), ...params);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
return api(wrapperOptions(interceptor, options), ...params);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return api(options, ...params);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function hasCallback(args) {
|
|
323
|
+
if (isPlainObject(args) &&
|
|
324
|
+
[API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) {
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
function handlePromise(promise) {
|
|
330
|
+
// if (__UNI_FEATURE_PROMISE__) {
|
|
331
|
+
// return promise
|
|
332
|
+
// .then((data) => {
|
|
333
|
+
// return [null, data]
|
|
334
|
+
// })
|
|
335
|
+
// .catch((err) => [err])
|
|
336
|
+
// }
|
|
337
|
+
return promise;
|
|
338
|
+
}
|
|
339
|
+
function promisify$1(name, fn) {
|
|
340
|
+
return (args = {}, ...rest) => {
|
|
341
|
+
if (hasCallback(args)) {
|
|
342
|
+
return wrapperReturnValue(name, invokeApi(name, fn, args, rest));
|
|
343
|
+
}
|
|
344
|
+
return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => {
|
|
345
|
+
invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest);
|
|
346
|
+
})));
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function formatApiArgs(args, options) {
|
|
351
|
+
args[0];
|
|
352
|
+
{
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function invokeSuccess(id, name, res) {
|
|
357
|
+
const result = {
|
|
358
|
+
errMsg: name + ':ok',
|
|
359
|
+
};
|
|
360
|
+
{
|
|
361
|
+
result.errSubject = name;
|
|
362
|
+
}
|
|
363
|
+
return invokeCallback(id, extend((res || {}), result));
|
|
364
|
+
}
|
|
365
|
+
function invokeFail(id, name, errMsg, errRes = {}) {
|
|
366
|
+
const errMsgPrefix = name + ':fail';
|
|
367
|
+
let apiErrMsg = '';
|
|
368
|
+
if (!errMsg) {
|
|
369
|
+
apiErrMsg = errMsgPrefix;
|
|
370
|
+
}
|
|
371
|
+
else if (errMsg.indexOf(errMsgPrefix) === 0) {
|
|
372
|
+
apiErrMsg = errMsg;
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
apiErrMsg = errMsgPrefix + ' ' + errMsg;
|
|
376
|
+
}
|
|
377
|
+
let res = extend({ errMsg: apiErrMsg }, errRes);
|
|
378
|
+
{
|
|
379
|
+
if (typeof UniError !== 'undefined') {
|
|
380
|
+
res =
|
|
381
|
+
typeof errRes.errCode !== 'undefined'
|
|
382
|
+
? new UniError(name, errRes.errCode, apiErrMsg)
|
|
383
|
+
: new UniError(apiErrMsg, errRes);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return invokeCallback(id, res);
|
|
387
|
+
}
|
|
388
|
+
function beforeInvokeApi(name, args, protocol, options) {
|
|
389
|
+
if ((process.env.NODE_ENV !== 'production')) {
|
|
390
|
+
validateProtocols(name, args, protocol);
|
|
391
|
+
}
|
|
392
|
+
const errMsg = formatApiArgs(args);
|
|
393
|
+
if (errMsg) {
|
|
394
|
+
return errMsg;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function parseErrMsg(errMsg) {
|
|
398
|
+
if (!errMsg || isString(errMsg)) {
|
|
399
|
+
return errMsg;
|
|
400
|
+
}
|
|
401
|
+
if (errMsg.stack) {
|
|
402
|
+
return errMsg.message;
|
|
403
|
+
}
|
|
404
|
+
return errMsg;
|
|
405
|
+
}
|
|
406
|
+
function wrapperTaskApi(name, fn, protocol, options) {
|
|
407
|
+
return (args) => {
|
|
408
|
+
const id = createAsyncApiCallback(name, args, options);
|
|
409
|
+
const errMsg = beforeInvokeApi(name, [args], protocol);
|
|
410
|
+
if (errMsg) {
|
|
411
|
+
return invokeFail(id, name, errMsg);
|
|
412
|
+
}
|
|
413
|
+
return fn(args, {
|
|
414
|
+
resolve: (res) => invokeSuccess(id, name, res),
|
|
415
|
+
reject: (errMsg, errRes) => invokeFail(id, name, parseErrMsg(errMsg), errRes),
|
|
416
|
+
});
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
function wrapperSyncApi(name, fn, protocol, options) {
|
|
420
|
+
return (...args) => {
|
|
421
|
+
const errMsg = beforeInvokeApi(name, args, protocol);
|
|
422
|
+
if (errMsg) {
|
|
423
|
+
throw new Error(errMsg);
|
|
424
|
+
}
|
|
425
|
+
return fn.apply(null, args);
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
function wrapperAsyncApi(name, fn, protocol, options) {
|
|
429
|
+
return wrapperTaskApi(name, fn, protocol, options);
|
|
430
|
+
}
|
|
431
|
+
function defineSyncApi(name, fn, protocol, options) {
|
|
432
|
+
return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined);
|
|
433
|
+
}
|
|
434
|
+
function defineAsyncApi(name, fn, protocol, options) {
|
|
435
|
+
return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options));
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// @ts-expect-error
|
|
439
|
+
const API_GET_ELEMENT_BY_ID = 'getElementById';
|
|
440
|
+
const getElementById = defineSyncApi(API_GET_ELEMENT_BY_ID, (id) => {
|
|
441
|
+
const pages = getCurrentPages();
|
|
442
|
+
const page = pages[pages.length - 1];
|
|
443
|
+
if (!page || !page.$vm) {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
return findUniElement(id, page.$vm.$);
|
|
447
|
+
//return page.getElementById(id)
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
const API_CREATE_CANVAS_CONTEXT_ASYNC = 'createCanvasContextAsync';
|
|
451
|
+
class CanvasContext {
|
|
452
|
+
constructor(element) {
|
|
453
|
+
this._element = element;
|
|
454
|
+
}
|
|
455
|
+
getContext(type) {
|
|
456
|
+
return this._element.getContext(type);
|
|
457
|
+
}
|
|
458
|
+
toDataURL(type, encoderOptions) {
|
|
459
|
+
return this._element.toDataURL(type, encoderOptions);
|
|
460
|
+
}
|
|
461
|
+
createImage() {
|
|
462
|
+
return this._element.createImage();
|
|
463
|
+
}
|
|
464
|
+
createImageData() {
|
|
465
|
+
return this._element.createImageData();
|
|
466
|
+
}
|
|
467
|
+
createPath2D() {
|
|
468
|
+
return this._element.createPath2D();
|
|
469
|
+
}
|
|
470
|
+
requestAnimationFrame(callback) {
|
|
471
|
+
return this._element.requestAnimationFrame(callback);
|
|
472
|
+
}
|
|
473
|
+
cancelAnimationFrame(taskId) {
|
|
474
|
+
this._element.cancelAnimationFrame(taskId);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const createCanvasContextAsync = defineAsyncApi(API_CREATE_CANVAS_CONTEXT_ASYNC, (options, { resolve, reject }) => {
|
|
478
|
+
const pages = getCurrentPages();
|
|
479
|
+
const page = pages[pages.length - 1];
|
|
480
|
+
if (!page || !page.$vm) {
|
|
481
|
+
reject('current page invalid.');
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
const query = options.component
|
|
485
|
+
? tt.createSelectorQuery().in(options.component)
|
|
486
|
+
: tt.createSelectorQuery();
|
|
487
|
+
query
|
|
488
|
+
.select('#' + options.id)
|
|
489
|
+
.fields({ node: true }, () => { })
|
|
490
|
+
.exec((res) => {
|
|
491
|
+
if (res.length > 0) {
|
|
492
|
+
const canvas = res[0].node;
|
|
493
|
+
resolve(new CanvasContext(canvas));
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
reject('canvas id invalid.');
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
function getBaseSystemInfo() {
|
|
503
|
+
return tt.getSystemInfoSync();
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const API_UPX2PX = 'upx2px';
|
|
507
|
+
const Upx2pxProtocol = [
|
|
508
|
+
{
|
|
509
|
+
name: 'upx',
|
|
510
|
+
type: [Number, String],
|
|
511
|
+
required: true,
|
|
512
|
+
},
|
|
513
|
+
];
|
|
514
|
+
|
|
515
|
+
const EPS = 1e-4;
|
|
516
|
+
const BASE_DEVICE_WIDTH = 750;
|
|
517
|
+
let isIOS = false;
|
|
518
|
+
let deviceWidth = 0;
|
|
519
|
+
let deviceDPR = 0;
|
|
520
|
+
function checkDeviceWidth() {
|
|
521
|
+
const { platform, pixelRatio, windowWidth } = getBaseSystemInfo();
|
|
522
|
+
deviceWidth = windowWidth;
|
|
523
|
+
deviceDPR = pixelRatio;
|
|
524
|
+
isIOS = platform === 'ios';
|
|
525
|
+
}
|
|
526
|
+
const upx2px = defineSyncApi(API_UPX2PX, (number, newDeviceWidth) => {
|
|
527
|
+
if (deviceWidth === 0) {
|
|
528
|
+
checkDeviceWidth();
|
|
529
|
+
}
|
|
530
|
+
number = Number(number);
|
|
531
|
+
if (number === 0) {
|
|
532
|
+
return 0;
|
|
533
|
+
}
|
|
534
|
+
let width = newDeviceWidth || deviceWidth;
|
|
535
|
+
let result = (number / BASE_DEVICE_WIDTH) * width;
|
|
536
|
+
if (result < 0) {
|
|
537
|
+
result = -result;
|
|
538
|
+
}
|
|
539
|
+
result = Math.floor(result + EPS);
|
|
540
|
+
if (result === 0) {
|
|
541
|
+
if (deviceDPR === 1 || !isIOS) {
|
|
542
|
+
result = 1;
|
|
543
|
+
}
|
|
544
|
+
else {
|
|
545
|
+
result = 0.5;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return number < 0 ? -result : result;
|
|
549
|
+
}, Upx2pxProtocol);
|
|
550
|
+
|
|
551
|
+
const API_ADD_INTERCEPTOR = 'addInterceptor';
|
|
552
|
+
const API_REMOVE_INTERCEPTOR = 'removeInterceptor';
|
|
553
|
+
const AddInterceptorProtocol = [
|
|
554
|
+
{
|
|
555
|
+
name: 'method',
|
|
556
|
+
type: [String, Object],
|
|
557
|
+
required: true,
|
|
558
|
+
},
|
|
559
|
+
];
|
|
560
|
+
const RemoveInterceptorProtocol = AddInterceptorProtocol;
|
|
561
|
+
|
|
562
|
+
function mergeInterceptorHook(interceptors, interceptor) {
|
|
563
|
+
Object.keys(interceptor).forEach((hook) => {
|
|
564
|
+
if (isFunction(interceptor[hook])) {
|
|
565
|
+
interceptors[hook] = mergeHook(interceptors[hook], interceptor[hook]);
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
function removeInterceptorHook(interceptors, interceptor) {
|
|
570
|
+
if (!interceptors || !interceptor) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
Object.keys(interceptor).forEach((name) => {
|
|
574
|
+
const hooks = interceptors[name];
|
|
575
|
+
const hook = interceptor[name];
|
|
576
|
+
if (isArray(hooks) && isFunction(hook)) {
|
|
577
|
+
remove(hooks, hook);
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
function mergeHook(parentVal, childVal) {
|
|
582
|
+
const res = childVal
|
|
583
|
+
? parentVal
|
|
584
|
+
? parentVal.concat(childVal)
|
|
585
|
+
: isArray(childVal)
|
|
586
|
+
? childVal
|
|
587
|
+
: [childVal]
|
|
588
|
+
: parentVal;
|
|
589
|
+
return res ? dedupeHooks(res) : res;
|
|
590
|
+
}
|
|
591
|
+
function dedupeHooks(hooks) {
|
|
592
|
+
const res = [];
|
|
593
|
+
for (let i = 0; i < hooks.length; i++) {
|
|
594
|
+
if (res.indexOf(hooks[i]) === -1) {
|
|
595
|
+
res.push(hooks[i]);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
return res;
|
|
599
|
+
}
|
|
600
|
+
const addInterceptor = defineSyncApi(API_ADD_INTERCEPTOR, (method, interceptor) => {
|
|
601
|
+
if (isString(method) && isPlainObject(interceptor)) {
|
|
602
|
+
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
|
|
603
|
+
}
|
|
604
|
+
else if (isPlainObject(method)) {
|
|
605
|
+
mergeInterceptorHook(globalInterceptors, method);
|
|
606
|
+
}
|
|
607
|
+
}, AddInterceptorProtocol);
|
|
608
|
+
const removeInterceptor = defineSyncApi(API_REMOVE_INTERCEPTOR, (method, interceptor) => {
|
|
609
|
+
if (isString(method)) {
|
|
610
|
+
if (isPlainObject(interceptor)) {
|
|
611
|
+
removeInterceptorHook(scopedInterceptors[method], interceptor);
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
delete scopedInterceptors[method];
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
else if (isPlainObject(method)) {
|
|
618
|
+
removeInterceptorHook(globalInterceptors, method);
|
|
619
|
+
}
|
|
620
|
+
}, RemoveInterceptorProtocol);
|
|
621
|
+
const interceptors = {};
|
|
622
|
+
|
|
623
|
+
const API_ON = '$on';
|
|
624
|
+
const OnProtocol = [
|
|
625
|
+
{
|
|
626
|
+
name: 'event',
|
|
627
|
+
type: String,
|
|
628
|
+
required: true,
|
|
629
|
+
},
|
|
630
|
+
{
|
|
631
|
+
name: 'callback',
|
|
632
|
+
type: Function,
|
|
633
|
+
required: true,
|
|
634
|
+
},
|
|
635
|
+
];
|
|
636
|
+
const API_ONCE = '$once';
|
|
637
|
+
const OnceProtocol = OnProtocol;
|
|
638
|
+
const API_OFF = '$off';
|
|
639
|
+
const OffProtocol = [
|
|
640
|
+
{
|
|
641
|
+
name: 'event',
|
|
642
|
+
type: [String, Array],
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
name: 'callback',
|
|
646
|
+
type: [Function, Number],
|
|
647
|
+
},
|
|
648
|
+
];
|
|
649
|
+
const API_EMIT = '$emit';
|
|
650
|
+
const EmitProtocol = [
|
|
651
|
+
{
|
|
652
|
+
name: 'event',
|
|
653
|
+
type: String,
|
|
654
|
+
required: true,
|
|
655
|
+
},
|
|
656
|
+
];
|
|
657
|
+
|
|
658
|
+
class EventBus {
|
|
659
|
+
constructor() {
|
|
660
|
+
this.$emitter = new Emitter();
|
|
661
|
+
}
|
|
662
|
+
on(name, callback) {
|
|
663
|
+
return this.$emitter.on(name, callback);
|
|
664
|
+
}
|
|
665
|
+
once(name, callback) {
|
|
666
|
+
return this.$emitter.once(name, callback);
|
|
667
|
+
}
|
|
668
|
+
off(name, callback) {
|
|
669
|
+
if (!name) {
|
|
670
|
+
this.$emitter.e = {};
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
this.$emitter.off(name, callback);
|
|
674
|
+
}
|
|
675
|
+
emit(name, ...args) {
|
|
676
|
+
this.$emitter.emit(name, ...args);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
const eventBus = new EventBus();
|
|
680
|
+
const $on = defineSyncApi(API_ON, (name, callback) => {
|
|
681
|
+
const id = eventBus.on(name, callback);
|
|
682
|
+
{
|
|
683
|
+
return id;
|
|
684
|
+
}
|
|
685
|
+
}, OnProtocol);
|
|
686
|
+
const $once = defineSyncApi(API_ONCE, (name, callback) => {
|
|
687
|
+
const id = eventBus.once(name, callback);
|
|
688
|
+
{
|
|
689
|
+
return id;
|
|
690
|
+
}
|
|
691
|
+
}, OnceProtocol);
|
|
692
|
+
const $off = defineSyncApi(API_OFF, (name, callback) => {
|
|
693
|
+
// 类型中不再体现 name 支持 string[] 类型, 仅在 uni.$off 保留该逻辑向下兼容
|
|
694
|
+
if (!isArray(name))
|
|
695
|
+
name = name ? [name] : [];
|
|
696
|
+
name.forEach((n) => eventBus.off(n, callback));
|
|
697
|
+
}, OffProtocol);
|
|
698
|
+
const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
|
|
699
|
+
eventBus.emit(name, ...args);
|
|
700
|
+
}, EmitProtocol);
|
|
701
|
+
|
|
702
|
+
let cid;
|
|
703
|
+
let cidErrMsg;
|
|
704
|
+
let enabled;
|
|
705
|
+
function normalizePushMessage(message) {
|
|
706
|
+
try {
|
|
707
|
+
return JSON.parse(message);
|
|
708
|
+
}
|
|
709
|
+
catch (e) { }
|
|
710
|
+
return message;
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* @private
|
|
714
|
+
* @param args
|
|
715
|
+
*/
|
|
716
|
+
function invokePushCallback(args) {
|
|
717
|
+
if (args.type === 'enabled') {
|
|
718
|
+
enabled = true;
|
|
719
|
+
}
|
|
720
|
+
else if (args.type === 'clientId') {
|
|
721
|
+
cid = args.cid;
|
|
722
|
+
cidErrMsg = args.errMsg;
|
|
723
|
+
invokeGetPushCidCallbacks(cid, args.errMsg);
|
|
724
|
+
}
|
|
725
|
+
else if (args.type === 'pushMsg') {
|
|
726
|
+
const message = {
|
|
727
|
+
type: 'receive',
|
|
728
|
+
data: normalizePushMessage(args.message),
|
|
729
|
+
};
|
|
730
|
+
for (let i = 0; i < onPushMessageCallbacks.length; i++) {
|
|
731
|
+
const callback = onPushMessageCallbacks[i];
|
|
732
|
+
callback(message);
|
|
733
|
+
// 该消息已被阻止
|
|
734
|
+
if (message.stopped) {
|
|
735
|
+
break;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
else if (args.type === 'click') {
|
|
740
|
+
onPushMessageCallbacks.forEach((callback) => {
|
|
741
|
+
callback({
|
|
742
|
+
type: 'click',
|
|
743
|
+
data: normalizePushMessage(args.message),
|
|
744
|
+
});
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
const getPushCidCallbacks = [];
|
|
749
|
+
function invokeGetPushCidCallbacks(cid, errMsg) {
|
|
750
|
+
getPushCidCallbacks.forEach((callback) => {
|
|
751
|
+
callback(cid, errMsg);
|
|
752
|
+
});
|
|
753
|
+
getPushCidCallbacks.length = 0;
|
|
754
|
+
}
|
|
755
|
+
const API_GET_PUSH_CLIENT_ID = 'getPushClientId';
|
|
756
|
+
const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => {
|
|
757
|
+
Promise.resolve().then(() => {
|
|
758
|
+
if (typeof enabled === 'undefined') {
|
|
759
|
+
enabled = false;
|
|
760
|
+
cid = '';
|
|
761
|
+
cidErrMsg = 'uniPush is not enabled';
|
|
762
|
+
}
|
|
763
|
+
getPushCidCallbacks.push((cid, errMsg) => {
|
|
764
|
+
if (cid) {
|
|
765
|
+
resolve({ cid });
|
|
766
|
+
}
|
|
767
|
+
else {
|
|
768
|
+
reject(errMsg);
|
|
769
|
+
}
|
|
770
|
+
});
|
|
771
|
+
if (typeof cid !== 'undefined') {
|
|
772
|
+
invokeGetPushCidCallbacks(cid, cidErrMsg);
|
|
773
|
+
}
|
|
774
|
+
});
|
|
775
|
+
});
|
|
776
|
+
const onPushMessageCallbacks = [];
|
|
777
|
+
// 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现
|
|
778
|
+
const onPushMessage = (fn) => {
|
|
779
|
+
if (onPushMessageCallbacks.indexOf(fn) === -1) {
|
|
780
|
+
onPushMessageCallbacks.push(fn);
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
const offPushMessage = (fn) => {
|
|
784
|
+
if (!fn) {
|
|
785
|
+
onPushMessageCallbacks.length = 0;
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
const index = onPushMessageCallbacks.indexOf(fn);
|
|
789
|
+
if (index > -1) {
|
|
790
|
+
onPushMessageCallbacks.splice(index, 1);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|requireGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getDeviceInfo|getAppBaseInfo|getWindowInfo|getSystemSetting|getAppAuthorizeSetting/;
|
|
796
|
+
const SYNC_API_RE_X = /getElementById/;
|
|
797
|
+
const CONTEXT_API_RE = /^create|Manager$/;
|
|
798
|
+
// Context例外情况
|
|
799
|
+
const CONTEXT_API_RE_EXC = ['createBLEConnection'];
|
|
800
|
+
// 同步例外情况
|
|
801
|
+
const ASYNC_API = ['createBLEConnection'];
|
|
802
|
+
const CALLBACK_API_RE = /^on|^off/;
|
|
803
|
+
function isContextApi(name) {
|
|
804
|
+
return CONTEXT_API_RE.test(name) && CONTEXT_API_RE_EXC.indexOf(name) === -1;
|
|
805
|
+
}
|
|
806
|
+
function isSyncApi(name) {
|
|
807
|
+
if (SYNC_API_RE_X.test(name)) {
|
|
808
|
+
return true;
|
|
809
|
+
}
|
|
810
|
+
return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1;
|
|
811
|
+
}
|
|
812
|
+
function isCallbackApi(name) {
|
|
813
|
+
return CALLBACK_API_RE.test(name) && name !== 'onPush';
|
|
814
|
+
}
|
|
815
|
+
function shouldPromise(name) {
|
|
816
|
+
if (isContextApi(name) || isSyncApi(name) || isCallbackApi(name)) {
|
|
817
|
+
return false;
|
|
818
|
+
}
|
|
819
|
+
return true;
|
|
820
|
+
}
|
|
821
|
+
/* eslint-disable no-extend-native */
|
|
822
|
+
if (!Promise.prototype.finally) {
|
|
823
|
+
Promise.prototype.finally = function (onfinally) {
|
|
824
|
+
const promise = this.constructor;
|
|
825
|
+
return this.then((value) => promise.resolve(onfinally && onfinally()).then(() => value), (reason) => promise.resolve(onfinally && onfinally()).then(() => {
|
|
826
|
+
throw reason;
|
|
827
|
+
}));
|
|
828
|
+
};
|
|
829
|
+
}
|
|
830
|
+
function promisify(name, api) {
|
|
831
|
+
if (!shouldPromise(name)) {
|
|
832
|
+
return api;
|
|
833
|
+
}
|
|
834
|
+
if (!isFunction(api)) {
|
|
835
|
+
return api;
|
|
836
|
+
}
|
|
837
|
+
return function promiseApi(options = {}, ...rest) {
|
|
838
|
+
if (isFunction(options.success) ||
|
|
839
|
+
isFunction(options.fail) ||
|
|
840
|
+
isFunction(options.complete)) {
|
|
841
|
+
return wrapperReturnValue(name, invokeApi(name, api, options, rest));
|
|
842
|
+
}
|
|
843
|
+
return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => {
|
|
844
|
+
invokeApi(name, api, extend({}, options, {
|
|
845
|
+
success: resolve,
|
|
846
|
+
fail: reject,
|
|
847
|
+
}), rest);
|
|
848
|
+
})));
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
const CALLBACKS = ['success', 'fail', 'cancel', 'complete'];
|
|
853
|
+
function initWrapper(protocols) {
|
|
854
|
+
function processCallback(methodName, method, returnValue) {
|
|
855
|
+
return function (res) {
|
|
856
|
+
return method(processReturnValue(methodName, res, returnValue));
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
function processArgs(methodName, fromArgs, argsOption = {}, returnValue = {}, keepFromArgs = false) {
|
|
860
|
+
if (isPlainObject(fromArgs)) {
|
|
861
|
+
// 一般 api 的参数解析
|
|
862
|
+
const toArgs = (keepFromArgs === true ? fromArgs : {}); // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值
|
|
863
|
+
if (isFunction(argsOption)) {
|
|
864
|
+
argsOption = argsOption(fromArgs, toArgs) || {};
|
|
865
|
+
}
|
|
866
|
+
for (const key in fromArgs) {
|
|
867
|
+
if (hasOwn(argsOption, key)) {
|
|
868
|
+
let keyOption = argsOption[key];
|
|
869
|
+
if (isFunction(keyOption)) {
|
|
870
|
+
keyOption = keyOption(fromArgs[key], fromArgs, toArgs);
|
|
871
|
+
}
|
|
872
|
+
if (!keyOption) {
|
|
873
|
+
// 不支持的参数
|
|
874
|
+
console.warn(`字节跳动小程序 ${methodName} 暂不支持 ${key}`);
|
|
875
|
+
}
|
|
876
|
+
else if (isString(keyOption)) {
|
|
877
|
+
// 重写参数 key
|
|
878
|
+
toArgs[keyOption] = fromArgs[key];
|
|
879
|
+
}
|
|
880
|
+
else if (isPlainObject(keyOption)) {
|
|
881
|
+
// {name:newName,value:value}可重新指定参数 key:value
|
|
882
|
+
toArgs[keyOption.name ? keyOption.name : key] = keyOption.value;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
else if (CALLBACKS.indexOf(key) !== -1) {
|
|
886
|
+
const callback = fromArgs[key];
|
|
887
|
+
if (isFunction(callback)) {
|
|
888
|
+
toArgs[key] = processCallback(methodName, callback, returnValue);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
else {
|
|
892
|
+
if (!keepFromArgs && !hasOwn(toArgs, key)) {
|
|
893
|
+
toArgs[key] = fromArgs[key];
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
return toArgs;
|
|
898
|
+
}
|
|
899
|
+
else if (isFunction(fromArgs)) {
|
|
900
|
+
fromArgs = processCallback(methodName, fromArgs, returnValue);
|
|
901
|
+
}
|
|
902
|
+
return fromArgs;
|
|
903
|
+
}
|
|
904
|
+
function processReturnValue(methodName, res, returnValue, keepReturnValue = false) {
|
|
905
|
+
if (isFunction(protocols.returnValue)) {
|
|
906
|
+
// 处理通用 returnValue
|
|
907
|
+
res = protocols.returnValue(methodName, res);
|
|
908
|
+
}
|
|
909
|
+
return processArgs(methodName, res, returnValue, {}, keepReturnValue);
|
|
910
|
+
}
|
|
911
|
+
return function wrapper(methodName, method) {
|
|
912
|
+
if (!hasOwn(protocols, methodName)) {
|
|
913
|
+
return method;
|
|
914
|
+
}
|
|
915
|
+
const protocol = protocols[methodName];
|
|
916
|
+
if (!protocol) {
|
|
917
|
+
// 暂不支持的 api
|
|
918
|
+
return function () {
|
|
919
|
+
console.error(`字节跳动小程序 暂不支持${methodName}`);
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
return function (arg1, arg2) {
|
|
923
|
+
// 目前 api 最多两个参数
|
|
924
|
+
let options = protocol;
|
|
925
|
+
if (isFunction(protocol)) {
|
|
926
|
+
options = protocol(arg1);
|
|
927
|
+
}
|
|
928
|
+
arg1 = processArgs(methodName, arg1, options.args, options.returnValue);
|
|
929
|
+
const args = [arg1];
|
|
930
|
+
if (typeof arg2 !== 'undefined') {
|
|
931
|
+
args.push(arg2);
|
|
932
|
+
}
|
|
933
|
+
const returnValue = tt[options.name || methodName].apply(tt, args);
|
|
934
|
+
if (isSyncApi(methodName)) {
|
|
935
|
+
// 同步 api
|
|
936
|
+
return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName));
|
|
937
|
+
}
|
|
938
|
+
return returnValue;
|
|
939
|
+
};
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
const getLocale = () => {
|
|
944
|
+
// 优先使用 $locale
|
|
945
|
+
const app = isFunction(getApp) && getApp({ allowDefault: true });
|
|
946
|
+
if (app && app.$vm) {
|
|
947
|
+
return app.$vm.$locale;
|
|
948
|
+
}
|
|
949
|
+
return normalizeLocale(tt.getSystemInfoSync().language) || LOCALE_EN;
|
|
950
|
+
};
|
|
951
|
+
const setLocale = (locale) => {
|
|
952
|
+
const app = isFunction(getApp) && getApp();
|
|
953
|
+
if (!app) {
|
|
954
|
+
return false;
|
|
955
|
+
}
|
|
956
|
+
const oldLocale = app.$vm.$locale;
|
|
957
|
+
if (oldLocale !== locale) {
|
|
958
|
+
app.$vm.$locale = locale;
|
|
959
|
+
onLocaleChangeCallbacks.forEach((fn) => fn({ locale }));
|
|
960
|
+
return true;
|
|
961
|
+
}
|
|
962
|
+
return false;
|
|
963
|
+
};
|
|
964
|
+
const onLocaleChangeCallbacks = [];
|
|
965
|
+
const onLocaleChange = (fn) => {
|
|
966
|
+
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
|
|
967
|
+
onLocaleChangeCallbacks.push(fn);
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
if (typeof global !== 'undefined') {
|
|
971
|
+
global.getLocale = getLocale;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
const UUID_KEY = '__DC_STAT_UUID';
|
|
975
|
+
let deviceId;
|
|
976
|
+
function useDeviceId(global = tt) {
|
|
977
|
+
return function addDeviceId(_, toRes) {
|
|
978
|
+
deviceId = deviceId || global.getStorageSync(UUID_KEY);
|
|
979
|
+
if (!deviceId) {
|
|
980
|
+
deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7);
|
|
981
|
+
tt.setStorage({
|
|
982
|
+
key: UUID_KEY,
|
|
983
|
+
data: deviceId,
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
toRes.deviceId = deviceId;
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
function addSafeAreaInsets(fromRes, toRes) {
|
|
990
|
+
if (fromRes.safeArea) {
|
|
991
|
+
const safeArea = fromRes.safeArea;
|
|
992
|
+
toRes.safeAreaInsets = {
|
|
993
|
+
top: safeArea.top,
|
|
994
|
+
left: safeArea.left,
|
|
995
|
+
right: fromRes.windowWidth - safeArea.right,
|
|
996
|
+
bottom: fromRes.screenHeight - safeArea.bottom,
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
function getOSInfo(system, platform) {
|
|
1001
|
+
let osName = '';
|
|
1002
|
+
let osVersion = '';
|
|
1003
|
+
if (platform &&
|
|
1004
|
+
("mp-toutiao" === 'mp-baidu')) {
|
|
1005
|
+
osName = platform;
|
|
1006
|
+
osVersion = system;
|
|
1007
|
+
}
|
|
1008
|
+
else {
|
|
1009
|
+
osName = system.split(' ')[0] || '';
|
|
1010
|
+
osVersion = system.split(' ')[1] || '';
|
|
1011
|
+
}
|
|
1012
|
+
return {
|
|
1013
|
+
osName: osName.toLocaleLowerCase(),
|
|
1014
|
+
osVersion,
|
|
1015
|
+
};
|
|
1016
|
+
}
|
|
1017
|
+
function populateParameters(fromRes, toRes) {
|
|
1018
|
+
const { brand = '', model = '', system = '', language = '', theme, version, platform, fontSizeSetting, SDKVersion, pixelRatio, deviceOrientation, } = fromRes;
|
|
1019
|
+
// const isQuickApp = "mp-toutiao".indexOf('quickapp-webview') !== -1
|
|
1020
|
+
// osName osVersion
|
|
1021
|
+
const { osName, osVersion } = getOSInfo(system, platform);
|
|
1022
|
+
let hostVersion = version;
|
|
1023
|
+
// deviceType
|
|
1024
|
+
let deviceType = getGetDeviceType(fromRes, model);
|
|
1025
|
+
// deviceModel
|
|
1026
|
+
let deviceBrand = getDeviceBrand(brand);
|
|
1027
|
+
// hostName
|
|
1028
|
+
let _hostName = getHostName(fromRes);
|
|
1029
|
+
// deviceOrientation
|
|
1030
|
+
let _deviceOrientation = deviceOrientation; // 仅 微信 百度 支持
|
|
1031
|
+
// devicePixelRatio
|
|
1032
|
+
let _devicePixelRatio = pixelRatio;
|
|
1033
|
+
// SDKVersion
|
|
1034
|
+
let _SDKVersion = SDKVersion;
|
|
1035
|
+
// hostLanguage
|
|
1036
|
+
const hostLanguage = language.replace(/_/g, '-');
|
|
1037
|
+
// wx.getAccountInfoSync
|
|
1038
|
+
const parameters = {
|
|
1039
|
+
appId: process.env.UNI_APP_ID,
|
|
1040
|
+
appName: process.env.UNI_APP_NAME,
|
|
1041
|
+
appVersion: process.env.UNI_APP_VERSION_NAME,
|
|
1042
|
+
appVersionCode: process.env.UNI_APP_VERSION_CODE,
|
|
1043
|
+
appLanguage: getAppLanguage(hostLanguage),
|
|
1044
|
+
uniCompileVersion: process.env.UNI_COMPILER_VERSION,
|
|
1045
|
+
uniCompilerVersion: process.env.UNI_COMPILER_VERSION,
|
|
1046
|
+
uniRuntimeVersion: process.env.UNI_COMPILER_VERSION,
|
|
1047
|
+
uniPlatform: process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM,
|
|
1048
|
+
deviceBrand,
|
|
1049
|
+
deviceModel: model,
|
|
1050
|
+
deviceType,
|
|
1051
|
+
devicePixelRatio: _devicePixelRatio,
|
|
1052
|
+
deviceOrientation: _deviceOrientation,
|
|
1053
|
+
osName,
|
|
1054
|
+
osVersion,
|
|
1055
|
+
hostTheme: theme,
|
|
1056
|
+
hostVersion,
|
|
1057
|
+
hostLanguage,
|
|
1058
|
+
hostName: _hostName,
|
|
1059
|
+
hostSDKVersion: _SDKVersion,
|
|
1060
|
+
hostFontSizeSetting: fontSizeSetting,
|
|
1061
|
+
windowTop: 0,
|
|
1062
|
+
windowBottom: 0,
|
|
1063
|
+
// TODO
|
|
1064
|
+
osLanguage: undefined,
|
|
1065
|
+
osTheme: undefined,
|
|
1066
|
+
ua: undefined,
|
|
1067
|
+
hostPackageName: undefined,
|
|
1068
|
+
browserName: undefined,
|
|
1069
|
+
browserVersion: undefined,
|
|
1070
|
+
isUniAppX: true,
|
|
1071
|
+
};
|
|
1072
|
+
{
|
|
1073
|
+
try {
|
|
1074
|
+
parameters.uniCompileVersionCode = parseFloat(process.env.UNI_COMPILER_VERSION);
|
|
1075
|
+
parameters.uniCompilerVersionCode = parseFloat(process.env.UNI_COMPILER_VERSION);
|
|
1076
|
+
parameters.uniRuntimeVersionCode = parseFloat(process.env.UNI_COMPILER_VERSION);
|
|
1077
|
+
}
|
|
1078
|
+
catch (error) { }
|
|
1079
|
+
}
|
|
1080
|
+
extend(toRes, parameters);
|
|
1081
|
+
}
|
|
1082
|
+
function getGetDeviceType(fromRes, model) {
|
|
1083
|
+
// deviceType
|
|
1084
|
+
let deviceType = fromRes.deviceType || 'phone';
|
|
1085
|
+
{
|
|
1086
|
+
const deviceTypeMaps = {
|
|
1087
|
+
ipad: 'pad',
|
|
1088
|
+
windows: 'pc',
|
|
1089
|
+
mac: 'pc',
|
|
1090
|
+
};
|
|
1091
|
+
const deviceTypeMapsKeys = Object.keys(deviceTypeMaps);
|
|
1092
|
+
const _model = model.toLocaleLowerCase();
|
|
1093
|
+
for (let index = 0; index < deviceTypeMapsKeys.length; index++) {
|
|
1094
|
+
const _m = deviceTypeMapsKeys[index];
|
|
1095
|
+
if (_model.indexOf(_m) !== -1) {
|
|
1096
|
+
deviceType = deviceTypeMaps[_m];
|
|
1097
|
+
break;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
return deviceType;
|
|
1102
|
+
}
|
|
1103
|
+
function getDeviceBrand(brand) {
|
|
1104
|
+
// deviceModel
|
|
1105
|
+
let deviceBrand = brand;
|
|
1106
|
+
if (deviceBrand) {
|
|
1107
|
+
deviceBrand = deviceBrand.toLocaleLowerCase();
|
|
1108
|
+
}
|
|
1109
|
+
return deviceBrand;
|
|
1110
|
+
}
|
|
1111
|
+
function getAppLanguage(defaultLanguage) {
|
|
1112
|
+
return getLocale ? getLocale() : defaultLanguage;
|
|
1113
|
+
}
|
|
1114
|
+
function getHostName(fromRes) {
|
|
1115
|
+
const _platform = "mp-toutiao".split('-')[1];
|
|
1116
|
+
let _hostName = fromRes.hostName || _platform; // mp-jd
|
|
1117
|
+
{
|
|
1118
|
+
_hostName = fromRes.appName;
|
|
1119
|
+
}
|
|
1120
|
+
return _hostName;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
const getSystemInfo = {
|
|
1124
|
+
returnValue: (fromRes, toRes) => {
|
|
1125
|
+
addSafeAreaInsets(fromRes, toRes);
|
|
1126
|
+
useDeviceId()(fromRes, toRes);
|
|
1127
|
+
populateParameters(fromRes, toRes);
|
|
1128
|
+
},
|
|
1129
|
+
};
|
|
1130
|
+
|
|
1131
|
+
const getSystemInfoSync = getSystemInfo;
|
|
1132
|
+
|
|
1133
|
+
const redirectTo = {};
|
|
1134
|
+
|
|
1135
|
+
const previewImage = {
|
|
1136
|
+
args(fromArgs, toArgs) {
|
|
1137
|
+
let currentIndex = parseInt(fromArgs.current);
|
|
1138
|
+
if (isNaN(currentIndex)) {
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
const urls = fromArgs.urls;
|
|
1142
|
+
if (!isArray(urls)) {
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
const len = urls.length;
|
|
1146
|
+
if (!len) {
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
if (currentIndex < 0) {
|
|
1150
|
+
currentIndex = 0;
|
|
1151
|
+
}
|
|
1152
|
+
else if (currentIndex >= len) {
|
|
1153
|
+
currentIndex = len - 1;
|
|
1154
|
+
}
|
|
1155
|
+
if (currentIndex > 0) {
|
|
1156
|
+
toArgs.current = urls[currentIndex];
|
|
1157
|
+
toArgs.urls = urls.filter((item, index) => index < currentIndex ? item !== urls[currentIndex] : true);
|
|
1158
|
+
}
|
|
1159
|
+
else {
|
|
1160
|
+
toArgs.current = urls[0];
|
|
1161
|
+
}
|
|
1162
|
+
return {
|
|
1163
|
+
indicator: false,
|
|
1164
|
+
loop: false,
|
|
1165
|
+
};
|
|
1166
|
+
},
|
|
1167
|
+
};
|
|
1168
|
+
|
|
1169
|
+
const eventChannels = {};
|
|
1170
|
+
let id = 0;
|
|
1171
|
+
function initEventChannel(events, cache = true) {
|
|
1172
|
+
id++;
|
|
1173
|
+
const eventChannel = new tt.EventChannel(id, events);
|
|
1174
|
+
if (cache) {
|
|
1175
|
+
eventChannels[id] = eventChannel;
|
|
1176
|
+
}
|
|
1177
|
+
return eventChannel;
|
|
1178
|
+
}
|
|
1179
|
+
function getEventChannel(id) {
|
|
1180
|
+
const eventChannel = eventChannels[id];
|
|
1181
|
+
delete eventChannels[id];
|
|
1182
|
+
return eventChannel;
|
|
1183
|
+
}
|
|
1184
|
+
const navigateTo$1 = () => {
|
|
1185
|
+
let eventChannel;
|
|
1186
|
+
return {
|
|
1187
|
+
args(fromArgs) {
|
|
1188
|
+
eventChannel = initEventChannel(fromArgs.events);
|
|
1189
|
+
if (fromArgs.url) {
|
|
1190
|
+
fromArgs.url =
|
|
1191
|
+
fromArgs.url +
|
|
1192
|
+
(fromArgs.url.indexOf('?') === -1 ? '?' : '&') +
|
|
1193
|
+
'__id__=' +
|
|
1194
|
+
eventChannel.id;
|
|
1195
|
+
}
|
|
1196
|
+
},
|
|
1197
|
+
returnValue(fromRes) {
|
|
1198
|
+
fromRes.eventChannel = eventChannel;
|
|
1199
|
+
},
|
|
1200
|
+
};
|
|
1201
|
+
};
|
|
1202
|
+
|
|
1203
|
+
const baseApis = {
|
|
1204
|
+
$on,
|
|
1205
|
+
$off,
|
|
1206
|
+
$once,
|
|
1207
|
+
$emit,
|
|
1208
|
+
upx2px,
|
|
1209
|
+
interceptors,
|
|
1210
|
+
addInterceptor,
|
|
1211
|
+
removeInterceptor,
|
|
1212
|
+
onCreateVueApp,
|
|
1213
|
+
invokeCreateVueAppHook,
|
|
1214
|
+
getLocale,
|
|
1215
|
+
setLocale,
|
|
1216
|
+
onLocaleChange,
|
|
1217
|
+
getPushClientId,
|
|
1218
|
+
onPushMessage,
|
|
1219
|
+
offPushMessage,
|
|
1220
|
+
invokePushCallback,
|
|
1221
|
+
getElementById,
|
|
1222
|
+
createCanvasContextAsync,
|
|
1223
|
+
};
|
|
1224
|
+
function initUni(api, protocols, platform = tt) {
|
|
1225
|
+
const wrapper = initWrapper(protocols);
|
|
1226
|
+
const UniProxyHandlers = {
|
|
1227
|
+
get(target, key) {
|
|
1228
|
+
if (hasOwn(target, key)) {
|
|
1229
|
+
return target[key];
|
|
1230
|
+
}
|
|
1231
|
+
if (hasOwn(api, key)) {
|
|
1232
|
+
return promisify(key, api[key]);
|
|
1233
|
+
}
|
|
1234
|
+
if (hasOwn(baseApis, key)) {
|
|
1235
|
+
return promisify(key, baseApis[key]);
|
|
1236
|
+
}
|
|
1237
|
+
// event-api
|
|
1238
|
+
// provider-api?
|
|
1239
|
+
return promisify(key, wrapper(key, platform[key]));
|
|
1240
|
+
},
|
|
1241
|
+
};
|
|
1242
|
+
// 处理 api mp 打包后为不同js,getEventChannel 无法共享问题
|
|
1243
|
+
{
|
|
1244
|
+
platform.getEventChannel = getEventChannel;
|
|
1245
|
+
}
|
|
1246
|
+
return new Proxy({}, UniProxyHandlers);
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
function initGetProvider(providers) {
|
|
1250
|
+
return function getProvider({ service, success, fail, complete, }) {
|
|
1251
|
+
let res;
|
|
1252
|
+
if (providers[service]) {
|
|
1253
|
+
res = {
|
|
1254
|
+
errMsg: 'getProvider:ok',
|
|
1255
|
+
service,
|
|
1256
|
+
provider: providers[service],
|
|
1257
|
+
};
|
|
1258
|
+
isFunction(success) && success(res);
|
|
1259
|
+
}
|
|
1260
|
+
else {
|
|
1261
|
+
res = {
|
|
1262
|
+
errMsg: 'getProvider:fail:服务[' + service + ']不存在',
|
|
1263
|
+
};
|
|
1264
|
+
isFunction(fail) && fail(res);
|
|
1265
|
+
}
|
|
1266
|
+
isFunction(complete) && complete(res);
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
const getProvider = initGetProvider({
|
|
1271
|
+
oauth: ['toutiao'],
|
|
1272
|
+
share: ['toutiao'],
|
|
1273
|
+
payment: ['toutiao'],
|
|
1274
|
+
push: ['toutiao'],
|
|
1275
|
+
});
|
|
1276
|
+
|
|
1277
|
+
var shims = /*#__PURE__*/Object.freeze({
|
|
1278
|
+
__proto__: null,
|
|
1279
|
+
getProvider: getProvider
|
|
1280
|
+
});
|
|
1281
|
+
|
|
1282
|
+
const navigateTo = navigateTo$1();
|
|
1283
|
+
const connectSocket = {
|
|
1284
|
+
args: {
|
|
1285
|
+
method: false,
|
|
1286
|
+
},
|
|
1287
|
+
};
|
|
1288
|
+
const chooseVideo = {
|
|
1289
|
+
args: {
|
|
1290
|
+
camera: false,
|
|
1291
|
+
},
|
|
1292
|
+
};
|
|
1293
|
+
const scanCode = {
|
|
1294
|
+
args: {
|
|
1295
|
+
onlyFromCamera: false,
|
|
1296
|
+
scanType: false,
|
|
1297
|
+
},
|
|
1298
|
+
};
|
|
1299
|
+
const startAccelerometer = {
|
|
1300
|
+
args: {
|
|
1301
|
+
interval: false,
|
|
1302
|
+
},
|
|
1303
|
+
};
|
|
1304
|
+
const login = {
|
|
1305
|
+
args: {
|
|
1306
|
+
scopes: false,
|
|
1307
|
+
timeout: false,
|
|
1308
|
+
},
|
|
1309
|
+
};
|
|
1310
|
+
const getUserInfo = {
|
|
1311
|
+
args: {
|
|
1312
|
+
lang: false,
|
|
1313
|
+
timeout: false,
|
|
1314
|
+
},
|
|
1315
|
+
};
|
|
1316
|
+
const requestPayment = {
|
|
1317
|
+
name: tt.pay ? 'pay' : 'requestPayment',
|
|
1318
|
+
args: {
|
|
1319
|
+
orderInfo: tt.pay ? 'orderInfo' : 'data',
|
|
1320
|
+
},
|
|
1321
|
+
};
|
|
1322
|
+
const getFileInfo = {
|
|
1323
|
+
args: {
|
|
1324
|
+
digestAlgorithm: false,
|
|
1325
|
+
},
|
|
1326
|
+
};
|
|
1327
|
+
|
|
1328
|
+
var protocols = /*#__PURE__*/Object.freeze({
|
|
1329
|
+
__proto__: null,
|
|
1330
|
+
chooseVideo: chooseVideo,
|
|
1331
|
+
connectSocket: connectSocket,
|
|
1332
|
+
getFileInfo: getFileInfo,
|
|
1333
|
+
getSystemInfo: getSystemInfo,
|
|
1334
|
+
getSystemInfoSync: getSystemInfoSync,
|
|
1335
|
+
getUserInfo: getUserInfo,
|
|
1336
|
+
login: login,
|
|
1337
|
+
navigateTo: navigateTo,
|
|
1338
|
+
previewImage: previewImage,
|
|
1339
|
+
redirectTo: redirectTo,
|
|
1340
|
+
requestPayment: requestPayment,
|
|
1341
|
+
scanCode: scanCode,
|
|
1342
|
+
startAccelerometer: startAccelerometer
|
|
1343
|
+
});
|
|
1344
|
+
|
|
1345
|
+
var index = initUni(shims, protocols);
|
|
1346
|
+
|
|
1347
|
+
export { index as default };
|