@dcloudio/uni-mp-kuaishou 2.0.0 → 2.0.1-32920211122002

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,83 @@
1
1
  import Vue from 'vue';
2
+ import { initVueI18n } from '@dcloudio/uni-i18n';
3
+
4
+ let realAtob;
5
+
6
+ const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
7
+ const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
8
+
9
+ if (typeof atob !== 'function') {
10
+ realAtob = function (str) {
11
+ str = String(str).replace(/[\t\n\f\r ]+/g, '');
12
+ if (!b64re.test(str)) { throw new Error("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.") }
13
+
14
+ // Adding the padding if missing, for semplicity
15
+ str += '=='.slice(2 - (str.length & 3));
16
+ var bitmap; var result = ''; var r1; var r2; var i = 0;
17
+ for (; i < str.length;) {
18
+ bitmap = b64.indexOf(str.charAt(i++)) << 18 | b64.indexOf(str.charAt(i++)) << 12 |
19
+ (r1 = b64.indexOf(str.charAt(i++))) << 6 | (r2 = b64.indexOf(str.charAt(i++)));
20
+
21
+ result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
22
+ : r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
23
+ : String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
24
+ }
25
+ return result
26
+ };
27
+ } else {
28
+ // 注意atob只能在全局对象上调用,例如:`const Base64 = {atob};Base64.atob('xxxx')`是错误的用法
29
+ realAtob = atob;
30
+ }
31
+
32
+ function b64DecodeUnicode (str) {
33
+ return decodeURIComponent(realAtob(str).split('').map(function (c) {
34
+ return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
35
+ }).join(''))
36
+ }
37
+
38
+ function getCurrentUserInfo () {
39
+ const token = ( ks).getStorageSync('uni_id_token') || '';
40
+ const tokenArr = token.split('.');
41
+ if (!token || tokenArr.length !== 3) {
42
+ return {
43
+ uid: null,
44
+ role: [],
45
+ permission: [],
46
+ tokenExpired: 0
47
+ }
48
+ }
49
+ let userInfo;
50
+ try {
51
+ userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1]));
52
+ } catch (error) {
53
+ throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message)
54
+ }
55
+ userInfo.tokenExpired = userInfo.exp * 1000;
56
+ delete userInfo.exp;
57
+ delete userInfo.iat;
58
+ return userInfo
59
+ }
60
+
61
+ function uniIdMixin (Vue) {
62
+ Vue.prototype.uniIDHasRole = function (roleId) {
63
+ const {
64
+ role
65
+ } = getCurrentUserInfo();
66
+ return role.indexOf(roleId) > -1
67
+ };
68
+ Vue.prototype.uniIDHasPermission = function (permissionId) {
69
+ const {
70
+ permission
71
+ } = getCurrentUserInfo();
72
+ return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1
73
+ };
74
+ Vue.prototype.uniIDTokenValid = function () {
75
+ const {
76
+ tokenExpired
77
+ } = getCurrentUserInfo();
78
+ return tokenExpired > Date.now()
79
+ };
80
+ }
2
81
 
3
82
  const _toString = Object.prototype.toString;
4
83
  const hasOwnProperty = Object.prototype.hasOwnProperty;
@@ -142,7 +221,7 @@ function queue (hooks, data) {
142
221
  }
143
222
  if (res === false) {
144
223
  return {
145
- then () {}
224
+ then () { }
146
225
  }
147
226
  }
148
227
  }
@@ -190,15 +269,15 @@ function getApiInterceptorHooks (method) {
190
269
  if (hook !== 'returnValue') {
191
270
  interceptor[hook] = globalInterceptors[hook].slice();
192
271
  }
193
- });
194
- const scopedInterceptor = scopedInterceptors[method];
195
- if (scopedInterceptor) {
196
- Object.keys(scopedInterceptor).forEach(hook => {
197
- if (hook !== 'returnValue') {
198
- interceptor[hook] = (interceptor[hook] || []).concat(scopedInterceptor[hook]);
199
- }
200
- });
201
- }
272
+ });
273
+ const scopedInterceptor = scopedInterceptors[method];
274
+ if (scopedInterceptor) {
275
+ Object.keys(scopedInterceptor).forEach(hook => {
276
+ if (hook !== 'returnValue') {
277
+ interceptor[hook] = (interceptor[hook] || []).concat(scopedInterceptor[hook]);
278
+ }
279
+ });
280
+ }
202
281
  return interceptor
203
282
  }
204
283
 
