@dcloudio/uni-mp-alipay 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 = ( my).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 my.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
@@ -800,8 +924,17 @@ const protocols = { // 需要做转换的 API 列表
800
924
  // TODO 有没有返回值还需要测试下
801
925
  },
802
926
  chooseImage: {
803
- returnValue: {
804
- apFilePaths: 'tempFilePaths'
927
+ returnValue (result) {
928
+ const hasTempFilePaths = hasOwn(result, 'tempFilePaths') && result.tempFilePaths;
929
+ if (hasOwn(result, 'apFilePaths') && !hasTempFilePaths) {
930
+ result.tempFilePaths = result.apFilePaths;
931
+ delete result.apFilePaths;
932
+ }
933
+ if (!hasOwn(result, 'tempFiles') && hasTempFilePaths) {
934
+ result.tempFiles = [];
935
+ result.tempFilePaths.forEach(tempFilePath => result.tempFiles.push({ path: tempFilePath }));
936
+ }
937
+ return {}
805
938
  }
806
939
  },
807
940
  previewImage: {
@@ -930,6 +1063,23 @@ const protocols = { // 需要做转换的 API 列表
930
1063
  };
931
1064
  }
932
1065
  },
1066
+ getUserProfile: {
1067
+ name: my.canIUse('getOpenUserInfo') ? 'getOpenUserInfo' : 'getAuthUserInfo',
1068
+ returnValue (result) {
1069
+ if (my.canIUse('getOpenUserInfo')) {
1070
+ let response = {};
1071
+ try {
1072
+ response = JSON.parse(result.response).response;
1073
+ } catch (e) {}
1074
+ result.nickName = response.nickName;
1075
+ result.avatar = response.avatar;
1076
+ }
1077
+ result.userInfo = {
1078
+ nickName: result.nickName,
1079
+ avatarUrl: result.avatar
1080
+ };
1081
+ }
1082
+ },
933
1083
  requestPayment: {
934
1084
  name: 'tradePay',
935
1085
  args: {
@@ -1569,6 +1719,11 @@ function initProperties (props, isBehavior = false, file = '') {
1569
1719
  type: Object,
1570
1720
  value: null
1571
1721
  };
1722
+ // scopedSlotsCompiler auto
1723
+ properties.scopedSlotsCompiler = {
1724
+ type: String,
1725
+ value: ''
1726
+ };
1572
1727
  properties.vueSlots = { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
1573
1728
  type: null,
1574
1729
  value: [],
@@ -1896,109 +2051,219 @@ function handleEvent (event) {
1896
2051
  }
1897
2052
  }
1898
2053
 
1899
- const hooks = [
1900
- 'onShow',
1901
- 'onHide',
1902
- 'onError',
1903
- 'onPageNotFound',
1904
- 'onThemeChange',
1905
- 'onUnhandledRejection'
1906
- ];
2054
+ let locale;
1907
2055
 
1908
- function initEventChannel$1 () {
1909
- Vue.prototype.getOpenerEventChannel = function () {
1910
- if (!this.__eventChannel__) {
1911
- this.__eventChannel__ = new EventChannel();
1912
- }
1913
- return this.__eventChannel__
1914
- };
1915
- const callHook = Vue.prototype.__call_hook;
1916
- Vue.prototype.__call_hook = function (hook, args) {
1917
- if (hook === 'onLoad' && args && args.__id__) {
1918
- this.__eventChannel__ = getEventChannel(args.__id__);
1919
- delete args.__id__;
1920
- }
1921
- return callHook.call(this, hook, args)
1922
- };
2056
+ {
2057
+ locale = my.getSystemInfoSync().language;
1923
2058
  }
1924
2059
 
1925
- function parseBaseApp (vm, {
1926
- mocks,
1927
- initRefs
1928
- }) {
1929
- initEventChannel$1();
1930
- if (vm.$options.store) {
1931
- Vue.prototype.$store = vm.$options.store;
2060
+ const i18n = initVueI18n(
2061
+ locale,
2062
+ {}
2063
+ );
2064
+ const t = i18n.t;
2065
+ const i18nMixin = (i18n.mixin = {
2066
+ beforeCreate () {
2067
+ const unwatch = i18n.i18n.watchLocale(() => {
2068
+ this.$forceUpdate();
2069
+ });
2070
+ this.$once('hook:beforeDestroy', function () {
2071
+ unwatch();
2072
+ });
2073
+ },
2074
+ methods: {
2075
+ $$t (key, values) {
2076
+ return t(key, values)
2077
+ }
1932
2078
  }
2079
+ });
2080
+ const setLocale$1 = i18n.setLocale;
2081
+ const getLocale$1 = i18n.getLocale;
1933
2082
 
1934
- Vue.prototype.mpHost = "mp-alipay";
1935
-
1936
- Vue.mixin({
1937
- beforeCreate () {
1938
- if (!this.$options.mpType) {
1939
- return
1940
- }
1941
-
1942
- this.mpType = this.$options.mpType;
1943
-
1944
- this.$mp = {
1945
- data: {},
1946
- [this.mpType]: this.$options.mpInstance
1947
- };
1948
-
1949
- this.$scope = this.$options.mpInstance;
1950
-
1951
- delete this.$options.mpType;
1952
- delete this.$options.mpInstance;
1953
- if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
1954
- const app = getApp();
1955
- if (app.$vm && app.$vm.$i18n) {
1956
- this._i18n = app.$vm.$i18n;
1957
- }
1958
- }
1959
- if (this.mpType !== 'app') {
1960
- initRefs(this);
1961
- initMocks(this, mocks);
1962
- }
1963
- }
2083
+ function initAppLocale (Vue, appVm, locale) {
2084
+ const state = Vue.observable({
2085
+ locale: locale || i18n.getLocale()
1964
2086
  });
1965
-
1966
- const appOptions = {
1967
- onLaunch (args) {
1968
- if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
1969
- return
1970
- }
1971
-
1972
- this.$vm = vm;
1973
-
1974
- this.$vm.$mp = {
1975
- app: this
1976
- };
1977
-
1978
- this.$vm.$scope = this;
1979
- // vm 上也挂载 globalData
1980
- this.$vm.globalData = this.globalData;
1981
-
1982
- this.$vm._isMounted = true;
1983
- this.$vm.__call_hook('mounted', args);
1984
-
1985
- this.$vm.__call_hook('onLaunch', args);
1986
- }
2087
+ const localeWatchers = [];
2088
+ appVm.$watchLocale = fn => {
2089
+ localeWatchers.push(fn);
1987
2090
  };
1988
-
1989
- // 兼容旧版本 globalData
1990
- appOptions.globalData = vm.$options.globalData || {};
1991
- // 将 methods 中的方法挂在 getApp() 中
1992
- const methods = vm.$options.methods;
1993
- if (methods) {
1994
- Object.keys(methods).forEach(name => {
1995
- appOptions[name] = methods[name];
1996
- });
1997
- }
1998
-
1999
- initHooks(appOptions, hooks);
2000
-
2001
- return appOptions
2091
+ Object.defineProperty(appVm, '$locale', {
2092
+ get () {
2093
+ return state.locale
2094
+ },
2095
+ set (v) {
2096
+ state.locale = v;
2097
+ localeWatchers.forEach(watch => watch(v));
2098
+ }
2099
+ });
2100
+ }
2101
+
2102
+ const hooks = [
2103
+ 'onShow',
2104
+ 'onHide',
2105
+ 'onError',
2106
+ 'onPageNotFound',
2107
+ 'onThemeChange',
2108
+ 'onUnhandledRejection'
2109
+ ];
2110
+
2111
+ {
2112
+ hooks.push('onShareAppMessage');
2113
+ }
2114
+
2115
+ function initEventChannel$1 () {
2116
+ Vue.prototype.getOpenerEventChannel = function () {
2117
+ if (!this.__eventChannel__) {
2118
+ this.__eventChannel__ = new EventChannel();
2119
+ }
2120
+ return this.__eventChannel__
2121
+ };
2122
+ const callHook = Vue.prototype.__call_hook;
2123
+ Vue.prototype.__call_hook = function (hook, args) {
2124
+ if (hook === 'onLoad' && args && args.__id__) {
2125
+ this.__eventChannel__ = getEventChannel(args.__id__);
2126
+ delete args.__id__;
2127
+ }
2128
+ return callHook.call(this, hook, args)
2129
+ };
2130
+ }
2131
+
2132
+ function initScopedSlotsParams () {
2133
+ const center = {};
2134
+ const parents = {};
2135
+
2136
+ Vue.prototype.$hasScopedSlotsParams = function (vueId) {
2137
+ const has = center[vueId];
2138
+ if (!has) {
2139
+ parents[vueId] = this;
2140
+ this.$on('hook:destroyed', () => {
2141
+ delete parents[vueId];
2142
+ });
2143
+ }
2144
+ return has
2145
+ };
2146
+
2147
+ Vue.prototype.$getScopedSlotsParams = function (vueId, name, key) {
2148
+ const data = center[vueId];
2149
+ if (data) {
2150
+ const object = data[name] || {};
2151
+ return key ? object[key] : object
2152
+ } else {
2153
+ parents[vueId] = this;
2154
+ this.$on('hook:destroyed', () => {
2155
+ delete parents[vueId];
2156
+ });
2157
+ }
2158
+ };
2159
+
2160
+ Vue.prototype.$setScopedSlotsParams = function (name, value) {
2161
+ const vueIds = this.$options.propsData.vueId;
2162
+ if (vueIds) {
2163
+ const vueId = vueIds.split(',')[0];
2164
+ const object = center[vueId] = center[vueId] || {};
2165
+ object[name] = value;
2166
+ if (parents[vueId]) {
2167
+ parents[vueId].$forceUpdate();
2168
+ }
2169
+ }
2170
+ };
2171
+
2172
+ Vue.mixin({
2173
+ destroyed () {
2174
+ const propsData = this.$options.propsData;
2175
+ const vueId = propsData && propsData.vueId;
2176
+ if (vueId) {
2177
+ delete center[vueId];
2178
+ delete parents[vueId];
2179
+ }
2180
+ }
2181
+ });
2182
+ }
2183
+
2184
+ function parseBaseApp (vm, {
2185
+ mocks,
2186
+ initRefs
2187
+ }) {
2188
+ initEventChannel$1();
2189
+ {
2190
+ initScopedSlotsParams();
2191
+ }
2192
+ if (vm.$options.store) {
2193
+ Vue.prototype.$store = vm.$options.store;
2194
+ }
2195
+ uniIdMixin(Vue);
2196
+
2197
+ Vue.prototype.mpHost = "mp-alipay";
2198
+
2199
+ Vue.mixin({
2200
+ beforeCreate () {
2201
+ if (!this.$options.mpType) {
2202
+ return
2203
+ }
2204
+
2205
+ this.mpType = this.$options.mpType;
2206
+
2207
+ this.$mp = {
2208
+ data: {},
2209
+ [this.mpType]: this.$options.mpInstance
2210
+ };
2211
+
2212
+ this.$scope = this.$options.mpInstance;
2213
+
2214
+ delete this.$options.mpType;
2215
+ delete this.$options.mpInstance;
2216
+ if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
2217
+ const app = getApp();
2218
+ if (app.$vm && app.$vm.$i18n) {
2219
+ this._i18n = app.$vm.$i18n;
2220
+ }
2221
+ }
2222
+ if (this.mpType !== 'app') {
2223
+ initRefs(this);
2224
+ initMocks(this, mocks);
2225
+ }
2226
+ }
2227
+ });
2228
+
2229
+ const appOptions = {
2230
+ onLaunch (args) {
2231
+ if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
2232
+ return
2233
+ }
2234
+
2235
+ this.$vm = vm;
2236
+
2237
+ this.$vm.$mp = {
2238
+ app: this
2239
+ };
2240
+
2241
+ this.$vm.$scope = this;
2242
+ // vm 上也挂载 globalData
2243
+ this.$vm.globalData = this.globalData;
2244
+
2245
+ this.$vm._isMounted = true;
2246
+ this.$vm.__call_hook('mounted', args);
2247
+
2248
+ this.$vm.__call_hook('onLaunch', args);
2249
+ }
2250
+ };
2251
+
2252
+ // 兼容旧版本 globalData
2253
+ appOptions.globalData = vm.$options.globalData || {};
2254
+ // 将 methods 中的方法挂在 getApp() 中
2255
+ const methods = vm.$options.methods;
2256
+ if (methods) {
2257
+ Object.keys(methods).forEach(name => {
2258
+ appOptions[name] = methods[name];
2259
+ });
2260
+ }
2261
+
2262
+ initAppLocale(Vue, vm, my.getSystemInfoSync().language || 'zh-Hans');
2263
+
2264
+ initHooks(appOptions, hooks);
2265
+
2266
+ return appOptions
2002
2267
  }
2003
2268
 
2004
2269
  function findVmByVueId (vm, vuePid) {
@@ -2598,6 +2863,7 @@ function createSubpackageApp (vm) {
2598
2863
  const app = getApp({
2599
2864
  allowDefault: true
2600
2865
  });
2866
+ vm.$scope = app;
2601
2867
  const globalData = app.globalData;
2602
2868
  if (globalData) {
2603
2869
  Object.keys(appOptions.globalData).forEach(name => {
@@ -2612,18 +2878,18 @@ function createSubpackageApp (vm) {
2612
2878
  }
2613
2879
  });
2614
2880
  if (isFn(appOptions.onShow) && my.onAppShow) {
2615
- my.onAppShow((...args) => {
2616
- appOptions.onShow.apply(app, args);
2881
+ my.onAppShow((...args) => {
2882
+ vm.__call_hook('onShow', args);
2617
2883
  });
2618
2884
  }
2619
2885
  if (isFn(appOptions.onHide) && my.onAppHide) {
2620
- my.onAppHide((...args) => {
2621
- appOptions.onHide.apply(app, args);
2886
+ my.onAppHide((...args) => {
2887
+ vm.__call_hook('onHide', args);
2622
2888
  });
2623
2889
  }
2624
2890
  if (isFn(appOptions.onLaunch)) {
2625
2891
  const args = my.getLaunchOptionsSync && my.getLaunchOptionsSync();
2626
- appOptions.onLaunch.call(app, args);
2892
+ vm.__call_hook('onLaunch', args);
2627
2893
  }
2628
2894
  return vm
2629
2895
  }