info.js
4.57 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
/**
* 逛详情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 URI_PACKAGE_ARTICLE = 'guang/service/v2/article/';
const URI_PACKAGE_AUTHOR = 'guang/service/v1/author/';
/**
* [逛资讯详情页数据封装]
* @param {[int]} id [内容ID]
* @param {Boolean} isApp [标识是否是APP访问]
* @return {[array]}
*/
const packageData = (id, isApp) => {
let result = {
getAuthor: {},
getArticle: {},
getArticleContent: {},
getBrand: {},
getOtherArticle: {}
};
// 客户端类型
let clientType = isApp ? 'iphone' : 'h5';
// 获取资讯
let param = {
article_id: id,
client_type: clientType,
// private_key: sign.privateKey[clientType]
};
return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticle`, param, {
cache: true
}).then(data => {
// 接口要判断一下返回状态是否成功
if (data === '' && data.code !== 200) {
result.code = 400;
return result;
}
let article = data.data;
result.getArticle = article;
let promises = [];
// 获取作者信息
param = {
author_id: article.author_id
};
// param.client_secret = sign.apiSign(param);
promises.push(serviceAPI.get(`${URI_PACKAGE_AUTHOR}getAuthor`, param, {
cache: true
}));
// 获取资讯内容
param = {
article_id: id,
client_type: clientType,
// private_key: sign.privateKey[clientType]
};
// param.client_secret = sign.apiSign(param);
promises.push(serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticleContent`, param, {
cache: true
}));
// 获取资讯相关的品牌
param = {
article_id: id,
client_type: clientType,
// private_key: sign.privateKey[clientType]
};
// param.client_secret = sign.apiSign(param);
promises.push(serviceAPI.get(`${URI_PACKAGE_ARTICLE}getBrand`, param, {
cache: true
}));
// 获取资讯相关的其它资讯
if (typeof article.tag !== 'undefined') {
param = {
article_id: id,
client_type: clientType,
tags: article.tag,
offset: 0,
limit: 3,
// private_key: sign.privateKey[clientType]
};
// param.client_secret = sign.apiSign(param);
promises.push(serviceAPI.get(`${URI_PACKAGE_ARTICLE}getOtherArticle`, param, {
cache: true
}));
}
if (isApp) {
promises.push(api.get('', {
method: 'app.resources.getSingleTemplate',
module: 'wechat',
key: 'guang_detail_wechat'
}));
}
return Promise.all(promises).then(datas => {
if (datas) {
if (datas[1]) {
let getArticleContent = datas[1].data;
result.getArticleContent = getArticleContent;
}
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++;
}
}
getArticleContent.splice(preCount, 0, {weixinPublic: datas[4].data});
}
if (datas[0]) {
result.getAuthor = datas[0].data;
}
if (datas[2]) {
result.getBrand = datas[2].data;
}
if (datas.length === 5 && isApp || datas.length === 4 && !isApp) {
if (datas[3]) {
result.getOtherArticle = datas[3].data;
}
}
}
return result;
});
});
};
/**
* [获取详情信息]
* @param {[int]} id [资讯id]
* @return {[object]}
*/
const intro = (id) => {
let param = {
article_id: id,
client_type: 'h5'
};
return serviceAPI.get(`${URI_PACKAGE_ARTICLE}getArticleContent`, param, {
cache: true
});
};
module.exports = {
packageData,
intro
};