sub-domain.js
4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* 匹配subdomain
* @author: 杨延青<yanqing.yang@yoho.cn>
* @date: 2016/6/16
*/
'use strict';
const querystring = require('querystring');
const _ = require('lodash');
const helpers = global.yoho.helpers;
module.exports = () => {
return (req, res, next) => {
if (/^\/home\/orders\/paynew/.test(req.url)) {
// 支付中心,由于微信安全配置限制在 home/orders 路径下,需要转发,误删!!!
req.url = `/cart${req.url}`;
}
if (/^\/coupon\/floor/.test(req.url)) {
// 领券中心 兼容php的url
req.url = `/activity${req.url}`;
}
if (/^\/shopping\/pay\/aliwapreturn/.test(req.url)) {
// 兼容php的url
req.url = `/cart${req.url}`;
}
if (/^\/sale/.test(req.url)) {
// sale 兼容php的url
res.redirect(301, helpers.urlFormat('/product/sale', req.query, 'default'));
return;
}
if (req.hostname === 'activity.yoho.cn') {
// 活动模版的活动页
if (req.path === '/') {
return res.redirect('//m.yohobuy.com');
}
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 = `/guang${req.url}`;
break;
case 'list': // list
case 'cdnsrclist':// CDN list 回源域名
if (req.path === '/') {
req.url = `/product/index/index?${querystring.stringify(req.query)}`;
}
break;
case 'search': // search
if (req.path === '/') {
// 有查询关键字
if (_.keys(req.query).length) {
req.url = `/product/search/list?${querystring.stringify(req.query)}`;
} else {
req.url = `/product/search/index?${querystring.stringify(req.query)}`;
}
}
if (req.path === '/search') {
req.url = `/product/search/index?${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 '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/index/brand?${querystring.stringify(req.query)}`;
}
break;
}
}
next();
};
};