Authored by htoooth

refactor

... ... @@ -46,8 +46,6 @@ exports.spager = function() {
base += '-' + (options.hash.pageSize ? (pageSizeVar + pageSize + '-') : '') + pageVar;
console.log(base);
function getPageNums(ntype) {
var pageNums = [];
var num = showNum;
... ...
... ... @@ -4,65 +4,66 @@
'use strict';
/**
* url
* @type {*[]}
*/
const TYPE = require('../type');
const MOBILE_DOMAIN = '//guang.m.yohobuy.com';
module.exports = [
// 首页
{
type: 'rewrite',
origin: /^\/(boys|girls|kids|lifestyle)$/,
type: TYPE.rewrite,
origin: /^\/(boys|gjirls|kids|lifestyle)$/,
target: (req, match, p1) => {
req.query.channel = p1;
return `/?chanel=${p1}`;
return `/guang/?chanel=${p1}`;
}
},
// 首页 + 类型
{
type: 'rewrite',
type: TYPE.rewrite,
origin: /^\/(boys|girls|kids|lifestyle)-t([\d]+)\/$/,
target: (req, match, p1, p2) => {
req.query.channel = p1;
req.query.type = p2;
return `/?chanel=${p1}&type=${p2}`;
return `/guang/?chanel=${p1}&type=${p2}`;
}
},
// 列表页 + 类型 + 翻页
{
type: 'rewrite',
type: TYPE.rewrite,
origin: /^\/(boys|girls|kids|lifestyle)-t([\d]+)-p([\d]+)\/$/,
target: (req, match, p1, p2, p3) => {
req.query.channel = p1;
req.query.type = p2;
req.query.page = p3;
return `/index/index/?type=${p2}&channel=${p3}`;
return `/guang/index/index/?type=${p2}&channel=${p3}`;
}
},
// 编缉首页
{
type: 'rewrite',
type: TYPE.rewrite,
origin: /^\/(boys|girls|kids|lifestyle)-author-i([\d]+)\/$/,
target: (req, match, p1, p2) => {
req.query.channel = p1;
req.query.author_id = p2;
return `/index/editor?channel=${p1}&author_id=${p2}`;
req.mobileUrl = `${MOBILE_DOMAIN}/author/${p2}/`;
return `/guang/index/editor?channel=${p1}&author_id=${p2}`;
}
},
// 编缉首页 + 翻页
{
type: 'rewrite',
type: TYPE.rewrite,
origin: /^\/(boys|girls|kids|lifestyle)-author-i([\d]+)-p([\d]+)\/$/,
target: (req, match, p1, p2, p3) => {
req.query.channel = p1;
req.query.author_id = p2;
req.query.page = p3;
return `/index/editor?channel=${p1}&author_id=${p2}&page=${p3}`;
req.mobileUrl = `${MOBILE_DOMAIN}/author/${p2}`;
return `/guang/index/editor?channel=${p1}&author_id=${p2}&page=${p3}`;
}
}
... ...
/**
* 后端:改写和跳转:中间件。
* Created by TaoHuang on 2017/2/21.
*/
... ... @@ -8,6 +9,8 @@ const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const TYPE = require('./type');
const curDir = __dirname;
const files = fs.readdirSync(curDir);
... ... @@ -29,6 +32,14 @@ files.forEach((file) => {
*/
module.exports = () => {
return (req, res, next) => {
let isMobile = /(nokia|iphone|android|ipad|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220)/i.test(req.get('user-agent')); // eslint-disable-line
req.isMobile = isMobile;
if (req.xhr) {
return next();
}
let curDomainRules = domainRules[req.subdomains[0]];
if (!curDomainRules) {
... ... @@ -65,9 +76,9 @@ module.exports = () => {
}
}
if (rule.type === '301') {
if (rule.type === TYPE.redirect) {
return res.redirect(301, newUrl);
} else if (rule.type === 'rewrite') {
} else if (rule.type === TYPE.rewrite) {
req.url = newUrl;
return next();
}
... ...
... ... @@ -3,18 +3,26 @@
*/
'use strict;'
const TYPE = require('../type');
module.exports = [
// 商品详情页老链接
{
type: '301',
type: TYPE.redirect,
origin: /^\/product\/pro_([\d]+)_([\d]+)\/(.*).html(.*)/,
target: (req, match, p1, p2, p3, p4) => `/p${p1}.html${p4}`
target: (req, match, p1, p2, p3, p4) => {
req.mobileUrl = `/product/pro_${p1}_1/1.html${p4}`;
return `/p${p1}.html${p4}`;
}
},
// 商品详情页新链接
{
type: 'rewrite',
origin: /^\/p([\d]+)/,
target: req => `/product${req.url}`
type: TYPE.rewrite,
origin: /^\/p([\d]+).html(.*)/,
target: (req, match, p1, p2) => {
req.mobileUrl = `/product/pro_${p1}_1/1.html${p2}`;
return `/product${req.url}`;
}
}
];
... ...
... ... @@ -4,10 +4,11 @@
'use strict';
const helpers = global.yoho.helpers;
const TYPE = require('../type');
module.exports = [
{
type: '301',
type: TYPE.redirect,
origin: /.*/,
target: req => helpers.urlFormat(req.url, null, 'www')
}
... ...
/**
* Created by TaoHuang on 2017/2/22.
*/
'use strict';
module.exports = {
redirect: '301',
rewrite: 'rewrite'
};
... ...
... ... @@ -5,18 +5,19 @@
'use strict';
const helpers = global.yoho.helpers;
const TYPE = require('../type');
module.exports = [
// 商品详情页老链接
{
type: '301',
type: TYPE.redirect,
origin: /^\/product\/pro_([\d]+)_([\d]+)\/(.*).html(.*)/,
target: (req, match, p1, p2, p3, p4) => helpers.urlFormat(`/p${p1}.html${p4}`, null, 'item')
},
// 商品详情页新链接
{
type: '301',
type: TYPE.redirect,
origin: /\/p([\d]+)(.*)/,
target: req => helpers.urlFormat(req.url, null, 'item')
}
... ...
... ... @@ -4,13 +4,10 @@
'use strict';
module.exports = () => {
return (req, res, next) => {
let isMobile = /(nokia|iphone|android|ipad|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220)/i.test(req.get('user-agent')); // eslint-disable-line
if (isMobile) {
req.url = req.url.replace(/^\/product\/p([\d]+).html(.*)/, '/product/pro_$1_1/1.html$2');
return res.redirect(`//m.yohobuy.com${req.url}`);
if (req.isMobile) {
return res.redirect(req.mobileUrl || `//m.yohobuy.com${req.url}`);
}
next();
return next();
};
};
... ...
... ... @@ -54,8 +54,8 @@ module.exports = () => {
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 (url === '/guang/index/editor') {
data.mobileRefer += `/author/${req.query.author_id}/`;
}
} else if (proRegNew.test(url)) {
data.mobileRefer = url.replace(proRegNew, `//${domain}/product/pro_$1_1/1.html$2`);
... ...