detail.js
13.4 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
* 逛详情models
* @author: chenfeng<feng.chen@yoho.cn>
* @date: 2016/09/07
*/
'use strict';
const serviceAPI = global.yoho.ServiceAPI;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const redis = require('../../../utils/redis');
const URI_PACKAGE_ARTICLE = 'guang/service/v2/article/';
const URI_PACKAGE_AUTHOR = 'guang/service/v1/author/';
const URI_PACKAGE_COMMENTS = 'guang/api/v1/comments/';
const URI_PACKAGE_PRAISE = 'guang/api/v1/article/';
class DetailModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取二级菜单顶部颜色
* @param {[string]} choosed
* @return {[string]}
*/
_getSidebarColor(choosed) {
let color = false;
if (choosed === 'girls') {
color = '#FF88AE';
} else if (choosed === 'kids') {
color = '#7ad9f9';
} else if (choosed === 'lifestyle') {
color = '#4f4138';
}
return color;
}
/**
* 微信侧边栏导航数据
* @param {*} list
* @param {*} choosed
*/
_processSideBar(list, choosed) {
const formatData = [];
let offset = 0; // 分割数组用到的游标
list = list || [];
_.forEach(list, (item, i) => {
if (item.sub) {
item.sub.unshift({
sort_name: item.sort_name,
sort_name_en: item.sort_name_en,
back: true,
isSelect: false,
bgColor: this._getSidebarColor(choosed)
});
}
// 如果有分隔符,分割数组
if (item.separative_sign === 'Y') {
formatData.push(list.slice(offset, i));
offset = i;
}
});
// 数组被分割剩余的部分
formatData.push(list.slice(offset));
return formatData;
}
_getLeftNav(choosed) {
choosed = choosed || 'all';
return this.get({
api: serviceAPI,
url: 'operations/api/v6/category/getCategory',
param: {
cache: true
}
}).then(result => {
if (result && result.code === 200) {
return this._processSideBar(result.data, choosed);
}
});
}
_getShareData(id) {
return this.get({
api: serviceAPI,
url: 'guang/api/v6/share/guang',
data: {
id: id
}
}).then(result => {
if (result && result.code === 200) {
return result.data;
}
});
}
/**
* 获取文章接口调用
* @param {*} articleId
*/
_getArticle(articleId) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_ARTICLE}getArticle`,
data: {
article_id: articleId
},
param: {
cache: true
}
});
}
/**
* 获取作者接口调用
* @param {*} authorId
*/
_getAuthor(authorId) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_AUTHOR}getAuthor`,
data: {
author_id: authorId
},
param: {
cache: true
}
});
}
/**
* 获取文章详情接口调用
* @param {*} articleId
*/
_getArticleContent(articleId) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_ARTICLE}getArticleContent`,
data: {
article_id: articleId
},
param: {
cache: true
}
});
}
/**
* 获取文章相关品牌接口调用
* @param {*} articleId
*/
_getBrand(articleId) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_ARTICLE}getBrand`,
data: {
article_id: articleId
},
param: {
cache: true
}
});
}
/**
* 获取资讯相关的其它资讯接口调用
* @param {*} articleId
* @param {*} tag
*/
_getOtherArticle(articleId, tag) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_ARTICLE}getOtherArticle`,
data: {
article_id: articleId,
tags: tag,
offset: 0,
limit: 3
},
param: {
cache: true
}
});
}
/**
* APP 获取微信模块
*/
_getSingleTemplateWechat() {
return this.get({
data: {
method: 'app.resources.getSingleTemplate',
module: 'wechat',
key: 'guang_detail_wechat'
}
});
}
// 获取逛文章分词内容
_getGuangArticleKeyword(id) {
return redis.all([
['get', `global:yoho:guang:detail:${id}`]
]).then(redisData => {
redisData = JSON.parse(redisData[0] || '{}');
if (!redisData.data) {
return this._getArticleContent(id);
}
return redisData;
});
}
/**
* [逛资讯详情页数据封装]
* @param {[int]} id [内容ID]
* @param {Boolean} isApp [标识是否是APP访问]
* @return {[array]}
*/
packageData(id, isApp, isWeixin, channel, isShare) {
let result = {
getAuthor: {},
getArticle: {},
getArticleContent: {},
getBrand: {},
getOtherArticle: {}
};
// 获取资讯
return this._getArticle(id).then(data => {
// 调用接口失败
if (!data || data.code !== 200 || _.get(data, 'data.status') !== 1) {
result.code = 400;
return result;
}
let article = result.getArticle = data && data.data || {};
let promises = [
this._getAuthor(article.author_id),
this._getGuangArticleKeyword(id),
this._getBrand(id)
];
// 获取资讯相关的其它资讯
if (typeof article.tag !== 'undefined') {
promises.push(this._getOtherArticle(id, article.tag));
}
// APP 获取微信模块
if (isApp) {
promises.push(this._getSingleTemplateWechat());
}
if (isShare) {
let navGender = _.cloneDeep(channel);
promises.push(
this._getLeftNav(navGender),
this._getShareData(id)
);
}
return Promise.all(promises).then(datas => {
let getArticleContent = [];
if (!datas) {
return result;
}
if (datas[1]) {
let textPosition = 1,
descriptionText;
result.getArticleContent = getArticleContent = datas[1].data || [];
for (let i = 0; i < getArticleContent.length; i++) {
if (getArticleContent[i].text && textPosition === 1) {
descriptionText = _.get(getArticleContent[i], 'text.data.text');
_.set(result, 'getArticle.descriptionText', _.replace(descriptionText, /<\/?[^>]*>/g, ''));
textPosition++;
}
}
}
if (isApp && datas[4] && datas[4].data) {
let preCount = 0;
let i;
for (i = 0; i < getArticleContent.length; i++) {
if (getArticleContent[i].singleImage ||
getArticleContent[i].text || getArticleContent[i].smallPic) {
preCount++;
}
}
_.forEach(datas[4].data, item => {
item.src = helpers.image(item.src, 280, 210, 1);
});
getArticleContent.splice(preCount, 0, {
weixinPublic: datas[4].data
});
}
if (isShare && datas[5]) {
if (datas[5].wechatShareImgUrl) {
datas[5].wechatShareImgUrl =
datas[5].wechatShareImgUrl.substring(datas[5].wechatShareImgUrl.indexOf('//'));
if (datas[5].wechatShareImgUrl.indexOf('?') === -1) {
datas[5].wechatShareImgUrl = datas[5].wechatShareImgUrl +
'?imageView2/2/interlace/1/q/75';
}
} else if (datas[5].qqShareImgUrl) {
datas[5].qqShareImgUrl =
datas[5].qqShareImgUrl.substring(datas[5].qqShareImgUrl.indexOf('//'));
if (datas[5].qqShareImgUrl.indexOf('?') === -1) {
datas[5].qqShareImgUrl = datas[5].wechatShareImgUrl + '?imageView2/2/interlace/1/q/75';
}
} else if (datas[5].showShareImgUrl) {
datas[5].showShareImgUrl =
datas[5].showShareImgUrl.substring(datas[5].showShareImgUrl.indexOf('//'));
if (datas[5].showShareImgUrl.indexOf('?') === -1) {
datas[5].showShareImgUrl = datas[5].showShareImgUrl + '?imageView2/2/interlace/1/q/75';
}
}
let preCount = 0;
let i;
for (i = 0; i < getArticleContent.length; i++) {
if (getArticleContent[i].singleImage ||
getArticleContent[i].text || getArticleContent[i].smallPic) {
preCount = i + 1;
}
}
getArticleContent.splice(preCount, 0, {
shareCode: datas[5]
});
}
if (datas[0]) {
result.getAuthor = datas[0].data;
}
if (datas[2]) {
result.getBrand = datas[2].data;
_.forEach(result.getBrand, function(val) {
val.url = `//m.yohobuy.com/shop?domain=${val.brandDomain}`;
});
}
if (isShare && datas[4]) {
result.sideNav = datas[4];
}
if (datas.length === 5 && isApp || datas.length === 4 && !isApp || datas.length === 6 && isShare) {
if (datas[3]) {
result.getOtherArticle = datas[3].data;
}
}
return result;
});
});
}
/**
* [获取详情信息]
* @param {[int]} id [资讯id]
* @return {[object]}
*/
intro(id) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_ARTICLE}getArticleContent`,
data: {
article_id: id,
client_type: 'h5'
},
param: {
cache: true
}
});
}
/**
* [根据商品SKN获取商品的简要信息]
* @param {[array]} sknString [skns]
* @return {[type]}
*/
productInfoBySkns(sknString) {
let limit = sknString && sknString.split(',').length;
return this.get({
data: {
method: 'app.search.recomandLi',
query: sknString,
order: 's_t_desc',
limit: limit || 1
},
param: {
cache: true
}
}).then(result => {
return _.get(result, 'data.product_list', []);
});
}
/**
* 获取文章评论列表
*/
comments(params) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_COMMENTS}getList`,
data: {
article_id: params.article_id,
page: params.page,
limit: params.limit || 20,
udid: params.udid
}
}).then(result => {
return _.get(result, 'data.list', []);
});
}
/**
* 获取评论数量
* @param {*} params
*/
commentsTotal(params) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_COMMENTS}getList`,
data: {
article_id: params.article_id,
page: 1,
limit: 1,
udid: params.udid
}
}).then(result => {
return _.get(result, 'data.total', 0);
});
}
/**
* 获取点赞相关数据
* @param {*} params
*/
getArticlePraiseAndFavor(params) {
return this.get({
api: serviceAPI,
url: `${URI_PACKAGE_PRAISE}getArticlePraiseAndFavor`,
data: {
uid: params.uid,
id: params.id,
udid: params.udid
}
}).then(result => {
return _.get(result, 'data', {});
});
}
}
module.exports = DetailModel;