1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tinymeng\Chinaums\Service\Common; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use tinymeng\Chinaums\Tools\Http; |
7
|
|
|
|
8
|
|
|
class Base |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array $config 网关 |
12
|
|
|
*/ |
13
|
|
|
protected $config = []; |
14
|
|
|
|
15
|
|
|
protected $gateway; |
16
|
|
|
protected $gateway_type = 'default'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string 接口地址 |
20
|
|
|
*/ |
21
|
|
|
protected $api; |
22
|
|
|
/** |
23
|
|
|
* @var array $body 请求参数 |
24
|
|
|
*/ |
25
|
|
|
protected $body; |
26
|
|
|
/** |
27
|
|
|
* 必传的值 |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $require = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param $config |
34
|
|
|
*/ |
35
|
|
|
public function __construct($config=[]) |
36
|
|
|
{ |
37
|
|
|
if(!empty($config)){ |
38
|
|
|
$this->config = $config; |
39
|
|
|
$this->loadConfigGateway(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 加载多网关 |
46
|
|
|
* @return Base |
47
|
|
|
*/ |
48
|
|
|
private function loadConfigGateway() |
49
|
|
|
{ |
50
|
|
|
$gateway = $this->config['gateway'];// 正式环境 |
51
|
|
|
if($this->config['sandbox'] === false){ |
52
|
|
|
$this->gateway = isset($gateway[$this->gateway_type]) ? $gateway[$this->gateway_type] : $gateway['default'];// 沙箱环境 |
53
|
|
|
}else{ |
54
|
|
|
$this->gateway = $gateway['sandbox'];// 沙箱环境 |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $data |
62
|
|
|
* @return false|mixed|string |
63
|
|
|
*/ |
64
|
|
|
public function request(array $data = []) |
65
|
|
|
{ |
66
|
|
|
$data['mid'] = $this->config['mid']; |
67
|
|
|
$data['tid'] = $this->config['tid']; |
68
|
|
|
if ($data) { |
|
|
|
|
69
|
|
|
$this->body = array_merge($this->body, $data); |
70
|
|
|
} |
71
|
|
|
try { |
72
|
|
|
$this->validate(); |
73
|
|
|
$data = $this->body; |
74
|
|
|
$sign = $this->generateSign($data); |
75
|
|
|
$gateway = $this->gateway . $this->api; |
76
|
|
|
$data = json_encode($data); |
77
|
|
|
if ('cli' == php_sapi_name()) { |
78
|
|
|
echo 'api:' . $gateway . PHP_EOL; |
79
|
|
|
echo 'request:' . $data . PHP_EOL; |
80
|
|
|
} |
81
|
|
|
$headers = [ |
82
|
|
|
'Content-Type: application/json', |
83
|
|
|
'Content-Length: ' . strlen($data), |
84
|
|
|
'Authorization: ' . $sign |
85
|
|
|
]; |
86
|
|
|
$headers = $headers; |
87
|
|
|
$options = [ |
88
|
|
|
CURLOPT_HTTPHEADER => $headers, |
89
|
|
|
CURLOPT_TIMEOUT => 60, |
90
|
|
|
CURLOPT_CONNECTTIMEOUT => 30 |
91
|
|
|
]; |
92
|
|
|
$response = Http::post($gateway, $data, $options); |
|
|
|
|
93
|
|
|
return $response; |
94
|
|
|
} catch (Exception $e) { |
95
|
|
|
return json_encode(['errCode' => -1, 'errMsg' => $e->getMessage(), 'responseTimestamp' => null]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
public function getH5Request() |
101
|
|
|
{ |
102
|
|
|
$data['mid'] = $this->config['mid']; |
|
|
|
|
103
|
|
|
$data['tid'] = $this->config['tid']; |
104
|
|
|
if ($data) { |
105
|
|
|
$this->body = array_merge($this->body, $data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->validate(); |
109
|
|
|
$data = $this->body; |
110
|
|
|
$sign = $this->generateSign($data); |
111
|
|
|
$gateway = $this->gateway . $this->api; |
112
|
|
|
$data = json_encode($data); |
113
|
|
|
if ('cli' == php_sapi_name()) { |
114
|
|
|
echo 'api:' . $gateway . PHP_EOL; |
115
|
|
|
echo 'request:' . $data . PHP_EOL; |
116
|
|
|
} |
117
|
|
|
$headers = [ |
118
|
|
|
'Content-Type: application/json', |
119
|
|
|
'Content-Length: ' . strlen($data), |
120
|
|
|
'Authorization: ' . $sign |
121
|
|
|
]; |
122
|
|
|
$headers = $headers; |
123
|
|
|
$options = [ |
124
|
|
|
CURLOPT_HTTPHEADER => $headers, |
125
|
|
|
CURLOPT_TIMEOUT => 60, |
126
|
|
|
CURLOPT_CONNECTTIMEOUT => 30 |
127
|
|
|
]; |
128
|
|
|
$response = Http::post($gateway, $data, $options); |
|
|
|
|
129
|
|
|
return $response; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $config |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function setConfig($config) |
137
|
|
|
{ |
138
|
|
|
$this->config = $config; |
139
|
|
|
$this->loadConfigGateway(); |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $value |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function setBody($value) |
148
|
|
|
{ |
149
|
|
|
$this->body = array_merge($this->body, $value); |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return true |
155
|
|
|
* @throws Exception |
156
|
|
|
*/ |
157
|
|
|
protected function validate() |
158
|
|
|
{ |
159
|
|
|
$require = $this->require; |
160
|
|
|
$key = array_keys($this->body); |
161
|
|
|
foreach ($require as $v) { |
162
|
|
|
if (!in_array($v, $key)) { |
163
|
|
|
throw new Exception($v . ' is require!!'); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
/** |
169
|
|
|
* 根绝类型生成sign |
170
|
|
|
* @param $params |
171
|
|
|
* @param string $signType |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function generateSign($body) |
175
|
|
|
{ |
176
|
|
|
$body = (!is_string($body)) ? json_encode($body) : $body; |
177
|
|
|
$appid = $this->config['appid']; |
178
|
|
|
$appkey = $this->config['appkey']; |
179
|
|
|
$timestamp = date("YmdHis", time()); |
180
|
|
|
$nonce = md5(uniqid(microtime(true), true)); |
181
|
|
|
$str = bin2hex(hash('sha256', $body, true)); |
182
|
|
|
$signature = base64_encode(hash_hmac('sha256', "$appid$timestamp$nonce$str", $appkey, true)); |
183
|
|
|
$authorization = "OPEN-BODY-SIG AppId=\"$appid\", Timestamp=\"$timestamp\", Nonce=\"$nonce\", Signature=\"$signature\""; |
184
|
|
|
return $authorization; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param $name |
189
|
|
|
* @param $value |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function __set($name, $value) |
193
|
|
|
{ |
194
|
|
|
$this->body[$name] = $value; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.