mobile-refer.js
2.22 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
/**
* page cache 前端js跳转url中间件
*/
'use strict';
const _ = require('lodash');
const querystring = require('querystring');
const ptm = {
// 首页
'/': '',
'/woman': '/girls',
'/kids': '/kids',
'/lifestyle': '/lifestyle',
// 登录注册
'/signin.html': '/signin.htm',
'/reg.html': '/passport/reg/index',
'/passport/back/index': '/passport/back/mobile',
// 列表
'/product/list/new': '/product/new',
// 品牌列表
'/brands': '/brands',
// 领券中心
'/coupon/index': '/coupon/floor?title=领券中心&code=b78b32ed81b18dde8ac84fd33602b88b'
};
module.exports = () => {
return (req, res, next) => {
let domain = 'm.yohobuy.com';
let proRegNew = /^\/product\/p([\d]+).html(.*)/,
guangReg = /^\/guang/,
guangDetailReg = /\/guang\/info\/index/,
qsReg = /\?/;
if (!req.xhr) {
let url = _.head(_.split(req.url, '?'));
let data = {};
if (ptm.hasOwnProperty(url)) {
data.mobileRefer = `//${domain}${ptm[url]}`;
} else if (url === '/product/list/index') {
data.mobileRefer = `//list.${domain}`;
} else if (url === '/product/index/brand') {
data.mobileRefer = `//${req.query.domain}.${domain}`;
} else if (url === '/product/search/index') {
data.mobileRefer = `//search.${domain}`;
} else if (guangReg.test(url)) {
data.mobileRefer = `//guang.${domain}`;
if (guangDetailReg.test(url)) {
data.mobileRefer += url.replace(/\/guang\/info\/index/, '/info/index');
} else if (url === '/guang/Index/editor') {
data.mobileRefer += `/author/index?id=${req.query.author_id}`;
}
} else if (proRegNew.text(url)) {
data.mobileRefer = url.replace(proRegNew, `//${domain}/product/pro_$1_1/1.html$2`);
}
if (!_.isEmpty(req.query) && !qsReg.test(data.mobileRefer)) {
data.mobileRefer += `?${querystring.stringify(req.query)}`;
}
Object.assign(res.locals, data);
}
next();
};
};