Authored by htoooth

fix htaccess

... ... @@ -149,6 +149,7 @@ const logger = global.yoho.logger;
// dispatcher
try {
const htaccess = require('./doraemon/middleware/htaccess');
const subDomain = require('./doraemon/middleware/sub-domain');
const mobileRefer = require('./doraemon/middleware/mobile-refer');
const mobileCheck = require('./doraemon/middleware/mobile-check');
... ... @@ -162,6 +163,7 @@ try {
const devtool = require('./doraemon/middleware/devtools');
// YOHO 前置中间件
app.use(htaccess());
app.use(subDomain());
app.use(mobileRefer());
app.use(mobileCheck());
... ...
... ... @@ -58,7 +58,7 @@ exports.index = (req, res, next) => {
pageSize: pageSize,
type: type,
pathNav: pathNav,
baseUrl: `?${querystring.stringify(req.query)}`,
baseUrl: helpers.urlFormat('/' + channel + `-t${type}`, null, 'guang'),
page: page,
total: (ret[2] && ret[2].total) || 0
},
... ... @@ -169,7 +169,7 @@ exports.editor = (req, res, next) => {
exRecos: ret[4],
gender: gender,
baseUrl: `?${querystring.stringify(req.query)}`,
baseUrl: helpers.urlFormat('/' + channel + `-author-i${authorId}`, null, 'guang'),
pageSize: pageSize,
pathNav: pathNav,
page: page,
... ...
/**
* guang
* @author: lcy<chuanyang.liu@yoho.cn>
* @date: 2016/09/01
*/
'use strict';
let _ = require('lodash');
let Handlebars = require('handlebars');
let ALL_TYPES = {
stand: 'p-page-n',
mini: 'p-n',
full: 'f-p-page-n-l-info-input-gobtn',
fullellipsis: 'f-p-pe-n-l-info-input-gobtn',
ellipsis: 'p-pe-n'
};
exports.spager = function() {
let options = arguments[arguments.length - 1];
let baseUrl = (arguments.length > 1 ? arguments[0] : null) || options.hash.baseUrl || '',
page = (options.hash.page || 1) * 1,
showNum = (options.hash.showNum || 7) * 1,
pageSize = options.hash.pageSize || 20,
totalPages = options.hash.totalPages,
totalRecords = options.hash.totalRecords,
theme = options.hash.theme || 'pager',
currentClass = options.hash.currentClass || 'cur',
// min full stand ellipsis or: f-首页, p-上一页, page-页码,n-下一页,l-最后一页,info-displayMsg,inout输入框,gobtn-goto Btn
type = options.hash.type || 'stand',
pageVar = options.hash.pageVar || 'page',
pageSizeVar = options.hash.pageSizeVar || 'pageSize';
// 清除原来page(page=1&) 重新定义page
let clearPageReg = new RegExp(pageVar + '=[^&]*(&|$)'),
clearSizeReg = new RegExp(pageSizeVar + '=[^&]*(&|$)'),
base = baseUrl.replace(clearPageReg, '');
if (options.hash.pageSize) {
base = base.replace(clearSizeReg, '');
}
base += '-' + (options.hash.pageSize ? (pageSizeVar + pageSize + '-') : '') + pageVar;
console.log(base);
function getPageNums(ntype) {
var pageNums = [];
var num = showNum;
if (ntype === 'e') {
num = num - 2;
num = num > 2 ? num : 2;
}
/** 分页展示页码个数begin 规则:展示最靠近当前页的指定个数 **/
let pageShowMax = num % 2 === 0 ? page - 1 : page;
let pageShowMin = page;
for (let i = 0; i < Math.floor(num / 2); i++) {
pageShowMax++;
pageShowMin--;
if (pageShowMax > totalPages) {
pageShowMax = totalPages;
if (pageShowMin > 1) {
pageShowMin--;
}
}
if (pageShowMin < 1) {
pageShowMin = 1;
if (pageShowMax < totalPages) {
pageShowMax++;
}
}
}
for (let n = pageShowMin; n <= pageShowMax; n++) {
pageNums.push(n);
}
if (ntype === 'e') {
if (pageShowMin > 3) {
pageNums.unshift(1, '.');
} else if (pageShowMin === 2) {
pageNums.unshift(1);
} else if (pageShowMin === 3) {
pageNums.unshift(1, 2);
}
if (pageShowMax < totalPages - 2) {
pageNums.push('.', totalPages);
} else {
for (let x = pageShowMax + 1; x <= totalPages; x++) {
pageNums.push(x);
}
}
}
return pageNums;
}
function renderItem(arr) {
/** 分页展示页码个数end **/
let ret = '';
if (_.isArray(arr) && arr.length) {
arr.forEach((val) => {
if (val === '.') {
ret += '<a>...</a>';
} else {
ret += `<a href="${base}${val}/"` +
(page === val ? `class="${currentClass}"` : '') +
` title="第${val}页">${val}</a>`;
}
});
}
return ret;
}
function createStandItems() {
return renderItem(getPageNums());
}
function createEllipsisItems() {
return renderItem(getPageNums('e'));
}
if (!totalPages) {
if (!totalRecords || !pageSize) {
return new Handlebars.SafeString('');
} else {
totalPages = Math.ceil(totalRecords / pageSize);
}
}
let items = ALL_TYPES[type] ? ALL_TYPES[type] : type;
items = _.isArray(items) ? items : (items || ALL_TYPES.stand).split('-');
let hasPage = false; // 配置中如果配置了多次 page/pe 则将忽略,只第一次有效
let ret = `<div class="pager ${theme}">`;
items.forEach(function(val) {
switch (val) {
case 'f' :
if (page > 1) {
ret += `<a href="${base}1/" title="首页">首页</a>`;
}
break;
case 'p' :
if (page > 1) {
ret += `<a href="${base}` + (page - 1) + '/' +
'" title="上一页"><span class="iconfont">&#xe60e;</span>上一页</a>';
}
break;
case 'n' :
if (page < totalPages) {
ret += `<a href="${base}` + (page + 1) + '/' +
'" title="下一页">下一页<span class="iconfont">&#xe60c;</span></a>';
}
break;
case 'l' :
if (page < totalPages) {
ret += `<a href="${base}${totalPages}" title="尾页">尾页</a>`;
}
break;
case 'info' :
ret += '共{totalRecords}条/{totalPages}页';
break;
case 'input' :
// input
// ret += '<span class="{cls}"><input value="{value}" type="text"></span>';
break;
case 'gobtn' :
// goto btn
break;
case 'page' :
if (!hasPage) {
ret += createStandItems();
}
hasPage = true;
break;
case 'pe' :
if (!hasPage) {
ret += createEllipsisItems();
}
hasPage = true;
break;
}
});
if (options.fn) {
ret += options.fn(options.context);
}
ret += '</div>';
return new Handlebars.SafeString(ret);
};
... ...
... ... @@ -24,7 +24,7 @@
layoutsDir: doraemon,
partialsDir: [path.join(__dirname, 'views/partial')],
views: path.join(__dirname, 'views/action'),
helpers: Object.assign(require('./helpers/pager'), global.yoho.helpers)
helpers: Object.assign(require('./helpers/pager'), require('./helpers/pager-seo'), global.yoho.helpers)
}));
// require('./helpers/json');
... ...
... ... @@ -8,6 +8,7 @@
const _ = require('lodash');
const moment = require('moment');
const ghelper = require('./guang-helper');
const urlHelper = require('./urlHelper');
const helpers = global.yoho.helpers;
const serviceApi = global.yoho.ServiceAPI;
... ... @@ -166,15 +167,7 @@ const _formatArticle = (articleData, showTag, showAuthor, channel) => {
let authorId = articleData.author.author_id;
if (authorId) {
let aparam = {
author_id: authorId
};
if (channel) {
aparam.channel = channel;
}
result.editorUrl = helpers.urlFormat('/Index/editor', aparam, 'guang');
result.editorUrl = urlHelper.editorUrl(channel, authorId);
}
let tags = [];
... ... @@ -439,7 +432,7 @@ const getCategory = (currentSortId, channel) => {
typeId: cat.id,
type: cat.name,
isActive: String(cat.id) === String(currentSortId),
navUrl: helpers.urlFormat('/index/index', param, 'guang')
navUrl: urlHelper.listUrl(channel, cat.id)
});
}
}
... ...
/**
* Created by TaoHuang on 2017/2/21.
*/
'use strict';
const helpers = global.yoho.helpers;
module.exports.editorUrl = function(channel, authorId) {
return helpers.urlFormat(`/${channel}-author-i${authorId}/`, null, 'guang');
};
module.exports.listUrl = function(channel, type) {
return helpers.urlFormat(`/${channel}-t${type}/`, null, 'guang');
};
... ...
... ... @@ -20,7 +20,7 @@
{{/ msgs}}
</div>
<div class="msg-pager pager">
{{gpager baseUrl totalRecords=total page=page type="ellipsis" theme="msg-pager"}}
{{spager baseUrl totalRecords=total page=page pageVar="p" type="ellipsis" theme="msg-pager"}}
</div>
</div>
<div class="right-side">
... ...
... ... @@ -30,7 +30,7 @@
{{> msg}}
{{/ msgs}}
</div>
{{gpager baseUrl totalRecords=total page=page type="ellipsis" theme="msg-pager"}}
{{spager baseUrl totalRecords=total page=page pageVar="p" type="ellipsis" theme="msg-pager"}}
</div>
</div>
<div class="right-side">
... ...
/**
* Created by TaoHuang on 2017/2/21.
*/
'use strict';
/**
* url
* @type {*[]}
*/
module.exports = [
// 首页
{
type: 'rewrite',
origin: /^\/(boys|girls|kids|lifestyle)$/,
target: (req, match, p1) => {
req.query.channel = p1;
return `/?chanel=${p1}`;
}
},
// 首页 + 类型
{
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}`;
}
},
// 列表页 + 类型 + 翻页
{
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}`;
}
},
// 编缉首页
{
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}`;
}
},
// 编缉首页 + 翻页
{
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}`;
}
}
];
... ...
/**
* Created by TaoHuang on 2017/2/21.
*/
'use strict';
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const curDir = __dirname;
const files = fs.readdirSync(curDir);
let domainRules = {};
files.forEach((file) => {
let info = fs.statSync(path.resolve(curDir, file));
if (info.isDirectory() && file !== '.git') {
domainRules[file] = require(path.resolve(curDir, file));
}
});
module.exports = () => {
return (req, res, next) => {
let curDomainRules = domainRules[req.subdomains[0]];
if (!curDomainRules) {
return next();
}
for (let i = 0; i < curDomainRules.length; i++) {
let rule = curDomainRules[i];
if (
(_.isRegExp(rule.origin) && !_.isEmpty(rule.origin.exec(req.url))) ||
(_.isFunction(rule.origin) && rule.origin(req))
) {
let newUrl = req.url;
if (_.isRegExp(rule.origin)) {
if (_.isFunction(rule.target)) {
newUrl = req.url.replace(rule.origin, _.partial(rule.target, req));
} else if (_.isString(rule.target)) {
newUrl = req.url.replace(rule.origin, req.target);
}
} else if (_.isFunction(rule.origin)) {
if (_.isFunction(rule.target)) {
newUrl = rule.target(req);
} else if (_.isString(rule.target)) {
newUrl = rule.target;
}
}
if (rule.type === '301') {
return res.redirect(301, newUrl);
} else if (rule.type === 'rewrite') {
req.url = newUrl;
return next();
}
break;
}
}
return next();
};
};
... ...
/**
* Created by TaoHuang on 2017/2/21.
*/
'use strict;'
module.exports = [
{ // 商品详情页老链接
type: '301',
origin: /^\/product\/pro_([\d]+)_([\d]+)\/(.*).html(.*)/,
target: (req, match, p1, p2, p3, p4) => `/p${p1}.html${p4}`
},
{ // 商品详情页新链接
type: 'rewrite',
origin: /^\/p([\d]+)/,
target: req => `/product${req.url}`
}
];
... ...
/**
* Created by TaoHuang on 2017/2/21.
*/
'use strict';
const helpers = global.yoho.helpers;
module.exports = [
{
type: '301',
origin: /.*/,
target: req => helpers.urlFormat(req.url, null, 'www')
}
];
... ...
/**
* Created by TaoHuang on 2017/2/22.
*/
'use strict';
const helpers = global.yoho.helpers;
module.exports = [
{ // 商品详情页老链接
type: '301',
origin: /^\/product\/pro_([\d]+)_([\d]+)\/(.*).html(.*)/,
target: (req, match, p1, p2, p3, p4) => helpers.urlFormat(`/p${p1}.html${p4}`, null, 'item')
},
{ // 商品详情页新链接
type: '301',
origin: /\/p([\d]+)(.*)/,
target: req => helpers.urlFormat(req.url, null, 'item')
}
];
... ...
... ... @@ -20,20 +20,8 @@ module.exports = () => {
case 'www': // 主站
case 'cdnsrcwww': // 主站的回源地址
case 'shop': // 商家入驻
break;
case 'new': // 原新版 重定向到301
return res.redirect(301, helpers.urlFormat(req.url, null, 'www'));
case 'item':// 商品详情页
if (/^\/p([\d]+)/.test(req.url)) { // new
req.url = `/product${req.url}`;
}
if (/^\/product\/pro_/.test(req.url)) { // old
return res.redirect(301,
req.url.replace(/^\/product\/pro_([\d]+)_([\d]+)\/(.*).html(.*)/, '/p$1.html$4')
);
}
break;
case 'guang': // 逛
case 'cdnsrcguang': // 逛CDN回源解析
... ...