sitemap.js
5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
const helpers = global.yoho.helpers;
const Promise = require('bluebird');
const _ = require('lodash');
const sm = require('sitemap');
const staticUrls = require('../../../config/staticUrls');
const headerModel = require('../../../doraemon/models/header');
const siteMapService = require('../models/site-map-service');
const cache = global.yoho.cache.master;
const getStaticUrls = (currentStatics) => {
let urls = [];
_.forEach(_.get(currentStatics, 'loc', []), url => {
urls.push({
url: url,
changefreq: currentStatics.changefreq,
priority: currentStatics.priority
});
});
_.forEach(currentStatics, (value) => {
_.forEach(value.loc, url => {
urls.push({
url: url,
changefreq: value.changefreq,
priority: value.priority
});
});
});
return Promise.resolve(urls);
};
// 关键词页面
const keywordsPage = (page) => {
return cache.getAsync(`global:yoho:seo:keywords:allIds:page:${page}`).then(function(res) {
let pages = [];
_.forEach(JSON.parse(res), val => {
pages.push({
url: `https://www.yohobuy.com/chanpin/${val}.html`,
changefreq: 'daily',
priority: 0.5
});
});
return pages;
}).catch(()=>{
return {};
});
};
// 逛详情
const getArticleUrls = (req) => {
let urls = [];
return req.ctx(siteMapService).guangArticles().then(res => {
_.forEach(_.get(res, 'data.artList', ''), val => {
urls.push({
url: `https:${helpers.urlFormat(`/guang/${val.articleId}.html`, null)}`,
changefreq: 'daily',
priority: 0.3
});
});
return urls;
});
};
// www 地图数据
const wwwXmlData = (req, page) => {// eslint-disable-line
if (page > 1) {
return keywordsPage(page);
} else {
return Promise.all([keywordsPage(page),
getStaticUrls(_.get(staticUrls, 'www')),
getArticleUrls(req)]).then(result => {
return _.concat(result[1], result[0], result[2]);
});
}
};
// list 地图数据
const listXmlData = (req) => {// eslint-disable-line
return Promise.all([getStaticUrls(_.get(staticUrls, 'list')),
req.ctx(headerModel).getSubNavGroupData()]).then(result => {
// 获取导航中的列表链接
let listNav = [],
listPatten = /list\.yohobuy\.com/;
_.forEach(result[1] || [], val => {
_.forEach(val.subNav, sub => {
if (listPatten.test(sub.link)) {
listNav.push({url: sub.link, changefreq: 'daily', priority: 0.3});
}
_.forEach(_.get(sub, 'thirdNav'), third => {
if (listPatten.test(third.link)) {
listNav.push({url: third.link, changefreq: 'daily', priority: 0.3});
}
});
});
});
return _.union(result[0], _.uniqBy(listNav, 'url'));
});
};
// item 地图数据
const itemXmlData = (req) => {// eslint-disable-line
let urls = [];
return req.ctx(siteMapService).itemList().then(res => {
_.forEach(_.get(res, 'data', ''), val => {
urls.push({
url: `https:${helpers.getUrlBySkc(val.erpProductId)}`,
changefreq: 'daily',
priority: 0.3
});
});
return urls;
});
};
// 站点地图
const siteMap = (req, res, next) => {
let siteList = ['www', 'list', 'item'],
subdomain = req.subdomains[0] || 'www';
if (_.indexOf(siteList, subdomain) === -1) {
res.end('end');
return;
}
let page = req.params[0] || 1;
eval(subdomain + 'XmlData')(req, page).then(urls => {// eslint-disable-line
sm.createSitemap({
hostname: `https://${subdomain}.yohobuy.com`,
xmlNs: ' ',
urls: urls
}).toXML(function(err, xml) {
if (err) {
return res.status(500).end();
}
res.header('Content-Type', 'application/xml');
res.send(xml);
});
}).catch(next);
};
const _createSitemap = (req, res, rdata, startUrl, endUrl) => {
sm.createSitemap({
hostname: 'https://www.yohobuy.com',
xmlNs: ' ',
urls: _.map(rdata, id => {
return {
url: `${startUrl}${id}${endUrl}`,
changefreq: 'daily',
priority: 0.3
};
})
}).toXML(function(err, xml) {
rdata = [];
if (err) {
return res.status(500).end();
}
res.header('Content-Type', 'application/xml');
res.send(xml);
});
};
const guangMap = (req, res, next) => {
req.ctx(siteMapService).guangList().then(rdata => {
return _createSitemap(req, res, rdata, 'https://www.yohobuy.com/guang/', '.html');
}).catch(next);
};
const newsMap = (req, res, next) => {
req.ctx(siteMapService).newsList(1, []).then(rdata => {
return _createSitemap(req, res, rdata, 'https://www.yohobuy.com/guang/news/', '.html');
}).catch(next);
};
module.exports = {
siteMap,
guangMap,
newsMap
};