...
|
...
|
@@ -8,6 +8,8 @@ const _ = require('lodash'); |
|
|
const logger = global.yoho.logger;
|
|
|
const helper = global.yoho.helpers;
|
|
|
const config = require('../config/config');
|
|
|
const staticUrl = require('../config/staticUrls');
|
|
|
const fs = require('fs');
|
|
|
|
|
|
const siteUrls = {
|
|
|
pcProduct: {
|
...
|
...
|
@@ -105,4 +107,71 @@ module.exports = class extends global.yoho.BaseModel { |
|
|
});
|
|
|
})();
|
|
|
}
|
|
|
|
|
|
getXmlUrl(maps) {
|
|
|
let xmlUrl = '';
|
|
|
|
|
|
_.forEach(_.get(maps, 'loc'), (url) => {
|
|
|
if (url) {
|
|
|
xmlUrl += `<url>
|
|
|
<loc>${url}</loc>
|
|
|
<lastmod>${maps.lastmod}</lastmod>
|
|
|
<changefreq>${maps.changefreq}</changefreq>
|
|
|
<priority>${maps.priority}</priority>
|
|
|
</url>`;
|
|
|
}
|
|
|
});
|
|
|
return xmlUrl;
|
|
|
}
|
|
|
|
|
|
generateSitemapXml(site, maps, sitemapPath) {
|
|
|
sitemapPath = sitemapPath || config.sitemapPath;
|
|
|
|
|
|
let content = '<?xml version="1.0" encoding="utf-8"?><urlset><url></url></urlset>';
|
|
|
let fileName = `${sitemapPath}/${site}_sitemap.xml`;
|
|
|
let urls = '';
|
|
|
let that = this;
|
|
|
|
|
|
if (maps.hasOwnProperty('loc')) {
|
|
|
urls += that.getXmlUrl(maps);
|
|
|
} else {
|
|
|
_.forEach(maps, (val)=>{
|
|
|
urls += that.getXmlUrl(val);
|
|
|
});
|
|
|
}
|
|
|
if (urls.length === 0) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
content = content.replace('<url></url>', urls);
|
|
|
fs.open(fileName, 'r+', ()=>{
|
|
|
fs.writeFile(fileName, content, function(err) {
|
|
|
if (err) {
|
|
|
console.error(err);
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* sitemap
|
|
|
*/
|
|
|
sitemap() {
|
|
|
let that = this;
|
|
|
|
|
|
co(function*() {
|
|
|
// 获取pc/wap的商品详情和逛的链接
|
|
|
let urls = yield that.getUrls();
|
|
|
|
|
|
staticUrl.item.loc = _.concat(staticUrl.item.loc, urls.pcProduct.url);
|
|
|
staticUrl.guang.loc = _.concat(staticUrl.guang.loc, urls.pcGuang.url);
|
|
|
staticUrl.m.item.loc = _.concat(staticUrl.m.item.loc, urls.mProduct.url);
|
|
|
staticUrl['guang.m'].loc = _.concat(staticUrl['guang.m'].loc, urls.mGuang.url);
|
|
|
|
|
|
_.forEach(staticUrl, (val, key) => {
|
|
|
that.generateSitemapXml(key, val);
|
|
|
});
|
|
|
})();
|
|
|
}
|
|
|
}; |
...
|
...
|
|