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

const origin = location.origin;

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


/**
 * 跳转 App 头部参数
 * 文档 http://git.yoho.cn/mobile/AppJSBridge/blob/master/HeaderType.md
 * @type {Object}
 */
const defaultTitleMap = {
    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/collection'
        },

        // mtitle: {
        //     des: '品牌',
        //     action: origin + '/me/collection?tab=brand'
        // },
        rtitle: {
            des: '资讯',
            action: origin + '/me/collection?tab=article'
        },
        right: {
            des: '编辑',
            action: 'editModel'
        },
        defaultSelectedIndex: '0'
    },
    6: {
        headerid: '6',
        title: {
            des: '资讯',
            action: ''
        }
    }
};

const matchHeader = (path, qs, titleMap) => {
    let header = {
        headerid: '-1'
    };

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

    if (/\/brand$/.test(path)) {
        header = titleMap[1];
        header.left.action = 'go.back';
        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 = '';

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

    if (/\/me\/order\/cancelreason$/.test(path)) {
        header = titleMap[1];
        header.title.des = '取消订单理由';
        return header;
    }

    if (/\/me\/return\/refund$/.test(path)) {
        header = titleMap[3];
        header.title.des = '退货申请';
        header.right.action = 'submitForm';
        return header;
    }

    if (/\/me\/return\/exchange$/.test(path)) {
        header = titleMap[3];
        header.title.des = '换货申请';
        header.right.action = 'submitForm';
        return header;
    }

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

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

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

    if (/\/me\/return\/logistics$/.test(path)) {
        header = titleMap[1];
        header.title.des = '商品寄回';
        return header;
    }

    if (/\/me\/order\/detail$/.test(path)) {
        header = titleMap[2];
        header.title.des = '订单详情';
        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 (/\/help$/.test(path)) {
        header = titleMap[1];
        header.title.des = '帮助中心';
        return header;
    }

    if (/\/help-detail$/.test(path)) {
        header = titleMap[1];
        header.title.des = qs.title; // 帮助中心详细
        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;
    }

    if (/\/me\/provisions$/.test(path)) {
        header = titleMap[1];
        header.title.des = '隐私条款';
        return header;
    }

    if (/\/me\/records/.test(path)) {
        header = titleMap[3];
        header.title.des = '浏览记录';
        header.right = {
            des: '清空',
            action: 'clearRecords'
        };
        if (qs.type === '0') {
            header.right.des = '';
        }
        return header;
    }

    return header;
};

const intercept = (url) => {
    if (yoho.isApp) {
        let titleMap = Object.assign({}, defaultTitleMap);
        let [path, qs] = url.split('?');

        qs = parse(qs);

        // 个人中心收藏
        if (/\/me\/collection$/.test(path)) {
            let header = titleMap[5];

            if (qs.tab === 'article') {
                header.defaultSelectedIndex = '1';
            } else {
                header.defaultSelectedIndex = '0';
            }
            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 (/\/cate$/.test(path)) {
            return yoho.goTab({
                index: 1,
                headerIndex: 1
            });
        }

        // 首页
        if (/\/$/.test(path) || !path) {
            return yoho.goTab({
                index: 0,
                url: /^(https?:)?\/\//i.test(url) ? url : origin + url
            });
        }

        const args = {
            // header: matchHeader(path, qs, titleMap),
            url: /^(https?:)?\/\//i.test(url) ? url : origin + url
        };

        if (/\/me\/service$/.test(path)) {
            // args.header = titleMap[1];
            // args.header.title.des = 'BLK在线客服';
            args.showLoading = 'no';
        }

        // 处理 feature.yoho.cn 等域名下的站外链接
        if (/^(https?:)?\/\//i.test(path) && !/m\.yohoblk\.com/.test(path)) {
            // args.header = titleMap[1];
            // args.header.title.des = qs.title || '';
            args.showLoading = 'no';
        }
        args.header = {
            headerid: '-1'
        };
        yoho.goNewPage(args);
    } else {
        location.href = url;
    }
};

export default {
    defaultTitleMap,
    intercept
};