...
|
...
|
@@ -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};
|
|
|
});
|
|
|
}
|
|
|
}
|
...
|
...
|
|