@dcloudio/uni-mp-weixin 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 = ( 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$|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
@@ -670,18 +794,13 @@ const customize = cached((str) => {
670
794
  });
671
795
 
672
796
  function initTriggerEvent (mpInstance) {
673
- {
674
- if (!wx.canIUse || !wx.canIUse('nextTick')) {
675
- return
676
- }
677
- }
678
797
  const oldTriggerEvent = mpInstance.triggerEvent;
679
798
  mpInstance.triggerEvent = function (event, ...args) {
680
799
  return oldTriggerEvent.apply(mpInstance, [customize(event), ...args])
681
800
  };
682
801
  }
683
802
 
684
- function initHook (name, options) {
803
+ function initHook (name, options, isComponent) {
685
804
  const oldHook = options[name];
686
805
  if (!oldHook) {
687
806
  options[name] = function () {
@@ -919,6 +1038,11 @@ function initProperties (props, isBehavior = false, file = '') {
919
1038
  type: Object,
920
1039
  value: null
921
1040
  };
1041
+ // scopedSlotsCompiler auto
1042
+ properties.scopedSlotsCompiler = {
1043
+ type: String,
1044
+ value: ''
1045
+ };
922
1046
  properties.vueSlots = { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
923
1047
  type: null,
924
1048
  value: [],
@@ -1246,6 +1370,54 @@ function handleEvent (event) {
1246
1370
  }
1247
1371
  }
1248
1372
 
1373
+ let locale;
1374
+
1375
+ {
1376
+ locale = wx.getSystemInfoSync().language;
1377
+ }
1378
+
1379
+ const i18n = initVueI18n(
1380
+ locale,
1381
+ {}
1382
+ );
1383
+ const t = i18n.t;
1384
+ const i18nMixin = (i18n.mixin = {
1385
+ beforeCreate () {
1386
+ const unwatch = i18n.i18n.watchLocale(() => {
1387
+ this.$forceUpdate();
1388
+ });
1389
+ this.$once('hook:beforeDestroy', function () {
1390
+ unwatch();
1391
+ });
1392
+ },
1393
+ methods: {
1394
+ $$t (key, values) {
1395
+ return t(key, values)
1396
+ }
1397
+ }
1398
+ });
1399
+ const setLocale$1 = i18n.setLocale;
1400
+ const getLocale$1 = i18n.getLocale;
1401
+
1402
+ function initAppLocale (Vue, appVm, locale) {
1403
+ const state = Vue.observable({
1404
+ locale: locale || i18n.getLocale()
1405
+ });
1406
+ const localeWatchers = [];
1407
+ appVm.$watchLocale = fn => {
1408
+ localeWatchers.push(fn);
1409
+ };
1410
+ Object.defineProperty(appVm, '$locale', {
1411
+ get () {
1412
+ return state.locale
1413
+ },
1414
+ set (v) {
1415
+ state.locale = v;
1416
+ localeWatchers.forEach(watch => watch(v));
1417
+ }
1418
+ });
1419
+ }
1420
+
1249
1421
  const eventChannels = {};
1250
1422
 
1251
1423
  const eventChannelStack = [];
@@ -1259,114 +1431,172 @@ function getEventChannel (id) {
1259
1431
  return eventChannelStack.shift()
1260
1432
  }
1261
1433
 
1262
- const hooks = [
1263
- 'onShow',
1264
- 'onHide',
1265
- 'onError',
1266
- 'onPageNotFound',
1267
- 'onThemeChange',
1268
- 'onUnhandledRejection'
1269
- ];
1270
-
1271
- function initEventChannel () {
1272
- Vue.prototype.getOpenerEventChannel = function () {
1273
- // 微信小程序使用自身getOpenerEventChannel
1274
- {
1275
- return this.$scope.getOpenerEventChannel()
1276
- }
1277
- };
1278
- const callHook = Vue.prototype.__call_hook;
1279
- Vue.prototype.__call_hook = function (hook, args) {
1280
- if (hook === 'onLoad' && args && args.__id__) {
1281
- this.__eventChannel__ = getEventChannel(args.__id__);
1282
- delete args.__id__;
1283
- }
1284
- return callHook.call(this, hook, args)
1285
- };
1286
- }
1287
-
1288
- function parseBaseApp (vm, {
1289
- mocks,
1290
- initRefs
1291
- }) {
1292
- initEventChannel();
1293
- if (vm.$options.store) {
1294
- Vue.prototype.$store = vm.$options.store;
1295
- }
1296
-
1297
- Vue.prototype.mpHost = "mp-weixin";
1298
-
1299
- Vue.mixin({
1300
- beforeCreate () {
1301
- if (!this.$options.mpType) {
1302
- return
1303
- }
1304
-
1305
- this.mpType = this.$options.mpType;
1306
-
1307
- this.$mp = {
1308
- data: {},
1309
- [this.mpType]: this.$options.mpInstance
1310
- };
1311
-
1312
- this.$scope = this.$options.mpInstance;
1313
-
1314
- delete this.$options.mpType;
1315
- delete this.$options.mpInstance;
1316
- if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1317
- const app = getApp();
1318
- if (app.$vm && app.$vm.$i18n) {
1319
- this._i18n = app.$vm.$i18n;
1320
- }
1321
- }
1322
- if (this.mpType !== 'app') {
1323
- initRefs(this);
1324
- initMocks(this, mocks);
1325
- }
1326
- }
1327
- });
1328
-
1329
- const appOptions = {
1330
- onLaunch (args) {
1331
- if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1332
- return
1333
- }
1334
- {
1335
- if (wx.canIUse && !wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断
1336
- console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上');
1337
- }
1338
- }
1339
-
1340
- this.$vm = vm;
1341
-
1342
- this.$vm.$mp = {
1343
- app: this
1344
- };
1345
-
1346
- this.$vm.$scope = this;
1347
- // vm 上也挂载 globalData
1348
- this.$vm.globalData = this.globalData;
1349
-
1350
- this.$vm._isMounted = true;
1351
- this.$vm.__call_hook('mounted', args);
1352
-
1353
- this.$vm.__call_hook('onLaunch', args);
1354
- }
1355
- };
1356
-
1357
- // 兼容旧版本 globalData
1358
- appOptions.globalData = vm.$options.globalData || {};
1359
- // 将 methods 中的方法挂在 getApp() 中
1360
- const methods = vm.$options.methods;
1361
- if (methods) {
1362
- Object.keys(methods).forEach(name => {
1363
- appOptions[name] = methods[name];
1364
- });
1365
- }
1366
-
1367
- initHooks(appOptions, hooks);
1368
-
1369
- return appOptions
1434
+ const hooks = [
1435
+ 'onShow',
1436
+ 'onHide',
1437
+ 'onError',
1438
+ 'onPageNotFound',
1439
+ 'onThemeChange',
1440
+ 'onUnhandledRejection'
1441
+ ];
1442
+
1443
+ function initEventChannel () {
1444
+ Vue.prototype.getOpenerEventChannel = function () {
1445
+ // 微信小程序使用自身getOpenerEventChannel
1446
+ {
1447
+ return this.$scope.getOpenerEventChannel()
1448
+ }
1449
+ };
1450
+ const callHook = Vue.prototype.__call_hook;
1451
+ Vue.prototype.__call_hook = function (hook, args) {
1452
+ if (hook === 'onLoad' && args && args.__id__) {
1453
+ this.__eventChannel__ = getEventChannel(args.__id__);
1454
+ delete args.__id__;
1455
+ }
1456
+ return callHook.call(this, hook, args)
1457
+ };
1458
+ }
1459
+
1460
+ function initScopedSlotsParams () {
1461
+ const center = {};
1462
+ const parents = {};
1463
+
1464
+ Vue.prototype.$hasScopedSlotsParams = function (vueId) {
1465
+ const has = center[vueId];
1466
+ if (!has) {
1467
+ parents[vueId] = this;
1468
+ this.$on('hook:destroyed', () => {
1469
+ delete parents[vueId];
1470
+ });
1471
+ }
1472
+ return has
1473
+ };
1474
+
1475
+ Vue.prototype.$getScopedSlotsParams = function (vueId, name, key) {
1476
+ const data = center[vueId];
1477
+ if (data) {
1478
+ const object = data[name] || {};
1479
+ return key ? object[key] : object
1480
+ } else {
1481
+ parents[vueId] = this;
1482
+ this.$on('hook:destroyed', () => {
1483
+ delete parents[vueId];
1484
+ });
1485
+ }
1486
+ };
1487
+
1488
+ Vue.prototype.$setScopedSlotsParams = function (name, value) {
1489
+ const vueIds = this.$options.propsData.vueId;
1490
+ if (vueIds) {
1491
+ const vueId = vueIds.split(',')[0];
1492
+ const object = center[vueId] = center[vueId] || {};
1493
+ object[name] = value;
1494
+ if (parents[vueId]) {
1495
+ parents[vueId].$forceUpdate();
1496
+ }
1497
+ }
1498
+ };
1499
+
1500
+ Vue.mixin({
1501
+ destroyed () {
1502
+ const propsData = this.$options.propsData;
1503
+ const vueId = propsData && propsData.vueId;
1504
+ if (vueId) {
1505
+ delete center[vueId];
1506
+ delete parents[vueId];
1507
+ }
1508
+ }
1509
+ });
1510
+ }
1511
+
1512
+ function parseBaseApp (vm, {
1513
+ mocks,
1514
+ initRefs
1515
+ }) {
1516
+ initEventChannel();
1517
+ {
1518
+ initScopedSlotsParams();
1519
+ }
1520
+ if (vm.$options.store) {
1521
+ Vue.prototype.$store = vm.$options.store;
1522
+ }
1523
+ uniIdMixin(Vue);
1524
+
1525
+ Vue.prototype.mpHost = "mp-weixin";
1526
+
1527
+ Vue.mixin({
1528
+ beforeCreate () {
1529
+ if (!this.$options.mpType) {
1530
+ return
1531
+ }
1532
+
1533
+ this.mpType = this.$options.mpType;
1534
+
1535
+ this.$mp = {
1536
+ data: {},
1537
+ [this.mpType]: this.$options.mpInstance
1538
+ };
1539
+
1540
+ this.$scope = this.$options.mpInstance;
1541
+
1542
+ delete this.$options.mpType;
1543
+ delete this.$options.mpInstance;
1544
+ if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1545
+ const app = getApp();
1546
+ if (app.$vm && app.$vm.$i18n) {
1547
+ this._i18n = app.$vm.$i18n;
1548
+ }
1549
+ }
1550
+ if (this.mpType !== 'app') {
1551
+ initRefs(this);
1552
+ initMocks(this, mocks);
1553
+ }
1554
+ }
1555
+ });
1556
+
1557
+ const appOptions = {
1558
+ onLaunch (args) {
1559
+ if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1560
+ return
1561
+ }
1562
+ {
1563
+ if (wx.canIUse && !wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断
1564
+ console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上');
1565
+ }
1566
+ }
1567
+
1568
+ this.$vm = vm;
1569
+
1570
+ this.$vm.$mp = {
1571
+ app: this
1572
+ };
1573
+
1574
+ this.$vm.$scope = this;
1575
+ // vm 上也挂载 globalData
1576
+ this.$vm.globalData = this.globalData;
1577
+
1578
+ this.$vm._isMounted = true;
1579
+ this.$vm.__call_hook('mounted', args);
1580
+
1581
+ this.$vm.__call_hook('onLaunch', args);
1582
+ }
1583
+ };
1584
+
1585
+ // 兼容旧版本 globalData
1586
+ appOptions.globalData = vm.$options.globalData || {};
1587
+ // 将 methods 中的方法挂在 getApp() 中
1588
+ const methods = vm.$options.methods;
1589
+ if (methods) {
1590
+ Object.keys(methods).forEach(name => {
1591
+ appOptions[name] = methods[name];
1592
+ });
1593
+ }
1594
+
1595
+ initAppLocale(Vue, vm, wx.getSystemInfoSync().language || 'zh-Hans');
1596
+
1597
+ initHooks(appOptions, hooks);
1598
+
1599
+ return appOptions
1370
1600
  }
1371
1601
 
1372
1602
  const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'];
@@ -1671,6 +1901,7 @@ function createSubpackageApp (vm) {
1671
1901
  const app = getApp({
1672
1902
  allowDefault: true
1673
1903
  });
1904
+ vm.$scope = app;
1674
1905
  const globalData = app.globalData;
1675
1906
  if (globalData) {
1676
1907
  Object.keys(appOptions.globalData).forEach(name => {
@@ -1685,18 +1916,18 @@ function createSubpackageApp (vm) {
1685
1916
  }
1686
1917
  });
1687
1918
  if (isFn(appOptions.onShow) && wx.onAppShow) {
1688
- wx.onAppShow((...args) => {
1689
- appOptions.onShow.apply(app, args);
1919
+ wx.onAppShow((...args) => {
1920
+ vm.__call_hook('onShow', args);
1690
1921
  });
1691
1922
  }
1692
1923
  if (isFn(appOptions.onHide) && wx.onAppHide) {
1693
- wx.onAppHide((...args) => {
1694
- appOptions.onHide.apply(app, args);
1924
+ wx.onAppHide((...args) => {
1925
+ vm.__call_hook('onHide', args);
1695
1926
  });
1696
1927
  }
1697
1928
  if (isFn(appOptions.onLaunch)) {
1698
1929
  const args = wx.getLaunchOptionsSync && wx.getLaunchOptionsSync();
1699
- appOptions.onLaunch.call(app, args);
1930
+ vm.__call_hook('onLaunch', args);
1700
1931
  }
1701
1932
  return vm
1702
1933
  }