DetailData.php
11.7 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
<?php
namespace LibModels\Wap\Guang;
use Api\Yohobuy;
use Configs\CacheConfig;
use Plugin\Cache;
use Api\Sign;
/**
* 逛资讯详情相关的数据模型
*
* @name DetailData
* @package LibModels/Wap/Guang
* @copyright yoho.inc
* @version 1.0 (2015-10-10 14:49:15)
* @author fei.hong <fei.hong@yoho.cn>
*/
class DetailData
{
const URI_PACKAGE_ARTICLE = 'guang/service/v2/article/';
const URI_PACKAGE_AUTHOR = 'guang/service/v1/author/';
/**
* 逛资讯详情页数据封装
*
* @param int $id 内容ID
* @param bool $isApp 标识是否是APP访问
* @return array
*/
public static function package($id, $isApp = false)
{
$result = array();
$result['getAuthor'] = array();
$result['getArticle'] = array();
$result['getArticleContent'] = array();
$result['getBrand'] = array();
$result['getOtherArticle'] = array();
// 客户端类型
$clientType = $isApp ? 'iphone' : 'h5';
$key = CacheConfig::KEY_ACTION_GUANG_DETAIL_DATA . strval($id) . $clientType;
if (USE_CACHE) {
// 先尝试获取一级缓存(master), 有数据则直接返回.
$cached = Cache::get($key, 'master');
if (!empty($cached)) {
// $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false, 1000);
// 获取资讯
$param = Yohobuy::param();
$param['article_id'] = $id;
$param['client_type'] = $clientType;
$param['private_key'] = Yohobuy::$privateKeyList[$clientType];
$param['client_secret'] = Sign::getSign($param);
$result1 = Yohobuy::get(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE . 'getArticle', $param);
if (isset($result1['code']) && $result1['code'] !== 200) {
$result['code'] = 400;
return $result;
}
$result = $result1;
$article = $result['data'];
if (isset($article['pageViews'])) {
$cached['getArticle']['pageViews'] = $article['pageViews'];
}
return $cached;
}
}
// 获取资讯
// $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false, 1500);
// ksort($article);
// file_put_contents('../../../../../../Desktop/app/o/getArticle.txt', var_export($article, true));
// 获取资讯
$param = Yohobuy::param();
$param['article_id'] = $id;
$param['client_type'] = $clientType;
$param['private_key'] = Yohobuy::$privateKeyList[$clientType];
$param['client_secret'] = Sign::getSign($param);
$result1 = Yohobuy::get(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE . 'getArticle', $param);
//接口要判断一下返回状态是否成功,OK,I 服了 YOU
if (isset($result1['code']) && $result1['code'] !== 200) {
$result['code'] = 400;
return $result;
}
$result = $result1;
$article = $result['data'];
// file_put_contents('../../../../../../Desktop/app/n/getArticle3.txt', var_export($article, true));
if (!isset($article['author_id'])) {
// 再尝试获取二级缓存(slave), 有数据则直接返回.
$cached = Cache::get($key, 'slave');
if (!empty($cached)) {
return $cached;
}
return $result;
}
$result['getArticle'] = $article;
// 获取作者信息
$param = Yohobuy::param();
$param['author_id'] = $article['author_id'];
$param['client_secret'] = Sign::getSign($param);
$urlList['getAuthor'] = Yohobuy::httpBuildQuery(Yohobuy::SERVICE_URL . self::URI_PACKAGE_AUTHOR . 'getAuthor', $param);
// 获取资讯内容
$param = Yohobuy::param();
$param['article_id'] = $id;
$param['client_type'] = $clientType;
$param['private_key'] = Yohobuy::$privateKeyList[$clientType];
$param['client_secret'] = Sign::getSign($param);
$urlList['getArticleContent'] = Yohobuy::httpBuildQuery(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE . 'getArticleContent', $param);
// 获取资讯相关的品牌
$param = Yohobuy::param();
$param['article_id'] = $id;
$param['client_type'] = $clientType;
$param['private_key'] = Yohobuy::$privateKeyList[$clientType];
$param['client_secret'] = Sign::getSign($param);
$urlList['getBrand'] = Yohobuy::httpBuildQuery(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE . 'getBrand', $param);
// 获取资讯相关的其它资讯
if (isset($article['tag'])) {
$param = Yohobuy::param();
$param['article_id'] = $id;
$param['client_type'] = $clientType;
$param['tags'] = $article['tag'];
$param['offset'] = 0;
$param['limit'] = 3;
$param['private_key'] = Yohobuy::$privateKeyList[$clientType];
$param['client_secret'] = Sign::getSign($param);
$urlList['getOtherArticle'] = Yohobuy::httpBuildQuery(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE . 'getOtherArticle', $param);
}
$multiResult = Yohobuy::getMulti($urlList, array());
$result['getAuthor'] = $multiResult['getAuthor'];
$result['getArticleContent'] = $multiResult['getArticleContent'];
$result['getBrand'] = $multiResult['getBrand'];
$result['getOtherArticle'] = $multiResult['getOtherArticle'];
// var_dump($urlList);
// file_put_contents('../../../../../../Desktop/app/n/getAuthor.txt', var_export($result['getAuthor'], true));
// file_put_contents('../../../../../../Desktop/app/n/getArticleContent.txt', var_export($result['getArticleContent'], true));
// file_put_contents('../../../../../../Desktop/app/n/getBrand.txt', var_export($result['getBrand'], true));
// file_put_contents('../../../../../../Desktop/app/n/getOtherArticle.txt', var_export($result['getOtherArticle'], true));
/*
// 获取作者信息
Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_AUTHOR, 'getAuthor', array($article['author_id'], $clientType), function ($retval) use (&$result) {
$result['getAuthor'] = empty($retval) ? array() : $retval;
ksort($retval);
file_put_contents('../../../../../../Desktop/app/o/getAuthor.txt', var_export($retval, true));
});
// 获取资讯内容
Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticleContent', array($id, $clientType), function ($retval) use (&$result) {
$result['getArticleContent'] = empty($retval) ? array() : $retval;
ksort($retval);
file_put_contents('../../../../../../Desktop/app/o/getArticleContent.txt', var_export($retval, true));
});
// 获取资讯相关的品牌
Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getBrand', array($id, $clientType), function ($retval) use (&$result) {
$result['getBrand'] = empty($retval) ? array() : $retval;
ksort($retval);
file_put_contents('../../../../../../Desktop/app/o/getBrand.txt', var_export($retval, true));
});
// 获取资讯相关的其它资讯
if (isset($article['tag'])) {
Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getOtherArticle', array($article['tag'], $id, 0, 3, $clientType), function ($retval) use (&$result) {
$result['getOtherArticle'] = empty($retval) ? array() : $retval;
ksort($retval);
file_put_contents('../../../../../../Desktop/app/o/getOtherArticle.txt', var_export($retval, true));
});
}
// 调用发起请求
Yohobuy::yarConcurrentLoop();
*/
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
elseif (!empty($result['getArticle']) && !empty($result['getArticleContent'])) {
Cache::set($key, $result);
}
}
return $result;
}
/**
* 逛资讯详情页数据封装 (专为YOHO!潮流志APP提供)
*
* @param int $id 内容ID
* @param bool $isApp 标识是否是APP访问
* @return array
*/
public static function packageFoEzine($id, $isApp = false)
{
$result = array();
$result['getAuthor'] = array();
$result['getArticle'] = array();
$result['getArticleContent'] = array();
$result['getBrand'] = array();
$result['getOtherArticle'] = array();
// 客户端类型
$clientType = $isApp ? 'iphone' : 'h5';
// 获取资讯
$article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType));
if (!isset($article['author_id'])) {
return $result;
}
$result['getArticle'] = $article;
// 获取资讯内容
Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticleContent', array($id, $clientType), function ($retval) use (&$result) {
$result['getArticleContent'] = empty($retval) ? array() : $retval;
});
// 获取资讯相关的品牌
Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getBrand', array($id, $clientType), function ($retval) use (&$result) {
$result['getBrand'] = empty($retval) ? array() : $retval;
});
// 调用发起请求
Yohobuy::yarConcurrentLoop();
return $result;
}
/**
* 逛资讯数据封装 (专为YOHO!男女生资讯站提供)
*
* @param int $id 内容ID
* @param bool $isApp 标识是否是APP访问
* @return array
*/
public static function packageForYoho($id, $isApp = false)
{
$result = array();
// 客户端类型
$clientType = $isApp ? 'iphone' : 'h5';
// 获取资讯内容
Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticleContent', array($id, $clientType), function ($retval) use (&$result) {
$result['getArticleContent'] = empty($retval) ? array() : $retval;
});
// 获取资讯相关的品牌
Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getBrand', array($id, $clientType), function ($retval) use (&$result) {
$result['getBrand'] = empty($retval) ? array() : $retval;
});
// 调用发起请求
Yohobuy::yarConcurrentLoop();
return $result;
}
/**
* 获取详情信息
*/
public static function intro($id)
{
$param = Yohobuy::param();
$param['article_id'] = $id;
$param['client_type'] = 'h5';
$param['private_key'] = Yohobuy::$privateKeyList['h5'];
$param['client_secret'] = Sign::getSign($param);
$result = Yohobuy::get(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE . 'getArticleContent', $param);
return $result['data'];
// return Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticleContent', array($id, 'h5'));
}
}