Repository navigation
Expand file tree
/
Copy pathafdian.php
More file actions
executable file
·378 lines (357 loc) · 15 KB
/
Copy pathafdian.php
File metadata and controls
executable file
·378 lines (357 loc) · 15 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
<?php
include(__DIR__ . '/inc/HttpRequest.php');
include(__DIR__ . '/inc/HttpResponse.php');
Class Afdian {
private $userId, $token, $http;
private $apiRootUrl = 'https://afdian.net/api/open/%s';
/*
* Name: __construct
* Desc: 初始化函数,设定用户 ID 和 Token
* Return: <Null> 无返回值
* Params:
* String $userId 用户 ID
* String $token 用户 Token
*/
public function __construct($userId, $token)
{
$this->userId = $userId;
$this->token = $token;
$this->http = new HttpRequest();
}
/*
* Name: getSignature
* Desc: 计算 API 请求签名
* Return: <String> 计算出来的签名结果
* Params:
* String $params 请求参数的 JSON 文本
* Int $time 时间戳
*/
public function getSignature($params, $time)
{
return md5("{$this->token}params{$params}ts{$time}user_id{$this->userId}");
}
/*
* Name: queryServer
* Desc: 访问 API 服务器进行查询
* Return: <HttpResponse> 服务器返回的查询结果
* Params:
* String $api 需要请求的 API 终端名称
* Array $params 请求的参数内容
*/
public function queryServer($api, $params) {
if(!isset($api) || empty($api)) {
return new HttpResponse("Empty api endpoint", [], '');
}
$params = json_encode($params);
$queryData = json_encode([
'user_id' => $this->userId,
'params' => $params,
'ts' => time(),
'sign' => $this->getSignature($params, time())
]);
return $this->http->query(sprintf($this->apiRootUrl, $api), $queryData, false, ['Content-Type: application/json']);
}
/*
* Name: pingServer
* Desc: 测试签名和服务器连接是否正常
* Return: <Boolean> 检测结果
* Params: 无参数
*/
public function pingServer()
{
$result = $this->queryServer('ping', ['ping' => 'hello world']);
if($result->status == 200) {
$json = json_decode($result->data, true);
if($json && is_array($json)) {
return (isset($json['ec']) && $json['ec'] == 200);
}
return false;
}
return false;
}
/*
* Name: getOrders
* Desc: 查询订单列表
* Return: <Array|String> 如果成功,则返回订单列表数组,失败则返回错误消息
* Params:
* Int $page 指定要查询的页面,页面数量可以通过返回的 total_page 获得
*/
public function getOrders($page = 1)
{
$result = $this->queryServer('query-order', ['page' => $page]);
if($result->status == 200) {
$json = json_decode($result->data, true);
if($json && is_array($json)) {
return (isset($json['ec']) && $json['ec'] == 200) ? $json : $json['em'];
}
return "Cannot parse json string";
}
return $result->status;
}
/*
* Name: getSponsors
* Desc: 查询赞助者列表
* Return: <Array|String> 如果成功,则返回赞助者列表数组,失败则返回错误消息
* Params:
* Int $page 指定要查询的页面,页面数量可以通过返回的 total_page 获得
*/
public function getSponsors($page = 1)
{
$result = $this->queryServer('query-sponsor', ['page' => $page]);
if($result->status == 200) {
$json = json_decode($result->data, true);
if($json && is_array($json)) {
return (isset($json['ec']) && $json['ec'] == 200) ? $json : $json['em'];
}
return "Cannot parse json string";
}
return $result->status;
}
/*
* Name: getAllOrders
* Desc: 获取所有的订单,每一页都读取一次,因为需要循环查询,请尽量避免频繁调用
* Return: <Array|String> 如果成功,则返回订单列表数组,失败则返回错误消息,注:本方法不会返回 getOrders 的附加信息,例如 total_page 等
* Params:
* Int $cacheTime 查询结果的缓存失效时间,设置为 0 则表示不缓存,立即进行新的查询
* String $cacheFile 查询结果要缓存到的文件名,默认 order_cache.json,如果需要使用 Redis 进行缓存,可以设定 &redis=服务器地址:端口
*/
public function getAllOrders($cacheTime = 0, $cacheFile = 'order_cache.json')
{
// 如果缓存失效时间 > 0 则表示使用缓存
if($cacheTime > 0) {
// 判断开头是否是 &redis=
if(substr($cacheFile, 0, 7) == '&redis=') {
// 获取 Redis 服务器地址和端口
$server = explode(':', substr($cacheFile, 7));
if(count($server) >= 2) {
// 开始连接 Redis
$redis = new Redis();
$redis->connect($server[0], Intval($server[1]));
$result = $redis->get('afdian-orders-cache');
// 如果结果不为空,则返回 Redis 查询结果,否则跳出语句,继续往下执行查询
if($result) {
$result = json_decode($result, true);
$result['cache'] = 'cached';
return $result;
}
} else {
return "Failed to connect redis server: invalid server address";
}
} else {
// 判断文件是否存在,以及文件修改时间是否已超出缓存失效时间
if(file_exists($cacheFile) && time() - filemtime($cacheFile) < $cacheTime) {
$result = json_decode(file_get_contents($cacheFile), true);
$result['cache'] = 'cached';
return $result;
}
}
}
// 缓存失效或不存在,则开始查询 API
$orders = ["data" => ["list" => []]];
$result = $this->getOrders(1);
if(isset($result['data']['list'], $result['data']['total_page'])) {
foreach($result['data']['list'] as $order) {
$orders['data']['list'][] = $order;
}
for($i = 2;$i <= $result['data']['total_page'];$i++) {
$result = $this->getOrders($i);
if(isset($result['data']['list'])) {
foreach($result['data']['list'] as $order) {
$orders['data']['list'][] = $order;
}
}
}
}
// 判断是否需要缓存
if($cacheTime > 0) {
// 如果已经定义了 Redis 变量,则表示使用 Redis 作为缓存
if(isset($redis)) {
$redis->set('afdian-orders-cache', json_encode($orders));
$redis->expireAt('afdian-orders-cache', time() + $cacheTime);
} else {
file_put_contents($cacheFile, json_encode($orders));
}
}
$orders['cache'] = 'none';
return $orders;
}
/*
* Name: getAllSponsors
* Desc: 获取所有的赞助者,每一页都读取一次,因为需要循环查询,请尽量避免频繁调用
* Return: <Array|String> 如果成功,则返回赞助者列表数组,失败则返回错误消息,注:本方法不会返回 getSponsors 的附加信息,例如 total_page 等
* Params:
* Int $cacheTime 查询结果的缓存失效时间,设置为 0 则表示不缓存,立即进行新的查询
* String $cacheFile 查询结果要缓存到的文件名,默认 sponsor_cache.json,如果需要使用 Redis 进行缓存,可以设定 &redis=服务器地址:端口
*/
public function getAllSponsors($cacheTime = 0, $cacheFile = 'sponsor_cache.json')
{
// 如果缓存失效时间 > 0 则表示使用缓存
if($cacheTime > 0) {
// 判断开头是否是 &redis=
if(substr($cacheFile, 0, 7) == '&redis=') {
// 获取 Redis 服务器地址和端口
$server = explode(':', substr($cacheFile, 7));
if(count($server) >= 2) {
// 开始连接 Redis
$redis = new Redis();
$redis->connect($server[0], Intval($server[1]));
$result = $redis->get('afdian-sponsors-cache');
// 如果结果不为空,则返回 Redis 查询结果,否则跳出语句,继续往下执行查询
if($result) {
$result = json_decode($result, true);
$result['cache'] = 'cached';
return $result;
}
} else {
return "Failed to connect redis server: invalid server address";
}
} else {
// 判断文件是否存在,以及文件修改时间是否已超出缓存失效时间
if(file_exists($cacheFile) && time() - filemtime($cacheFile) < $cacheTime) {
$result = json_decode(file_get_contents($cacheFile), true);
$result['cache'] = 'cached';
return $result;
}
}
}
// 缓存失效或不存在,则开始查询 API
$sponsors = ["data" => ["list" => []]];
$result = $this->getSponsors(1);
if(isset($result['data']['list'], $result['data']['total_page'])) {
foreach($result['data']['list'] as $order) {
$sponsors['data']['list'][] = $order;
}
for($i = 2;$i <= $result['data']['total_page'];$i++) {
$result = $this->getSponsors($i);
if(isset($result['data']['list'])) {
foreach($result['data']['list'] as $order) {
$sponsors['data']['list'][] = $order;
}
}
}
}
// 判断是否需要缓存
if($cacheTime > 0) {
// 如果已经定义了 Redis 变量,则表示使用 Redis 作为缓存
if(isset($redis)) {
$redis->set('afdian-sponsors-cache', json_encode($sponsors));
$redis->expireAt('afdian-sponsors-cache', time() + $cacheTime);
} else {
file_put_contents($cacheFile, json_encode($sponsors));
}
}
$sponsors['cache'] = 'none';
return $sponsors;
}
/*
* Name: getOrderById
* Desc: 根据订单号获取订单信息
* Return: <Array|String> 如果成功,则返回订单信息的数组,失败则返回错误消息
* Params:
* Array $result 订单列表,可以通过 getOrders 或 getAllOrders 查询得到
* Int $orderId 要查询的订单号
*/
public function getOrderById($result, $orderId)
{
if(!isset($orderId, $result) || empty($result) || empty($orderId)) {
return "Empty result or order id";
}
if(isset($result['data']['list'])) {
foreach($result['data']['list'] as $order) {
if($order['out_trade_no'] == $orderId) {
return $order;
}
}
}
}
/*
* Name: getOrderByUserId
* Desc: 根据用户 ID 获取订单列表
* Return: <Array|String> 如果成功,则返回订单信息的数组,失败则返回错误消息
* Params:
* Array $result 订单列表,可以通过 getOrders 或 getAllOrders 查询得到
* Int $userId 要查询的用户 ID
*/
public function getOrderByUserId($result, $userId)
{
if(!isset($userId, $result) || empty($result) || empty($userId)) {
return "Empty result or user id";
}
$orders = [];
if(isset($result['data']['list'])) {
foreach($result['data']['list'] as $order) {
if($order['user_id'] == $userId) {
$orders[] = $order;
}
}
}
return $orders;
}
/*
* Name: getOrderByPlanId
* Desc: 根据赞助方案获取订单列表
* Return: <Array|String> 如果成功,则返回订单信息的数组,失败则返回错误消息
* Params:
* Array $result 订单列表,可以通过 getOrders 或 getAllOrders 查询得到
* Int $planId 要查询的赞助方案 ID
*/
public function getOrderByPlanId($result, $planId)
{
if(!isset($planId, $result) || empty($result) || empty($planId)) {
return "Empty result or plan id";
}
$orders = [];
if(isset($result['data']['list'])) {
foreach($result['data']['list'] as $order) {
if($order['plan_id'] == $planId) {
$orders[] = $order;
}
}
}
return $orders;
}
/*
* Name: getSponsorById
* Desc: 根据用户 ID 获取赞助者信息
* Return: <Array|String> 如果成功,则返回赞助者信息的数组,失败则返回错误消息
* Params:
* Array $result 赞助者列表,可以通过 getSponsors 或 getAllSponsors 查询得到
* Int $userId 要查询的用户 ID
*/
public function getSponsorById($result, $userId)
{
if(!isset($userId, $result) || empty($result) || empty($userId)) {
return "Empty result or plan id";
}
if(isset($result['data']['list'])) {
foreach($result['data']['list'] as $sponsor) {
if(isset($sponsor['user'], $sponsor['user']['user_id']) && $sponsor['user']['user_id'] == $userId) {
return $sponsor;
}
}
}
return "User id not found";
}
/*
* Name: getSponsorByName
* Desc: 根据用户名字获取赞助者信息
* Return: <Array|String> 如果成功,则返回赞助者信息的数组,失败则返回错误消息
* Params:
* Array $result 赞助者列表,可以通过 getSponsors 或 getAllSponsors 查询得到
* Int $userName 要查询的用户名字
*/
public function getSponsorByName($result, $userName)
{
if(!isset($userName, $result) || empty($result) || empty($userName)) {
return "Empty result or plan id";
}
if(isset($result['data']['list'])) {
foreach($result['data']['list'] as $sponsor) {
if(isset($sponsor['user'], $sponsor['user']['name']) && $sponsor['user']['name'] == $userName) {
return $sponsor;
}
}
}
return "User name not found";
}
}