const ori_page = Page
const lifecycle = ['onLoad', 'onReady', 'onShow', 'onHide', 'onUnload', 'onPullDownRefresh', 'onReachBottom', 'onShareAppMessage', 'onPageScroll']
let globalMixin = null
wx.mixin = config => {
if (isType(config, 'object')) {
globalMixin = config
}
}
Page = config => {
let mixins = config.mixins
if(globalMixin) {
(mixins || (mixins = [])).unshift(globalMixin)
}
if(isType(mixins, 'array') && mixins.length > 0) {
Reflect.deleteProperty(config, 'mixins')
merge(mixins, config)
}
ori_page(config)
}
function merge(mixins, config) {
mixins.forEach(mixin => {
if(isType(mixin, 'object')) {
Object.keys(mixin).forEach(key => {
if(key === 'data') {
config.data = Object.assign({},mixin[key],config[key])
}else if (lifecycle.includes(key)) {
let ori_lifecycle = config[key]
config[key] = function() {
let arg = Array.prototype.slice.call(arguments)
mixin[key].call(this, arg)
ori_lifecycle && ori_lifecycle.call(this, arg)
}
}else {
config[key] = mixin[key]
}
})
}
})
}
function isType(target, type) {
let targetType = Object.prototype.toString.call(target).slice(8, -1).toLowerCase()
type = type.toLowerCase()
return targetType === type
}