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

const origin = location.origin;

/**
 * TODO: 结构优化
 */


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

const matchHeader = (url) => {
    let header = {
        headerid: '-1'
    };
    let path = url.split('?')[0];

    if (/\/cate-all$/.test(path)) {
        header = titleMap[1];
        header.title.des = '全部分类';
        return header;
    }

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

    if (/\/me\/order$/.test(path)) {
        let des = '';
        let 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 (/\/me\/order-detail$/.test(path)) {
        header = titleMap[2];
        header.title.des = '订单详情';
        header.title.right.action = origin + '/me/service';
        return header;
    }

    if (/\/me\/return$/.test(path)) {
        header = titleMap[1];
        header.title.des = '退/换货';
        return header;
    }

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

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

    if (/\/me\/service$/.test(path)) {
        header = titleMap[1];
        header.title.des = 'Yoho!Blk在线客服';
        return header;
    }

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

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

    return header;
};


module.exports = (url) => {
    if (yoho.isApp) {
        let path = url.split('?')[0];

        // 个人中心收藏
        if (/\/me\/favorite$/.test(path)) {
            let u = url.split('?')[1];
            let header = titleMap[5];

            u = parse(u);
            if (u.tab === 'brand') {
                header.defaultSelectedIndex = '1';
            }
            return yoho.goPageView({
                header: header
            });
        }

        // 个人中心首页
        if (/\/me$/.test(path)) {
            return yoho.goTab({index: 4});
        }

        // 资讯
        if (/\/editorial\/list$/.test(path)) {
            return yoho.goTab({index: 2});
        }

        // 品牌 品类
        if (/\/brand$/.test(path) || /\/cate$/.test(path)) {
            return yoho.goTab({index: 1});
        }

        // 首页
        if (/\/$/.test(path)) {
            return yoho.goTab({index: 0});
        }

        yoho.goNewPage({
            header: matchHeader(url),
            url: /^(https?:)?\/\//i.test(url) ? url : origin + url
        });
    } else {
        location.href = url;
    }
};