seo-service.js
4.28 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const redis = global.yoho.redis;
const helpers = global.yoho.helpers;
const logger = global.yoho.logger;
const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header');
const SearchApi = require('./search-api');
const searchHandler = require('./search-handler');
const utils = '../../../utils';
const productProcess = require(`${utils}/product-process-simple`);
const _setHotKeywordData = (result, params, channel) => {
let changeQuery = Object.assign({}, params);
let finalResult = {
headerData: Object.assign(result[0].headerData, {
header: true
})
};
_.unset(changeQuery, 'query');
// 获取商品数据和顶部筛选条件
if (result[1].code === 200) {
let data = result[1].data;
Object.assign(finalResult,
searchHandler.handlePathNavData({total: data.total}, params, 'search', channel),
{
product: {
opts: searchHandler.handleOptsData(changeQuery, data.total),
totalCount: data.total,
footPager: searchHandler.handlePagerData(data.total, changeQuery),
goods: productProcess.processProductList(data.product_list,
Object.assign({showDiscount: false, from: {type: 'hot', params: params}}, params)),
hasNextPage: searchHandler.handleNextPage(changeQuery, data.total)
}
}
);
finalResult.hotBrands = _.get(data, 'filter.brand', []);
finalResult.hotBrands.forEach((val) => {
val.href = helpers.urlFormat(`/list/bd${val.id}.html`);
return val;
});
finalResult.criteo = {skn: searchHandler.getCriteo(_.get(finalResult.search, 'goods'))};
}
if (result[2].code === 200) {
let data = result[2].data;
finalResult.latestWalkExtra = [{
extraTabName: '相关推荐',
extraGoodsList: productProcess.processProductList(data.product_list,
Object.assign({showDiscount: false, from: {type: 'hot', params: params}}, params))
}];
}
return finalResult;
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.searchApi = new SearchApi(ctx);
}
getSearchProduct(params, channel) {
let searchParams = searchHandler.getSearchParams(params);
switch (channel) {
case 'boys':
searchParams.physical_channel = 1;
break;
case 'girls':
searchParams.physical_channel = 2;
break;
case 'kids':
searchParams.physical_channel = 3;
break;
case 'lifestyle':
searchParams.physical_channel = 4;
break;
default:
break;
}
searchParams.need_filter = 'yes';
return Promise.all([
headerModel.requestHeaderData(channel),
this.searchApi.getSeoProductList(searchParams, 'fuzzySearch'),
this.searchApi.getSeoProductList(Object.assign(searchParams, {
order: 's_n_desc',
limit: 5
}), 'fuzzySearch'),
]).then(result => {
return _setHotKeywordData(result, params, channel);
});
}
getHotKeywordDate(id, params, channel) {
return redis.all([
['get', `global:yoho:seo:hot:keywords:id:${id}`]
]).then(redisData => {
let keyword = redisData[0];
try {
keyword = JSON.parse(keyword);
} catch (e) {
logger.debug('getProductList cache data parse fail.');
}
if (!_.get(keyword, 'name')) {
return Promise.reject(`cannot find hot keywords by id(${id})`);
}
params.query = keyword.name;
return this.getSearchProduct(params, channel).then(result => {
result.hotKeys = (keyword.data || []).map(val => {
val.href = helpers.urlFormat(`/hot/${val.id}.html`);
return val;
});
result.keyword = keyword;
result.latestWalk = 5;
return result;
});
})
}
};