'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
  cached,
  camelize
} from 'uni-shared'
import { markMPComponent } from './wrapper/util'
 
const MPPage = Page
const MPComponent = Component
 
const customizeRE = /:/g
 
const customize = cached((str) => {
  return camelize(str.replace(customizeRE, '-'))
})
 
function initTriggerEvent (mpInstance) {
  const oldTriggerEvent = mpInstance.triggerEvent
  const newTriggerEvent = function (event, ...args) {
    // 事件名统一转驼峰格式,仅处理:当前组件为 vue 组件、当前组件为 vue 组件子组件
    if (this.$vm || (this.dataset && this.dataset.comType)) {
      event = customize(event)
    } else if (__PLATFORM__ === 'mp-weixin' || __PLATFORM__ === 'mp-qq') {
      // 针对微信/QQ小程序单独补充驼峰格式事件,以兼容历史项目
      const newEvent = customize(event)
      if (newEvent !== event) {
        oldTriggerEvent.apply(this, [newEvent, ...args])
      }
    }
    return oldTriggerEvent.apply(this, [event, ...args])
  }
  try {
    // 京东小程序 triggerEvent 为只读
    mpInstance.triggerEvent = newTriggerEvent
  } catch (error) {
    mpInstance._triggerEvent = newTriggerEvent
  }
}
 
function initHook (name, options, isComponent) {
  if (__PLATFORM__ === 'mp-toutiao') {
    // fix by Lxh 字节自定义组件Component构造器文档上写有created,但是实测只触发了lifetimes上的created
    isComponent && options.lifetimes && options.lifetimes[name] && (options = options.lifetimes)
  }
  const oldHook = options[name]
  options[name] = function (...args) {
    markMPComponent(this)
    initTriggerEvent(this)
    if (oldHook) {
      return oldHook.apply(this, args)
    }
  }
}
if (!MPPage.__$wrappered) {
  MPPage.__$wrappered = true
  Page = function (options = {}) {
    initHook('onLoad', options)
    return MPPage(options)
  }
  Page.after = MPPage.after
 
  Component = function (options = {}) {
    initHook('created', options, true)
    return MPComponent(options)
  }
}