Blame view

doraemon/middleware/sub-domain.js 3.46 KB
刘传洋 authored
1 2 3 4 5 6
/**
 * 匹配subdomain
 * @author: 杨延青<yanqing.yang@yoho.cn>
 * @date: 2016/6/16
 */
'use strict';
htoooth authored
7 8
const _ = require('lodash');
const qs = require('querystring');
刘传洋 authored
9 10 11

module.exports = () => {
    return (req, res, next) => {
yyq authored
12
        if (req.subdomains.length) {
刘传洋 authored
13 14
            switch (req.subdomains[0]) {
                case 'www': // 主站
周奇琪 authored
15
                case 'cdnsrcwww': // 主站的回源地址
刘传洋 authored
16
                case 'shop': // 商家入驻
htoooth authored
17
                case 'new':
刘传洋 authored
18 19
                case 'item':// 商品详情页
                    break;
刘传洋 authored
20
                case 'guang': // 逛
21
                case 'cdnsrcguang': // 逛CDN回源解析
htoooth authored
22 23 24 25
                {  // eslint-disable-line
                    let guangReg = /^\/guang/;
                    let guangDetailReg = /.html$/;
刘传洋 authored
26
                    if (guangDetailReg.test(req.path)) {
27 28 29 30 31 32 33
                        // 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;
刘传洋 authored
34
                    } else if (!guangReg.test(req.path)) {
刘传洋 authored
35
                        req.url = '/guang' + req.url;
刘传洋 authored
36 37
                    }
                    break;
htoooth authored
38 39 40 41 42
                }
                case 'search':  // 搜索
                { // eslint-disable-line
                    let searchReg = /^\/product\//;
43 44 45 46 47 48
                    if (!searchReg.test(req.path)) {
                        if (req.path === '/api/suggest') {
                            req.url = '/product/api/suggest';
                        } else {
                            req.url = '/product/search/index';
                        }
刘传洋 authored
49 50
                    }
                    break;
htoooth authored
51
                }
刘传洋 authored
52
                case 'list': // 商品列表
53
                case 'cdnsrclist': // 商品列表CDN回源解析
htoooth authored
54
                { // eslint-disable-line
yyq authored
55
                    if (!req.path || req.path === '/') {
yyq authored
56
                        req.url = '/product/list/index';
yyq authored
57 58 59
                    } else if (req.path === '/new') {
                        req.url = '/product/list/new';
                    }
刘传洋 authored
60
                    break;
htoooth authored
61
                }
刘传洋 authored
62
                case 'sale': // 促销
63
                case 'cdnsrcsale': // 促销CDN回源解析
htoooth authored
64
                { // eslint-disable-line
yyq authored
65 66 67
                    if (!req.path || req.path === '/') {
                        req.url = '/product/sale/special/detail';
                    }
刘传洋 authored
68
                    break;
htoooth authored
69
                }
刘传洋 authored
70
                default: // 其它(识别为品牌)
htoooth authored
71
                { // eslint-disable-line
htoooth authored
72 73 74 75 76 77 78 79
                    let queryString = (function() {
                        if (!_.isEmpty(req.query)) {
                            return '&' + qs.stringify(req.query);
                        } else {
                            return '';
                        }
                    }());
yyq authored
80
                    req.query.domain = req.subdomains[0];
刘传洋 authored
81
                    if (!req.path || req.path === '/') {
htoooth authored
82
                        req.url = `/product/index/brand?domain=${req.subdomains[0]}${queryString}`;
刘传洋 authored
83
                    } else if (req.path === '/about') {
htoooth authored
84
                        req.url = `/product/index/brand?domain=${req.subdomains[0]}${queryString}`;
刘传洋 authored
85 86
                    }
                    break;
htoooth authored
87
                }
刘传洋 authored
88 89 90 91 92
            }
        }
        next();
    };
};