sub-domain.js 6.81 KB
/**
 * 匹配subdomain
 * @author: 杨延青<yanqing.yang@yoho.cn>
 * @date: 2016/6/16
 */
'use strict';
const querystring = require('querystring');
const _ = require('lodash');
const helpers = global.yoho.helpers;
const utils = '../../utils';
const listParamsProcess = require(`${utils}/list-params-process`);

module.exports = () => {
    return (req, res, next) => {
        if (req.hostname === 'activity.yoho.cn') {
            // 活动模版的活动页
            if (req.path === '/') {
                return res.redirect('//m.yohobuy.com');
            }
            if (req.path !== '/.well-known/apple-app-site-association') {
                req.url = `/activity${req.url}`;
            }
        } else if (req.subdomains.length) {
            switch (req.subdomains[0]) {
                case 'guang':
                case 'cdnsrcguang': // CDN 逛 回源地址
                    req.url = req.url.replace('/guang', '');
                    req.url = `//m.yohobuy.com/guang${req.url}`;
                    return res.redirect(301, req.url);
                case 'list': // list
                case 'cdnsrclist':// CDN list 回源域名
                    return res.redirect(301, listParamsProcess.generatePathUrl(req.query));
                case 'search': // search
                    // 有查询关键字
                    if (_.keys(req.query).length) {
                        return res.redirect(301, `//m.yohobuy.com/search/list?${querystring.stringify(req.query)}`);
                    } else {
                        return res.redirect(301, '//m.yohobuy.com/search');
                    }

                    // 已经废弃,只是对老页面做兼容 --start
                case 'sale': // sale 跳转到 m.yohobuy.com/product/sale
                    if (_.keys(req.query).length) {
                        req.query.title = '专区活动';

                        delete req.query['openby:yohobuy'];
                        res.redirect(301, helpers.urlFormat('/', req.query, 'list'));
                    } else {
                        res.redirect(301, helpers.urlFormat('/product/sale', req.query, 'default'));
                    }
                    return;
                case 'cart': // 购物车 跳转到 m.yohobuy.com/cart/index/index
                    res.redirect(301, helpers.urlFormat('/cart/index/index', req.query, 'default'));
                    return;
                case 'new': // new.m.yohobuy.com 全部跳转到 m.yohobuy.com
                    if (req.path === '/hotrank') {
                        res.redirect(301, helpers.urlFormat('/product/newsale/hotrank', req.query, 'default'));
                        return;
                    }
                    res.redirect(301, helpers.urlFormat(req.path, req.query, 'default'));
                    return;
                case 'item': // item.m.yohobuy.com 全部跳转到 m.yohobuy.com
                    res.redirect(301, helpers.urlFormat(req.path, req.query, 'default'));
                    return;

                    // 已经废弃,只是对老页面做兼容 --end

                default: // 其它(识别为品牌)
                    if (req.path === '/') {
                        req.query = Object.assign(req.query, {
                            domain: req.subdomains[0]
                        });
                        req.url = `/product/shop?${querystring.stringify(req.query)}`;
                    }
                    break;
            }
        } else {
            switch (req.path.split('/')[1]) {

                // case 'guang':
                // case 'cdnsrcguang': // CDN 逛 回源地址
                //     req.url = req.url.replace('/guang', '');
                //     req.url = `//m.yohobuy.com/guang${req.url}`;
                //     return res.redirect(301, req.url);
                case 'list': // list
                case 'cdnsrclist':// CDN list 回源域名
                    // return res.redirect(301, listParamsProcess.generatePathUrl(req.query));
                    req.query = listParamsProcess.generatePathUrl(req.query);
                    break;

                /**
                 * 搜索配置:落地页 /search
                 *         结果页 /search/list
                 */

                // case 'search': // search
                // // /search/ 跳转list
                // // /search 跳转搜索页
                //     // 有查询关键字
                //     if (req.path.endsWith('/search/')) {
                //         return res.redirect(301, `//m.yohobuy.com/search/list?${querystring.stringify(req.query)}`);
                //     }
                //     break;

                    // 已经废弃,只是对老页面做兼容 --start

                case 'sale': // sale 跳转到 m.yohobuy.com/product/sale
                    if (_.keys(req.query).length) {
                        req.query.title = '专区活动';

                        delete req.query['openby:yohobuy'];
                        res.redirect(301, helpers.urlFormat('/', req.query, 'list'));
                    } else {
                        res.redirect(301, helpers.urlFormat('/product/sale', req.query, 'default'));
                    }
                    return;
                case 'new':
                    // new.m.yohobuy.com/a/b/c 全部跳转到 m.yohobuy.com/new/a/b/c
                    // m.yohobuy.com/new/hotrank
                    if (req.path.startsWith('/new/hotrank')) {
                        // res.redirect(301, helpers.urlFormat('/product/newsale/hotrank', req.query, 'default'));
                        req.url = `/product/newsale/hotrank?${querystring.stringify(req.query)}`;
                        break;
                    }
                    req.url = `${req.path.repalce(/^\/new/, '')}?${querystring.stringify(req.query)}`;
                    break;

                    // res.redirect(301, helpers.urlFormat(req.path, req.query, 'default'));

                case 'item': // item.m.yohobuy.com 全部跳转到 m.yohobuy.com
                    // res.redirect(301, helpers.urlFormat(req.path, req.query, 'default'));
                    req.url = `${req.path.repalce(/^\/item/, '')}?${querystring.stringify(req.query)}`;
                    break;

                    // 已经废弃,只是对老页面做兼容 --end

                default: // 其它(识别为品牌)
                    // if (req.path === '/') {
                    //     console.log('------req.subdomains------');
                    //     req.query = Object.assign(req.query, {
                    //         domain: req.subdomains[0]
                    //     });
                    //     req.url = `/product/shop?${querystring.stringify(req.query)}`;
                    //     console.log('------req.url------', req.url);

                    // }
                    break;
            }
        }

        next();
    };
};