|
@@ -2,11 +2,12 @@ |
|
@@ -2,11 +2,12 @@ |
2
|
|
2
|
|
3
|
const ROOT_PATH = global.ROOT_PATH;
|
3
|
const ROOT_PATH = global.ROOT_PATH;
|
4
|
const _ = require('lodash');
|
4
|
const _ = require('lodash');
|
5
|
-const fs = require('fs');
|
5
|
+const fs = require('fs');
|
6
|
const moment = require('moment');
|
6
|
const moment = require('moment');
|
7
|
const helpers = global.yoho.helpers;
|
7
|
const helpers = global.yoho.helpers;
|
8
|
const goodsHbs = require(`${ROOT_PATH}/hbs/partials/seo/index.hbs`);
|
8
|
const goodsHbs = require(`${ROOT_PATH}/hbs/partials/seo/index.hbs`);
|
9
|
-const STEP = 5;
|
9
|
+const util = require(`${ROOT_PATH}/libs/util`);
|
|
|
10
|
+const SIZE = 5000;
|
10
|
|
11
|
|
11
|
class SeoIndexModel extends global.yoho.BaseModel {
|
12
|
class SeoIndexModel extends global.yoho.BaseModel {
|
12
|
constructor(ctx) {
|
13
|
constructor(ctx) {
|
|
@@ -14,17 +15,23 @@ class SeoIndexModel extends global.yoho.BaseModel { |
|
@@ -14,17 +15,23 @@ class SeoIndexModel extends global.yoho.BaseModel { |
14
|
}
|
15
|
}
|
15
|
|
16
|
|
16
|
writerGoodsXml(params) {
|
17
|
writerGoodsXml(params) {
|
17
|
- let page = ((params.page || 1) - 1) * STEP + 1;
|
|
|
18
|
- let fileName = `goods-${page}.xml`;
|
18
|
+ params.start = parseInt(params.start || 1, 10);
|
19
|
|
19
|
|
20
|
- return this.searchGoodsHandle(params, []).then(result => {
|
20
|
+ let limit = 100; // max 100
|
|
|
21
|
+ // limt接口最大支持100,查询5000条数据返回。则page转换成如下公式
|
|
|
22
|
+ let page = (params.start - 1) * (SIZE / limit) + 1;
|
|
|
23
|
+ let fileName = `goods-${params.start}.xml`;
|
|
|
24
|
+
|
|
|
25
|
+ return this.searchGoodsHandle(Object.assign({}, params, {page: page, limit: limit}), []).then(result => {
|
21
|
if (result.length <= 0) {
|
26
|
if (result.length <= 0) {
|
22
|
- return {code: 400, data: ''};
|
27
|
+ return {code: 400, data: '', message: 'data is empty'};
|
23
|
}
|
28
|
}
|
24
|
|
29
|
|
25
|
const fWrite = fs.createWriteStream(`${ROOT_PATH}/public/dist/${fileName}`);
|
30
|
const fWrite = fs.createWriteStream(`${ROOT_PATH}/public/dist/${fileName}`);
|
26
|
|
31
|
|
27
|
- fWrite.write(goodsHbs({products: result}));
|
32
|
+ fWrite.write(goodsHbs({products: result}).replace(/\s+/g, ' '));// max 9.5M
|
|
|
33
|
+
|
|
|
34
|
+ result = [];
|
28
|
|
35
|
|
29
|
return {code: 200, data: `http://127.0.0.1:6005/dist/${fileName}`};
|
36
|
return {code: 200, data: `http://127.0.0.1:6005/dist/${fileName}`};
|
30
|
});
|
37
|
});
|
|
@@ -32,7 +39,13 @@ class SeoIndexModel extends global.yoho.BaseModel { |
|
@@ -32,7 +39,13 @@ class SeoIndexModel extends global.yoho.BaseModel { |
32
|
|
39
|
|
33
|
searchGoodsHandle(params, products) {
|
40
|
searchGoodsHandle(params, products) {
|
34
|
|
41
|
|
35
|
- return this.searchList(params, products).then(rdata => {
|
42
|
+ if (products.length >= SIZE) {
|
|
|
43
|
+ return Promise.resolve(products);
|
|
|
44
|
+ }
|
|
|
45
|
+
|
|
|
46
|
+ return util.sleep(2000).then(() => {
|
|
|
47
|
+ return this.searchList(params, products);
|
|
|
48
|
+ }).then(rdata => {
|
36
|
let productLists = _.get(rdata, 'data.product_list', []);
|
49
|
let productLists = _.get(rdata, 'data.product_list', []);
|
37
|
|
50
|
|
38
|
if (productLists.length <= 0) {
|
51
|
if (productLists.length <= 0) {
|
|
@@ -110,7 +123,10 @@ class SeoIndexModel extends global.yoho.BaseModel { |
|
@@ -110,7 +123,10 @@ class SeoIndexModel extends global.yoho.BaseModel { |
110
|
});
|
123
|
});
|
111
|
});
|
124
|
});
|
112
|
|
125
|
|
113
|
- return products;
|
126
|
+ rdata = [];
|
|
|
127
|
+ productLists = [];
|
|
|
128
|
+
|
|
|
129
|
+ return this.searchGoodsHandle(Object.assign({}, params, {page: ++params.page}), products);
|
114
|
});
|
130
|
});
|
115
|
}
|
131
|
}
|
116
|
|
132
|
|
|
@@ -129,6 +145,9 @@ class SeoIndexModel extends global.yoho.BaseModel { |
|
@@ -129,6 +145,9 @@ class SeoIndexModel extends global.yoho.BaseModel { |
129
|
param: {
|
145
|
param: {
|
130
|
cache: 86400
|
146
|
cache: 86400
|
131
|
}
|
147
|
}
|
|
|
148
|
+ }).catch(e => {
|
|
|
149
|
+ console.log(e.message);
|
|
|
150
|
+ return {code: 400, data: {}, message: e.message};
|
132
|
});
|
151
|
});
|
133
|
}
|
152
|
}
|
134
|
}
|
153
|
}
|