Showing
3 changed files
with
45 additions
and
29 deletions
@@ -19,19 +19,51 @@ const co = require('bluebird').coroutine; | @@ -19,19 +19,51 @@ const co = require('bluebird').coroutine; | ||
19 | 19 | ||
20 | const shop = { | 20 | const shop = { |
21 | 21 | ||
22 | - onlyDomain(req, res, next) { | ||
23 | - req.query.domain = req.params.domain; | ||
24 | - shop.entry(req, res, next); | ||
25 | - }, | 22 | + /** |
23 | + * 老路由处理,查数据,拼链接 | ||
24 | + */ | ||
25 | + redirect(req, res, next) { | ||
26 | + let shopId = _.parseInt(stringProcess.paramsFilter(req.query.shop_id)); | ||
27 | + let domain = stringProcess.paramsFilter(req.query.domain); | ||
28 | + let brandId = _.parseInt(stringProcess.paramsFilter(req.query.brand_id)); | ||
26 | 29 | ||
27 | - onlyShopId(req, res, next) { | ||
28 | - req.query.shop_id = req.params[0]; | ||
29 | - shop.entry(req, res, next); | 30 | + co(function* () { |
31 | + if (shopId && domain) { | ||
32 | + return res.redirect(301, `/shop/${domain}-${shopId}.html`); | ||
33 | + } | ||
34 | + | ||
35 | + if (shopId) { | ||
36 | + let shopInfoApi = (yield req.ctx(shopModel).getShopInfo(shopId)) || {}; | ||
37 | + | ||
38 | + return res.redirect(301, `/shop/${_.get(shopInfoApi, 'data.shop_domain')}-${shopId}.html`); | ||
39 | + } | ||
40 | + | ||
41 | + if (domain) { | ||
42 | + let domainInfo = (yield req.ctx(shopModel).getBrandLogoByDomain(domain)) || {}; // 通过域名查询店铺类型,或者品牌信息 | ||
43 | + | ||
44 | + if (domainInfo.shopId && domainInfo.type === '2') { | ||
45 | + return res.redirect(301, `/shop/${domain}-${_.get(domainInfo, 'shopId')}.html`); | ||
46 | + } else { | ||
47 | + return res.redirect(301, helpers.urlFormat('/index/brand', { | ||
48 | + brand_id: _.get(domainInfo, 'id') | ||
49 | + })); | ||
50 | + } | ||
51 | + } | ||
52 | + | ||
53 | + if (brandId) { | ||
54 | + return res.redirect(301, helpers.urlFormat('/index/brand', { brand_id: brandId })); | ||
55 | + } | ||
56 | + })().catch(next); | ||
30 | }, | 57 | }, |
31 | 58 | ||
32 | - rewrite(req, res, next) { | ||
33 | - req.query.domain = req.params.domain; | ||
34 | - req.query.shop_id = req.params.shop_id; | 59 | + /** |
60 | + * 新路由承接页,参数解析 | ||
61 | + */ | ||
62 | + resolveParams(req, res, next) { | ||
63 | + let pathParams = _.last(_.split(req.url.replace('.html', ''), '/')); | ||
64 | + let shopId = _.last(_.split(pathParams, '-')); | ||
65 | + | ||
66 | + req.query.domain = pathParams.replace(`-${shopId}`, ''); | ||
35 | shop.entry(req, res, next); | 67 | shop.entry(req, res, next); |
36 | }, | 68 | }, |
37 | 69 |
@@ -167,10 +167,9 @@ router.get('/index/category', list.shopCategory); | @@ -167,10 +167,9 @@ router.get('/index/category', list.shopCategory); | ||
167 | router.get('/index/getBrandCouponsList', list.getBrandCouponsList); | 167 | router.get('/index/getBrandCouponsList', list.getBrandCouponsList); |
168 | 168 | ||
169 | // 店铺重构 | 169 | // 店铺重构 |
170 | -router.get('/shop', newShop.entry); // 统一店铺入口 | ||
171 | -router.get('/shop/:domain/:shop_id.html', newShop.rewrite); // 域名和店铺 ID 均存在 | ||
172 | -router.get(/^\/shop\/(\d+)\.html/, newShop.onlyShopId); // 只有店铺 ID 的情况 | ||
173 | -router.get('/shop/:domain.html', newShop.onlyDomain); // 只有域名的情况 | 170 | +router.get('/shop', newShop.redirect); // 老路由重定向前置处理 |
171 | +router.get('/shop/:domain-:shop_id.html', newShop.resolveParams); // 新入口 | ||
172 | + | ||
174 | router.get('/index/brand', newShop.entry); // 旧的路由,走到新的 controller | 173 | router.get('/index/brand', newShop.entry); // 旧的路由,走到新的 controller |
175 | router.get('/index/brandFav', newShop.brandFav); | 174 | router.get('/index/brandFav', newShop.brandFav); |
176 | router.get('/new/shop/hotlist', newShop.shopHotList); | 175 | router.get('/new/shop/hotlist', newShop.shopHotList); |
@@ -71,21 +71,6 @@ module.exports = () => { | @@ -71,21 +71,6 @@ module.exports = () => { | ||
71 | req.url = `/product/${req.url}`; | 71 | req.url = `/product/${req.url}`; |
72 | } | 72 | } |
73 | 73 | ||
74 | - if (/^\/product\/shop/.test(req.url)) { | ||
75 | - // 老路由 301 跳转到新路由 | ||
76 | - if (req.query.shop_id && req.query.domain) { | ||
77 | - return res.redirect(301, `/shop/${req.query.domain}/${req.query.shop_id}.html`); | ||
78 | - } | ||
79 | - | ||
80 | - if (req.query.shop_id) { | ||
81 | - return res.redirect(301, `/shop/${req.query.shop_id}.html`); | ||
82 | - } | ||
83 | - | ||
84 | - if (req.query.domain) { | ||
85 | - return res.redirect(301, `/shop/${req.query.domain}.html`); | ||
86 | - } | ||
87 | - } | ||
88 | - | ||
89 | next(); | 74 | next(); |
90 | }; | 75 | }; |
91 | }; | 76 | }; |
-
Please register or login to post a comment