Authored by 郝肖肖

'xml-max-9.5M'

... ... @@ -36,7 +36,7 @@ app.use('/dist', express.static(path.join(__dirname, './public/dist/')));
// 添加请求上下文
app.use(global.yoho.httpCtx());
app.get('/goods-:page-:limit.xml', cseo.index);
app.get('/goods-:start.xml', cseo.index);
app.get('/demo-xml', cseo.demoXml);
// 定时任务 主动推送和生成xml
... ...
... ... @@ -6,7 +6,8 @@ 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;
const util = require(`${ROOT_PATH}/libs/util`);
const SIZE = 5000;
class SeoIndexModel extends global.yoho.BaseModel {
constructor(ctx) {
... ... @@ -14,17 +15,23 @@ class SeoIndexModel extends global.yoho.BaseModel {
}
writerGoodsXml(params) {
let page = ((params.page || 1) - 1) * STEP + 1;
let fileName = `goods-${page}.xml`;
params.start = parseInt(params.start || 1, 10);
return this.searchGoodsHandle(params, []).then(result => {
let limit = 100; // max 100
// limt接口最大支持100,查询5000条数据返回。则page转换成如下公式
let page = (params.start - 1) * (SIZE / limit) + 1;
let fileName = `goods-${params.start}.xml`;
return this.searchGoodsHandle(Object.assign({}, params, {page: page, limit: limit}), []).then(result => {
if (result.length <= 0) {
return {code: 400, data: ''};
return {code: 400, data: '', message: 'data is empty'};
}
const fWrite = fs.createWriteStream(`${ROOT_PATH}/public/dist/${fileName}`);
fWrite.write(goodsHbs({products: result}));
fWrite.write(goodsHbs({products: result}).replace(/\s+/g, ' '));// max 9.5M
result = [];
return {code: 200, data: `http://127.0.0.1:6005/dist/${fileName}`};
});
... ... @@ -32,7 +39,13 @@ class SeoIndexModel extends global.yoho.BaseModel {
searchGoodsHandle(params, products) {
return this.searchList(params, products).then(rdata => {
if (products.length >= SIZE) {
return Promise.resolve(products);
}
return util.sleep(2000).then(() => {
return this.searchList(params, products);
}).then(rdata => {
let productLists = _.get(rdata, 'data.product_list', []);
if (productLists.length <= 0) {
... ... @@ -110,7 +123,10 @@ class SeoIndexModel extends global.yoho.BaseModel {
});
});
return products;
rdata = [];
productLists = [];
return this.searchGoodsHandle(Object.assign({}, params, {page: ++params.page}), products);
});
}
... ... @@ -129,6 +145,9 @@ class SeoIndexModel extends global.yoho.BaseModel {
param: {
cache: 86400
}
}).catch(e => {
console.log(e.message);
return {code: 400, data: {}, message: e.message};
});
}
}
... ...
... ... @@ -9,7 +9,7 @@
<data>
<display>
<fromSrc>{{fromSrc}}</fromSrc>
{{#images }}
{{#images}}
<image>
<contentUrl>{{contentUrl}}</contentUrl>
<height>{{height}}</height>
... ... @@ -35,17 +35,19 @@
<seller>
<name>{{seller_name}}</name>
</seller>
{{#if ratingValue}}
<aggregateRating>
<ratingValue>{{ratingValue}}</ratingValue>
<favorableRating>{{favorableRating}}</favorableRating>
</aggregateRating>
<onShelfTime>{{onShelfTime}}</onShelfTime>
<offShelfTime>{{offShelfTime}}</offShelfTime>
{{/if}}
{{#if onShelfTime}}<onShelfTime>{{onShelfTime}}</onShelfTime>{{/if}}
{{#if offShelfTime}}<offShelfTime>{{offShelfTime}}</offShelfTime>{{/if}}
<type>{{type}}</type>
<isOverseas>{{isOverseas}}</isOverseas>
<discount>{{discount}}</discount>
{{#if discount}}<discount>{{discount}}</discount>{{/if}}
<isFreightFree>{{isFreightFree}}</isFreightFree>
<commentCount>{{commentCount}}</commentCount>
{{#if commentCount}}<commentCount>{{commentCount}}</commentCount>{{/if}}
<deliveryPlace>
<administrativeAreaLv1>{{deliveryPlace.administrativeAreaLv1}}</administrativeAreaLv1>
</deliveryPlace>
... ...