@dcloudio/uni-mp-toutiao 3.0.0-alpha-3050020220622001 → 3.0.0-alpha-3050020220623001

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.
Files changed (2) hide show
  1. package/dist/uni.api.esm.js +133 -24
  2. package/package.json +6 -6
@@ -1,6 +1,6 @@
1
- import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, remove, extend } from '@vue/shared';
1
+ import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise, extend, remove } from '@vue/shared';
2
2
  import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n';
3
- import { Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared';
3
+ import { LINEFEED, Emitter, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared';
4
4
 
5
5
  const eventChannels = {};
6
6
  const eventChannelStack = [];
@@ -190,6 +190,32 @@ function tryCatch(fn) {
190
190
  };
191
191
  }
192
192
 
193
+ let invokeCallbackId = 1;
194
+ const invokeCallbacks = {};
195
+ function addInvokeCallback(id, name, callback, keepAlive = false) {
196
+ invokeCallbacks[id] = {
197
+ name,
198
+ keepAlive,
199
+ callback,
200
+ };
201
+ return id;
202
+ }
203
+ // onNativeEventReceive((event,data)=>{}) 需要两个参数,目前写死最多两个参数
204
+ function invokeCallback(id, res, extras) {
205
+ if (typeof id === 'number') {
206
+ const opts = invokeCallbacks[id];
207
+ if (opts) {
208
+ if (!opts.keepAlive) {
209
+ delete invokeCallbacks[id];
210
+ }
211
+ return opts.callback(res, extras);
212
+ }
213
+ }
214
+ return res;
215
+ }
216
+ const API_SUCCESS = 'success';
217
+ const API_FAIL = 'fail';
218
+ const API_COMPLETE = 'complete';
193
219
  function getApiCallbacks(args) {
194
220
  const apiCallbacks = {};
195
221
  for (const name in args) {
@@ -200,6 +226,36 @@ function getApiCallbacks(args) {
200
226
  }
201
227
  }
202
228
  return apiCallbacks;
229
+ }
230
+ function normalizeErrMsg$1(errMsg, name) {
231
+ if (!errMsg || errMsg.indexOf(':fail') === -1) {
232
+ return name + ':ok';
233
+ }
234
+ return name + errMsg.substring(errMsg.indexOf(':fail'));
235
+ }
236
+ function createAsyncApiCallback(name, args = {}, { beforeAll, beforeSuccess } = {}) {
237
+ if (!isPlainObject(args)) {
238
+ args = {};
239
+ }
240
+ const { success, fail, complete } = getApiCallbacks(args);
241
+ const hasSuccess = isFunction(success);
242
+ const hasFail = isFunction(fail);
243
+ const hasComplete = isFunction(complete);
244
+ const callbackId = invokeCallbackId++;
245
+ addInvokeCallback(callbackId, name, (res) => {
246
+ res = res || {};
247
+ res.errMsg = normalizeErrMsg$1(res.errMsg, name);
248
+ isFunction(beforeAll) && beforeAll(res);
249
+ if (res.errMsg === name + ':ok') {
250
+ isFunction(beforeSuccess) && beforeSuccess(res, args);
251
+ hasSuccess && success(res);
252
+ }
253
+ else {
254
+ hasFail && fail(res);
255
+ }
256
+ hasComplete && complete(res);
257
+ });
258
+ return callbackId;
203
259
  }
204
260
 
205
261
  const HOOK_SUCCESS = 'success';
@@ -301,6 +357,13 @@ function invokeApi(method, api, options, params) {
301
357
  return api(options, ...params);
302
358
  }
303
359
 
360
+ function hasCallback(args) {
361
+ if (isPlainObject(args) &&
362
+ [API_SUCCESS, API_FAIL, API_COMPLETE].find((cb) => isFunction(args[cb]))) {
363
+ return true;
364
+ }
365
+ return false;
366
+ }
304
367
  function handlePromise(promise) {
305
368
  // if (__UNI_FEATURE_PROMISE__) {
306
369
  // return promise
@@ -310,6 +373,16 @@ function handlePromise(promise) {
310
373
  // .catch((err) => [err])
311
374
  // }
312
375
  return promise;
376
+ }
377
+ function promisify$1(name, fn) {
378
+ return (args = {}, ...rest) => {
379
+ if (hasCallback(args)) {
380
+ return wrapperReturnValue(name, invokeApi(name, fn, args, rest));
381
+ }
382
+ return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => {
383
+ invokeApi(name, fn, extend(args, { success: resolve, fail: reject }), rest);
384
+ })));
385
+ };
313
386
  }
314
387
 
315
388
  function formatApiArgs(args, options) {
@@ -337,6 +410,12 @@ function formatApiArgs(args, options) {
337
410
  }
338
411
  }
339
412
  }
