Authored by 周少峰

Merge branch 'feature/sitemap' into feature/seo

'use strict';
const helpers = global.yoho.helpers;
const Promise = require('bluebird');
const _ = require('lodash');
const sm = require('sitemap');
const staticUrls = require('../../../config/staticUrls');
const api = global.yoho.API;
const Service = global.yoho.ServiceAPI;
const headerModel = require('../../../doraemon/models/header');
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);
};
// www 地图数据
const wwwXmlData = () => {// eslint-disable-line
return getStaticUrls(_.get(staticUrls, 'www'));
};
// list 地图数据
const listXmlData = () => {// eslint-disable-line
return Promise.all([getStaticUrls(_.get(staticUrls, 'list')), headerModel.requestHeaderData()]).then(result => {
// 获取导航中的列表链接
let listNav = [],
listPatten = /list\.yohobuy\.com/;
_.forEach(_.get(result[1], 'headerData.subNavGroup'), 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 = () => {// eslint-disable-line
let urls = [];
return api.get('', {method: 'web.product.bdPromotion'}, {cache: 86400}).then(res => {
_.forEach(_.get(res, 'data', ''), val => {
urls.push({
url: `https:${helpers.getUrlBySkc(val.id)}`,
changefreq: 'daily',
priority: 0.3
});
});
return urls;
});
};
const getArticleUrls = () => {
let urls = [];
return Service.get('/guang/api/v2/article/getLastArticleList', {limit: 1000}, {cache: 86400}).then(res => {
_.forEach(_.get(res, 'data.artList', ''), val => {
urls.push({
url: `https:${helpers.urlFormat(`/${val.articleId}.html`, '', 'guang')}`,
changefreq: 'daily',
priority: 0.3
});
});
return urls;
});
};
// guang 地图数据
const guangXmlData = () => {// eslint-disable-line
return Promise.all([getStaticUrls(_.get(staticUrls, 'guang')), getArticleUrls()]).then(res => {
return _.union(res[0], res[1]);
});
};
// 站点地图
const siteMap = (req, res, next) => {
let siteList = ['www', 'list', 'item', 'guang'],
subdomain = req.subdomains[0] || 'www';
if (_.find(siteList, subdomain)) {
res.end('end');
return;
}
eval(subdomain + 'XmlData')().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);
};
module.exports = {
siteMap
};
... ...
... ... @@ -31,5 +31,4 @@ router.get('/material/getRecommendlist', auth, materialController.getRecommendli
router.get('/activate-count', auth, activate.index);
router.get('/activate-count/getList', auth, activate.getList);
router.get('/questionnaire/:id', auth, questionnaire.getQuestionnaire);
module.exports = router;
... ...
... ... @@ -11,6 +11,8 @@ const cRoot = './controllers';
// Your controller here
const channelController = require(`${cRoot}/index`);
const sitemap = require('../3party/controllers/sitemap');
// 频道页路由
router.get('/', channelController.index);
... ... @@ -27,4 +29,7 @@ router.get('/channel/isNewUserAjax', channelController.hasNewUserFloor);
router.post('/common/getNewArrival', channelController.getNewArrival);
router.get('/guide', channelController.getIndexGuide);
// www站点地图
router.get('/sitemap.xml', sitemap.siteMap);
module.exports = router;
... ...
... ... @@ -10,6 +10,7 @@ const router = require('express').Router(); // eslint-disable-line
const cRoot = './controllers';
const guangController = require(`${cRoot}/index`);
const sitemap = require('../3party/controllers/sitemap');
router.get(['/', '/index/index'], guangController.index);
router.get(['/detail/:id', '/info/index'], guangController.detail); // guang/info/index
... ... @@ -28,4 +29,7 @@ router.get('/info/detailData', guangController.detailDynamicData);
// router.get('/info/commentData', guangController.detailCommentData);
// guang站点地图
router.get('/sitemap.xml', sitemap.siteMap);
module.exports = router;
... ...
... ... @@ -10,6 +10,7 @@ const router = require('express').Router(); // eslint-disable-line
const cRoot = './controllers';
const auth = require(`${global.middleware}/auth`);
const gbk2utf = require(`${global.middleware}/gbk2utf`);
const sitemap = require('../3party/controllers/sitemap');
// 商品详情controller
const detail = require(`${cRoot}/detail`);
... ... @@ -133,4 +134,8 @@ router.get('/students/list', students.list); // 获取优惠券领取状态
// 新品到着改版
router.get('/newArrive', newArrive.index); // 获取优惠券领取状态
// list&item 站点地图
router.get('/sitemap.xml', sitemap.siteMap);
module.exports = router;
... ...
/**
* pc/wap站静态url 用于站点地图
*
*/
const moment = require('moment');
const today = moment().format('Y-M-D');
module.exports = {
www: {
// 频道
channel: {
loc: ['https://www.yohobuy.com/', 'https://www.yohobuy.com/girls/', 'https://www.yohobuy.com/kids/', 'https://www.yohobuy.com/lifestyle/'],
lastmod: today,
changefreq: 'daily',
priority: 0.8
},
// 品牌一览
brands: {
loc: ['https://www.yohobuy.com/boys-brands/', 'https://www.yohobuy.com/girls-brands/', 'https://www.yohobuy.com/kids-brands/', 'https://www.yohobuy.com/lifestyle-brands/'],
lastmod: today,
changefreq: 'daily',
priority: 0.5
},
// 领券中心
coupon: {
loc: ['https://www.yohobuy.com/coupon/'],
lastmod: today,
changefreq: 'weekly',
priority: 0.3
},
// sale
salse: {
loc: ['https://www.yohobuy.com/product/boys-sale/', 'https://www.yohobuy.com/product/girls-sale/', 'https://www.yohobuy.com/product/kids-sale/', 'https://www.yohobuy.com/product/lifestyle-sale/'],
lastmod: today,
changefreq: 'daily',
priority: 0.3
}
},
list: {
// 新品到着
new: {
loc: ['https://list.yohobuy.com/boys-new/', 'https://list.yohobuy.com/girls-new/', 'https://list.yohobuy.com/kids-new/', 'https://list.yohobuy.com/lifestyle-new/'],
lastmod: today,
changefreq: 'daily',
priority: 0.5
},
// 分类列表
category: {
loc: [],
lastmod: today,
changefreq: 'daily',
priority: 0.3
}
},
// 搜索列表
search: {
loc: ['https://search.yohobuy.com/'],
lastmod: today,
changefreq: 'daily',
priority: 0.3
},
// 逛 编辑列表/标签列表/逛详情动态添加
guang: {
loc: ['https://guang.yohobuy.com/boys/', 'https://guang.yohobuy.com/girls/', 'https://guang.yohobuy.com/kids/', 'https://guang.yohobuy.com/lifestyle/'],
lastmod: today,
changefreq: 'daily',
priority: 0.3
},
// 商品详情动态添加
item: {
loc: [],
lastmod: today,
changefreq: 'daily',
priority: 0.3
}
};
... ...
... ... @@ -36,6 +36,7 @@ module.exports = [
return `/product${req.url}`;
}
},
// 商品详情页新链接
{
type: TYPE.rewrite,
... ...
... ... @@ -48,6 +48,7 @@
"request-ip": "^1.2.2",
"request-promise": "^3.0.0",
"serve-favicon": "^2.3.0",
"sitemap": "^1.12.0",
"urlencode": "^1.1.0",
"uuid": "^2.0.2",
"yoho-express-session": "^2.0.0",
... ...