chanpin.js
2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const utils = '../../../utils';
const redis = require(`${utils}/redis`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const camelCase = global.yoho.camelCase;
class List extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
list(params) {
let options = {
data: {
method: 'web.search.forseo',
sales: params.sales,
outlets: params.outlets,
stocknumber: params.stocknumber,
need_filter: params.need_filter,
query: params.query,
type: params.type,
page: params.page || 1,
limit: params.limit || 24,
order: params.order || 0
},
param: {
code: 200
}
};
return this.get(options).then(result => {
return result;
});
}
getSearchKeywordDataById(params) {
return redis.all([
['get', `global:yoho:seo:keywords:id:${params.id}`]
]).then(redisData => {
return redisData;
});
}
index(params) {
return Promise.all([
this.getSearchKeywordDataById(params),
]).then(result => {
let resu = {
list: [],
fuzzyWord: []
};
if (_.get(result, '[0][0]')) {
let build = [];
let redisData = JSON.parse(result[0][0]);
return this.list(Object.assign(params, {query: redisData.name})).then(listData => {
_.forEach(_.slice(redisData.data, 0, 12), value => {
build.push({
name: value.keyword,
link: helpers.urlFormat(`/mip/list/${value.id}.html`, null)
});
});
resu.name = redisData.name;
resu.fuzzyWord = build;
if (_.get(listData, 'data.product_list')) {
resu.list = camelCase(listData.data.product_list);
_.forEach(resu.list, value => {
value.isSoonSoldOut = value.isSoonSoldOut === 'Y';
});
}
return resu;
});
} else {
return resu;
}
});
}
}
module.exports = List;