413
+ function invokeSuccess(id, name, res) {
414
+ return invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' }));
415
+ }
416
+ function invokeFail(id, name, errMsg, errRes) {
417
+ return invokeCallback(id, extend({ errMsg: name + ':fail' + (errMsg ? ' ' + errMsg : '') }, errRes));
418
+ }
340
419
  function beforeInvokeApi(name, args, protocol, options) {
341
420
  if ((process.env.NODE_ENV !== 'production')) {
342
421
  validateProtocols(name, args, protocol);
@@ -352,6 +431,29 @@ function beforeInvokeApi(name, args, protocol, options) {
352
431
  return errMsg;
353
432
  }
354
433
  }
434
+ function normalizeErrMsg(errMsg) {
435
+ if (!errMsg || isString(errMsg)) {
436
+ return errMsg;
437
+ }
438
+ if (errMsg.stack) {
439
+ console.error(errMsg.message + LINEFEED + errMsg.stack);
440
+ return errMsg.message;
441
+ }
442
+ return errMsg;
443
+ }
444
+ function wrapperTaskApi(name, fn, protocol, options) {
445
+ return (args) => {
446
+ const id = createAsyncApiCallback(name, args, options);
447
+ const errMsg = beforeInvokeApi(name, [args], protocol, options);
448
+ if (errMsg) {
449
+ return invokeFail(id, name, errMsg);
450
+ }
451
+ return fn(args, {
452
+ resolve: (res) => invokeSuccess(id, name, res),
453
+ reject: (errMsg, errRes) => invokeFail(id, name, normalizeErrMsg(errMsg), errRes),
454
+ });
455
+ };
456
+ }
355
457
  function wrapperSyncApi(name, fn, protocol, options) {
356
458
  return (...args) => {
357
459
  const errMsg = beforeInvokeApi(name, args, protocol, options);
@@ -361,8 +463,14 @@ function wrapperSyncApi(name, fn, protocol, options) {
361
463
  return fn.apply(null, args);
362
464
  };
363
465
  }
466
+ function wrapperAsyncApi(name, fn, protocol, options) {
467
+ return wrapperTaskApi(name, fn, protocol, options);
468
+ }
364
469
  function defineSyncApi(name, fn, protocol, options) {
365
470
  return wrapperSyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options);
471
+ }
472
+ function defineAsyncApi(name, fn, protocol, options) {
473
+ return promisify$1(name, wrapperAsyncApi(name, fn, (process.env.NODE_ENV !== 'production') ? protocol : undefined, options));
366
474
  }
367
475
 
368
476
  const API_UPX2PX = 'upx2px';
@@ -541,6 +649,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
541
649
 
542
650
  let cid;
543
651
  let cidErrMsg;
652
+ let enabled;
544
653
  function normalizePushMessage(message) {
545
654
  try {
546
655
  return JSON.parse(message);
@@ -553,7 +662,10 @@ function normalizePushMessage(message) {
553
662
  * @param args
554
663
  */
555
664
  function invokePushCallback(args) {
556
- if (args.type === 'clientId') {
665
+ if (args.type === 'enabled') {
666
+ enabled = true;
667
+ }
668
+ else if (args.type === 'clientId') {
557
669
  cid = args.cid;
558
670
  cidErrMsg = args.errMsg;
559
671
  invokeGetPushCidCallbacks(cid, args.errMsg);
@@ -582,30 +694,27 @@ function invokeGetPushCidCallbacks(cid, errMsg) {
582
694
  });
583
695
  getPushCidCallbacks.length = 0;
584
696
  }
585
- function getPushClientId(args) {
586
- if (!isPlainObject(args)) {
587
- args = {};
588
- }
589
- const { success, fail, complete } = getApiCallbacks(args);
590
- const hasSuccess = isFunction(success);
591
- const hasFail = isFunction(fail);
592
- const hasComplete = isFunction(complete);
593
- getPushCidCallbacks.push((cid, errMsg) => {
594
- let res;
595
- if (cid) {
596
- res = { errMsg: 'getPushClientId:ok', cid };
597
- hasSuccess && success(res);
697
+ const API_GET_PUSH_CLIENT_ID = 'getPushClientId';
698
+ const getPushClientId = defineAsyncApi(API_GET_PUSH_CLIENT_ID, (_, { resolve, reject }) => {
699
+ Promise.resolve().then(() => {
700
+ if (typeof enabled === 'undefined') {
701
+ enabled = false;
702
+ cid = '';
703
+ cidErrMsg = 'unipush is not enabled';
598
704
  }
599
- else {
600
- res = { errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') };
601
- hasFail && fail(res);
705
+ getPushCidCallbacks.push((cid, errMsg) => {
706
+ if (cid) {
707
+ resolve({ cid });
708
+ }
709
+ else {
710
+ reject(errMsg);
711
+ }
712
+ });
713
+ if (typeof cid !== 'undefined') {
714
+ invokeGetPushCidCallbacks(cid, cidErrMsg);
602
715
  }
603
- hasComplete && complete(res);
604
716
  });
605
- if (typeof cid !== 'undefined') {
606
- Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg));
607
- }
608
- }
717
+ });
609
718
  const onPushMessageCallbacks = [];
610
719
  // 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现
611
720
  const onPushMessage = (fn) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcloudio/uni-mp-toutiao",
3
- "version": "3.0.0-alpha-3050020220622001",
3
+ "version": "3.0.0-alpha-3050020220623001",
4
4
  "description": "uni-app mp-toutiao",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -22,11 +22,11 @@
22
22
  },
23
23
  "gitHead": "33e807d66e1fe47e2ee08ad9c59247e37b8884da",
24
24
  "dependencies": {
25
- "@dcloudio/uni-cli-shared": "3.0.0-alpha-3050020220622001",
26
- "@dcloudio/uni-mp-compiler": "3.0.0-alpha-3050020220622001",
27
- "@dcloudio/uni-mp-vite": "3.0.0-alpha-3050020220622001",
28
- "@dcloudio/uni-mp-vue": "3.0.0-alpha-3050020220622001",
29
- "@dcloudio/uni-shared": "3.0.0-alpha-3050020220622001",
25
+ "@dcloudio/uni-cli-shared": "3.0.0-alpha-3050020220623001",
26
+ "@dcloudio/uni-mp-compiler": "3.0.0-alpha-3050020220623001",
27
+ "@dcloudio/uni-mp-vite": "3.0.0-alpha-3050020220623001",
28
+ "@dcloudio/uni-mp-vue": "3.0.0-alpha-3050020220623001",
29
+ "@dcloudio/uni-shared": "3.0.0-alpha-3050020220623001",
30
30
  "@vue/shared": "3.2.37",
31
31
  "@vue/compiler-core": "3.2.37"
32
32
  }