@dcloudio/uni-mp-baidu 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.
@@ -1,4 +1,329 @@
1
- import { isArray, hasOwn, toNumber, isPlainObject, isObject, isFunction, extend, NOOP, camelize } from '@vue/shared';
1
+ import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared';
2
+ import { onUnmounted, injectHook, ref } from 'vue';
3
+
4
+ const encode = encodeURIComponent;
5
+ function stringifyQuery(obj, encodeStr = encode) {
6
+ const res = obj
7
+ ? Object.keys(obj)
8
+ .map((key) => {
9
+ let val = obj[key];
10
+ if (typeof val === undefined || val === null) {
11
+ val = '';
12
+ }
13
+ else if (isPlainObject(val)) {
14
+ val = JSON.stringify(val);
15
+ }
16
+ return encodeStr(key) + '=' + encodeStr(val);
17
+ })
18
+ .filter((x) => x.length > 0)
19
+ .join('&')
20
+ : null;
21
+ return res ? `?${res}` : '';
22
+ }
23
+
24
+ function cache(fn) {
25
+ const cache = Object.create(null);
26
+ return (str) => {
27
+ const hit = cache[str];
28
+ return hit || (cache[str] = fn(str));
29
+ };
30
+ }
31
+ const invokeArrayFns = (fns, arg) => {
32
+ let ret;
33
+ for (let i = 0; i < fns.length; i++) {
34
+ ret = fns[i](arg);
35
+ }
36
+ return ret;
37
+ };
38
+ // lifecycle
39
+ // App and Page
40
+ const ON_SHOW = 'onShow';
41
+ const ON_HIDE = 'onHide';
42
+ //App
43
+ const ON_LAUNCH = 'onLaunch';
44
+ const ON_ERROR = 'onError';
45
+ const ON_THEME_CHANGE = 'onThemeChange';
46
+ const ON_PAGE_NOT_FOUND = 'onPageNotFound';
47
+ const ON_UNHANDLE_REJECTION = 'onUnhandledRejection';
48
+ //Page
49
+ const ON_LOAD = 'onLoad';
50
+ const ON_READY = 'onReady';
51
+ const ON_UNLOAD = 'onUnload';
52
+ const ON_RESIZE = 'onResize';
53
+ const ON_TAB_ITEM_TAP = 'onTabItemTap';
54
+ const ON_REACH_BOTTOM = 'onReachBottom';
55
+ const ON_PULL_DOWN_REFRESH = 'onPullDownRefresh';
56
+ const ON_ADD_TO_FAVORITES = 'onAddToFavorites';
57
+
58
+ class EventChannel {
59
+ constructor(id, events) {
60
+ this.id = id;
61
+ this.listener = {};
62
+ this.emitCache = {};
63
+ if (events) {
64
+ Object.keys(events).forEach((name) => {
65
+ this.on(name, events[name]);
66
+ });
67
+ }
68
+ }
69
+ emit(eventName, ...args) {
70
+ const fns = this.listener[eventName];
71
+ if (!fns) {
72
+ return (this.emitCache[eventName] || (this.emitCache[eventName] = [])).push(args);
73
+ }
74
+ fns.forEach((opt) => {
75
+ opt.fn.apply(opt.fn, args);
76
+ });
77
+ this.listener[eventName] = fns.filter((opt) => opt.type !== 'once');
78
+ }
79
+ on(eventName, fn) {
80
+ this._addListener(eventName, 'on', fn);
81
+ this._clearCache(eventName);
82
+ }
83
+ once(eventName, fn) {
84
+ this._addListener(eventName, 'once', fn);
85
+ this._clearCache(eventName);
86
+ }
87
+ off(eventName, fn) {
88
+ const fns = this.listener[eventName];
89
+ if (!fns) {
90
+ return;
91
+ }
92
+ if (fn) {
93
+ for (let i = 0; i < fns.length;) {
94
+ if (fns[i].fn === fn) {
95
+ fns.splice(i, 1);
96
+ i--;
97
+ }
98
+ i++;
99
+ }
100
+ }
101
+ else {
102
+ delete this.listener[eventName];
103
+ }
104
+ }
105
+ _clearCache(eventName) {
106
+ const cacheArgs = this.emitCache[eventName];
107
+ if (cacheArgs) {
108
+ for (; cacheArgs.length > 0;) {
109
+ this.emit.apply(this, [eventName, ...cacheArgs.shift()]);
110
+ }
111
+ }
112
+ }
113
+ _addListener(eventName, type, fn) {
114
+ (this.listener[eventName] || (this.listener[eventName] = [])).push({
115
+ fn,
116
+ type,
117
+ });
118
+ }
119
+ }
120
+
121
+ const eventChannels = {};
122
+ const eventChannelStack = [];
123
+ function getEventChannel(id) {
124
+ if (id) {
125
+ const eventChannel = eventChannels[id];
126
+ delete eventChannels[id];
127
+ return eventChannel;
128
+ }
129
+ return eventChannelStack.shift();
130
+ }
131
+
132
+ function initBehavior(options) {
133
+ return Behavior(options);
134
+ }
135
+ function initVueIds(vueIds, mpInstance) {
136
+ if (!vueIds) {
137
+ return;
138
+ }
139
+ const ids = vueIds.split(',');
140
+ const len = ids.length;
141
+ if (len === 1) {
142
+ mpInstance._$vueId = ids[0];
143
+ }
144
+ else if (len === 2) {
145
+ mpInstance._$vueId = ids[0];
146
+ mpInstance._$vuePid = ids[1];
147
+ }
148
+ }
149
+ const EXTRAS = ['externalClasses'];
150
+ function initExtraOptions(miniProgramComponentOptions, vueOptions) {
151
+ EXTRAS.forEach((name) => {
152
+ if (hasOwn(vueOptions, name)) {
153
+ miniProgramComponentOptions[name] = vueOptions[name];
154
+ }
155
+ });
156
+ }
157
+ function initWxsCallMethods(methods, wxsCallMethods) {
158
+ if (!isArray(wxsCallMethods)) {
159
+ return;
160
+ }
161
+ wxsCallMethods.forEach((callMethod) => {
162
+ methods[callMethod] = function (args) {
163
+ return this.$vm[callMethod](args);
164
+ };
165
+ });
166
+ }
167
+ function selectAllComponents(mpInstance, selector, $refs) {
168
+ const components = mpInstance.selectAllComponents(selector);
169
+ components.forEach((component) => {
170
+ const ref = component.dataset.ref;
171
+ $refs[ref] = component.$vm || component;
172
+ });
173
+ }
174
+ function initRefs(instance, mpInstance) {
175
+ Object.defineProperty(instance, 'refs', {
176
+ get() {
177
+ const $refs = {};
178
+ selectAllComponents(mpInstance, '.vue-ref', $refs);
179
+ const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
180
+ forComponents.forEach((component) => {
181
+ const ref = component.dataset.ref;
182
+ if (!$refs[ref]) {
183
+ $refs[ref] = [];
184
+ }
185
+ $refs[ref].push(component.$vm || component);
186
+ });
187
+ return $refs;
188
+ },
189
+ });
190
+ }
191
+ function findVmByVueId(instance, vuePid) {
192
+ // 标准 vue3 中 没有 $children,定制了内核
193
+ const $children = instance.$children;
194
+ // 优先查找直属(反向查找:https://github.com/dcloudio/uni-app/issues/1200)
195
+ for (let i = $children.length - 1; i >= 0; i--) {
196
+ const childVm = $children[i];
197
+ if (childVm.$scope._$vueId === vuePid) {
198
+ return childVm;
199
+ }
200
+ }
201
+ // 反向递归查找
202
+ let parentVm;
203
+ for (let i = $children.length - 1; i >= 0; i--) {
204
+ parentVm = findVmByVueId($children[i], vuePid);
205
+ if (parentVm) {
206
+ return parentVm;
207
+ }
208
+ }
209
+ }
210
+ function getTarget(obj, path) {
211
+ const parts = path.split('.');
212
+ let key = parts[0];
213
+ if (key.indexOf('__$n') === 0) {
214
+ //number index
215
+ key = parseInt(key.replace('__$n', ''));
216
+ }
217
+ if (!obj) {
218
+ obj = {};
219
+ }
220
+ if (parts.length === 1) {
221
+ return obj[key];
222
+ }
223
+ return getTarget(obj[key], parts.slice(1).join('.'));
224
+ }
225
+
226
+ function getValue(dataPath, target) {
227
+ return getTarget(target || this, dataPath);
228
+ }
229
+ function getClass(dynamicClass, staticClass) {
230
+ return renderClass(staticClass, dynamicClass);
231
+ }
232
+ function getStyle(dynamicStyle, staticStyle) {
233
+ if (!dynamicStyle && !staticStyle) {
234
+ return '';
235
+ }
236
+ var dynamicStyleObj = normalizeStyleBinding(dynamicStyle);
237
+ var styleObj = staticStyle
238
+ ? extend(staticStyle, dynamicStyleObj)
239
+ : dynamicStyleObj;
240
+ return Object.keys(styleObj)
241
+ .map(function (name) {
242
+ return hyphenate(name) + ':' + styleObj[name];
243
+ })
244
+ .join(';');
245
+ }
246
+ function toObject(arr) {
247
+ var res = {};
248
+ for (var i = 0; i < arr.length; i++) {
249
+ if (arr[i]) {
250
+ extend(res, arr[i]);
251
+ }
252
+ }
253
+ return res;
254
+ }
255
+ function normalizeStyleBinding(bindingStyle) {
256
+ if (Array.isArray(bindingStyle)) {
257
+ return toObject(bindingStyle);
258
+ }
259
+ if (typeof bindingStyle === 'string') {
260
+ return parseStyleText(bindingStyle);
261
+ }
262
+ return bindingStyle;
263
+ }
264
+ var parseStyleText = cache(function parseStyleText(cssText) {
265
+ var res = {};
266
+ var listDelimiter = /;(?![^(]*\))/g;
267
+ var propertyDelimiter = /:(.+)/;
268
+ cssText.split(listDelimiter).forEach(function (item) {
269
+ if (item) {
270
+ var tmp = item.split(propertyDelimiter);
271
+ tmp.length > 1 && (res[tmp[0].trim()] = tmp[1].trim());
272
+ }
273
+ });
274
+ return res;
275
+ });
276
+ function isDef(v) {
277
+ return v !== undefined && v !== null;
278
+ }
279
+ function renderClass(staticClass, dynamicClass) {
280
+ if (isDef(staticClass) || isDef(dynamicClass)) {
281
+ return concat(staticClass, stringifyClass(dynamicClass));
282
+ }
283
+ /* istanbul ignore next */
284
+ return '';
285
+ }
286
+ function concat(a, b) {
287
+ return a ? (b ? a + ' ' + b : a) : b || '';
288
+ }
289
+ function stringifyClass(value) {
290
+ if (Array.isArray(value)) {
291
+ return stringifyArray(value);
292
+ }
293
+ if (isObject(value)) {
294
+ return stringifyObject(value);
295
+ }
296
+ if (typeof value === 'string') {
297
+ return value;
298
+ }
299
+ /* istanbul ignore next */
300
+ return '';
301
+ }
302
+ function stringifyArray(value) {
303
+ var res = '';
304
+ var stringified;
305
+ for (var i = 0, l = value.length; i < l; i++) {
306
+ if (isDef((stringified = stringifyClass(value[i]))) && stringified !== '') {
307
+ if (res) {
308
+ res += ' ';
309
+ }
310
+ res += stringified;
311
+ }
312
+ }
313
+ return res;
314
+ }
315
+ function stringifyObject(value) {
316
+ var res = '';
317
+ for (var key in value) {
318
+ if (value[key]) {
319
+ if (res) {
320
+ res += ' ';
321
+ }
322
+ res += key;
323
+ }
324
+ }
325
+ return res;
326
+ }
2
327
 
