@dcloudio/uni-mp-qq 2.0.0 → 2.0.1-33420211227001

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 = ( wx).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$|sendHostEvent|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 wx.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: wx.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
  'preloadPage',
@@ -937,7 +1069,7 @@ function initTriggerEvent (mpInstance) {
937
1069
  };
938
1070
  }
939
1071
 
940
- function initHook (name, options) {
1072
+ function initHook (name, options, isComponent) {
941
1073
  const oldHook = options[name];
942
1074
  if (!oldHook) {
943
1075
  options[name] = function () {
@@ -1175,6 +1307,11 @@ function initProperties (props, isBehavior = false, file = '') {
1175
1307
  type: Object,
1176
1308
  value: null
1177
1309
  };
1310
+ // scopedSlotsCompiler auto
1311
+ properties.scopedSlotsCompiler = {
1312
+ type: String,
1313
+ value: ''
1314
+ };
1178
1315
  properties.vueSlots = { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
1179
1316
  type: null,
1180
1317
  value: [],
@@ -1502,114 +1639,255 @@ function handleEvent (event) {
1502
1639
  }
1503
1640
  }
1504
1641
 
1505
- const hooks = [
1506
- 'onShow',
1507
- 'onHide',
1508
- 'onError',
1509
- 'onPageNotFound',
1510
- 'onThemeChange',
1511
- 'onUnhandledRejection'
1512
- ];
1642
+ const messages = {};
1513
1643
 
1514
- function initEventChannel$1 () {
1515
- Vue.prototype.getOpenerEventChannel = function () {
1516
- if (!this.__eventChannel__) {
1517
- this.__eventChannel__ = new EventChannel();
1518
- }
1519
- return this.__eventChannel__
1520
- };
1521
- const callHook = Vue.prototype.__call_hook;
1522
- Vue.prototype.__call_hook = function (hook, args) {
1523
- if (hook === 'onLoad' && args && args.__id__) {
1524
- this.__eventChannel__ = getEventChannel(args.__id__);
1525
- delete args.__id__;
1526
- }
1527
- return callHook.call(this, hook, args)
1528
- };
1644
+ let locale;
1645
+
1646
+ {
1647
+ locale = wx.getSystemInfoSync().language;
1529
1648
  }
1530
1649
 
1531
- function parseBaseApp (vm, {
1532
- mocks,
1533
- initRefs
1534
- }) {
1535
- initEventChannel$1();
1536
- if (vm.$options.store) {
1537
- Vue.prototype.$store = vm.$options.store;
1650
+ function initI18nMessages () {
1651
+ if (!isEnableLocale()) {
1652
+ return
1538
1653
  }
1539
-
1540
- Vue.prototype.mpHost = "mp-qq";
1541
-
1542
- Vue.mixin({
1543
- beforeCreate () {
1544
- if (!this.$options.mpType) {
1545
- return
1654
+ const localeKeys = Object.keys(__uniConfig.locales);
1655
+ if (localeKeys.length) {
1656
+ localeKeys.forEach((locale) => {
1657
+ const curMessages = messages[locale];
1658
+ const userMessages = __uniConfig.locales[locale];
1659
+ if (curMessages) {
1660
+ Object.assign(curMessages, userMessages);
1661
+ } else {
1662
+ messages[locale] = userMessages;
1546
1663
  }
1664
+ });
1665
+ }
1666
+ }
1547
1667
 
1548
- this.mpType = this.$options.mpType;
1549
-
1550
- this.$mp = {
1551
- data: {},
1552
- [this.mpType]: this.$options.mpInstance
1553
- };
1554
-
1555
- this.$scope = this.$options.mpInstance;
1668
+ initI18nMessages();
1556
1669
 
1557
- delete this.$options.mpType;
1558
- delete this.$options.mpInstance;
1559
- if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1560
- const app = getApp();
1561
- if (app.$vm && app.$vm.$i18n) {
1562
- this._i18n = app.$vm.$i18n;
1563
- }
1564
- }
1565
- if (this.mpType !== 'app') {
1566
- initRefs(this);
1567
- initMocks(this, mocks);
1568
- }
1670
+ const i18n = initVueI18n(
1671
+ locale,
1672
+ {}
1673
+ );
1674
+ const t = i18n.t;
1675
+ const i18nMixin = (i18n.mixin = {
1676
+ beforeCreate () {
1677
+ const unwatch = i18n.i18n.watchLocale(() => {
1678
+ this.$forceUpdate();
1679
+ });
1680
+ this.$once('hook:beforeDestroy', function () {
1681
+ unwatch();
1682
+ });
1683
+ },
1684
+ methods: {
1685
+ $$t (key, values) {
1686
+ return t(key, values)
1569
1687
  }
1570
- });
1571
-
1572
- const appOptions = {
1573
- onLaunch (args) {
1574
- if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1575
- return
1576
- }
1577
- {
1578
- if (wx.canIUse && !wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断
1579
- console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上');
1580
- }
1581
- }
1582
-
1583
- this.$vm = vm;
1584
-
1585
- this.$vm.$mp = {
1586
- app: this
1587
- };
1588
-
1589
- this.$vm.$scope = this;
1590
- // vm 上也挂载 globalData
1591
- this.$vm.globalData = this.globalData;
1592
-
1593
- this.$vm._isMounted = true;
1594
- this.$vm.__call_hook('mounted', args);
1688
+ }
1689
+ });
1690
+ const setLocale$1 = i18n.setLocale;
1691
+ const getLocale$1 = i18n.getLocale;
1595
1692
 
1596
- this.$vm.__call_hook('onLaunch', args);
1597
- }
1693
+ function initAppLocale (Vue, appVm, locale) {
1694
+ const state = Vue.observable({
1695
+ locale: locale || i18n.getLocale()
1696
+ });
1697
+ const localeWatchers = [];
1698
+ appVm.$watchLocale = fn => {
1699
+ localeWatchers.push(fn);
1598
1700
  };
1701
+ Object.defineProperty(appVm, '$locale', {
1702
+ get () {
1703
+ return state.locale
1704
+ },
1705
+ set (v) {
1706
+ state.locale = v;
1707
+ localeWatchers.forEach(watch => watch(v));
1708
+ }
1709
+ });
1710
+ }
1599
1711
 
1600
- // 兼容旧版本 globalData
1601
- appOptions.globalData = vm.$options.globalData || {};
1602
- // 将 methods 中的方法挂在 getApp() 中
1603
- const methods = vm.$options.methods;
1604
- if (methods) {
1605
- Object.keys(methods).forEach(name => {
1606
- appOptions[name] = methods[name];
1607
- });
1608
- }
1609
-
1610
- initHooks(appOptions, hooks);
1712
+ function isEnableLocale () {
1713
+ return typeof __uniConfig !== 'undefined' && __uniConfig.locales && !!Object.keys(__uniConfig.locales).length
1714
+ }
1611
1715
 
1612
- return appOptions
1716
+ // export function initI18n() {
1717
+ // const localeKeys = Object.keys(__uniConfig.locales || {})
1718
+ // if (localeKeys.length) {
1719
+ // localeKeys.forEach((locale) =>
1720
+ // i18n.add(locale, __uniConfig.locales[locale])
1721
+ // )
1722
+ // }
1723
+ // }
1724
+
1725
+ const hooks = [
1726
+ 'onShow',
1727
+ 'onHide',
1728
+ 'onError',
1729
+ 'onPageNotFound',
1730
+ 'onThemeChange',
1731
+ 'onUnhandledRejection'
1732
+ ];
1733
+
1734
+ function initEventChannel$1 () {
1735
+ Vue.prototype.getOpenerEventChannel = function () {
1736
+ if (!this.__eventChannel__) {
1737
+ this.__eventChannel__ = new EventChannel();
1738
+ }
1739
+ return this.__eventChannel__
1740
+ };
1741
+ const callHook = Vue.prototype.__call_hook;
1742
+ Vue.prototype.__call_hook = function (hook, args) {
1743
+ if (hook === 'onLoad' && args && args.__id__) {
1744
+ this.__eventChannel__ = getEventChannel(args.__id__);
1745
+ delete args.__id__;
1746
+ }
1747
+ return callHook.call(this, hook, args)
1748
+ };
1749
+ }
1750
+
1751
+ function initScopedSlotsParams () {
1752
+ const center = {};
1753
+ const parents = {};
1754
+
1755
+ Vue.prototype.$hasScopedSlotsParams = function (vueId) {
1756
+ const has = center[vueId];
1757
+ if (!has) {
1758
+ parents[vueId] = this;
1759
+ this.$on('hook:destroyed', () => {
1760
+ delete parents[vueId];
1761
+ });
1762
+ }
1763
+ return has
1764
+ };
1765
+
1766
+ Vue.prototype.$getScopedSlotsParams = function (vueId, name, key) {
1767
+ const data = center[vueId];
1768
+ if (data) {
1769
+ const object = data[name] || {};
1770
+ return key ? object[key] : object
1771
+ } else {
1772
+ parents[vueId] = this;
1773
+ this.$on('hook:destroyed', () => {
1774
+ delete parents[vueId];
1775
+ });
1776
+ }
1777
+ };
1778
+
1779
+ Vue.prototype.$setScopedSlotsParams = function (name, value) {
1780
+ const vueIds = this.$options.propsData.vueId;
1781
+ if (vueIds) {
1782
+ const vueId = vueIds.split(',')[0];
1783
+ const object = center[vueId] = center[vueId] || {};
1784
+ object[name] = value;
1785
+ if (parents[vueId]) {
1786
+ parents[vueId].$forceUpdate();
1787
+ }
1788
+ }
1789
+ };
1790
+
1791
+ Vue.mixin({
1792
+ destroyed () {
1793
+ const propsData = this.$options.propsData;
1794
+ const vueId = propsData && propsData.vueId;
1795
+ if (vueId) {
1796
+ delete center[vueId];
1797
+ delete parents[vueId];
1798
+ }
1799
+ }
1800
+ });
1801
+ }
1802
+
1803
+ function parseBaseApp (vm, {
1804
+ mocks,
1805
+ initRefs
1806
+ }) {
1807
+ initEventChannel$1();
1808
+ {
1809
+ initScopedSlotsParams();
1810
+ }
1811
+ if (vm.$options.store) {
1812
+ Vue.prototype.$store = vm.$options.store;
1813
+ }
1814
+ uniIdMixin(Vue);
1815
+
1816
+ Vue.prototype.mpHost = "mp-qq";
1817
+
1818
+ Vue.mixin({
1819
+ beforeCreate () {
1820
+ if (!this.$options.mpType) {
1821
+ return
1822
+ }
1823
+
1824
+ this.mpType = this.$options.mpType;
1825
+
1826
+ this.$mp = {
1827
+ data: {},
1828
+ [this.mpType]: this.$options.mpInstance
1829
+ };
1830
+
1831
+ this.$scope = this.$options.mpInstance;
1832
+
1833
+ delete this.$options.mpType;
1834
+ delete this.$options.mpInstance;
1835
+ if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1836
+ const app = getApp();
1837
+ if (app.$vm && app.$vm.$i18n) {
1838
+ this._i18n = app.$vm.$i18n;
1839
+ }
1840
+ }
1841
+ if (this.mpType !== 'app') {
1842
+ initRefs(this);
1843
+ initMocks(this, mocks);
1844
+ }
1845
+ }
1846
+ });
1847
+
1848
+ const appOptions = {
1849
+ onLaunch (args) {
1850
+ if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1851
+ return
1852
+ }
1853
+ {
1854
+ if (wx.canIUse && !wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断
1855
+ console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上');
1856
+ }
1857
+ }
1858
+
1859
+ this.$vm = vm;
1860
+
1861
+ this.$vm.$mp = {
1862
+ app: this
1863
+ };
1864
+
1865
+ this.$vm.$scope = this;
1866
+ // vm 上也挂载 globalData
1867
+ this.$vm.globalData = this.globalData;
1868
+
1869
+ this.$vm._isMounted = true;
1870
+ this.$vm.__call_hook('mounted', args);
1871
+
1872
+ this.$vm.__call_hook('onLaunch', args);
1873
+ }
1874
+ };
1875
+
1876
+ // 兼容旧版本 globalData
1877
+ appOptions.globalData = vm.$options.globalData || {};
1878
+ // 将 methods 中的方法挂在 getApp() 中
1879
+ const methods = vm.$options.methods;
1880
+ if (methods) {
1881
+ Object.keys(methods).forEach(name => {
1882
+ appOptions[name] = methods[name];
1883
+ });
1884
+ }
1885
+
1886
+ initAppLocale(Vue, vm, wx.getSystemInfoSync().language || 'zh-Hans');
1887
+
1888
+ initHooks(appOptions, hooks);
1889
+
1890
+ return appOptions
1613
1891
  }
1614
1892
 
1615
1893
  const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'];
@@ -1919,6 +2197,7 @@ function createSubpackageApp (vm) {
1919
2197
  const app = getApp({
1920
2198
  allowDefault: true
1921
2199
  });
2200
+ vm.$scope = app;
1922
2201
  const globalData = app.globalData;
1923
2202
  if (globalData) {
1924
2203
  Object.keys(appOptions.globalData).forEach(name => {
@@ -1933,18 +2212,18 @@ function createSubpackageApp (vm) {
1933
2212
  }
1934
2213
  });
1935
2214
  if (isFn(appOptions.onShow) && wx.onAppShow) {
1936
- wx.onAppShow((...args) => {
1937
- appOptions.onShow.apply(app, args);
2215
+ wx.onAppShow((...args) => {
2216
+ vm.__call_hook('onShow', args);
1938
2217
  });
1939
2218
  }
1940
2219
  if (isFn(appOptions.onHide) && wx.onAppHide) {
1941
- wx.onAppHide((...args) => {
1942
- appOptions.onHide.apply(app, args);
2220
+ wx.onAppHide((...args) => {
2221
+ vm.__call_hook('onHide', args);
1943
2222
  });
1944
2223
  }
1945
2224
  if (isFn(appOptions.onLaunch)) {
1946
2225
  const args = wx.getLaunchOptionsSync && wx.getLaunchOptionsSync();
1947
- appOptions.onLaunch.call(app, args);
2226
+ vm.__call_hook('onLaunch', args);
1948
2227
  }
1949
2228
  return vm
1950
2229
  }