seo-service.js
5.99 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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 seoHandler = require('./seo-handler');
const utils = '../../../utils';
const productProcess = require(`${utils}/product-process-simple`);
const _handleImageUrl = (url) => {
if (url) {
let imgArr = _.split(url, '?', 1);
imgArr.push('imageView2/1/w/{width}/h/{height}/q/90');
url = imgArr.join('?');
}
return url;
};
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;
let goodsList = productProcess.processProductList(data.product_list,
Object.assign({showDiscount: false, from: {type: 'hot', params: params}}, params));
goodsList.map(goods => {
goods.productTitle = `${params.query}|${goods.product_name}`;
return goods;
});
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: goodsList,
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: '相关推荐',
active: true,
extraGoodsList: productProcess.processProductList(data.product_list,
Object.assign({showDiscount: false, from: {type: 'hot', params: params}}, params))
}, {
extraTabName: '最近预览',
latestWalk: 5
}];
}
return finalResult;
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.searchApi = new SearchApi(ctx);
}
getSearchProduct(params, channel, sort) {
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';
let headerModelCtx = new HeaderModel(this.ctx);
return Promise.all([
headerModelCtx.requestHeaderData(channel, true),
this.searchApi.getSeoProductList(searchParams, 'fuzzySearch'),
this.searchApi.getSeoProductList(Object.assign({}, searchParams, {
order: 's_n_desc',
limit: 5
}), 'fuzzySearch'),
]).then(result => {
if (!sort || _.get(result[1], 'data.total') > 0) {
return result;
}
delete searchParams.query;
searchParams.sort = sort;
return this.searchApi.getSeoProductList(searchParams,
'fuzzySearch').then(subRes => {
result[1] = subRes;
return result;
});
}).then(result => {
return _setHotKeywordData(result, params, channel);
});
}
getHotKeywordData(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')) {
logger.error(`cannot find hot keywords by id(${id})`);
return false;
}
params.query = keyword.name;
return this.getSearchProduct(params, channel, keyword.sort_id).then(result => {
const keyNum = 10;
let hotKeys = (keyword.data || []).map(val => {
val.href = helpers.urlFormat(`/hot/${val.id}.html`);
return val;
});
let seoTDK = seoHandler.getHotKeywordsSeo(keyword.name, _.get(result, 'product.totalCount', '多'));
if (keyword.goods_img) {
keyword.goods_img = _handleImageUrl(keyword.goods_img);
} else {
let goods = _.last(_.get(result, 'product.goods', []));
keyword.goods_img = _handleImageUrl(_.get(goods, 'thumb', ''));
}
keyword.list = _.take(hotKeys, keyNum);
keyword.describe = keyword.describe || seoTDK.description;
Object.assign(result, {
hotKeys: _.drop(hotKeys, keyNum),
keyword: keyword
}, seoTDK);
return result;
});
});
}
};