handlePageParamMerge(arg) {
let numargs = arg.length;
let data = {}
let page = {}
for (let ix in arg) {
let item = arg[ix]
if (item.data && typeof (item.data) === 'object') {
data = Object.assign(data, item.data)
}
if (item.methods && typeof (item.methods) === 'object') {
page = Object.assign(page, item.methods)
} else {
page = Object.assign(page, item)
}
}
page.data = data
return page
}
mergePage() {
return this.handlePageParamMerge(arguments)
}
handleCompParamMerge(arg) {
let numargs = arg.length;
let data = {}
let options = {}
let properties = {}
let methods = {}
let comp = {}
for (let ix in arg) {
let item = arg[ix]
if (item.data && typeof (item.data) === 'object') {
data = Object.assign(data, item.data)
}
if (item.properties && typeof (item.properties) === 'object') {
properties = Object.assign(properties, item.properties)
}
if (item.methods && typeof (item.methods) === 'object') {
methods = Object.assign(methods, item.methods)
}
if (item.options && typeof (item.options) === 'object') {
options = Object.assign(options, item.options)
}
comp = Object.assign(comp, item)
}
comp.data = data
comp.options = options
comp.properties = properties
comp.methods = methods
return comp
}
mergeComponent() {
return this.handleCompParamMerge(arguments)
}
newPage() {
let options = this.handlePageParamMerge(arguments)
let that = this
let app = getApp()
if (!options.publicCheckLogin){
options.publicCheckLogin = function (e) {
let pages = getCurrentPages()
let page = pages[pages.length - 1]
let dataset = e.currentTarget.dataset
let callback = null
if (dataset.callback && typeof (page[dataset.callback]) === "function"){
callback = page[dataset.callback]
}
if (callback && app.isRegister()){
callback(e)
}
else{
wx.navigateTo({
url: '/pages/login/login'
})
}
}
}
const { onLoad } = options
options.onLoad = function (arg) {
options.watch && that.setWatcher(this)
onLoad && onLoad.call(this, arg)
}
const { onShow } = options
options.onShow = function (arg) {
if (options.data.noAutoLogin || app.isRegister()) {
onShow && onShow.call(this, arg)
app.ga({})
}
else {
wx.navigateTo({
url: '/pages/login/login'
})
}
}
return Page(options)
}
newComponent() {
let options = this.handleCompParamMerge(arguments)
let that = this
const { ready } = options
options.ready = function (arg) {
options.watch && that.setWatcher(this)
ready && ready.call(this, arg)
}
return Component(options)
}
setWatcher(page) {
let data = page.data;
let watch = page.watch;
Object.keys(watch).forEach(v => {
let key = v.split('.'); // 将watch中的属性以'.'切分成数组
let nowData = data;
for (let i = 0; i < key.length - 1; i++) {
nowData = nowData[key[i]];
}
let lastKey = key[key.length - 1];
let watchFun = watch[v].handler || watch[v];
let deep = watch[v].deep;
this.observe(nowData, lastKey, watchFun, deep, page);
})
}
observe(obj, key, watchFun, deep, page) {
var val = obj[key];
if (deep && val != null && typeof val === 'object') {
Object.keys(val).forEach(childKey => {
this.observe(val, childKey, watchFun, deep, page);
})
}
var that = this;
Object.defineProperty(obj, key, {
configurable: true,
enumerable: true,
set: function (value) {
if (val === value) {
return
}
watchFun.call(page, value, val);
val = value;
if (deep) {
that.observe(obj, key, watchFun, deep, page);
}
},
get: function () {
return val;
}
})
}