guang.js
9.91 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
/**
* 逛详情models
* @author: chenfeng<feng.chen@yoho.cn>
* @date: 2016/09/07
*/
'use strict';
const serviceAPI = global.yoho.ServiceAPI;
const api = global.yoho.API;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const URI_PACKAGE_ARTICLE = 'guang/service/v2/article/';
const URI_PACKAGE_AUTHOR = 'guang/service/v1/author/';
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 serviceAPI.get('operations/api/v6/category/getCategory', {}, {
cache: true
}).then(result => {
if (result && result.code === 200) {
return this._processSideBar(result.data, choosed);
}
});
}
_getShareData(id) {
return serviceAPI.get('guang/api/v6/share/guang', {
id: id
}).then(result => {
if (result && result.code === 200) {
return result.data;
}
});
}
/**
* 获取文章接口调用
* @param {*} articleId
*/
_getArticle(articleId) {
return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticle`, {
article_id: articleId
}, {
cache: true
});
}
/**
* 获取作者接口调用
* @param {*} authorId
*/
_getAuthor(authorId) {
return serviceAPI.get(`${URI_PACKAGE_AUTHOR}getAuthor`, {
author_id: authorId
}, {
cache: true
});
}
/**
* 获取文章详情接口调用
* @param {*} articleId
*/
_getArticleContent(articleId) {
return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticleContent`, {
article_id: articleId
}, {
cache: true
});
}
/**
* 获取文章相关品牌接口调用
* @param {*} articleId
*/
_getBrand(articleId) {
return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getBrand`, {
article_id: articleId
}, {
cache: true
});
}
/**
* 获取资讯相关的其它资讯接口调用
* @param {*} articleId
* @param {*} tag
*/
_getOtherArticle(articleId, tag) {
return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getOtherArticle`, {
article_id: articleId,
tags: tag,
offset: 0,
limit: 3
}, {
cache: true
});
}
/**
* APP 获取微信模块
*/
_getSingleTemplateWechat() {
return api.get('', {
method: 'app.resources.getSingleTemplate',
module: 'wechat',
key: 'guang_detail_wechat'
}, {
cache: true
});
}
/**
* [逛资讯详情页数据封装]
* @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) {
result.code = 400;
return result;
}
let article = result.getArticle = data && data.data || {};
let promises = [
this._getAuthor(article.author_id),
this._getArticleContent(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]) {
result.getArticleContent = getArticleContent = datas[1].data;
}
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;
}
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) {
let param = {
article_id: id,
client_type: 'h5'
};
return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticleContent`, param, {
cache: true
});
}
/**
* [根据商品SKN获取商品的简要信息]
* @param {[array]} sknString [skns]
* @return {[type]}
*/
productInfoBySkns(sknString) {
// 调用搜索接口
let param = {
method: 'h5.product.batch',
productSkn: sknString,
order: 's_t_desc'
};
return api.get('', param, {
cache: true
}).then(result => {
return _.get(result, 'data.product_list', []);
});
}
}
module.exports = DetailModel;