sitemap-service.js
6.24 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
198
199
200
201
202
203
204
205
206
207
208
const sm = require('sitemap');
const _ = require('lodash');
const sitemapConfig = require('./sitemap-config');
const sitemapStore = global.yoho.config.sitemap;
const ProductService = require('./product-service');
const ListService = require('./list-service');
const ShopService = require('./shop-service');
const GuangService = require('./guang-service');
const ChanpinService = require('./chanpin-service');
const HotService = require('./hot-service');
const logger = global.yoho.logger;
async function _createSiteMapIndex(host, opts) {
const urls = [];
opts.urls.forEach((url) => {
if (!_.startsWith(url, 'https://')) {
urls.push(host + url);
}
});
opts.urls = urls;
const xml = sm.buildSitemapIndex(opts);
return xml;
}
function _createXml(host, urls, opts) {
return new Promise((resolve, reject) => {
sm.createSitemap({
hostname: host,
xmlNs: '',
urls: _.map(urls, url => {
return Object.assign({url}, opts)
})
}).toXML((err, xml) => {
if (err) {
return reject(err);
}
resolve(xml);
});
});
}
function _createXml2(host, urls) {
return new Promise((resolve, reject) => {
sm.createSitemap({
hostname: host,
xmlNs: '',
urls
}).toXML((err, xml) => {
if (err) {
return reject(err);
}
resolve(xml);
});
});
}
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.host = sitemapStore.pc.host;
this.props = sitemapStore.pc.props;
this.type = sitemapStore.pc.type;
this.listService = new ListService(ctx);
this.shopService = new ShopService(ctx);
this.productService = new ProductService(ctx);
this.guangService = new GuangService(ctx);
this.chanpinService = new ChanpinService(ctx);
this.hotService = new HotService(ctx);
this.config = sitemapConfig();
}
async indexXml() {
const listIndex = await this.listService.getIndex(this.type);
const productIndex = await this.productService.getIndex(this.type);
const guangIndex = await this.guangService.getIndex(this.type);
const shopIndex = await this.shopService.getIndex(this.type);
const homeIndex = await this.getHomeIndex(this.type);
const chanpinIndex = await this.chanpinService.getIndex(this.type);
const hotIndex = await this.hotService.getIndex(this.type);
this.config.index.urls = _.concat(this.config.index.urls, listIndex, productIndex, guangIndex, shopIndex, homeIndex, chanpinIndex, hotIndex);
return _createSiteMapIndex(this.host, this.config.index);
}
async getHomeIndex(type) {
return [`/sitemap_${type}/home_1.xml`];
}
async homeXml() {
const home = this.config.www;
const urls = Object.values(home).map(opts => {
const opt = _.pick(opts, this.props);
return opts.urls.map((url) => {
return Object.assign({url}, opt);
});
});
return _createXml2(this.host, _.flatten(urls));
}
async listXml(page) {
logger.info(`list page ${page} start`);
const listConfig = this.config.list;
const opt = _.pick(this.config.list, this.props);
let urls = await this.listService.getUrls(page);
listConfig.urls = listConfig.urls.concat(urls);
return _createXml(this.host, listConfig.urls, opt);
}
async shopXml() {
const shopConfig = this.config.shop;
const opt = _.pick(this.config.shop, this.props);
let urls = await this.shopService.getUrls();
shopConfig.urls = shopConfig.urls.concat(urls);
return _createXml(this.host, shopConfig.urls, opt);
}
async productXml(page) {
const productConfig = this.config.product;
const opt = _.pick(this.config.product, this.props);
let urls = await this.productService.getUrls(page);
productConfig.urls = productConfig.urls.concat(urls);
return _createXml(this.host, productConfig.urls, opt);
}
async guangXml(page) {
let guangConfig = this.config.guang;
let opt = _.pick(this.config.guang, this.props);
let urls = await this.guangService.getUrls(page);
if (_.has(guangConfig, 'mobileUrls') && this.type === 'h5') {
urls = guangConfig.mobileUrls.concat(urls);
} else {
urls = guangConfig.urls.concat(urls);
}
return _createXml(this.host, urls, opt);
}
async chanpinXml(page) {
let chanpinConfig = this.config.chanpin;
let opt = _.pick(this.config.chanpin, this.props);
let urls = await this.chanpinService.getUrls(page);
chanpinConfig.urls = chanpinConfig.urls.concat(urls);
return _createXml(this.host, chanpinConfig.urls, opt);
}
async hotXml(page) {
let hotConfig = this.config.hot;
let opt = _.pick(this.config.hot, this.props);
let urls = await this.hotService.getUrls(page);
hotConfig.urls = hotConfig.urls.concat(urls);
return _createXml(this.host, hotConfig.urls, opt);
}
async indexJson() {
const index = this.config.index;
const listIndex = await this.listService.getIndex(this.type);
const productIndex = await this.productService.getIndex(this.type);
const guangIndex = await this.guangService.getIndex(this.type);
const shopIndex = await this.shopService.getIndex(this.type);
const homeIndex = await this.getHomeIndex(this.type);
const chanpinIndex = await this.chanpinService.getIndex(this.type);
const hotIndex = await this.hotService.getIndex(this.type);
index.urls = _.concat(index.urls, listIndex, productIndex, guangIndex, shopIndex, homeIndex, chanpinIndex, hotIndex);
return index.urls.map(url => this.host + url);
}
async productIndexXml() {
const productIndex = await this.productService.getIndex(this.type);
this.config.index.urls = _.concat(this.config.index.urls, productIndex)
return _createSiteMapIndex(this.host, this.config.index);
}
};