You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.0 KiB

4 years ago
/**
*
* Page扩展函数
*
* @param {*} Page 原生Page
*/
4 years ago
import {http,httpUtil} from './utils/util'
4 years ago
const pageExtend = Page => {
return object => {
// 导出原生Page传入的object参数中的生命周期函数
// 由于命名冲突所以将onLoad生命周期函数命名成了onLoaded
const {
onLoad
} = object
4 years ago
4 years ago
// 公共的onLoad生命周期函数
object.onLoad = function (options) {
// 在onLoad中执行的代码
4 years ago
console.log(getCurrentPages()[getCurrentPages().length-1].route,'拦截页面参数:')
4 years ago
console.log(options)
if (options.cps) {
let cps = wx.getStorageSync("cps") || null
if (cps == null || options.cps !== cps.value) {
let vTime = new Date().getTime() + 1000 * 60 * 60 * 24 * 2
let cps = Object.assign({
value: options.cps
}, {
vTime: vTime
})
4 years ago
if(getApp().globalData.uid){
http('/api/v1/save-cps','get',{cps:options.cps,route:getCurrentPages()[getCurrentPages().length-1].route}).then(res=>{
wx.setStorageSync('cps', cps)
})
}else{
httpUtil('/api/v1/save-cps','get',{cps:options.cps,route:getCurrentPages()[getCurrentPages().length-1].route}).then(res=>{
wx.setStorageSync('cps', cps)
})
}
4 years ago
}
}
// 执行onLoaded生命周期函数
if (typeof onLoad === 'function') {
onLoad.call(this, options)
}
}
return Page(object)
}
}
// 获取原生Page
const originalPage = Page
// 定义一个新的Page将原生Page传入Page扩展函数
Page = pageExtend(originalPage)