about.js
5.65 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
/**
* about model
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2017/11/20
*/
const _ = require('lodash');
const cache = global.yoho.cache;
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const redis = global.yoho.redis;
const handleStaticUrl = require(`${global.utils}/parameter`).fullParamToMinPath;
const hotBrandsModel = require('../../../doraemon/models/hot-brands');
const CACHE_TIME_S = 60 * 60;
const indexUrl = {
boys: helpers.urlFormat('/'),
girls: helpers.urlFormat('/woman'),
kids: helpers.urlFormat('/kids'),
lifestyle: helpers.urlFormat('/lifestyle')
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
getCategoryFromApi(channel) {
return this.get({data: {
method: 'web.regular.groupsort.sale',
sales: 'Y', // 在销售商品分类
status: 1, // 上架商品分类
stocknumber: 1, // 过滤掉已售罄
yh_channel: channel
}, param: {cache: true}});
}
getCategoryData(channel) {
return Promise.all([
this.getCategoryFromApi(1),
this.getCategoryFromApi(2),
this.getCategoryFromApi(3),
this.getCategoryFromApi(4),
hotBrandsModel.hotBrands()
]).then(result => {
const nameMap = ['男生', '女生', '潮童', '创意生活'];
const enMap = ['boys', 'girls', 'kids', 'lifestyle'];
const genderMap = ['1,3', '2,3'];
const baseUrl = '/list';
const hotBrands = result.pop();
let categoryList = [];
_.forEach(result, (res, index) => {
if (res.code !== 200) {
return;
}
let list = [],
gender = genderMap[index],
ename = enMap[index];
_.forEach(res.data || [], cate => {
let sub = [];
let seatUrl = helpers.urlFormat(handleStaticUrl(baseUrl, {
category_id: '{categoryId}',
gender: gender,
channel: ename
}));
_.forEach(cate.sub, scate => {
sub.push({
name: scate.category_name,
url: seatUrl.replace('{categoryId}', scate.category_id)
});
});
list.push({
title: cate.category_name,
sub: sub
});
});
categoryList.push({
channelName: nameMap[index],
list: list
});
});
let upChannel = _.toUpper(channel);
return {
pathNav: [
{pathTitle: `${upChannel}首页`, name: `${upChannel}首页`, href: indexUrl[channel]},
{pathTitle: '全部分类', name: '全部分类'}
],
hotBrands,
categoryList
};
});
}
getCategoryDataWithCache(channel) {
let cacheKey = 'seo_category_group_map';
return cache.get(cacheKey).then(cdata => {
let cdataObj;
if (cdata) {
try {
cdataObj = JSON.parse(cdata);
} catch (e) {
logger.debug('category map cache data parse fail.');
}
}
if (cdataObj) {
return cdataObj;
}
return this.getCategoryData(channel).then(list => {
if (list && list.length) {
cache.set(cacheKey, list, CACHE_TIME_S);
}
return list;
});
}).catch(err => {
logger.debug(`get category map cache data fail:${err.toString()}`);
return this.getCategoryData(channel);
});
}
getChanpinData(channel) {
return Promise.all([
redis.all([['get', 'global:yoho:seo:goodsAll']]),
hotBrandsModel.hotBrands()
]).then(result => {
let upChannel = _.toUpper(channel);
let chanpinList = [];
try {
chanpinList = JSON.parse(_.get(result, '[0][0]'));
} catch (e) {
logger.debug('chanpin all goods data parse fail.');
}
return {
pathNav: [
{pathTitle: `${upChannel}首页`, name: `${upChannel}首页`, href: indexUrl[channel]},
{pathTitle: '产品大全', name: '产品大全'}
],
hotBrands: result[1],
chanpinList
};
});
}
link() {
return redis.all([
['get', 'friend:text:links'],
['get', 'friend:img:links']
]).then(redisData => {
let textLinks = [],
imgLinks = [];
try {
textLinks = JSON.parse(redisData[0]);
} catch (e) {
logger.error(`friend text links parse error : ${JSON.stringify(e)}`);
}
try {
imgLinks = JSON.parse(redisData[1]);
} catch (e) {
logger.error(`friend img links parse error : ${JSON.stringify(e)}`);
}
return {
textLinks: _.sortBy(textLinks, o => {
return -o.sort;
}),
imgLinks: _.sortBy(imgLinks, o => {
return -o.sort;
})
};
});
}
};