sub-domain.js 3.58 KB
/**
 * 匹配subdomain
 * @author: 杨延青<yanqing.yang@yoho.cn>
 * @date: 2016/6/16
 */
'use strict';
const helpers = global.yoho.helpers;

module.exports = () => {
    return (req, res, next) => {
        let searchReg = /^\/product\//,
            guangReg = /^\/guang/,
            guangDetailReg = /.html$/;

        if (req.subdomains.length > 1 && req.subdomains[1] === 'www') {
            return res.redirect(301, helpers.urlFormat(req.path, req.query || '', req.subdomains[0]));
        }
        if (req.subdomains.length) {
            switch (req.subdomains[0]) {
                case 'www': // 主站
                case 'cdnsrcwww': // 主站的回源地址
                case 'shop': // 商家入驻
                    break;
                case 'new': // 原新版  重定向到301
                    return res.redirect(301, helpers.urlFormat(req.url, null, 'www'));
                case 'item':// 商品详情页
                    if (/^\/p([\d]+)/.test(req.url)) { // new
                        req.url = `/product${req.url}`;
                    }

                    if (/^\/product\/pro_/.test(req.url)) { // old
                        return res.redirect(301,
                            req.url.replace(/^\/product\/pro_([\d]+)_([\d]+)\/(.*).html(.*)/, '/p$1.html$4')
                        );
                    }

                    break;
                case 'guang': // 逛
                case 'cdnsrcguang': // 逛CDN回源解析
                    if (guangDetailReg.test(req.path)) {
                        // req.url = '/guang/detail' + req.url;
                        /\/([\d]*).html(\?)?(.*)/.exec(req.url);
                        req.url = '/guang/info/index?id=' + RegExp.$1;
                        if (RegExp.$3) {
                            req.url += '&' + RegExp.$3;
                        }
                        req.query.id = RegExp.$1;
                    } else if (!guangReg.test(req.path)) {
                        req.url = '/guang' + req.url;
                    }
                    break;
                case 'search': // 搜索
                    if (!searchReg.test(req.path)) {
                        if (req.path === '/api/suggest') {
                            req.url = '/product/api/suggest';
                        } else {
                            req.url = '/product/search/index';
                        }
                    }
                    break;
                case 'list': // 商品列表
                case 'cdnsrclist': // 商品列表CDN回源解析
                    if (!req.path || req.path === '/') {
                        req.url = '/product/list/index';
                    } else if (req.path === '/new') {
                        req.url = '/product/list/new';
                    }
                    break;
                case 'sale': // 促销
                case 'cdnsrcsale': // 促销CDN回源解析
                    if (!req.path || req.path === '/') {
                        req.url = '/product/sale/special/detail';
                    }
                    break;
                default: // 其它(识别为品牌)
                    if (!req.path || req.path === '/') {
                        req.url = `/product/index/brand?domain=${req.subdomains[0]}`;
                        req.query.domain = req.subdomains[0];
                    } else if (req.path === '/about') {
                        req.url = `/product/index/about?domain=${req.subdomains[0]}`;
                        req.query.domain = req.subdomains[0];
                    }
                    break;
            }
        }
        next();
    };
};