sitemap.js 3.02 KB
'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 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);
};

// 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.urlFormat(`/product/${val.erpProductId}.html`, '', null),
                changefreq: 'daily',
                priority: 0.3
            });
        });

        return urls;
    });
};

// m 地图数据
const mXmlData = () => {// eslint-disable-line
    return Promise.all([getStaticUrls(_.get(staticUrls, 'm')), itemXmlData()]).then(result => {
        return _.union(result[0], result[1]);
    });
};

// list 地图数据
const listXmlData = () => {// eslint-disable-line
    return getStaticUrls(_.get(staticUrls, 'list'));
};

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 = ['m', 'list', 'item', 'guang'],
        subdomain = req.subdomains[1] || 'm';

    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
};