index.js
8.22 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
/**
* 逛models
* @author: chenfeng<feng.chen@yoho.cn>
* @date: 2016/09/07
*/
'use strict';
const serviceAPI = global.yoho.ServiceAPI;
const api = global.yoho.API;
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const guangProcess = require(`${global.utils}/guang-process`);
const _ = require('lodash');
/**
* [获取作者信息]
* @param {[int]} id [作者id]
* @return {[object]}
*/
const getAuthor = (id) => {
return serviceAPI.get('guang/service/v1/author/getAuthor', {
author_id: id
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return result;
} else {
logger.error('getAuthor code no 200');
return {};
}
});
};
/**
* 逛分类
*/
const _category = () => {
return serviceAPI.get('/guang/api/v1/category/get', {
}, {
cache: true,
code: 200
});
};
/**
* [逛内容列表]
* @param {[string]} gender ["1,3"表示男, "2,3"表示女, "1,2,3"表示所有]
* @param {[int]} sortId [分类ID]
* @param {Number} uid [用户ID]
* @param {String} udid [客户端唯一标识]
* @param {Number} page [分页第几页, 默认第1页]
* @param {[string]} tag [标签]
* @param {[int]} authorId [作者ID]
* @param {[int]} limit [返回的限制数]
* @param {Boolean} useCache [是否使用缓存]
* @return {[array]}
*/
const getArticleList = (gender, sortId, uid, udid, page, tag, authorId, limit, useCache) => {
let param = {
page: page || 1,
uid: uid || 0,
udid: udid || '',
gender: gender || '',
sort_id: sortId,
tag: tag,
author_id: authorId,
limit: limit
};
return serviceAPI.get('guang/api/v2/article/getList', param, {
cache: useCache
}).then((result) => {
if (result && result.code === 200) {
return result;
} else {
logger.error('getAuthor code no 200');
return [];
}
});
};
/**
* [获取切换逛类别或者分页时的文章数据]
* @param {[string]} gender ["1,3"表示男, "2,3"表示女]
* @param {[int]} sortId [分类ID]
* @param {[int]} uid [用户ID]
* @param {[string]} udid [客户端唯一标识]
* @param {[int]} page [分页第几页, 默认第1页]
* @param {[string]} tag [标签]
* @param {[string]} authorId [作者ID]
* @param {Boolean} isApp [是否是APP]
* @param {[Boolean]} showAuthor [是否显示作者]
* @param {Boolean} isTab [是否为tab切换操作]
* @return {[array]}
*/
const getPageData = (gender, sortId, uid, udid, page, tag, authorId, isApp, showAuthor, isTab) => {
return getArticleList(gender, sortId, uid, udid, page, tag, authorId).then(article => {
let result = {};
if (!article.data.list.artList) {
return result;
}
let adList = article.data.list.adlist;
// 广告列表
if (isTab && adList) {
let build = [];
adList.forEach(ad => {
build.push({
url: guangProcess.getFilterUrl(ad.url),
img: helpers.image(ad.src, 830, 327)
});
});
result.swiper = build;
}
/* 构建资讯文章内容 */
let build = [];
let artList = article.data.list.artList;
artList.forEach(art => {
build.push(guangProcess.formatArticle(art, true, isApp, showAuthor, uid));
// build.push(guangProcess.formatArticle(art, true, isApp, true, uid));
});
result.infos = build;
return result;
});
};
/**
* 逛内容列表
*/
const _article = (param) => {
return serviceAPI.get('/guang/api/v2/article/getList', {
gender: param.gender,
page: param.page || 1,
uid: param.uid,
udid: param.udid,
sort_id: 0,
tag: param.tag ? param.tag : null
// author_id: param.authorId ? param.authorId : null,
// limit: param.limit ? param.limit : null
}, {
cache: true,
code: 200
}).then(result => {
return result;
});
};
/**
* 逛
* @param params
*/
const getArticle = (param) => {
let page = param.page ? param.page : 1;
Object.assign(param, { page: page });
return api.all([
_category(),
_article(param)
]).then(result => {
let type = param.type;
let resu = {
guang: {
}
};
// 顶部的分类列表
let curIndex = 0;// 当前tab顺序
let indexTmp = 0;
if (result[0] && result[0].data) {
indexTmp = 0;
let cateList = result[0].data;
let build = [];
let inf = [];
cateList.forEach(val => {
build.push({
typeId: val.id,
type: val.name,
focus: (val.id === type)
});
inf.push({
show: (val.id === type),
typeId: type,
info: []
});
if ((val.id === type)) {
curIndex = indexTmp;
}
indexTmp++;
resu.guang.navs = build;
resu.guang.infos = inf;
});
}
if (result && result[1] && result[1].data && result[1].data.list) {
let swp = [];
let swiperList = result[1].data.list.adlist;
swiperList.forEach(val => {
swp.push({
url: guangProcess.getFilterUrl(val.url),
img: helpers.image(val.src, 640, 275)
});
});
resu.guang.swiper = swp;
}
if (result && result[1] && result[1].data && result[1].data.list) {
let inf = [];
let infoList = result[1].data.list.artList;
infoList.forEach(val => {
inf.push(guangProcess.formatArticle(val, true, false, true));
});
resu.guang.infos[curIndex].info = inf;
}
return resu;
});
};
const getTagEditor = (param) => {
let page = param.page ? param.page : 1;
Object.assign(param, { page: page });
return api.all([
_article(param)
]).then(result => {
let resu = {
guang: {
tag: param.tag,
gender: param.gender,
isApp: param.isApp ? 1 : 0,
guangList: true
}
};
if (result && result[0] && result[0].data && result[0].data.list && result[0].data.list.artList) {
let inf = [];
let infoList = result[0].data.list.artList;
infoList.forEach(val => {
val.url = helpers.https(`${val.url}&openby:yohobuy={"action":"go.h5","params":{"param":{},"share":"","id":"","type":0,"islogin":"N","url":"${val.url}"}}`);
inf.push(guangProcess.formatArticle(val, true, false, true));
});
resu.guang.infos = inf;
}
return resu;
});
};
/**
* 获取制指定文章的动态信息
* @param ids
* @param udid
* @param other [Obejct] 包含uid,query,type等非必传参数
* @returns {Promise.<T>|*}
*/
const getDynamicDataByIds = (ids, udid, other) => {
let params = {
articleIds: ids,
udid: udid
};
if (other.uid) {
_.assign(params, {
uid: other.uid
});
}
if (other.query) {
_.assign(params, {
query: other.query
});
}
if (other.type) {
_.assign(params, {
type: other.type
});
}
return serviceAPI.get('guang/api/v6/article/getSimpleArticleList', params, {cache: true});
};
/**
* 获取制指定文章的动态信息
* @param ids
* @returns {Promise.<T>|*}
*/
const getDynamicDataById = (id, uid, udid) => {
return serviceAPI.get('/gateway/guang/api/v6/article/getArticlePraiseAndFavor', {
id: id,
uid: uid,
udid: udid
});
};
module.exports = {
getAuthor,
getArticleList,
getPageData,
getArticle,
getTagEditor,
getDynamicDataByIds,
getDynamicDataById
};