index-service.js
11.1 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const Fn = require('lodash/fp');
const helpers = global.yoho.helpers;
const OrderService = require('./orders-service');
const IndexApi = require('./index-api');
const UserApi = require('./user-api');
const MsgApi = require('./message');
const headerModel = require('../../../doraemon/models/header');
const cache = global.yoho.cache;
const cachePreKey = 'HOME_INDEX_';
const defaultAvatar = '//img10.static.yhbimg.com/headimg/' +
'2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100';
const numbersKey = {
pending: 'pengding',
unread: 'unread',
needComment: 'needComment'
};
const CHANNEL_NUM = {
boys: 1,
girls: 2,
kids: 3,
lifestyle: 4
};
const IMG_DOMAIN = {
'01': [
'img10.static.yhbimg.com',
'img11.static.yhbimg.com'
],
'02': [
'img12.static.yhbimg.com',
'img13.static.yhbimg.com'
]
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 处理品牌的图片
*/
_handleBrandLogo(url) {
if (_.startsWith(url, 'http://') || !url) {
return url;
}
let node = url.substr(15, 2);
return `//${IMG_DOMAIN[node][(url.length % 2)]}/brandLogo${url}`;
}
_channelNum(channel) {
return CHANNEL_NUM[channel] || CHANNEL_NUM.boys;
}
/**
* 处理品牌
*/
_handleBrand(brands, needNum) {
let that = this;
const handle = Fn.pipe(Fn.filter({is_hot: 'Y'}), Fn.take(needNum), Fn.map((brand) => ({
href: helpers.urlFormat('', null, brand.brand_domain),
logo: that._handleBrandLogo(brand.brand_ico, 'brandLogo'),
name: brand.brand_name
})));
return handle(brands);
}
/**
* 处理商品
*/
_handleProduct(products) {
return products.map(product => {
let img = helpers.image(_.get(product, 'default_images', ''), 100, 100);
if (img.indexOf('imageView') !== -1) {
img = img.split('imageView', 1) +
'imageMogr2/thumbnail/100x100/extent/100x100/background/d2hpdGU=/position/center/quality/90';
}
return {
href: helpers.getUrlBySkc(product.product_skn),
thumb: img,
name: product.product_name,
price: product.sales_price,
productId: product.product_id
};
});
}
/**
* 消息数量提示
*/
_msgNumber(uid, udid) {
let that = this;
return co(function * () {
let result = [
{href: helpers.urlFormat('/home/orders'), name: '待处理订单', count: 0},
{href: helpers.urlFormat('/home/message'), name: '未读消息', count: 0},
{href: helpers.urlFormat('/home/comment'), name: '待评论商品', count: 0}
];
let reqData = yield Promise.props({
pending: new IndexApi(that.ctx).pendingOrderCount(uid), // 待处理订单
unread: new IndexApi(that.ctx).unreadMessageCount(uid, udid), // 未读消息
needComment: new IndexApi(that.ctx).needCommentCount(uid) // 待评论商品
});
result[0].count = _.get(reqData, 'pending.data.count', 0);
result[1].count = _.get(reqData, 'unread.data.inbox_total', 0);
result[2].count = _.get(reqData, 'needComment.data', 0);
return result;
})();
}
/**
* 最新订单
*/
_recentOrder(uid) {
let orderServiceModel = new OrderService(this.ctx);
return orderServiceModel.getOrders(uid, 1, 2, orderServiceModel.ORDER_TYPE.all).then(latestOrder => {
return {
more: helpers.urlFormat('/home/orders'),
orders: latestOrder
};
});
}
/**
* 你喜欢的品牌
*/
_guessYouLikeBrand() {
const NEED_BRAND_NUM = 6;
return new IndexApi(this.ctx).guessBrand().then(brand => {
return this._handleBrand(_.get(brand, 'data', []), NEED_BRAND_NUM);
});
}
/**
* 新品
*/
_newProduct() {
return new IndexApi(this.ctx).newArrival().then(newProduct => {
return this._handleProduct(_.get(newProduct, 'data.product_list', []));
});
}
/**
* 为你优选
*/
_recommend(channelNum, uid, udid) {
return new IndexApi(this.ctx).recommend(channelNum, uid, udid, '100004', 30).then(resData => {
return this._handleProduct(_.get(resData, 'data.product_list', []));
});
}
/**
* 底部banner
*/
_footerBanner() {
const CODE = '20110609-152143';
return new IndexApi(this.ctx).getByNodeContent(CODE, {}, {cache: 86400}).then(banner => {
return _.get(banner, 'data', '').replace(/http:\/\//g, '//');
});
}
/**
* 取消订单
*/
_cancelReason(uid) {
return new OrderService(this.ctx).closeReason(uid);
}
// 个人中心首页 异步获取新品上架数据
async newArrive() {
let arriveCaceKey = `${cachePreKey}arrive`;
let arrive = await cache.get(arriveCaceKey).then(result => {
return JSON.parse(result);
}).catch(() => {
return {};
});
if (_.isEmpty(arrive)) {
let arriveOrigin = await new IndexApi(this.ctx).newArrival();
arrive = this._handleProduct(_.get(arriveOrigin, 'data.product_list', []));
if (!_.isEmpty(arrive)) {
cache.set(arriveCaceKey, arrive, 3600);
}
}
return Promise.resolve({newArrive: arrive});
}
// 推荐数据
async recommend(uid, udid, channelNum) {
let recommendCaceKey = `${cachePreKey}${uid}recommend`;
let recommend = await cache.get(recommendCaceKey).then(result => {
return JSON.parse(result);
}).catch(() => {
return {};
});
if (_.isEmpty(recommend)) {
let recommendOrigin = await new IndexApi(this.ctx).recommend(channelNum, uid, udid, '100004', 30);
recommend = this._handleProduct(_.get(recommendOrigin, 'data.product_list', []));
if (!_.isEmpty(recommend)) {
cache.set(recommendCaceKey, recommend, 3600);
}
}
return Promise.resolve({recommend: recommend});
}
// 个人中心首页 异步获取品牌数据
async brands() {
let brandsCaceKey = `${cachePreKey}brands`;
let brand = await cache.get(brandsCaceKey).then(result => {
return JSON.parse(result);
}).catch(() => {
return {};
});
if (_.isEmpty(brand)) {
let brandOrigin = await new IndexApi(this.ctx).guessBrand();
brand = this._handleBrand(_.get(brandOrigin, 'data', []), 6);
if (!_.isEmpty(brand)) {
cache.set(brandsCaceKey, brand, 86400);
}
}
return Promise.resolve({brands: brand, more: '/brands'});
}
// 个人中心异步查询我的消息和头像
async newsAvatar(uid) {
let result = await Promise.props(
{
profile: new UserApi(this.ctx).getUserInfo(uid),
msg: new MsgApi(this.ctx).unreadTotal(uid)
}
).catch(() => {
return {};
});
let avatar = helpers.image(_.get(result, 'profile.data.head_ico', ''), 100, 100) || defaultAvatar;
let msgCount = _.get(result, 'msg.data.total', 0);
return Promise.resolve({avatar: avatar, msg: msgCount});
}
// 首页顶部数量
async numbers(uid, udid) {
let result = {};
let numbers = await Promise.props({
pending: new IndexApi(this.ctx).pendingOrderCount(uid), // 待处理订单
unread: new IndexApi(this.ctx).unreadMessageCount(uid, udid), // 未读消息
needComment: new IndexApi(this.ctx).needCommentCount(uid) // 待评论商品
}).catch(() => {
return {};
});
result[numbersKey.pending] = _.get(numbers, 'pending.data.count', 0);
result[numbersKey.unread] = _.get(numbers, 'unread.data.inbox_total', 0);
result[numbersKey.needComment] = _.get(numbers, 'needComment.data', 0);
return Promise.resolve(result);
}
// 个人中心首页同步数据
async indexNew(uid, udid, channel, isStudent) {
let indexData = await Promise.props({
header: headerModel.requestHeaderData(channel),
recentOrder: this._recentOrder(uid),
footerBanner: this._footerBanner(),
reason: this._cancelReason(uid)
}).catch(() => {
return {};
});
return Object.assign(
{
content: {
content: {
messages: [
{href: helpers.urlFormat('/home/orders'), name: '待处理订单', id: numbersKey.pending, count: 0},
{href: helpers.urlFormat('/home/message'), name: '未读消息', id: numbersKey.unread, count: 0},
{href: helpers.urlFormat('/home/comment'), name: '待评论商品',
id: numbersKey.needComment, count: 0}
],
certifiedName: +isStudent ? '学生身份已验证' : '身份验证',
certifiedUrl: helpers.urlFormat('/product/students/'),
latestOrders: Object.assign(indexData.recentOrder, {cancelReason: indexData.reason})
},
banner: indexData.footerBanner
}
},
{
header: indexData.header
}
);
}
index(uid, udid, channel, isStudent) {
let that = this;
return co(function * () {
let reqData = yield Promise.props({
msgNumber: that._msgNumber(uid, udid),
recentOrder: that._recentOrder(uid),
guessBrand: that._guessYouLikeBrand(),
newProduct: that._newProduct(),
recommendProduct: that._recommend(that._channelNum(channel), uid, udid),
footerBanner: that._footerBanner(),
reason: that._cancelReason()
});
return {
content: {
certifiedName: +isStudent ? '学生身份已验证' : '身份验证',
certifiedUrl: helpers.urlFormat('/product/students/'),
messages: reqData.msgNumber,
latestOrders: Object.assign(reqData.recentOrder, {cancelReason: reqData.reason}),
favBrand: {
more: '/brands',
brands: reqData.guessBrand
},
newArrival: reqData.newProduct
},
recommend: reqData.recommendProduct,
banner: reqData.footerBanner
};
})();
}
};