1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the mucts.com. |
4
|
|
|
* |
5
|
|
|
* This source file is subject to the MIT license that is bundled |
6
|
|
|
* with this source code in the file LICENSE. |
7
|
|
|
* |
8
|
|
|
* @version 1.0 |
9
|
|
|
* @author herry<[email protected]> |
10
|
|
|
* @copyright © 2020 MuCTS.com All Rights Reserved. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace MuCTS\Laravel\GuiJK; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use Exception; |
17
|
|
|
use Illuminate\Support\Arr; |
18
|
|
|
use MuCTS\Laravel\GuiJK\Config\Config; |
19
|
|
|
use MuCTS\Laravel\GuiJK\Exceptions\InvalidArgumentException; |
20
|
|
|
use MuCTS\Laravel\GuiJK\Exceptions\Exception as GjkException; |
21
|
|
|
use GuzzleHttp\Client; |
22
|
|
|
use GuzzleHttp\Exception\RequestException; |
23
|
|
|
|
24
|
|
|
class Gjk |
25
|
|
|
{ |
26
|
|
|
use Sign; |
27
|
|
|
|
28
|
|
|
/** @var Config */ |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
|
|
public function __construct(?array $config) |
32
|
|
|
{ |
33
|
|
|
$config = $config ?? config('gjk'); |
34
|
|
|
$this->setConfig(new Config($config)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Modify configuration information. |
39
|
|
|
* |
40
|
|
|
* @param Config $config |
41
|
|
|
* @return Gjk |
42
|
|
|
*/ |
43
|
|
|
public function setConfig(Config $config): self |
44
|
|
|
{ |
45
|
|
|
$rules = [ |
46
|
|
|
'url' => 'required|active_url', |
47
|
|
|
'app_id' => 'required|int', |
48
|
|
|
'secret_key' => 'required|string', |
49
|
|
|
'version' => 'required|int' |
50
|
|
|
]; |
51
|
|
|
$validator = validator($config->all(), [$rules]); |
52
|
|
|
if ($validator->fails()) { |
53
|
|
|
throw new InvalidArgumentException('Please set the correct configuration,error:' . $validator->errors()); |
54
|
|
|
} |
55
|
|
|
$this->config = $config; |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get GuiJk Server API request url |
61
|
|
|
* |
62
|
|
|
* @return string|null |
63
|
|
|
*/ |
64
|
|
|
protected function getUrl(): string |
65
|
|
|
{ |
66
|
|
|
return $this->config->get('url'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get secret key |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
protected function getSecretKey(): string |
75
|
|
|
{ |
76
|
|
|
return $this->config->get('secret_key'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* get basic params |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
* @throws Exception |
84
|
|
|
*/ |
85
|
|
|
protected function getBasicParams(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
"app_id" => $this->config->get('app_id'), |
89
|
|
|
"c_ver" => $this->config->get('version'), |
90
|
|
|
"nonce_str" => str_random(mt_rand(6, 32)) |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gjk Api Request |
96
|
|
|
* |
97
|
|
|
* @param string $route |
98
|
|
|
* @param array $param |
99
|
|
|
* @param int $timeOut |
100
|
|
|
* @return array|null |
101
|
|
|
* @throws Exception |
102
|
|
|
*/ |
103
|
|
|
public function request(string $route, array $param = [], $timeOut = 15): ?array |
104
|
|
|
{ |
105
|
|
|
$param = ['user_id' => Arr::pull($param, 'user_id', mt_rand(1000000, 3000000))] |
106
|
|
|
+ $this->getBasicParams() + $param; |
107
|
|
|
Arr::set($param, 'sign', $this->generateSign($param, $this->getSecretKey())); |
108
|
|
|
$client = new Client(["headers" => [], "timeout" => $timeOut]); |
109
|
|
|
$options = ['json' => $param]; |
110
|
|
|
try { |
111
|
|
|
$response = $client->request('POST', rtrim($this->getUrl(), '/') . '/' . ltrim($route, '/'), $options); |
112
|
|
|
} catch (RequestException $exception) { |
113
|
|
|
$response = $exception->getResponse(); |
114
|
|
|
} catch (Exception $exception) { |
115
|
|
|
throw new GjkException($exception->getMessage(), $exception->getCode(), $exception->getPrevious()); |
116
|
|
|
} |
117
|
|
|
$response = $response ? $response->getBody() : null; |
118
|
|
|
return $response ? json_decode($response, true) : null; |
119
|
|
|
} |
120
|
|
|
} |