Authored by 郭成尧

router-sure

... ... @@ -19,19 +19,51 @@ const co = require('bluebird').coroutine;
const shop = {
onlyDomain(req, res, next) {
req.query.domain = req.params.domain;
shop.entry(req, res, next);
},
/**
* 老路由处理,查数据,拼链接
*/
redirect(req, res, next) {
let shopId = _.parseInt(stringProcess.paramsFilter(req.query.shop_id));
let domain = stringProcess.paramsFilter(req.query.domain);
let brandId = _.parseInt(stringProcess.paramsFilter(req.query.brand_id));
onlyShopId(req, res, next) {
req.query.shop_id = req.params[0];
shop.entry(req, res, next);
co(function* () {
if (shopId && domain) {
return res.redirect(301, `/shop/${domain}-${shopId}.html`);
}
if (shopId) {
let shopInfoApi = (yield req.ctx(shopModel).getShopInfo(shopId)) || {};
return res.redirect(301, `/shop/${_.get(shopInfoApi, 'data.shop_domain')}-${shopId}.html`);
}
if (domain) {
let domainInfo = (yield req.ctx(shopModel).getBrandLogoByDomain(domain)) || {}; // 通过域名查询店铺类型,或者品牌信息
if (domainInfo.shopId && domainInfo.type === '2') {
return res.redirect(301, `/shop/${domain}-${_.get(domainInfo, 'shopId')}.html`);
} else {
return res.redirect(301, helpers.urlFormat('/index/brand', {
brand_id: _.get(domainInfo, 'id')
}));
}
}
if (brandId) {
return res.redirect(301, helpers.urlFormat('/index/brand', { brand_id: brandId }));
}
})().catch(next);
},
rewrite(req, res, next) {
req.query.domain = req.params.domain;
req.query.shop_id = req.params.shop_id;
/**
* 新路由承接页,参数解析
*/
resolveParams(req, res, next) {
let pathParams = _.last(_.split(req.url.replace('.html', ''), '/'));
let shopId = _.last(_.split(pathParams, '-'));
req.query.domain = pathParams.replace(`-${shopId}`, '');
shop.entry(req, res, next);
},
... ...
... ... @@ -167,10 +167,9 @@ router.get('/index/category', list.shopCategory);
router.get('/index/getBrandCouponsList', list.getBrandCouponsList);
// 店铺重构
router.get('/shop', newShop.entry); // 统一店铺入口
router.get('/shop/:domain/:shop_id.html', newShop.rewrite); // 域名和店铺 ID 均存在
router.get(/^\/shop\/(\d+)\.html/, newShop.onlyShopId); // 只有店铺 ID 的情况
router.get('/shop/:domain.html', newShop.onlyDomain); // 只有域名的情况
router.get('/shop', newShop.redirect); // 老路由重定向前置处理
router.get('/shop/:domain-:shop_id.html', newShop.resolveParams); // 新入口
router.get('/index/brand', newShop.entry); // 旧的路由,走到新的 controller
router.get('/index/brandFav', newShop.brandFav);
router.get('/new/shop/hotlist', newShop.shopHotList);
... ...
... ... @@ -71,21 +71,6 @@ module.exports = () => {
req.url = `/product/${req.url}`;
}
if (/^\/product\/shop/.test(req.url)) {
// 老路由 301 跳转到新路由
if (req.query.shop_id && req.query.domain) {
return res.redirect(301, `/shop/${req.query.domain}/${req.query.shop_id}.html`);
}
if (req.query.shop_id) {
return res.redirect(301, `/shop/${req.query.shop_id}.html`);
}
if (req.query.domain) {
return res.redirect(301, `/shop/${req.query.domain}.html`);
}
}
next();
};
};
... ...