Showing
5 changed files
with
126 additions
and
4 deletions
@@ -8,6 +8,7 @@ const staticUrls = require('../../../config/staticUrls'); | @@ -8,6 +8,7 @@ const staticUrls = require('../../../config/staticUrls'); | ||
8 | const api = global.yoho.API; | 8 | const api = global.yoho.API; |
9 | const Service = global.yoho.ServiceAPI; | 9 | const Service = global.yoho.ServiceAPI; |
10 | const headerModel = require('../../../doraemon/models/header'); | 10 | const headerModel = require('../../../doraemon/models/header'); |
11 | +const siteMapService = require('../models/site-map-service'); | ||
11 | const redis = global.yoho.redis; | 12 | const redis = global.yoho.redis; |
12 | 13 | ||
13 | 14 | ||
@@ -156,6 +157,44 @@ const siteMap = (req, res, next) => { | @@ -156,6 +157,44 @@ const siteMap = (req, res, next) => { | ||
156 | }).catch(next); | 157 | }).catch(next); |
157 | }; | 158 | }; |
158 | 159 | ||
160 | +const _createSitemap = (req, res, rdata, startUrl, endUrl) => { | ||
161 | + sm.createSitemap({ | ||
162 | + hostname: 'https://www.yohobuy.com', | ||
163 | + xmlNs: ' ', | ||
164 | + urls: _.map(rdata, id => { | ||
165 | + return { | ||
166 | + url: `${startUrl}${id}${endUrl}`, | ||
167 | + changefreq: 'daily', | ||
168 | + priority: 0.3 | ||
169 | + }; | ||
170 | + }) | ||
171 | + }).toXML(function(err, xml) { | ||
172 | + rdata = []; | ||
173 | + | ||
174 | + if (err) { | ||
175 | + return res.status(500).end(); | ||
176 | + } | ||
177 | + | ||
178 | + res.header('Content-Type', 'application/xml'); | ||
179 | + res.send(xml); | ||
180 | + }); | ||
181 | +}; | ||
182 | + | ||
183 | +const guangMap = (req, res, next) => { | ||
184 | + | ||
185 | + req.ctx(siteMapService).guangList(1, []).then(rdata => { | ||
186 | + return _createSitemap(req, res, rdata, 'https://www.yohobuy.com/guang/', '.html'); | ||
187 | + }).catch(next); | ||
188 | +}; | ||
189 | + | ||
190 | +const newsMap = (req, res, next) => { | ||
191 | + req.ctx(siteMapService).newsList(1, []).then(rdata => { | ||
192 | + return _createSitemap(req, res, rdata, 'https://www.yohobuy.com/news/', '.html'); | ||
193 | + }).catch(next); | ||
194 | +}; | ||
195 | + | ||
159 | module.exports = { | 196 | module.exports = { |
160 | - siteMap | 197 | + siteMap, |
198 | + guangMap, | ||
199 | + newsMap | ||
161 | }; | 200 | }; |
apps/3party/models/site-map-service.js
0 → 100644
1 | +/** | ||
2 | + * map | ||
3 | + * @author: xiaoxiao<xiaoxiao.hao@yoho.cn> | ||
4 | + * @date: 2017/10/24 | ||
5 | + */ | ||
6 | +'use strict'; | ||
7 | + | ||
8 | +const _ = require('lodash'); | ||
9 | + | ||
10 | +module.exports = class extends global.yoho.BaseModel { | ||
11 | + constructor(ctx) { | ||
12 | + super(ctx); | ||
13 | + } | ||
14 | + | ||
15 | + guangList(page, lres) { | ||
16 | + return this.get({ | ||
17 | + url: 'guang/api/v2/article/getList', | ||
18 | + data: { | ||
19 | + page: page, | ||
20 | + limit: 1000 | ||
21 | + }, | ||
22 | + param: { | ||
23 | + cache: true | ||
24 | + }, | ||
25 | + api: global.yoho.ServiceAPI | ||
26 | + }).then(rdata => { | ||
27 | + let artList = _.get(rdata, 'data.list.artList', []); | ||
28 | + | ||
29 | + if (artList.length <= 0) { | ||
30 | + return lres; | ||
31 | + } | ||
32 | + | ||
33 | + lres = lres.concat(_.map(artList, (art) => { | ||
34 | + return art.id; | ||
35 | + })); | ||
36 | + | ||
37 | + rdata = []; | ||
38 | + artList = []; | ||
39 | + | ||
40 | + return this.guangList(++page, lres); | ||
41 | + }); | ||
42 | + } | ||
43 | + | ||
44 | + newsList(page, lres) { | ||
45 | + return this.get({ | ||
46 | + url: 'yohonow/api/page/getPolymerizationList', | ||
47 | + data: { | ||
48 | + type: 'wechat', | ||
49 | + limit: 100, | ||
50 | + id: 'yohobuy4008899646,yohogroup,YOHO_GIRL,mars-app', | ||
51 | + page: page | ||
52 | + }, | ||
53 | + param: { | ||
54 | + cache: true | ||
55 | + }, | ||
56 | + api: global.yoho.YohoNowApi | ||
57 | + }).then(rdata => { | ||
58 | + let artList = _.get(rdata, 'data.content', []); | ||
59 | + | ||
60 | + if (artList.length <= 0) { | ||
61 | + return lres; | ||
62 | + } | ||
63 | + | ||
64 | + lres = lres.concat(_.map(artList, (art) => { | ||
65 | + return `${art.id}_${art.cid}`; | ||
66 | + })); | ||
67 | + | ||
68 | + rdata = []; | ||
69 | + artList = []; | ||
70 | + | ||
71 | + return this.newsList(++page, lres); | ||
72 | + }); | ||
73 | + } | ||
74 | + | ||
75 | +}; | ||
76 | + | ||
77 | + | ||
78 | + | ||
79 | + | ||
80 | + |
@@ -31,6 +31,6 @@ router.post('/common/getNewArrival', channelController.getNewArrival); | @@ -31,6 +31,6 @@ router.post('/common/getNewArrival', channelController.getNewArrival); | ||
31 | router.get('/guide', channelController.getIndexGuide); | 31 | router.get('/guide', channelController.getIndexGuide); |
32 | 32 | ||
33 | // www站点地图 | 33 | // www站点地图 |
34 | -router.get(/\/sitemap(\d*)\.xml/, sitemap.siteMap); | 34 | +router.get(/^\/sitemap(\d*)\.xml/, sitemap.siteMap); |
35 | 35 | ||
36 | module.exports = router; | 36 | module.exports = router; |
@@ -30,7 +30,7 @@ router.get('/info/detailData', guangController.detailDynamicData); | @@ -30,7 +30,7 @@ router.get('/info/detailData', guangController.detailDynamicData); | ||
30 | 30 | ||
31 | // router.get('/info/commentData', guangController.detailCommentData); | 31 | // router.get('/info/commentData', guangController.detailCommentData); |
32 | 32 | ||
33 | -// guang站点地图 | ||
34 | -router.get('/sitemap.xml', sitemap.siteMap); | 33 | +// seo-站点地图 |
34 | +router.get('/sitemap.xml', sitemap.guangMap); | ||
35 | 35 | ||
36 | module.exports = router; | 36 | module.exports = router; |
@@ -10,10 +10,13 @@ const router = require('express').Router(); // eslint-disable-line | @@ -10,10 +10,13 @@ const router = require('express').Router(); // eslint-disable-line | ||
10 | const cRoot = './controllers'; | 10 | const cRoot = './controllers'; |
11 | 11 | ||
12 | const newsController = require(`${cRoot}/index`); | 12 | const newsController = require(`${cRoot}/index`); |
13 | +const sitemap = require('../3party/controllers/sitemap'); | ||
13 | 14 | ||
14 | router.get(['/', '/index/index'], newsController.index); | 15 | router.get(['/', '/index/index'], newsController.index); |
15 | router.get(/\/([\d]+)_([\d]+).html/, newsController.detail); | 16 | router.get(/\/([\d]+)_([\d]+).html/, newsController.detail); |
16 | 17 | ||
18 | +// seo-站点地图 | ||
19 | +router.get('/sitemap.xml', sitemap.newsMap); | ||
17 | 20 | ||
18 | // ajax | 21 | // ajax |
19 | 22 |
-
Please register or login to post a comment