3
328
  function setModel(target, key, value, modifiers) {
4
329
  if (isArray(modifiers)) {
@@ -50,7 +375,7 @@ const MP_METHODS = [
50
375
  'createSelectorQuery',
51
376
  'createIntersectionObserver',
52
377
  'selectAllComponents',
53
- 'selectComponent'
378
+ 'selectComponent',
54
379
  ];
55
380
  function createEmitFn(oldEmit, ctx) {
56
381
  return function emit(event, ...args) {
@@ -77,18 +402,29 @@ function initBaseInstance(instance, options) {
77
402
  {
78
403
  instance.slots = {};
79
404
  if (isArray(options.slots) && options.slots.length) {
80
- options.slots.forEach(name => {
405
+ options.slots.forEach((name) => {
81
406
  instance.slots[name] = true;
82
407
  });
83
408
  }
84
409
  }
410
+ ctx.getOpenerEventChannel = function () {
411
+ if (!this.__eventChannel__) {
412
+ this.__eventChannel__ = new EventChannel();
413
+ }
414
+ return this.__eventChannel__;
415
+ };
416
+ ctx.$hasHook = hasHook;
417
+ ctx.$callHook = callHook;
85
418
  // $emit
86
419
  instance.emit = createEmitFn(instance.emit, ctx);
87
420
  }
88
421
  function initComponentInstance(instance, options) {
89
422
  initBaseInstance(instance, options);
423
+ {
424
+ initScopedSlotsParams(instance);
425
+ }
90
426
  const ctx = instance.ctx;
91
- MP_METHODS.forEach(method => {
427
+ MP_METHODS.forEach((method) => {
92
428
  ctx[method] = function (...args) {
93
429
  const mpInstance = ctx.$scope;
94
430
  if (mpInstance && mpInstance[method]) {
@@ -101,36 +437,105 @@ function initComponentInstance(instance, options) {
101
437
  ctx.__set_sync = setSync;
102
438
  ctx.__get_orig = getOrig;
103
439
  // TODO
104
- // ctx.__get_style = getStyle
440
+ ctx.__get_value = getValue;
441
+ ctx.__get_class = getClass;
442
+ ctx.__get_style = getStyle;
105
443
  ctx.__map = map;
106
444
  }
107
445
  function initMocks(instance, mpInstance, mocks) {
108
446
  const ctx = instance.ctx;
109
- mocks.forEach(mock => {
447
+ mocks.forEach((mock) => {
110
448
  if (hasOwn(mpInstance, mock)) {
111
449
  ctx[mock] = mpInstance[mock];
112
450
  }
113
451
  });
452
+ }
453
+ function hasHook(name) {
454
+ const hooks = this.$[name];
455
+ if (hooks && hooks.length) {
456
+ return true;
457
+ }
458
+ return false;
459
+ }
460
+ function callHook(name, args) {
461
+ if (name === 'mounted') {
462
+ callHook.call(this, 'bm'); // beforeMount
463
+ this.$.isMounted = true;
464
+ name = 'm';
465
+ }
466
+ else if (name === 'onLoad' && args && args.__id__) {
467
+ this.__eventChannel__ = getEventChannel(args.__id__);
468
+ delete args.__id__;
469
+ }
470
+ const hooks = this.$[name];
471
+ return hooks && invokeArrayFns(hooks, args);
472
+ }
473
+ const center = {};
474
+ const parents = {};
475
+ function initScopedSlotsParams(instance) {
476
+ const ctx = instance.ctx;
477
+ ctx.$hasScopedSlotsParams = function (vueId) {
478
+ const has = center[vueId];
479
+ if (!has) {
480
+ parents[vueId] = this;
481
+ onUnmounted(() => {
482
+ delete parents[vueId];
483
+ }, instance);
484
+ }
485
+ return has;
486
+ };
487
+ ctx.$getScopedSlotsParams = function (vueId, name, key) {
488
+ const data = center[vueId];
489
+ if (data) {
490
+ const object = data[name] || {};
491
+ return key ? object[key] : object;
492
+ }
493
+ else {
494
+ parents[vueId] = this;
495
+ onUnmounted(() => {
496
+ delete parents[vueId];
497
+ }, instance);
498
+ }
499
+ };
500
+ ctx.$setScopedSlotsParams = function (name, value) {
501
+ const vueIds = instance.attrs.vueId;
502
+ if (vueIds) {
503
+ const vueId = vueIds.split(',')[0];
504
+ const object = (center[vueId] = center[vueId] || {});
505
+ object[name] = value;
506
+ if (parents[vueId]) {
507
+ parents[vueId].$forceUpdate();
508
+ }
509
+ }
510
+ };
511
+ onUnmounted(function () {
512
+ const propsData = instance.attrs;
513
+ const vueId = propsData && propsData.vueId;
514
+ if (vueId) {
515
+ delete center[vueId];
516
+ delete parents[vueId];
517
+ }
518
+ }, instance);
114
519
  }
115
520
 
116
521
  const PAGE_HOOKS = [
117
- 'onLoad',
118
- 'onShow',
522
+ ON_LOAD,
523
+ ON_SHOW,
524
+ ON_HIDE,
525
+ ON_UNLOAD,
526
+ ON_RESIZE,
527
+ ON_TAB_ITEM_TAP,
528
+ ON_REACH_BOTTOM,
529
+ ON_PULL_DOWN_REFRESH,
530
+ ON_ADD_TO_FAVORITES,
119
531
  // 'onReady', // lifetimes.ready
120
- 'onHide',
121
- 'onUnload',
122
- 'onResize',
123
532
  // 'onPageScroll', // 影响性能,开发者手动注册
124
- 'onTabItemTap',
125
- 'onReachBottom',
126
- 'onPullDownRefresh',
127
533
  // 'onShareTimeline', // 右上角菜单,开发者手动注册
128
- 'onAddToFavorites'
129
534
  // 'onShareAppMessage' // 右上角菜单,开发者手动注册
130
535
  ];
131
536
  function findHooks(vueOptions, hooks = new Set()) {
132
537
  if (vueOptions) {
133
- Object.keys(vueOptions).forEach(name => {
538
+ Object.keys(vueOptions).forEach((name) => {
134
539
  if (name.indexOf('on') === 0 && isFunction(vueOptions[name])) {
135
540
  hooks.add(name);
136
541
  }
@@ -138,7 +543,7 @@ function findHooks(vueOptions, hooks = new Set()) {
138
543
  if (__VUE_OPTIONS_API__) {
139
544
  const { extends: extendsOptions, mixins } = vueOptions;
140
545
  if (mixins) {
141
- mixins.forEach(mixin => findHooks(mixin, hooks));
546
+ mixins.forEach((mixin) => findHooks(mixin, hooks));
142
547
  }
143
548
  if (extendsOptions) {
144
549
  findHooks(extendsOptions, hooks);
@@ -147,28 +552,35 @@ function findHooks(vueOptions, hooks = new Set()) {
147
552
  }
148
553
  return hooks;
149
554
  }
150
- function initHook(mpOptions, hook, excludes) {
555
+ function initHook$1(mpOptions, hook, excludes) {
151
556
  if (excludes.indexOf(hook) === -1 && !hasOwn(mpOptions, hook)) {
152
557
  mpOptions[hook] = function (args) {
153
558
  return this.$vm && this.$vm.$callHook(hook, args);
154
559
  };
155
560
  }
156
561
  }
157
- const EXCLUDE_HOOKS = ['onReady'];
562
+ const EXCLUDE_HOOKS = [ON_READY];
158
563
  function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
159
- hooks.forEach(hook => initHook(mpOptions, hook, excludes));
564
+ hooks.forEach((hook) => initHook$1(mpOptions, hook, excludes));
160
565
  }
161
566
  function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
162
- findHooks(vueOptions).forEach(hook => initHook(mpOptions, hook, excludes));
567
+ findHooks(vueOptions).forEach((hook) => initHook$1(mpOptions, hook, excludes));
568
+ }
569
+
570
+ swan.appLaunchHooks = [];
571
+ function injectAppLaunchHooks(appInstance) {
572
+ swan.appLaunchHooks.forEach((hook) => {
573
+ injectHook(ON_LAUNCH, hook, appInstance);
574
+ });
163
575
  }
164
576
 
165
577
  const HOOKS = [
166
- 'onShow',
167
- 'onHide',
168
- 'onError',
169
- 'onThemeChange',
170
- 'onPageNotFound',
171
- 'onUnhandledRejection'
578
+ ON_SHOW,
579
+ ON_HIDE,
580
+ ON_ERROR,
581
+ ON_THEME_CHANGE,
582
+ ON_PAGE_NOT_FOUND,
583
+ ON_UNHANDLE_REJECTION,
172
584
  ];
173
585
  function parseApp(instance, parseAppOptions) {
174
586
  const internalInstance = instance.$;
@@ -184,12 +596,14 @@ function parseApp(instance, parseAppOptions) {
184
596
  initBaseInstance(internalInstance, {
185
597
  mpType: 'app',
186
598
  mpInstance: this,
187
- slots: []
599
+ slots: [],
188
600
  });
601
+ injectAppLaunchHooks(internalInstance);
189
602
  ctx.globalData = this.globalData;
190
- instance.$callHook('onLaunch', options);
191
- }
603
+ instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
604
+ },
192
605
  };
606
+ initLocale(instance);
193
607
  const vueOptions = instance.$.type;
194
608
  initHooks(appOptions, HOOKS);
195
609
  initUnknownHooks(appOptions, vueOptions);
@@ -206,102 +620,17 @@ function initCreateApp(parseAppOptions) {
206
620
  return function createApp(vm) {
207
621
  return App(parseApp(vm, parseAppOptions));
208
622
  };
209
- }
210
-
211
- const encode = encodeURIComponent;
212
- function stringifyQuery(obj, encodeStr = encode) {
213
- const res = obj
214
- ? Object.keys(obj)
215
- .map(key => {
216
- let val = obj[key];
217
- if (typeof val === undefined || val === null) {
218
- val = '';
219
- }
220
- else if (isPlainObject(val)) {
221
- val = JSON.stringify(val);
222
- }
223
- return encodeStr(key) + '=' + encodeStr(val);
224
- })
225
- .filter(x => x.length > 0)
226
- .join('&')
227
- : null;
228
- return res ? `?${res}` : '';
229
- }
230
-
231
- function initBehavior(options) {
232
- return Behavior(options);
233
- }
234
- function initVueIds(vueIds, mpInstance) {
235
- if (!vueIds) {
236
- return;
237
- }
238
- const ids = vueIds.split(',');
239
- const len = ids.length;
240
- if (len === 1) {
241
- mpInstance._$vueId = ids[0];
242
- }
243
- else if (len === 2) {
244
- mpInstance._$vueId = ids[0];
245
- mpInstance._$vuePid = ids[1];
246
- }
247
- }
248
- const EXTRAS = ['externalClasses'];
249
- function initExtraOptions(miniProgramComponentOptions, vueOptions) {
250
- EXTRAS.forEach(name => {
251
- if (hasOwn(vueOptions, name)) {
252
- miniProgramComponentOptions[name] = vueOptions[name];
253
- }
254
- });
255
- }
256
- function initWxsCallMethods(methods, wxsCallMethods) {
257
- if (!isArray(wxsCallMethods)) {
258
- return;
259
- }
260
- wxsCallMethods.forEach((callMethod) => {
261
- methods[callMethod] = function (args) {
262
- return this.$vm[callMethod](args);
263
- };
264
- });
265
623
  }
266
- function initRefs(instance, mpInstance) {
267
- Object.defineProperty(instance, 'refs', {
624
+ function initLocale(appVm) {
625
+ const locale = ref(swan.getSystemInfoSync().language || 'zh-Hans');
626
+ Object.defineProperty(appVm, '$locale', {
268
627
  get() {
269
- const $refs = {};
270
- const components = mpInstance.selectAllComponents('.vue-ref');
271
- components.forEach(component => {
272
- const ref = component.dataset.ref;
273
- $refs[ref] = component.$vm || component;
274
- });
275
- const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
276
- forComponents.forEach(component => {
277
- const ref = component.dataset.ref;
278
- if (!$refs[ref]) {
279
- $refs[ref] = [];
280
- }
281
- $refs[ref].push(component.$vm || component);
282
- });
283
- return $refs;
284
- }
628
+ return locale.value;
629
+ },
630
+ set(v) {
631
+ locale.value = v;
632
+ },
285
633
  });
286
- }
287
- function findVmByVueId(instance, vuePid) {
288
- // TODO vue3 中 没有 $children
289
- const $children = instance.$children;
290
- // 优先查找直属(反向查找:https://github.com/dcloudio/uni-app/issues/1200)
291
- for (let i = $children.length - 1; i >= 0; i--) {
292
- const childVm = $children[i];
293
- if (childVm.$scope._$vueId === vuePid) {
294
- return childVm;
295
- }
296
- }
297
- // 反向递归查找
298
- let parentVm;
299
- for (let i = $children.length - 1; i >= 0; i--) {
300
- parentVm = findVmByVueId($children[i], vuePid);
301
- if (parentVm) {
302
- return parentVm;
303
- }
304
- }
305
634
  }
306
635
 
307
636
  const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
@@ -335,7 +664,7 @@ function initDefaultProps(isBehavior = false) {
335
664
  if (!isBehavior) {
336
665
  properties.vueId = {
337
666
  type: String,
338
- value: ''
667
+ value: '',
339
668
  };
340
669
  // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
341
670
  properties.vueSlots = {
@@ -347,9 +676,9 @@ function initDefaultProps(isBehavior = false) {
347
676
  $slots[slotName] = true;
348
677
  });
349
678
  this.setData({
350
- $slots
679
+ $slots,
351
680
  });
352
- }
681
+ },
353
682
  };
354
683
  }
355
684
  return properties;
@@ -361,14 +690,14 @@ function createProperty(key, prop) {
361
690
  function initProps(mpComponentOptions, rawProps, isBehavior = false) {
362
691
  const properties = initDefaultProps(isBehavior);
363
692
  if (isArray(rawProps)) {
364
- rawProps.forEach(key => {
693
+ rawProps.forEach((key) => {
365
694
  properties[key] = createProperty(key, {
366
- type: null
695
+ type: null,
367
696
  });
368
697
  });
369
698
  }
370
699
  else if (isPlainObject(rawProps)) {
371
- Object.keys(rawProps).forEach(key => {
700
+ Object.keys(rawProps).forEach((key) => {
372
701
  const opts = rawProps[key];
373
702
  if (isPlainObject(opts)) {
374
703
  // title:{type:String,default:''}
@@ -380,14 +709,14 @@ function initProps(mpComponentOptions, rawProps, isBehavior = false) {
380
709
  opts.type = parsePropType(key, type, value);
381
710
  properties[key] = createProperty(key, {
382
711
  type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
383
- value
712
+ value,
384
713
  });
385
714
  }
386
715
  else {
387
716
  // content:String
388
717
  const type = parsePropType(key, opts, null);
389
718
  properties[key] = createProperty(key, {
390
- type: PROP_TYPES.indexOf(type) !== -1 ? type : null
719
+ type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
391
720
  });
392
721
  }
393
722
  });
@@ -399,8 +728,7 @@ function initData(vueOptions) {
399
728
  let data = vueOptions.data || {};
400
729
  if (typeof data === 'function') {
401
730
  try {
402
- const appConfig = getApp().$vm.$.appContext
403
- .config;
731
+ const appConfig = getApp().$vm.$.appContext.config;
404
732
  data = data.call(appConfig.globalProperties);
405
733
  }
406
734
  catch (e) {
@@ -431,7 +759,7 @@ function initBehaviors(vueOptions, initBehavior) {
431
759
  }
432
760
  const behaviors = [];
433
761
  if (isArray(vueBehaviors)) {
434
- vueBehaviors.forEach(behavior => {
762
+ vueBehaviors.forEach((behavior) => {
435
763
  behaviors.push(behavior.replace('uni://', `${__PLATFORM_PREFIX__}://`));
436
764
  if (behavior === 'uni://form-field') {
437
765
  if (isArray(vueProps)) {
@@ -441,24 +769,24 @@ function initBehaviors(vueOptions, initBehavior) {
441
769
  else {
442
770
  vueProps.name = {
443
771
  type: String,
444
- default: ''
772
+ default: '',
445
773
  };
446
774
  vueProps.value = {
447
775
  type: [String, Number, Boolean, Array, Object, Date],
448
- default: ''
776
+ default: '',
449
777
  };
450
778
  }
451
779
  }
452
780
  });
453
781
  }
454
- if (isPlainObject(vueExtends) && vueExtends.props) {
782
+ if (vueExtends && vueExtends.props) {
455
783
  const behavior = {};
456
784
  initProps(behavior, vueExtends.props, true);
457
785
  behaviors.push(initBehavior(behavior));
458
786
  }
459
787
  if (isArray(vueMixins)) {
460
- vueMixins.forEach(vueMixin => {
461
- if (isPlainObject(vueMixin) && vueMixin.props) {
788
+ vueMixins.forEach((vueMixin) => {
789
+ if (vueMixin.props) {
462
790
  const behavior = {};
463
791
  initProps(behavior, vueMixin.props, true);
464
792
  behaviors.push(initBehavior(behavior));
@@ -472,21 +800,9 @@ function applyOptions(componentOptions, vueOptions, initBehavior) {
472
800
  componentOptions.behaviors = initBehaviors(vueOptions, initBehavior);
473
801
  }
474
802
 
475
- function getValue(obj, path) {
476
- const parts = path.split('.');
477
- let key = parts[0];
478
- if (key.indexOf('__$n') === 0) {
479
- //number index
480
- key = parseInt(key.replace('__$n', ''));
481
- }
482
- if (parts.length === 1) {
483
- return obj[key];
484
- }
485
- return getValue(obj[key], parts.slice(1).join('.'));
486
- }
487
803
  function getExtraValue(instance, dataPathsArray) {
488
804
  let context = instance;
489
- dataPathsArray.forEach(dataPathArray => {
805
+ dataPathsArray.forEach((dataPathArray) => {
490
806
  const dataPath = dataPathArray[0];
491
807
  const value = dataPathArray[2];
492
808
  if (dataPath || typeof value !== 'undefined') {
@@ -505,7 +821,7 @@ function getExtraValue(instance, dataPathsArray) {
505
821
  vFor = dataPath.substr(3);
506
822
  }
507
823
  else {
508
- vFor = getValue(context, dataPath);
824
+ vFor = getTarget(context, dataPath);
509
825
  }
510
826
  }
511
827
  if (Number.isInteger(vFor)) {
@@ -516,13 +832,13 @@ function getExtraValue(instance, dataPathsArray) {
516
832
  }
517
833
  else {
518
834
  if (isArray(vFor)) {
519
- context = vFor.find(vForItem => {
520
- return getValue(vForItem, propPath) === value;
835
+ context = vFor.find((vForItem) => {
836
+ return getTarget(vForItem, propPath) === value;
521
837
  });
522
838
  }
523
839
  else if (isPlainObject(vFor)) {
524
- context = Object.keys(vFor).find(vForKey => {
525
- return getValue(vFor[vForKey], propPath) === value;
840
+ context = Object.keys(vFor).find((vForKey) => {
841
+ return getTarget(vFor[vForKey], propPath) === value;
526
842
  });
527
843
  }
528
844
  else {
@@ -530,7 +846,7 @@ function getExtraValue(instance, dataPathsArray) {
530
846
  }
531
847
  }
532
848
  if (valuePath) {
533
- context = getValue(context, valuePath);
849
+ context = getTarget(context, valuePath);
534
850
  }
535
851
  }
536
852
  });
@@ -571,10 +887,10 @@ function processEventExtra(instance, extra, event) {
571
887
  }
572
888
  else if (dataPath.indexOf('$event.') === 0) {
573
889
  // $event.target.value
574
- extraObj['$' + index] = getValue(event, dataPath.replace('$event.', ''));
890
+ extraObj['$' + index] = getTarget(event, dataPath.replace('$event.', ''));
575
891
  }
576
892
  else {
577
- extraObj['$' + index] = getValue(instance, dataPath);
893
+ extraObj['$' + index] = getTarget(instance, dataPath);
578
894
  }
579
895
  }
580
896
  }
@@ -611,7 +927,7 @@ function processEventArgs(instance, event, args = [], extra = [], isCustom, meth
611
927
  }
612
928
  const extraObj = processEventExtra(instance, extra, event);
613
929
  const ret = [];
614
- args.forEach(arg => {
930
+ args.forEach((arg) => {
615
931
  if (arg === '$event') {
616
932
  if (methodName === '__set_model' && !isCustom) {
617
933
  // input v-model value
@@ -661,7 +977,7 @@ function wrapper(event) {
661
977
  }
662
978
  }
663
979
  if (isPlainObject(event.detail)) {
664
- event.target = Object.assign({}, event.target, event.detail);
980
+ event.target = extend({}, event.target, event.detail);
665
981
  }
666
982
  return event;
667
983
  }
@@ -719,7 +1035,14 @@ function handleEvent(event) {
719
1035
  }
720
1036
  handler.once = true;
721
1037
  }
722
- ret.push(handler.apply(handlerCtx, processEventArgs(this.$vm, event, eventArray[1], eventArray[2], isCustom, methodName)));
1038
+ let params = processEventArgs(this.$vm, event, eventArray[1], eventArray[2], isCustom, methodName);
1039
+ params = Array.isArray(params) ? params : [];
1040
+ // 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
1041
+ if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
1042
+ // eslint-disable-next-line no-sparse-arrays
1043
+ params = params.concat([, , , , , , , , , , event]);
1044
+ }
1045
+ ret.push(handler.apply(handlerCtx, params));
723
1046
  }
724
1047
  });
725
1048
  }
@@ -731,11 +1054,11 @@ function handleEvent(event) {
731
1054
  }
732
1055
  }
733
1056
 
734
- function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes }) {
1057
+ function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes, }) {
735
1058
  vueOptions = vueOptions.default || vueOptions;
736
1059
  const options = {
737
1060
  multipleSlots: true,
738
- addGlobalClass: true
1061
+ addGlobalClass: true,
739
1062
  };
740
1063
  if (vueOptions.options) {
741
1064
  extend(options, vueOptions.options);
@@ -752,12 +1075,12 @@ function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handle
752
1075
  },
753
1076
  resize(size) {
754
1077
  this.$vm && this.$vm.$callHook('onPageResize', size);
755
- }
1078
+ },
756
1079
  },
757
1080
  methods: {
758
1081
  __l: handleLink,
759
- __e: handleEvent
760
- }
1082
+ __e: handleEvent,
1083
+ },
761
1084
  };
762
1085
  if (__VUE_OPTIONS_API__) {
763
1086
  applyOptions(mpComponentOptions, vueOptions, initBehavior);
@@ -797,15 +1120,15 @@ function parsePage(vueOptions, parseOptions) {
797
1120
  isPage,
798
1121
  initRelation,
799
1122
  handleLink,
800
- initLifetimes
1123
+ initLifetimes,
801
1124
  });
802
1125
  const methods = miniProgramPageOptions.methods;
803
1126
  methods.onLoad = function (query) {
804
1127
  this.options = query;
805
1128
  this.$page = {
806
- fullPath: '/' + this.route + stringifyQuery(query)
1129
+ fullPath: '/' + this.route + stringifyQuery(query),
807
1130
  };
808
- return this.$vm && this.$vm.$callHook('onLoad', query);
1131
+ return this.$vm && this.$vm.$callHook(ON_LOAD, query);
809
1132
  };
810
1133
  initHooks(methods, PAGE_HOOKS);
811
1134
  initUnknownHooks(methods, vueOptions);
@@ -830,7 +1153,7 @@ function initTriggerEvent(mpInstance) {
830
1153
  return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]);
831
1154
  };
832
1155
  }
833
- function initHook$1(name, options) {
1156
+ function initHook(name, options) {
834
1157
  const oldHook = options[name];
835
1158
  if (!oldHook) {
836
1159
  options[name] = function () {
@@ -845,36 +1168,75 @@ function initHook$1(name, options) {
845
1168
  }
846
1169
  }
847
1170
  Page = function (options) {
848
- initHook$1('onLoad', options);
1171
+ initHook(ON_LOAD, options);
849
1172
  return MPPage(options);
850
1173
  };
851
1174
  Component = function (options) {
852
- initHook$1('created', options);
1175
+ initHook('created', options);
853
1176
  return MPComponent(options);
854
1177
  };
855
1178
 
856
- function parse(appOptions) {
1179
+ function parse$2(appOptions) {
857
1180
  // 百度 onShow 竟然会在 onLaunch 之前
858
1181
  appOptions.onShow = function onShow(args) {
859
1182
  if (!this.$vm) {
860
1183
  this.onLaunch(args);
861
1184
  }
862
- this.$vm.$callHook('onShow', args);
1185
+ this.$vm.$callHook(ON_SHOW, args);
863
1186
  };
864
1187
  }
865
1188
 
866
1189
  var parseAppOptions = /*#__PURE__*/Object.freeze({
867
- __proto__: null,
868
- parse: parse
1190
+ __proto__: null,
1191
+ parse: parse$2
869
1192
  });
870
1193
 
871
- function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
1194
+ /**
1195
+ * 用于延迟调用 setData
1196
+ * 在 setData 真实调用的时机需执行 fixSetDataEnd
1197
+ * @param {*} mpInstance
1198
+ */
1199
+ function fixSetDataStart(mpInstance) {
1200
+ const setData = mpInstance.setData;
1201
+ const setDataArgs = [];
1202
+ mpInstance.setData = function () {
1203
+ setDataArgs.push(arguments);
1204
+ };
1205
+ mpInstance.__fixInitData = function () {
1206
+ this.setData = setData;
1207
+ const fn = () => {
1208
+ setDataArgs.forEach((args) => {
1209
+ setData.apply(this, args);
1210
+ });
1211
+ };
1212
+ if (setDataArgs.length) {
1213
+ if (this.groupSetData) {
1214
+ this.groupSetData(fn);
1215
+ }
1216
+ else {
1217
+ fn();
1218
+ }
1219
+ }
1220
+ };
1221
+ }
1222
+ /**
1223
+ * 恢复真实的 setData 方法
1224
+ * @param {*} mpInstance
1225
+ */
1226
+ function fixSetDataEnd(mpInstance) {
1227
+ if (mpInstance.__fixInitData) {
1228
+ mpInstance.__fixInitData();
1229
+ delete mpInstance.__fixInitData;
1230
+ }
1231
+ }
1232
+
1233
+ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) {
872
1234
  return {
873
1235
  attached() {
874
1236
  const properties = this.properties;
875
1237
  initVueIds(properties.vueId, this);
876
1238
  const relationOptions = {
877
- vuePid: this._$vuePid
1239
+ vuePid: this._$vuePid,
878
1240
  };
879
1241
  // 处理父子关系
880
1242
  initRelation(this, relationOptions);
@@ -882,7 +1244,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
882
1244
  const mpInstance = this;
883
1245
  this.$vm = $createComponent({
884
1246
  type: vueOptions,
885
- props: properties
1247
+ props: properties,
886
1248
  }, {
887
1249
  mpType: isPage(mpInstance) ? 'page' : 'component',
888
1250
  mpInstance,
@@ -892,7 +1254,7 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
892
1254
  initRefs(instance, mpInstance);
893
1255
  initMocks(instance, mpInstance, mocks);
894
1256
  initComponentInstance(instance, options);
895
- }
1257
+ },
896
1258
  });
897
1259
  },
898
1260
  ready() {
@@ -900,12 +1262,12 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions }) {
900
1262
  // https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
901
1263
  if (this.$vm) {
902
1264
  this.$vm.$callHook('mounted');
903
- this.$vm.$callHook('onReady');
1265
+ this.$vm.$callHook(ON_READY);
904
1266
  }
905
1267
  },
906
1268
  detached() {
907
1269
  this.$vm && $destroyComponent(this.$vm);
908
- }
1270
+ },
909
1271
  };
910
1272
  }
911
1273
 
@@ -939,16 +1301,35 @@ function parse$1(componentOptions) {
939
1301
  // lifetimes:attached --> methods:onShow --> methods:onLoad --> methods:onReady
940
1302
  // 这里在强制将onShow挪到onLoad之后触发,另外一处修改在page-parser.js
941
1303
  const oldAttached = lifetimes.attached;
942
- lifetimes.attached = function attached() {
1304
+ // 百度小程序基础库 3.260 以上支持页面 onInit 生命周期,提前创建 vm 实例
1305
+ lifetimes.onInit = function onInit(query) {
1306
+ // 百度小程序后续可能移除 pageinstance 属性,为向后兼容进行补充
1307
+ if (!this.pageinstance || !this.pageinstance.setData) {
1308
+ const pages = getCurrentPages();
1309
+ this.pageinstance = pages[pages.length - 1];
1310
+ }
1311
+ // 处理百度小程序 onInit 生命周期调用 setData 无效的问题
1312
+ fixSetDataStart(this);
943
1313
  oldAttached.call(this);
1314
+ this.pageinstance.$vm = this.$vm;
1315
+ this.$vm.__call_hook('onInit', query);
1316
+ };
1317
+ lifetimes.attached = function attached() {
1318
+ if (!this.$vm) {
1319
+ oldAttached.call(this);
1320
+ }
1321
+ else {
1322
+ initMocks(this.$vm.$, this, mocks);
1323
+ fixSetDataEnd(this);
1324
+ }
944
1325
  if (isPage(this) && this.$vm) {
945
- // 百度 onLoad 在 attached 之前触发
1326
+ // 百度 onLoad 在 attached 之前触发(基础库小于 3.70)
946
1327
  // 百度 当组件作为页面时 pageinstance 不是原来组件的 instance
947
1328
  const pageInstance = this.pageinstance;
948
1329
  pageInstance.$vm = this.$vm;
949
1330
  if (hasOwn(pageInstance, '_$args')) {
950
- this.$vm.$callHook('onLoad', pageInstance._$args);
951
- this.$vm.$callHook('onShow');
1331
+ this.$vm.$callHook(ON_LOAD, pageInstance._$args);
1332
+ this.$vm.$callHook(ON_SHOW);
952
1333
  delete pageInstance._$args;
953
1334
  }
954
1335
  }
@@ -964,36 +1345,36 @@ function parse$1(componentOptions) {
964
1345
  delete lifetimes.ready;
965
1346
  }
966
1347
  componentOptions.messages = {
967
- __l: methods.__l
1348
+ __l: methods.__l,
968
1349
  };
969
1350
  delete methods.__l;
970
1351
  }
971
1352
 
972
1353
  var parseComponentOptions = /*#__PURE__*/Object.freeze({
973
- __proto__: null,
974
- mocks: mocks,
975
- isPage: isPage,
976
- initRelation: initRelation,
977
- parse: parse$1,
978
- handleLink: handleLink,
979
- initLifetimes: initLifetimes
1354
+ __proto__: null,
1355
+ mocks: mocks,
1356
+ isPage: isPage,
1357
+ initRelation: initRelation,
1358
+ parse: parse$1,
1359
+ handleLink: handleLink,
1360
+ initLifetimes: initLifetimes
980
1361
  });
981
1362
 
982
- function parse$2(pageOptions) {
1363
+ function parse(pageOptions) {
983
1364
  parse$1(pageOptions);
984
1365
  const methods = pageOptions.methods;
985
1366
  // 纠正百度小程序生命周期methods:onShow在methods:onLoad之前触发的问题
986
1367
  methods.onShow = function onShow() {
987
1368
  if (this.$vm && this._$loaded) {
988
- this.$vm.$callHook('onShow');
1369
+ this.$vm.$callHook(ON_SHOW);
989
1370
  }
990
1371
  };
991
1372
  methods.onLoad = function onLoad(args) {
992
1373
  // 百度 onLoad 在 attached 之前触发,先存储 args, 在 attached 里边触发 onLoad
993
1374
  if (this.$vm) {
994
1375
  this._$loaded = true;
995
- this.$vm.$callHook('onLoad', args);
996
- this.$vm.$callHook('onShow');
1376
+ this.$vm.$callHook(ON_LOAD, args);
1377
+ this.$vm.$callHook(ON_SHOW);
997
1378
  }
998
1379
  else {
999
1380
  this.pageinstance._$args = args;
@@ -1002,17 +1383,21 @@ function parse$2(pageOptions) {
1002
1383
  }
1003
1384
 
1004
1385
  var parsePageOptions = /*#__PURE__*/Object.freeze({
1005
- __proto__: null,
1006
- parse: parse$2,
1007
- handleLink: handleLink,
1008
- initLifetimes: initLifetimes,
1009
- mocks: mocks,
1010
- isPage: isPage,
1011
- initRelation: initRelation
1386
+ __proto__: null,
1387
+ parse: parse,
1388
+ handleLink: handleLink,
1389
+ initLifetimes: initLifetimes,
1390
+ mocks: mocks,
1391
+ isPage: isPage,
1392
+ initRelation: initRelation
1012
1393
  });
1013
1394
 
1014
1395
  const createApp = initCreateApp(parseAppOptions);
1015
1396
  const createPage = initCreatePage(parsePageOptions);
1016
- const createComponent = initCreateComponent(parseComponentOptions);
1397
+ const createComponent = initCreateComponent(parseComponentOptions);
1398
+ swan.EventChannel = EventChannel;
1399
+ swan.createApp = createApp;
1400
+ swan.createPage = createPage;
1401
+ swan.createComponent = createComponent;
1017
1402
 
1018
1403
  export { createApp, createComponent, createPage };