@dcloudio/uni-quickapp-webview 2.0.0-32220210818001 → 2.0.0-32920210926001

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/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import Vue from 'vue';
2
+ import { initVueI18n } from '@dcloudio/uni-i18n';
2
3
 
3
4
  function b64DecodeUnicode (str) {
4
5
  return decodeURIComponent(atob(str).split('').map(function (c) {
@@ -285,7 +286,7 @@ const promiseInterceptor = {
285
286
  };
286
287
 
287
288
  const SYNC_API_RE =
288
- /^\$|Window$|WindowStyle$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
289
+ /^\$|Window$|WindowStyle$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getLocale|setLocale/;
289
290
 
290
291
  const CONTEXT_API_RE = /^create|Manager$/;
291
292
 
@@ -398,6 +399,40 @@ function upx2px (number, newDeviceWidth) {
398
399
  return number < 0 ? -result : result
399
400
  }
400
401
 
402
+ function getLocale () {
403
+ // 优先使用 $locale
404
+ const app = getApp({
405
+ allowDefault: true
406
+ });
407
+ if (app && app.$vm) {
408
+ return app.$vm.$locale
409
+ }
410
+ return qa.getSystemInfoSync().language || 'zh-Hans'
411
+ }
412
+
413
+ function setLocale (locale) {
414
+ const app = getApp();
415
+ if (!app) {
416
+ return false
417
+ }
418
+ const oldLocale = app.$vm.$locale;
419
+ if (oldLocale !== locale) {
420
+ app.$vm.$locale = locale;
421
+ onLocaleChangeCallbacks.forEach((fn) => fn({
422
+ locale
423
+ }));
424
+ return true
425
+ }
426
+ return false
427
+ }
428
+
429
+ const onLocaleChangeCallbacks = [];
430
+ function onLocaleChange (fn) {
431
+ if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
432
+ onLocaleChangeCallbacks.push(fn);
433
+ }
434
+ }
435
+
401
436
  const interceptors = {
402
437
  promiseInterceptor
403
438
  };
@@ -405,6 +440,9 @@ const interceptors = {
405
440
  var baseApi = /*#__PURE__*/Object.freeze({
406
441
  __proto__: null,
407
442
  upx2px: upx2px,
443
+ getLocale: getLocale,
444
+ setLocale: setLocale,
445
+ onLocaleChange: onLocaleChange,
408
446
  addInterceptor: addInterceptor,
409
447
  removeInterceptor: removeInterceptor,
410
448
  interceptors: interceptors
@@ -805,7 +843,7 @@ function initTriggerEvent (mpInstance) {
805
843
  };
806
844
  }
807
845
 
808
- function initHook (name, options) {
846
+ function initHook (name, options, isComponent) {
809
847
  const oldHook = options[name];
810
848
  if (!oldHook) {
811
849
  options[name] = function () {
@@ -1375,110 +1413,160 @@ function handleEvent (event) {
1375
1413
  }
1376
1414
  }
1377
1415
 
1378
- const hooks = [
1379
- 'onShow',
1380
- 'onHide',
1381
- 'onError',
1382
- 'onPageNotFound',
1383
- 'onThemeChange',
1384
- 'onUnhandledRejection'
1385
- ];
1416
+ let locale;
1386
1417
 
1387
- function initEventChannel$1 () {
1388
- Vue.prototype.getOpenerEventChannel = function () {
1389
- if (!this.__eventChannel__) {
1390
- this.__eventChannel__ = new EventChannel();
1391
- }
1392
- return this.__eventChannel__
1393
- };
1394
- const callHook = Vue.prototype.__call_hook;
1395
- Vue.prototype.__call_hook = function (hook, args) {
1396
- if (hook === 'onLoad' && args && args.__id__) {
1397
- this.__eventChannel__ = getEventChannel(args.__id__);
1398
- delete args.__id__;
1399
- }
1400
- return callHook.call(this, hook, args)
1401
- };
1418
+ {
1419
+ locale = qa.getSystemInfoSync().language;
1402
1420
  }
1403
1421
 
1404
- function parseBaseApp (vm, {
1405
- mocks,
1406
- initRefs
1407
- }) {
1408
- initEventChannel$1();
1409
- if (vm.$options.store) {
1410
- Vue.prototype.$store = vm.$options.store;
1422
+ const i18n = initVueI18n(
1423
+ locale,
1424
+ {}
1425
+ );
1426
+ const t = i18n.t;
1427
+ const i18nMixin = (i18n.mixin = {
1428
+ beforeCreate () {
1429
+ const unwatch = i18n.i18n.watchLocale(() => {
1430
+ this.$forceUpdate();
1431
+ });
1432
+ this.$once('hook:beforeDestroy', function () {
1433
+ unwatch();
1434
+ });
1435
+ },
1436
+ methods: {
1437
+ $$t (key, values) {
1438
+ return t(key, values)
1439
+ }
1411
1440
  }
1412
- uniIdMixin(Vue);
1413
-
1414
- Vue.prototype.mpHost = "quickapp-webview";
1415
-
1416
- Vue.mixin({
1417
- beforeCreate () {
1418
- if (!this.$options.mpType) {
1419
- return
1420
- }
1421
-
1422
- this.mpType = this.$options.mpType;
1423
-
1424
- this.$mp = {
1425
- data: {},
1426
- [this.mpType]: this.$options.mpInstance
1427
- };
1428
-
1429
- this.$scope = this.$options.mpInstance;
1441
+ });
1442
+ const setLocale$1 = i18n.setLocale;
1443
+ const getLocale$1 = i18n.getLocale;
1430
1444
 
1431
- delete this.$options.mpType;
1432
- delete this.$options.mpInstance;
1433
- if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1434
- const app = getApp();
1435
- if (app.$vm && app.$vm.$i18n) {
1436
- this._i18n = app.$vm.$i18n;
1437
- }
1438
- }
1439
- if (this.mpType !== 'app') {
1440
- initRefs(this);
1441
- initMocks(this, mocks);
1442
- }
1443
- }
1445
+ function initAppLocale (Vue, appVm, locale) {
1446
+ const state = Vue.observable({
1447
+ locale: locale || i18n.getLocale()
1444
1448
  });
1445
-
1446
- const appOptions = {
1447
- onLaunch (args) {
1448
- if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1449
- return
1450
- }
1451
-
1452
- this.$vm = vm;
1453
-
1454
- this.$vm.$mp = {
1455
- app: this
1456
- };
1457
-
1458
- this.$vm.$scope = this;
1459
- // vm 上也挂载 globalData
1460
- this.$vm.globalData = this.globalData;
1461
-
1462
- this.$vm._isMounted = true;
1463
- this.$vm.__call_hook('mounted', args);
1464
-
1465
- this.$vm.__call_hook('onLaunch', args);
1466
- }
1449
+ const localeWatchers = [];
1450
+ appVm.$watchLocale = fn => {
1451
+ localeWatchers.push(fn);
1467
1452
  };
1468
-
1469
- // 兼容旧版本 globalData
1470
- appOptions.globalData = vm.$options.globalData || {};
1471
- // 将 methods 中的方法挂在 getApp() 中
1472
- const methods = vm.$options.methods;
1473
- if (methods) {
1474
- Object.keys(methods).forEach(name => {
1475
- appOptions[name] = methods[name];
1476
- });
1477
- }
1478
-
1479
- initHooks(appOptions, hooks);
1480
-
1481
- return appOptions
1453
+ Object.defineProperty(appVm, '$locale', {
1454
+ get () {
1455
+ return state.locale
1456
+ },
1457
+ set (v) {
1458
+ state.locale = v;
1459
+ localeWatchers.forEach(watch => watch(v));
1460
+ }
1461
+ });
1462
+ }
1463
+
1464
+ const hooks = [
1465
+ 'onShow',
1466
+ 'onHide',
1467
+ 'onError',
1468
+ 'onPageNotFound',
1469
+ 'onThemeChange',
1470
+ 'onUnhandledRejection'
1471
+ ];
1472
+
1473
+ function initEventChannel$1 () {
1474
+ Vue.prototype.getOpenerEventChannel = function () {
1475
+ if (!this.__eventChannel__) {
1476
+ this.__eventChannel__ = new EventChannel();
1477
+ }
1478
+ return this.__eventChannel__
1479
+ };
1480
+ const callHook = Vue.prototype.__call_hook;
1481
+ Vue.prototype.__call_hook = function (hook, args) {
1482
+ if (hook === 'onLoad' && args && args.__id__) {
1483
+ this.__eventChannel__ = getEventChannel(args.__id__);
1484
+ delete args.__id__;
1485
+ }
1486
+ return callHook.call(this, hook, args)
1487
+ };
1488
+ }
1489
+
1490
+ function parseBaseApp (vm, {
1491
+ mocks,
1492
+ initRefs
1493
+ }) {
1494
+ initEventChannel$1();
1495
+ if (vm.$options.store) {
1496
+ Vue.prototype.$store = vm.$options.store;
1497
+ }
1498
+ uniIdMixin(Vue);
1499
+
1500
+ Vue.prototype.mpHost = "quickapp-webview";
1501
+
1502
+ Vue.mixin({
1503
+ beforeCreate () {
1504
+ if (!this.$options.mpType) {
1505
+ return
1506
+ }
1507
+
1508
+ this.mpType = this.$options.mpType;
1509
+
1510
+ this.$mp = {
1511
+ data: {},
1512
+ [this.mpType]: this.$options.mpInstance
1513
+ };
1514
+
1515
+ this.$scope = this.$options.mpInstance;
1516
+
1517
+ delete this.$options.mpType;
1518
+ delete this.$options.mpInstance;
1519
+ if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1520
+ const app = getApp();
1521
+ if (app.$vm && app.$vm.$i18n) {
1522
+ this._i18n = app.$vm.$i18n;
1523
+ }
1524
+ }
1525
+ if (this.mpType !== 'app') {
1526
+ initRefs(this);
1527
+ initMocks(this, mocks);
1528
+ }
1529
+ }
1530
+ });
1531
+
1532
+ const appOptions = {
1533
+ onLaunch (args) {
1534
+ if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1535
+ return
1536
+ }
1537
+
1538
+ this.$vm = vm;
1539
+
1540
+ this.$vm.$mp = {
1541
+ app: this
1542
+ };
1543
+
1544
+ this.$vm.$scope = this;
1545
+ // vm 上也挂载 globalData
1546
+ this.$vm.globalData = this.globalData;
1547
+
1548
+ this.$vm._isMounted = true;
1549
+ this.$vm.__call_hook('mounted', args);
1550
+
1551
+ this.$vm.__call_hook('onLaunch', args);
1552
+ }
1553
+ };
1554
+
1555
+ // 兼容旧版本 globalData
1556
+ appOptions.globalData = vm.$options.globalData || {};
1557
+ // 将 methods 中的方法挂在 getApp() 中
1558
+ const methods = vm.$options.methods;
1559
+ if (methods) {
1560
+ Object.keys(methods).forEach(name => {
1561
+ appOptions[name] = methods[name];
1562
+ });
1563
+ }
1564
+
1565
+ initAppLocale(Vue, vm, qa.getSystemInfoSync().language || 'zh-Hans');
1566
+
1567
+ initHooks(appOptions, hooks);
1568
+
1569
+ return appOptions
1482
1570
  }
1483
1571
 
1484
1572
  const mocks = ['nodeId', 'componentName', '_componentId', 'uniquePrefix'];
@@ -1,4 +1,8 @@
1
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';
2
6
 
3
7
  const eventChannels = {};
4
8
  const eventChannelStack = [];
@@ -36,6 +40,15 @@ const navigateTo = {
36
40
  },
37
41
  };
38
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
+
39
52
  function getBaseSystemInfo() {
40
53
  return qa.getSystemInfoSync()
41
54
  }
@@ -570,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
570
583
  emitter.emit(name, ...args);
571
584
  }, EmitProtocol);
572
585
 
573
- const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
586
+ const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
574
587
  const CONTEXT_API_RE = /^create|Manager$/;
575
588
  // Context例外情况
576
589
  const CONTEXT_API_RE_EXC = ['createBLEConnection'];
@@ -714,6 +727,34 @@ function initWrapper(protocols) {
714
727
  };
715
728
  }
716
729
 
730
+ const getLocale = () => {
731
+ // 优先使用 $locale
732
+ const app = getApp({ allowDefault: true });
733
+ if (app && app.$vm) {
734
+ return app.$vm.$locale;
735
+ }
736
+ return uni.getSystemInfoSync().language || 'zh-Hans';
737
+ };
738
+ const setLocale = (locale) => {
739
+ const app = getApp();
740
+ if (!app) {
741
+ return false;
742
+ }
743
+ const oldLocale = app.$vm.$locale;
744
+ if (oldLocale !== locale) {
745
+ app.$vm.$locale = locale;
746
+ onLocaleChangeCallbacks.forEach((fn) => fn({ locale }));
747
+ return true;
748
+ }
749
+ return false;
750
+ };
751
+ const onLocaleChangeCallbacks = [];
752
+ const onLocaleChange = (fn) => {
753
+ if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
754
+ onLocaleChangeCallbacks.push(fn);
755
+ }
756
+ };
757
+
717
758
  const baseApis = {
718
759
  $on,
719
760
  $off,
@@ -722,6 +763,10 @@ const baseApis = {
722
763
  upx2px,
723
764
  addInterceptor,
724
765
  removeInterceptor,
766
+ onAppLaunch,
767
+ getLocale,
768
+ setLocale,
769
+ onLocaleChange,
725
770
  };
726
771
  function initUni(api, protocols) {
727
772
  const wrapper = initWrapper(protocols);
@@ -834,17 +879,17 @@ if (qa.canIUse('getVendorPaymentProvider')) {
834
879
  const getProvider = initGetProvider(providers);
835
880
 
836
881
  var shims = /*#__PURE__*/Object.freeze({
837
- __proto__: null,
838
- getProvider: getProvider
882
+ __proto__: null,
883
+ getProvider: getProvider
839
884
  });
840
885
 
841
886
  var protocols = /*#__PURE__*/Object.freeze({
842
- __proto__: null,
843
- redirectTo: redirectTo,
844
- navigateTo: navigateTo,
845
- previewImage: previewImage,
846
- getSystemInfo: getSystemInfo,
847
- getSystemInfoSync: getSystemInfoSync
887
+ __proto__: null,
888
+ redirectTo: redirectTo,
889
+ navigateTo: navigateTo,
890
+ previewImage: previewImage,
891
+ getSystemInfo: getSystemInfo,
892
+ getSystemInfoSync: getSystemInfoSync
848
893
  });
849
894
 
850
895
  var index = initUni(shims, protocols);
@@ -1,4 +1,5 @@
1
1
  import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared';
2
+ import { injectHook, ref } from 'vue';
2
3
 
3
4
  const encode = encodeURIComponent;
4
5
  function stringifyQuery(obj, encodeStr = encode) {
@@ -497,6 +498,13 @@ function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
497
498
  findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes));
498
499
  }
499
500
 
501
+ qa.appLaunchHooks = [];
502
+ function injectAppLaunchHooks(appInstance) {
503
+ qa.appLaunchHooks.forEach((hook) => {
504
+ injectHook(ON_LAUNCH, hook, appInstance);
505
+ });
506
+ }
507
+
500
508
  const HOOKS = [
501
509
  ON_SHOW,
502
510
  ON_HIDE,
@@ -521,10 +529,12 @@ function parseApp(instance, parseAppOptions) {
521
529
  mpInstance: this,
522
530
  slots: [],
523
531
  });
532
+ injectAppLaunchHooks(internalInstance);
524
533
  ctx.globalData = this.globalData;
525
- instance.$callHook(ON_LAUNCH, options);
534
+ instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
526
535
  },
527
536
  };
537
+ initLocale(instance);
528
538
  const vueOptions = instance.$.type;
529
539
  initHooks(appOptions, HOOKS);
530
540
  initUnknownHooks(appOptions, vueOptions);
@@ -541,6 +551,17 @@ function initCreateApp(parseAppOptions) {
541
551
  return function createApp(vm) {
542
552
  return App(parseApp(vm, parseAppOptions));
543
553
  };
554
+ }
555
+ function initLocale(appVm) {
556
+ const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans');
557
+ Object.defineProperty(appVm, '$locale', {
558
+ get() {
559
+ return locale.value;
560
+ },
561
+ set(v) {
562
+ locale.value = v;
563
+ },
564
+ });
544
565
  }
545
566
 
546
567
  const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
@@ -1201,7 +1222,7 @@ function initLifetimes(lifetimesOptions) {
1201
1222
  if (this.pageinstance) {
1202
1223
  this.__webviewId__ = this.pageinstance.__pageId__;
1203
1224
  }
1204
- this.$vm.$callSyncHook('created');
1225
+ this.$vm.$callCreatedHook();
1205
1226
  this.$vm.$callHook('mounted');
1206
1227
  this.$vm.$callHook(ON_READY);
1207
1228
  }
@@ -1258,7 +1279,7 @@ function handleLink({ detail: { nodeId, webviewId }, }) {
1258
1279
  initInjections(vm);
1259
1280
  initProvide(vm);
1260
1281
  }
1261
- vm.$callSyncHook('created');
1282
+ vm.$callCreatedHook();
1262
1283
  };
1263
1284
  const mountedVm = function () {
1264
1285
  // 处理当前 vm 子
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcloudio/uni-quickapp-webview",
3
- "version": "2.0.0-32220210818001",
3
+ "version": "2.0.0-32920210926001",
4
4
  "description": "uni-app quickapp-webview",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -17,5 +17,5 @@
17
17
  "name": "quickapp-webview",
18
18
  "title": "快应用(Webview)版"
19
19
  },
20
- "gitHead": "d297b8ae6037fb857408d1d6d140f7d7b4929b91"
20
+ "gitHead": "5e7404fe087c97dfc46bfa7b0ab9044a2fa90142"
21
21
  }