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.

84 lines
3.4 KiB

4 years ago
/**
*
* Page扩展函数
*
* @param {*} Page 原生Page
*/
import { http, httpUtil, getRouter, location_city } from './utils/util'
4 years ago
const pageExtend = Page => {
return object => {
4 years ago
// 导出原生Page传入的object参数中的生命周期函数
// 由于命名冲突所以将onLoad生命周期函数命名成了onLoaded
const {
onLoad
} = object
4 years ago
// 公共的onLoad生命周期函数
object.onLoad = function (options) {
wx.getStorageSync('xid') ? "" : this.getxid()
if (!getApp().globalData.city_id) {
location_city().then(res => {
this.getroutermi(options, res.city_info.city_id)
})
} else {
this.getroutermi(options, getApp().globalData.city_id)
}
4 years ago
// 在onLoad中执行的代码
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
})
if (getApp().globalData.uid) {
http('/api/v1/save-cps', 'get', { cps: options.cps, route: getCurrentPages()[getCurrentPages().length - 1].route }).then(res => {
4 years ago
wx.setStorageSync('cps', cps)
})
} else {
httpUtil('/api/v1/save-cps', 'get', { cps: options.cps, route: getCurrentPages()[getCurrentPages().length - 1].route }).then(res => {
4 years ago
wx.setStorageSync('cps', cps)
})
}
4 years ago
}
}
// 执行onLoaded生命周期函数
if (typeof onLoad === 'function') {
onLoad.call(this, options)
}
}
object.getroutermi = function (option, cityId) {
let xid = wx.getStorageSync('xid')
if (new Date().getTime() > xid.substring(0, xid.length - 4) - 0 + 1200000) {
this.getxid()
}
let action_id = getRouter(getCurrentPages()[getCurrentPages().length - 1].route)
action_id==34?option=[]:''
let data = { xid:wx.getStorageSync('xid'),action_id:getRouter(getCurrentPages()[getCurrentPages().length - 1].route),city_id:cityId-0,mini_type:1,...option}
http('/api/v1/user-behavior', 'post', data).then(res => {
console.log(res,'res');
})
}
object.getxid = function () {
// console.log(new Date().getTime(),'min');
// console.log(new Date().getTime()+(Math.floor(Math.random()*(9999-1000))+1000).toString(),'min');
let xid = new Date().getTime() + (Math.floor(Math.random() * (9999 - 1000)) + 1000).toString()
wx.setStorageSync('xid', xid)
}
4 years ago
return Page(object)
}
}
// 获取原生Page
const originalPage = Page
// 定义一个新的Page将原生Page传入Page扩展函数
Page = pageExtend(originalPage)