Authored by 郝肖肖

'xml-max-9.5M'

@@ -36,7 +36,7 @@ app.use('/dist', express.static(path.join(__dirname, './public/dist/'))); @@ -36,7 +36,7 @@ app.use('/dist', express.static(path.join(__dirname, './public/dist/')));
36 // 添加请求上下文 36 // 添加请求上下文
37 app.use(global.yoho.httpCtx()); 37 app.use(global.yoho.httpCtx());
38 38
39 -app.get('/goods-:page-:limit.xml', cseo.index); 39 +app.get('/goods-:start.xml', cseo.index);
40 app.get('/demo-xml', cseo.demoXml); 40 app.get('/demo-xml', cseo.demoXml);
41 41
42 // 定时任务 主动推送和生成xml 42 // 定时任务 主动推送和生成xml
@@ -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 }
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 <data> 9 <data>
10 <display> 10 <display>
11 <fromSrc>{{fromSrc}}</fromSrc> 11 <fromSrc>{{fromSrc}}</fromSrc>
12 - {{#images }} 12 + {{#images}}
13 <image> 13 <image>
14 <contentUrl>{{contentUrl}}</contentUrl> 14 <contentUrl>{{contentUrl}}</contentUrl>
15 <height>{{height}}</height> 15 <height>{{height}}</height>
@@ -35,17 +35,19 @@ @@ -35,17 +35,19 @@
35 <seller> 35 <seller>
36 <name>{{seller_name}}</name> 36 <name>{{seller_name}}</name>
37 </seller> 37 </seller>
38 - <aggregateRating>  
39 - <ratingValue>{{ratingValue}}</ratingValue>  
40 - <favorableRating>{{favorableRating}}</favorableRating>  
41 - </aggregateRating>  
42 - <onShelfTime>{{onShelfTime}}</onShelfTime>  
43 - <offShelfTime>{{offShelfTime}}</offShelfTime> 38 + {{#if ratingValue}}
  39 + <aggregateRating>
  40 + <ratingValue>{{ratingValue}}</ratingValue>
  41 + <favorableRating>{{favorableRating}}</favorableRating>
  42 + </aggregateRating>
  43 + {{/if}}
  44 + {{#if onShelfTime}}<onShelfTime>{{onShelfTime}}</onShelfTime>{{/if}}
  45 + {{#if offShelfTime}}<offShelfTime>{{offShelfTime}}</offShelfTime>{{/if}}
44 <type>{{type}}</type> 46 <type>{{type}}</type>
45 <isOverseas>{{isOverseas}}</isOverseas> 47 <isOverseas>{{isOverseas}}</isOverseas>
46 - <discount>{{discount}}</discount> 48 + {{#if discount}}<discount>{{discount}}</discount>{{/if}}
47 <isFreightFree>{{isFreightFree}}</isFreightFree> 49 <isFreightFree>{{isFreightFree}}</isFreightFree>
48 - <commentCount>{{commentCount}}</commentCount> 50 + {{#if commentCount}}<commentCount>{{commentCount}}</commentCount>{{/if}}
49 <deliveryPlace> 51 <deliveryPlace>
50 <administrativeAreaLv1>{{deliveryPlace.administrativeAreaLv1}}</administrativeAreaLv1> 52 <administrativeAreaLv1>{{deliveryPlace.administrativeAreaLv1}}</administrativeAreaLv1>
51 </deliveryPlace> 53 </deliveryPlace>
@@ -64,10 +66,10 @@ @@ -64,10 +66,10 @@
64 </shelfSpecification> 66 </shelfSpecification>
65 <purchaseMethod>{{purchaseMethod}}</purchaseMethod> 67 <purchaseMethod>{{purchaseMethod}}</purchaseMethod>
66 {{#coupon}} 68 {{#coupon}}
67 - <coupon>  
68 - <url>{{url}}</url>  
69 - <name>{{name}}</name>  
70 - </coupon> 69 + <coupon>
  70 + <url>{{url}}</url>
  71 + <name>{{name}}</name>
  72 + </coupon>
71 {{/coupon}} 73 {{/coupon}}
72 </offers> 74 </offers>
73 {{/offers}} 75 {{/offers}}