/**
 *
 * Page扩展函数
 *
 * @param {*} Page 原生Page
 */
import { http, httpUtil, getRouter, location_city } from './utils/util';

const pageExtend = (Page) => {
    return (object) => {
        // 导出原生Page传入的object参数中的生命周期函数
        // 由于命名冲突，所以将onLoad生命周期函数命名成了onLoaded
        const { onLoad } = object; // 公共的onLoad生命周期函数
        object.onLoad = function (options) {
            if (uni.getStorageSync('xid')) {
                ('');
            } else {
                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);
            } // 在onLoad中执行的代码

            console.log(getCurrentPages()[getCurrentPages().length - 1].route, '拦截页面参数：');
            console.log(options);

            if (options.cps) {
                let cps = uni.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) => {
                            uni.setStorageSync('cps', cps);
                        });
                    } else {
                        httpUtil('/api/v1/save-cps', 'get', {
                            cps: options.cps,
                            route: getCurrentPages()[getCurrentPages().length - 1].route
                        }).then((res) => {
                            uni.setStorageSync('cps', cps);
                        });
                    }
                }
            } // 执行onLoaded生命周期函数

            if (typeof onLoad === 'function') {
                onLoad.call(this, options);
            }
        };

        object.getroutermi = function (option, cityId) {
            let xid = uni.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);

            if (action_id == 34) {
                option = [];
            } else {
                ('');
            }

            let data = {
                xid: uni.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();
            uni.setStorageSync('xid', xid);
        };

        return Page(object);
    };
}; // 获取原生Page

const originalPage = Page; // 定义一个新的Page，将原生Page传入Page扩展函数

Page = pageExtend(originalPage);