@@ -222,16 +301,20 @@ const promiseInterceptor = {
222
301
  if (!isPromise(res)) {
223
302
  return res
224
303
  }
225
- return res.then(res => {
226
- return res[1]
227
- }).catch(res => {
228
- return res[0]
304
+ return new Promise((resolve, reject) => {
305
+ res.then(res => {
306
+ if (res[0]) {
307
+ reject(res[0]);
308
+ } else {
309
+ resolve(res[1]);
310
+ }
311
+ });
229
312
  })
230
313
  }
231
314
  };
232
315
 
233
316
  const SYNC_API_RE =
234
- /^\$|Window$|WindowStyle$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
317
+ /^\$|Window$|WindowStyle$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getLocale|setLocale/;
235
318
 
236
319
  const CONTEXT_API_RE = /^create|Manager$/;
237
320
 
@@ -344,6 +427,44 @@ function upx2px (number, newDeviceWidth) {
344
427
  return number < 0 ? -result : result
345
428
  }
346
429
 
430
+ function getLocale () {
431
+ // 优先使用 $locale
432
+ const app = getApp({
433
+ allowDefault: true
434
+ });
435
+ if (app && app.$vm) {
436
+ return app.$vm.$locale
437
+ }
438
+ return ks.getSystemInfoSync().language || 'zh-Hans'
439
+ }
440
+
441
+ function setLocale (locale) {
442
+ const app = getApp();
443
+ if (!app) {
444
+ return false
445
+ }
446
+ const oldLocale = app.$vm.$locale;
447
+ if (oldLocale !== locale) {
448
+ app.$vm.$locale = locale;
449
+ onLocaleChangeCallbacks.forEach((fn) => fn({
450
+ locale
451
+ }));
452
+ return true
453
+ }
454
+ return false
455
+ }
456
+
457
+ const onLocaleChangeCallbacks = [];
458
+ function onLocaleChange (fn) {
459
+ if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
460
+ onLocaleChangeCallbacks.push(fn);
461
+ }
462
+ }
463
+
464
+ if (typeof global !== 'undefined') {
465
+ global.getLocale = getLocale;
466
+ }
467
+
347
468
  const interceptors = {
348
469
  promiseInterceptor
349
470
  };
