Authored by 毕凯

Merge branch 'hotfix/cheerio' into 'release/6.3'

cheerio-data-type



See merge request !1173
1 const cheerio = require('cheerio'); 1 const cheerio = require('cheerio');
  2 +const _ = require('lodash');
2 3
3 module.exports = { 4 module.exports = {
4 /** 5 /**
@@ -99,6 +100,8 @@ module.exports = { @@ -99,6 +100,8 @@ module.exports = {
99 */ 100 */
100 101
101 process(html = '', prefix = 0) { 102 process(html = '', prefix = 0) {
  103 + html = _.isString(html) ? html : '';
  104 +
102 let $ = cheerio.load(html, { 105 let $ = cheerio.load(html, {
103 decodeEntities: false 106 decodeEntities: false
104 }); 107 });
@@ -9,6 +9,8 @@ const util = { @@ -9,6 +9,8 @@ const util = {
9 return html; 9 return html;
10 } 10 }
11 11
  12 + html = _.isString(html) ? html : '';
  13 +
12 let $ = cheerio.load(html, {decodeEntities: false}); 14 let $ = cheerio.load(html, {decodeEntities: false});
13 15
14 _.each($('p'), (item) => { 16 _.each($('p'), (item) => {
@@ -35,6 +37,8 @@ const util = { @@ -35,6 +37,8 @@ const util = {
35 return html; 37 return html;
36 } 38 }
37 39
  40 + html = _.isString(html) ? html : '';
  41 +
38 let $ = cheerio.load(html, {decodeEntities: false}); 42 let $ = cheerio.load(html, {decodeEntities: false});
39 43
40 $('a').removeAttr('style').not('.a-anchor').attr('href', 'javascript:void(0);').css({cursor: 'text'});// eslint-disable-line 44 $('a').removeAttr('style').not('.a-anchor').attr('href', 'javascript:void(0);').css({cursor: 'text'});// eslint-disable-line
@@ -52,6 +56,8 @@ const util = { @@ -52,6 +56,8 @@ const util = {
52 return html; 56 return html;
53 } 57 }
54 58
  59 + html = _.isString(html) ? html : '';
  60 +
55 let $ = cheerio.load(html, {decodeEntities: false}); 61 let $ = cheerio.load(html, {decodeEntities: false});
56 62
57 _.each($('img').slice(0, num), item => { 63 _.each($('img').slice(0, num), item => {
@@ -92,7 +92,7 @@ exports.gethtml = (param) => { @@ -92,7 +92,7 @@ exports.gethtml = (param) => {
92 return globalapi.get('product/api/v1/detail/gethtml', param, { 92 return globalapi.get('product/api/v1/detail/gethtml', param, {
93 cache: true 93 cache: true
94 }).then((result) => { 94 }).then((result) => {
95 - result = result || ''; 95 + result = _.isString(result) ? result : '';
96 result = $.load(result); 96 result = $.load(result);
97 result = result('.good-detail-page'); 97 result = result('.good-detail-page');
98 98
@@ -353,7 +353,7 @@ module.exports = class extends global.yoho.BaseModel { @@ -353,7 +353,7 @@ module.exports = class extends global.yoho.BaseModel {
353 }, 353 },
354 param: {cache: true} 354 param: {cache: true}
355 }).then(result => { 355 }).then(result => {
356 - result = result || ''; 356 + result = _.isString(result) ? result : '';
357 result = $.load(result); 357 result = $.load(result);
358 358
359 result = result('#productDesc'); 359 result = result('#productDesc');
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 */ 5 */
6 6
7 'use strict'; 7 'use strict';
  8 +const _ = require('lodash');
8 const $ = require('cheerio'); 9 const $ = require('cheerio');
9 const helpers = global.yoho.helpers; 10 const helpers = global.yoho.helpers;
10 11
@@ -35,6 +36,8 @@ module.exports = class extends global.yoho.BaseModel { @@ -35,6 +36,8 @@ module.exports = class extends global.yoho.BaseModel {
35 return ''; 36 return '';
36 } 37 }
37 38
  39 + result = _.isString(result) ? result : '';
  40 +
38 let goodsContainer = $.load(result)('#shop-goods-container'); 41 let goodsContainer = $.load(result)('#shop-goods-container');
39 let goodThumb = goodsContainer.find('.good-thumb'); 42 let goodThumb = goodsContainer.find('.good-thumb');
40 43