Authored by 郝肖肖

'xml-hbs'

... ... @@ -6,6 +6,9 @@ const hbs = require('express-handlebars');
const config = require('./config/config');
const yohoLib = require('yoho-node-lib');
// set root path
global.ROOT_PATH = path.join(__dirname, '');
// 全局注册library
yohoLib.global(config);
global.yoho.redis = require('./libs/redis');
... ... @@ -27,6 +30,9 @@ app.engine('hbs', hbs({
app.set('views', path.join(__dirname, 'apps/views'));
app.set('view engine', 'hbs');
// 设置public文件夹为存放静态文件的目录
app.use('/dist', express.static(path.join(__dirname, './public/dist/')));
// 添加请求上下文
app.use(global.yoho.httpCtx());
... ...
... ... @@ -2,10 +2,8 @@
const SeoIndexModel = require('../../models/seo/index');
const index = (req, res) => {
return req.ctx(SeoIndexModel).searchMipHandle(req.params).then(result => {
res.setHeader('Content-Type', 'text/xml; charset=utf-8');
res.render('seo/index', {products: result});
return req.ctx(SeoIndexModel).writerGoodsXml(req.params).then(result => {
return res.json(result);
});
};
... ...
'use strict';
const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const fs = require('fs');
const moment = require('moment');
const helpers = global.yoho.helpers;
const goodsHbs = require(`${ROOT_PATH}/hbs/partials/seo/index.hbs`);
const STEP = 5;
class SeoIndexModel extends global.yoho.BaseModel {
... ... @@ -9,24 +13,31 @@ class SeoIndexModel extends global.yoho.BaseModel {
super(ctx);
}
searchMipHandle(params) {
let products = [];
writerGoodsXml(params) {
let page = ((params.page || 1) - 1) * STEP + 1;
let fileName = `goods-${page}.xml`;
params.limit = params.limit > 100 ? 100 : params.limit;
return this.searchGoodsHandle(params, []).then(result => {
if (result.length <= 0) {
return {code: 400, data: ''};
}
return Promise.all([
this.searchList(Object.assign({}, params, {page: page})),
this.searchList(Object.assign({}, params, {page: page + 1})),
this.searchList(Object.assign({}, params, {page: page + 2})),
this.searchList(Object.assign({}, params, {page: page + 3})),
this.searchList(Object.assign({}, params, {page: page + 4}))
]).then(rdata => {
let productLists = [];
const fWrite = fs.createWriteStream(`${ROOT_PATH}/public/dist/${fileName}`);
_.each(rdata, item => {
productLists = productLists.concat(_.get(item, 'data.product_list', []));
});
fWrite.write(goodsHbs({products: result}));
return {code: 200, data: `http://127.0.0.1:6005/dist/${fileName}`};
});
}
searchGoodsHandle(params, products) {
return this.searchList(params, products).then(rdata => {
let productLists = _.get(rdata, 'data.product_list', []);
if (productLists.length <= 0) {
return products;
}
_.each(productLists, item => {
let images = [];
... ... @@ -43,7 +54,7 @@ class SeoIndexModel extends global.yoho.BaseModel {
});
products.push({
loc: `https://www.yohobuy.com`,
loc: 'https://www.yohobuy.com',
lastmod: moment.unix(item.edit_time).format('YYYY-MM-DD'),
changefreq: 'weekly',
priority: 1.0,
... ...
... ... @@ -72,18 +72,20 @@
</offers>
{{/offers}}
<structuredCategory>
<categoryLv1>{{structuredCategory.categoryLv1}}</categoryLv1>
<categoryLv2>{{structuredCategory.categoryLv2}}</categoryLv2>
<categoryLv3>{{structuredCategory.categoryLv3}}</categoryLv3>
<categoryLv4>{{structuredCategory.categoryLv4}}</categoryLv4>
{{#if structuredCategory.categoryLv1}}<categoryLv1>{{structuredCategory.categoryLv1}}</categoryLv1>{{/if}}
{{#if structuredCategory.categoryLv2}}<categoryLv2>{{structuredCategory.categoryLv2}}</categoryLv2>{{/if}}
{{#if structuredCategory.categoryLv3}}<categoryLv3>{{structuredCategory.categoryLv3}}</categoryLv3>{{/if}}
{{#if structuredCategory.categoryLv4}}<categoryLv4>{{structuredCategory.categoryLv4}}</categoryLv4>{{/if}}
</structuredCategory>
<exifData>
<name>{{exifData.name}}</name>
<value>{{exifData.value}}</value>
<propertyID>{{exifData.propertyID}}</propertyID>
<unitText>{{exifData.unitText}}</unitText>
<unitCode>{{exifData.unitCode}}</unitCode>
</exifData>
{{#if exifData.name}}
<exifData>
<name>{{exifData.name}}</name>
<value>{{exifData.value}}</value>
<propertyID>{{exifData.propertyID}}</propertyID>
<unitText>{{exifData.unitText}}</unitText>
<unitCode>{{exifData.unitCode}}</unitCode>
</exifData>
{{/if}}
<id>{{id}}</id>
</display>
</data>
... ...