@@ -351,6 +472,9 @@ const interceptors = {
351
472
  var baseApi = /*#__PURE__*/Object.freeze({
352
473
  __proto__: null,
353
474
  upx2px: upx2px,
475
+ getLocale: getLocale,
476
+ setLocale: setLocale,
477
+ onLocaleChange: onLocaleChange,
354
478
  addInterceptor: addInterceptor,
355
479
  removeInterceptor: removeInterceptor,
356
480
  interceptors: interceptors
@@ -560,12 +684,20 @@ var getSystemInfo = {
560
684
  }
561
685
  };
562
686
 
687
+ const oName = 'getUserInfo';
688
+ const nName = 'getUserProfile';
689
+
690
+ var getUserProfile = {
691
+ name: ks.canIUse(nName) ? nName : oName
692
+ };
693
+
563
694
  const protocols = {
564
695
  navigateTo,
565
696
  redirectTo,
566
697
  previewImage,
567
698
  getSystemInfo,
568
- getSystemInfoSync: getSystemInfo
699
+ getSystemInfoSync: getSystemInfo,
700
+ getUserProfile
569
701
  };
570
702
  const todos = [
571
703
  'vibrate'
@@ -776,7 +908,7 @@ function initTriggerEvent (mpInstance) {
776
908
  };
777
909
  }
778
910
 
779
- function initHook (name, options) {
911
+ function initHook (name, options, isComponent) {
780
912
  const oldHook = options[name];
781
913
  if (!oldHook) {
782
914
  options[name] = function () {
@@ -1014,6 +1146,11 @@ function initProperties (props, isBehavior = false, file = '') {
1014
1146
  type: Object,
1015
1147
  value: null
1016
1148
  };
1149
+ // scopedSlotsCompiler auto
1150
+ properties.scopedSlotsCompiler = {
1151
+ type: String,
1152
+ value: ''
1153
+ };
1017
1154
  properties.vueSlots = { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
1018
1155
  type: null,
1019
1156
  value: [],
@@ -1341,109 +1478,215 @@ function handleEvent (event) {
1341
1478
  }
1342
1479
  }
1343
1480
 
1344
- const hooks = [
1345
- 'onShow',
1346
- 'onHide',
1347
- 'onError',
1348
- 'onPageNotFound',
1349
- 'onThemeChange',
1350
- 'onUnhandledRejection'
1351
- ];
1481
+ let locale;
1352
1482
 
1353
- function initEventChannel$1 () {
1354
- Vue.prototype.getOpenerEventChannel = function () {
1355
- if (!this.__eventChannel__) {
1356
- this.__eventChannel__ = new EventChannel();
1357
- }
1358
- return this.__eventChannel__
1359
- };
1360
- const callHook = Vue.prototype.__call_hook;
1361
- Vue.prototype.__call_hook = function (hook, args) {
1362
- if (hook === 'onLoad' && args && args.__id__) {
1363
- this.__eventChannel__ = getEventChannel(args.__id__);
1364
- delete args.__id__;
1365
- }
1366
- return callHook.call(this, hook, args)
1367
- };
1483
+ {
1484
+ locale = ks.getSystemInfoSync().language;
1368
1485
  }
1369
1486
 
1370
- function parseBaseApp (vm, {
1371
- mocks,
1372
- initRefs
1373
- }) {
1374
- initEventChannel$1();
1375
- if (vm.$options.store) {
1376
- Vue.prototype.$store = vm.$options.store;
1487
+ const i18n = initVueI18n(
1488
+ locale,
1489
+ {}
1490
+ );
1491
+ const t = i18n.t;
1492
+ const i18nMixin = (i18n.mixin = {
1493
+ beforeCreate () {
1494
+ const unwatch = i18n.i18n.watchLocale(() => {
1495
+ this.$forceUpdate();
1496
+ });
1497
+ this.$once('hook:beforeDestroy', function () {
1498
+ unwatch();
1499
+ });
1500
+ },
1501
+ methods: {
1502
+ $$t (key, values) {
1503
+ return t(key, values)
1504
+ }
1377
1505
  }
1506
+ });
1507
+ const setLocale$1 = i18n.setLocale;
1508
+ const getLocale$1 = i18n.getLocale;
1378
1509
 
1379
- Vue.prototype.mpHost = "mp-kuaishou";
1380
-
1381
- Vue.mixin({
1382
- beforeCreate () {
1383
- if (!this.$options.mpType) {
1384
- return
1385
- }
1386
-
1387
- this.mpType = this.$options.mpType;
1388
-
1389
- this.$mp = {
1390
- data: {},
1391
- [this.mpType]: this.$options.mpInstance
1392
- };
1393
-
1394
- this.$scope = this.$options.mpInstance;
1395
-
1396
- delete this.$options.mpType;
1397
- delete this.$options.mpInstance;
1398
- if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1399
- const app = getApp();
1400
- if (app.$vm && app.$vm.$i18n) {
1401
- this._i18n = app.$vm.$i18n;
1402
- }
1403
- }
1404
- if (this.mpType !== 'app') {
1405
- initRefs(this);
1406
- initMocks(this, mocks);
1407
- }
1408
- }
1510
+ function initAppLocale (Vue, appVm, locale) {
1511
+ const state = Vue.observable({
1512
+ locale: locale || i18n.getLocale()
1409
1513
  });
1410
-
1411
- const appOptions = {
1412
- onLaunch (args) {
1413
- if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1414
- return
1415
- }
1416
-
1417
- this.$vm = vm;
1418
-
1419
- this.$vm.$mp = {
1420
- app: this
1421
- };
1422
-
1423
- this.$vm.$scope = this;
1424
- // vm 上也挂载 globalData
1425
- this.$vm.globalData = this.globalData;
1426
-
1427
- this.$vm._isMounted = true;
1428
- this.$vm.__call_hook('mounted', args);
1429
-
1430
- this.$vm.__call_hook('onLaunch', args);
1431
- }
1514
+ const localeWatchers = [];
1515
+ appVm.$watchLocale = fn => {
1516
+ localeWatchers.push(fn);
1432
1517
  };
1433
-
1434
- // 兼容旧版本 globalData
1435
- appOptions.globalData = vm.$options.globalData || {};
1436
- // 将 methods 中的方法挂在 getApp() 中
1437
- const methods = vm.$options.methods;
1438
- if (methods) {
1439
- Object.keys(methods).forEach(name => {
1440
- appOptions[name] = methods[name];
1441
- });
1442
- }
1443
-
1444
- initHooks(appOptions, hooks);
1445
-
1446
- return appOptions
1518
+ Object.defineProperty(appVm, '$locale', {
1519
+ get () {
1520
+ return state.locale
1521
+ },
1522
+ set (v) {
1523
+ state.locale = v;
1524
+ localeWatchers.forEach(watch => watch(v));
1525
+ }
1526
+ });
1527
+ }
1528
+
1529
+ const hooks = [
1530
+ 'onShow',
1531
+ 'onHide',
1532
+ 'onError',
1533
+ 'onPageNotFound',
1534
+ 'onThemeChange',
1535
+ 'onUnhandledRejection'
1536
+ ];
1537
+
1538
+ function initEventChannel$1 () {
1539
+ Vue.prototype.getOpenerEventChannel = function () {
1540
+ if (!this.__eventChannel__) {
1541
+ this.__eventChannel__ = new EventChannel();
1542
+ }
1543
+ return this.__eventChannel__
1544
+ };
1545
+ const callHook = Vue.prototype.__call_hook;
1546
+ Vue.prototype.__call_hook = function (hook, args) {
1547
+ if (hook === 'onLoad' && args && args.__id__) {
1548
+ this.__eventChannel__ = getEventChannel(args.__id__);
1549
+ delete args.__id__;
1550
+ }
1551
+ return callHook.call(this, hook, args)
1552
+ };
1553
+ }
1554
+
1555
+ function initScopedSlotsParams () {
1556
+ const center = {};
1557
+ const parents = {};
1558
+
1559
+ Vue.prototype.$hasScopedSlotsParams = function (vueId) {
1560
+ const has = center[vueId];
1561
+ if (!has) {
1562
+ parents[vueId] = this;
1563
+ this.$on('hook:destroyed', () => {
1564
+ delete parents[vueId];
1565
+ });
1566
+ }
1567
+ return has
1568
+ };
1569
+
1570
+ Vue.prototype.$getScopedSlotsParams = function (vueId, name, key) {
1571
+ const data = center[vueId];
1572
+ if (data) {
1573
+ const object = data[name] || {};
1574
+ return key ? object[key] : object
1575
+ } else {
1576
+ parents[vueId] = this;
1577
+ this.$on('hook:destroyed', () => {
1578
+ delete parents[vueId];
1579
+ });
1580
+ }
1581
+ };
1582
+
1583
+ Vue.prototype.$setScopedSlotsParams = function (name, value) {
1584
+ const vueIds = this.$options.propsData.vueId;
1585
+ if (vueIds) {
1586
+ const vueId = vueIds.split(',')[0];
1587
+ const object = center[vueId] = center[vueId] || {};
1588
+ object[name] = value;
1589
+ if (parents[vueId]) {
1590
+ parents[vueId].$forceUpdate();
1591
+ }
1592
+ }
1593
+ };
1594
+
1595
+ Vue.mixin({
1596
+ destroyed () {
1597
+ const propsData = this.$options.propsData;
1598
+ const vueId = propsData && propsData.vueId;
1599
+ if (vueId) {
1600
+ delete center[vueId];
1601
+ delete parents[vueId];
1602
+ }
1603
+ }
1604
+ });
1605
+ }
1606
+
1607
+ function parseBaseApp (vm, {
1608
+ mocks,
1609
+ initRefs
1610
+ }) {
1611
+ initEventChannel$1();
1612
+ {
1613
+ initScopedSlotsParams();
1614
+ }
1615
+ if (vm.$options.store) {
1616
+ Vue.prototype.$store = vm.$options.store;
1617
+ }
1618
+ uniIdMixin(Vue);
1619
+
1620
+ Vue.prototype.mpHost = "mp-kuaishou";
1621
+
1622
+ Vue.mixin({
1623
+ beforeCreate () {
1624
+ if (!this.$options.mpType) {
1625
+ return
1626
+ }
1627
+
1628
+ this.mpType = this.$options.mpType;
1629
+
1630
+ this.$mp = {
1631
+ data: {},
1632
+ [this.mpType]: this.$options.mpInstance
1633
+ };
1634
+
1635
+ this.$scope = this.$options.mpInstance;
1636
+
1637
+ delete this.$options.mpType;
1638
+ delete this.$options.mpInstance;
1639
+ if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1640
+ const app = getApp();
1641
+ if (app.$vm && app.$vm.$i18n) {
1642
+ this._i18n = app.$vm.$i18n;
1643
+ }
1644
+ }
1645
+ if (this.mpType !== 'app') {
1646
+ initRefs(this);
1647
+ initMocks(this, mocks);
1648
+ }
1649
+ }
1650
+ });
1651
+
1652
+ const appOptions = {
1653
+ onLaunch (args) {
1654
+ if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1655
+ return
1656
+ }
1657
+
1658
+ this.$vm = vm;
1659
+
1660
+ this.$vm.$mp = {
1661
+ app: this
1662
+ };
1663
+
1664
+ this.$vm.$scope = this;
1665
+ // vm 上也挂载 globalData
1666
+ this.$vm.globalData = this.globalData;
1667
+
1668
+ this.$vm._isMounted = true;
1669
+ this.$vm.__call_hook('mounted', args);
1670
+
1671
+ this.$vm.__call_hook('onLaunch', args);
1672
+ }
1673
+ };
1674
+
1675
+ // 兼容旧版本 globalData
1676
+ appOptions.globalData = vm.$options.globalData || {};
1677
+ // 将 methods 中的方法挂在 getApp() 中
1678
+ const methods = vm.$options.methods;
1679
+ if (methods) {
1680
+ Object.keys(methods).forEach(name => {
1681
+ appOptions[name] = methods[name];
1682
+ });
1683
+ }
1684
+
1685
+ initAppLocale(Vue, vm, ks.getSystemInfoSync().language || 'zh-Hans');
1686
+
1687
+ initHooks(appOptions, hooks);
1688
+
1689
+ return appOptions
1447
1690
  }
1448
1691
 
1449
1692
  const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'];
@@ -1684,8 +1927,59 @@ function parseComponent (vueComponentOptions) {
1684
1927
  })
1685
1928
  }
1686
1929
 
1930
+ /**
1931
+ * 用于延迟调用 setData
1932
+ * 在 setData 真实调用的时机需执行 fixSetDataEnd
1933
+ * @param {*} mpInstance
1934
+ */
1935
+ function fixSetDataStart (mpInstance) {
1936
+ const setData = mpInstance.setData;
1937
+ const setDataArgs = [];
1938
+ mpInstance.setData = function () {
1939
+ setDataArgs.push(arguments);
1940
+ };
1941
+ mpInstance.__fixInitData = function () {
1942
+ this.setData = setData;
1943
+ const fn = () => {
1944
+ setDataArgs.forEach(args => {
1945
+ setData.apply(this, args);
1946
+ });
1947
+ };
1948
+ if (setDataArgs.length) {
1949
+ if (this.groupSetData) {
1950
+ this.groupSetData(fn);
1951
+ } else {
1952
+ fn();
1953
+ }
1954
+ }
1955
+ };
1956
+ }
1957
+ /**
1958
+ * 恢复真实的 setData 方法
1959
+ * @param {*} mpInstance
1960
+ */
1961
+ function fixSetDataEnd (mpInstance) {
1962
+ if (mpInstance.__fixInitData) {
1963
+ mpInstance.__fixInitData();
1964
+ delete mpInstance.__fixInitData;
1965
+ }
1966
+ }
1967
+
1687
1968
  function parseComponent$1 (vueComponentOptions) {
1688
- return parseComponent(vueComponentOptions)
1969
+ const componentOptions = parseComponent(vueComponentOptions);
1970
+ const oldAttached = componentOptions.lifetimes.attached;
1971
+ componentOptions.lifetimes.attached = function attached () {
1972
+ // 暂不区分版本
1973
+ if (isPage.call(this)) {
1974
+ // 解决快手小程序页面 attached 生命周期 setData 导致数据同步异常的问题
1975
+ fixSetDataStart(this);
1976
+ setTimeout(() => {
1977
+ fixSetDataEnd(this);
1978
+ }, 0);
1979
+ }
1980
+ oldAttached.call(this);
1981
+ };
1982
+ return componentOptions
1689
1983
  }
1690
1984
 
1691
1985
  const hooks$1 = [
@@ -1746,6 +2040,7 @@ function createSubpackageApp (vm) {
1746
2040
  const app = getApp({
1747
2041
  allowDefault: true
1748
2042
  });
2043
+ vm.$scope = app;
1749
2044
  const globalData = app.globalData;
1750
2045
  if (globalData) {
1751
2046
  Object.keys(appOptions.globalData).forEach(name => {
@@ -1760,18 +2055,18 @@ function createSubpackageApp (vm) {
1760
2055
  }
1761
2056
  });
1762
2057
  if (isFn(appOptions.onShow) && ks.onAppShow) {
1763
- ks.onAppShow((...args) => {
1764
- appOptions.onShow.apply(app, args);
2058
+ ks.onAppShow((...args) => {
2059
+ vm.__call_hook('onShow', args);
1765
2060
  });
1766
2061
  }
1767
2062
  if (isFn(appOptions.onHide) && ks.onAppHide) {
1768
- ks.onAppHide((...args) => {
1769
- appOptions.onHide.apply(app, args);
2063
+ ks.onAppHide((...args) => {
2064
+ vm.__call_hook('onHide', args);
1770
2065
  });
1771
2066
  }
1772
2067
  if (isFn(appOptions.onLaunch)) {
1773
2068
  const args = ks.getLaunchOptionsSync && ks.getLaunchOptionsSync();
1774
- appOptions.onLaunch.call(app, args);
2069
+ vm.__call_hook('onLaunch', args);
1775
2070
  }
1776
2071
  return vm
1777
2072
  }