intercept-click.js 4.32 KB
/**
 * 拦截跳转, 在这里给将要打开的页面设置一下 header 样式
 * @return {[type]}
 */
const yoho = require('yoho');
const parse = require('yoho-qs/parse'); // 提供解析函数

const origin = location.origin;

/**
 * 跳转 App 头部参数
 * 文档 http://git.yoho.cn/mobile/AppJSBridge/blob/master/HeaderType.md
 * @type {Object}
 */
const titleMap = {
    1: {
        headerid: '1',
        left: {
            action: ''
        },
        title: {
            des: '标题1',
            action: ''
        }
    },
    2: {
        headerid: '2',
        left: {
            action: ''
        },
        title: {
            des: '标题2',
            action: ''
        },
        right: {
            action: ''
        }
    },
    3: {
        headerid: '3',
        left: {
            action: ''
        },
        title: {
            des: '标题3',
            action: ''
        },
        right: {
            des: '提交',
            action: ''
        }
    },
    4: {
        headerid: '4',
        ltitle: {
            des: '品牌',
            action: origin + '/brands'
        },
        rtitle: {
            des: '品类',
            action: origin + '/cate'
        }
    },
    5: {
        headerid: '5',
        left: {
            action: ''
        },
        ltitle: {
            des: '商品5',
            action: ''
        },
        rtitle: {
            des: '品牌5',
            action: ''
        },
        right: {
            des: '编辑5',
            action: ''
        }
    },
    6: {
        headerid: '6',
        title: {
            des: '资讯6',
            action: ''
        }
    }
};

const matchHeader = (url) => {
    let header = {
        headerid: '-1' // 默认不显示头部
    };

    if (/\/product\/new/.test(url)) {
        header = titleMap[1];

        // header.xxx = '111';// 匹配到头类型以后,可修改里边的内容
        return header;
    }

    if (/\/brands/.test(url) || /\/cate/.test(url)) {
        header = titleMap[4];
        return header;
    }

    if (/\/home\/mydetails$/.test(url)) {
        header = titleMap[1];
        header.title.des = '个人信息';
        return header;
    }

    if (/\/home\/orders/.test(url)) {
        var des = '',
            u = url.split('?')[1],
            u = parse(u);

        if (u.type === '1') {
            des = '我的订单';
        } else if (u.type === '2') {
            des = '待付款';
        } else if (u.type === '3') {
            des = '待发货';
        } else if (u.type === '4') {
            des = '待收货';
        }
        header = titleMap[1];
        header.title.des = des;
        return header;
    }

    if (/\/home\/refund\/orders/.test(url)) {
        header = titleMap[1];
        header.title.des = '退/换货';
        return header;
    }

    if (/\/home\/favorite/.test(url)) {
        header = titleMap[5];
        header.ltitle = {
            des: '商品',
            action: origin + '/home/favorite'
        }
        header.rtitle = {
            des: '品牌',
            action: origin + '/home/favorite?tab=brand'
        }
        header.right = {
            des: '编辑',
            action: 'editModel'
        }
        return header;
    }

    if (/\/home\/mycurrency/.test(url)) {
        header = titleMap[1];
        header.title.des = '有货币';
        return header;
    }

    if (/\/home\/help$/.test(url)) {
        header = titleMap[1];
        header.title.des = '帮助中心';
        return header;
    }

    if (/chat8.live800.com/.test(url)) {
        header = titleMap[1];
        header.title.des = 'Yoho!Blk在线客服';
        return header;
    }

    if (/\/home\/feedback/.test(url)) {
        header = titleMap[3];
        header.title.des = '意见反馈';
        header.right = {
            des: '提交',
            action: 'saveFeedback'
        }
        return header;
    }

    if (/\/home\/about/.test(url)) {
        header = titleMap[1];
        header.title.des = '关于';
        return header;
    }

    return header;
};


module.exports = (url) => {
    if (yoho.isApp) {
        let data = {
            header: matchHeader(url),
            url: /^(https?:)?\/\//i.test(url) ? url : origin + url
        };

        // console.log(data);
        yoho.goNewPage(data);
    } else {
        location.href = url;
    }
};