1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vicens\Alidayu; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleHttp; |
6
|
|
|
use function GuzzleHttp\Psr7\build_query; |
7
|
|
|
use Vicens\Alidayu\Request\AbstractRequest; |
8
|
|
|
use Vicens\Alidayu\Request\DoubleCall; |
9
|
|
|
use Vicens\Alidayu\Request\FlowCharge; |
10
|
|
|
use Vicens\Alidayu\Request\FlowChargeProvince; |
11
|
|
|
use Vicens\Alidayu\Request\FlowGrade; |
12
|
|
|
use Vicens\Alidayu\Request\FlowQuery; |
13
|
|
|
use Vicens\Alidayu\Request\SingleCall; |
14
|
|
|
use Vicens\Alidayu\Request\Sms; |
15
|
|
|
use Vicens\Alidayu\Request\SmsQuery; |
16
|
|
|
use Vicens\Alidayu\Request\TtsSingleCall; |
17
|
|
|
|
18
|
|
|
class Client |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* 正式环境http请求地址 |
23
|
|
|
*/ |
24
|
|
|
const HTTP_URL = 'http://gw.api.taobao.com/router/rest'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* 正式环境的https请求地址 |
28
|
|
|
*/ |
29
|
|
|
const HTTPS_URL = 'https://eco.taobao.com/router/rest'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 沙箱环境的http请求地址 |
33
|
|
|
*/ |
34
|
|
|
const SANDBOX_HTTP_URL = 'http://gw.api.tbsandbox.com/router/rest'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* 沙箱环境的https请求地址 |
38
|
|
|
*/ |
39
|
|
|
const SANDBOX_HTTPS_URL = 'https://gw.api.tbsandbox.com/router/rest'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* 是否是沙箱环境 |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
protected $sandbox = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* 是否使用https接口 |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $secure = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 阿里大于App Key |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $appKey = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* 阿里大于App Secret |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $appSecret = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Alidayu constructor. |
67
|
|
|
* @param array $config 阿里大于配置 |
68
|
|
|
*/ |
69
|
|
|
public function __construct(array $config = []) |
70
|
|
|
{ |
71
|
|
|
if (!empty($config)) { |
72
|
|
|
$this->setConfig($config); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 设置配置 |
79
|
|
|
* @param array $config |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setConfig(array $config = []) |
83
|
|
|
{ |
84
|
|
|
// 设置App Key |
85
|
|
|
$this->appKey = isset($config['appKey']) ? $config['appKey'] : ''; |
86
|
|
|
|
87
|
|
|
// 设置App Secret |
88
|
|
|
$this->appSecret = isset($config['appSecret']) ? $config['appSecret'] : ''; |
89
|
|
|
|
90
|
|
|
// 沙箱环境 |
91
|
|
|
$this->sandbox = isset($config['sandbox']) ? (bool)$config['sandbox'] : false; |
92
|
|
|
|
93
|
|
|
// 使用HTTPS |
94
|
|
|
$this->secure = isset($config['secure']) ? (bool)$config['secure'] : false; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* 发送请求 |
101
|
|
|
* @param AbstractRequest $request |
102
|
|
|
* @return Response |
103
|
|
|
*/ |
104
|
|
|
public function send(AbstractRequest $request) |
105
|
|
|
{ |
106
|
|
|
$params = array_merge($request->getParams(), [ |
107
|
|
|
'app_key' => $this->appKey, |
108
|
|
|
'sign_method' => 'md5', |
109
|
|
|
'timestamp' => date('Y-m-d H:i:s'), |
110
|
|
|
'format' => 'json', |
111
|
|
|
'v' => '2.0', |
112
|
|
|
'method' => $request->getMethod() |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
// 生成签名 |
116
|
|
|
$params['sign'] = $this->buildSignature($params, $this->appSecret); |
117
|
|
|
|
118
|
|
|
$client = new GuzzleHttp(); |
119
|
|
|
|
120
|
|
|
// 发起请求 |
121
|
|
|
$response = $client->get($this->getUrl() . '?' . build_query($params)); |
122
|
|
|
|
123
|
|
|
return new Response($response, $request); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* 返回发送短信通知 |
128
|
|
|
* @param string $recNum 短信接收号码 |
129
|
|
|
* @param string $smsTemplateCode 模板ID |
130
|
|
|
* @param string $smsFreeSignName 短信签名 |
131
|
|
|
* @param array $smsParam 短信模板变量参数 |
132
|
|
|
* @param string $extend 回传参数 |
133
|
|
|
* @return Sms |
134
|
|
|
*/ |
135
|
|
|
public function sms($recNum, $smsTemplateCode, $smsFreeSignName, array $smsParam = [], $extend = '') |
136
|
|
|
{ |
137
|
|
|
return new Sms($recNum, $smsTemplateCode, $smsFreeSignName, $smsParam, $extend); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* 查询短信发送记录 |
142
|
|
|
* @param string $recNum 短信接收号码 |
143
|
|
|
* @param int $currentPage 当前页码 |
144
|
|
|
* @param int $pageSize 每页多少条数据 |
145
|
|
|
* @param string $queryDate 查询时间 |
146
|
|
|
* @param string $bizId 短信发送流水 |
147
|
|
|
* @return SmsQuery |
148
|
|
|
*/ |
149
|
|
|
public function smsQuery($recNum, $currentPage = 1, $pageSize = 10, $queryDate = null, $bizId = '') |
150
|
|
|
{ |
151
|
|
|
return new SmsQuery($recNum, $currentPage, $pageSize, $queryDate, $bizId); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* 语音通知 |
156
|
|
|
* @param string $calledNum 被叫号码 |
157
|
|
|
* @param string $calledShowNum 被叫号码侧的号码显示 |
158
|
|
|
* @param string $voiceCode 语音文件ID |
159
|
|
|
* @param string $extend 回传参数 |
160
|
|
|
* @return SingleCall |
161
|
|
|
*/ |
162
|
|
|
public function singleCall($calledNum, $calledShowNum, $voiceCode, $extend = '') |
163
|
|
|
{ |
164
|
|
|
return new SingleCall($calledNum, $calledShowNum, $voiceCode, $extend); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* 多方通话 |
169
|
|
|
* @param string $callerNum 主叫号码 |
170
|
|
|
* @param string $callerShowNum 主叫号码侧的号码显示 |
171
|
|
|
* @param string $calledNum 被叫号码 |
172
|
|
|
* @param string $calledShowNum 被叫号码侧的号码显示 |
173
|
|
|
* @param string $extend 回传参数 |
174
|
|
|
* @return DoubleCall |
175
|
|
|
*/ |
176
|
|
|
public function doubleCall($callerNum, $callerShowNum, $calledNum, $calledShowNum, $extend = '') |
177
|
|
|
{ |
178
|
|
|
return new DoubleCall($callerNum, $callerShowNum, $calledNum, $calledShowNum, $extend); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* 文本转语音通知 |
183
|
|
|
* @param string $calledNum 被叫号码 |
184
|
|
|
* @param string $calledShowNum 被叫号码侧的号码显示 |
185
|
|
|
* @param string $ttsCode 模板ID |
186
|
|
|
* @param array $ttsParams 模板变量 |
187
|
|
|
* @param string $extend 回传参数 |
188
|
|
|
* @return TtsSingleCall |
189
|
|
|
*/ |
190
|
|
|
public function ttsSingleCall($calledNum, $calledShowNum, $ttsCode, array $ttsParams = [], $extend = '') |
191
|
|
|
{ |
192
|
|
|
return new TtsSingleCall($calledNum, $calledShowNum, $ttsCode, $ttsParams, $extend); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* 流量直充查询 |
197
|
|
|
* @param string|null $outId 唯一流水号 |
198
|
|
|
* @return FlowQuery |
199
|
|
|
*/ |
200
|
|
|
public function flowQuery($outId = null) |
201
|
|
|
{ |
202
|
|
|
return new FlowQuery($outId); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* 流量直充档位表 |
207
|
|
|
* @param string|null $phoneNum 手机号 |
208
|
|
|
* @return FlowGrade |
209
|
|
|
*/ |
210
|
|
|
public function flowGrade($phoneNum = null) |
211
|
|
|
{ |
212
|
|
|
return new FlowGrade($phoneNum); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* 流量直充 |
217
|
|
|
* @param string $phoneNum 手机号 |
218
|
|
|
* @param int $grade 充值数量 |
219
|
|
|
* @param string $outRechargeId 唯一流水号 |
220
|
|
|
* @param int $scope 0:全国漫游流量 1:省内流量 |
221
|
|
|
* @param bool $isProvince 是否分省通道(scope为0时生效) |
222
|
|
|
* @param string $reason 充值原因 |
223
|
|
|
* @return FlowCharge |
224
|
|
|
*/ |
225
|
|
|
public function flowCharge($phoneNum, $grade, $outRechargeId, $scope = 0, $isProvince = false, $reason = '') |
226
|
|
|
{ |
227
|
|
|
return new FlowCharge($phoneNum, $grade, $outRechargeId, $scope, $isProvince, $reason); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* 流量直充分省 |
232
|
|
|
* @param string $phoneNum 手机号 |
233
|
|
|
* @param int $grade 充值数量 |
234
|
|
|
* @param string $outRechargeId 唯一流水号 |
235
|
|
|
* @param string $reason 充值原因 |
236
|
|
|
* @return FlowChargeProvince |
237
|
|
|
*/ |
238
|
|
|
public function flowChargeProvince($phoneNum, $grade, $outRechargeId, $reason = '') |
239
|
|
|
{ |
240
|
|
|
return new FlowChargeProvince($phoneNum, $grade, $outRechargeId, $reason); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* 返回接口请求地址 |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
protected function getUrl() |
248
|
|
|
{ |
249
|
|
|
|
250
|
|
|
if (!($this->secure && $this->sandbox)) { |
251
|
|
|
return static::HTTP_URL; |
252
|
|
|
} else if (!$this->secure && $this->sandbox) { |
253
|
|
|
return static::SANDBOX_HTTP_URL; |
254
|
|
|
} else if ($this->secure && $this->sandbox) { |
255
|
|
|
return static::SANDBOX_HTTPS_URL; |
256
|
|
|
} else { |
257
|
|
|
return static::HTTPS_URL; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* 生成签名 |
263
|
|
|
* @param array $parameters |
264
|
|
|
* @param $appSecret |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
static private function buildSignature(array $parameters, $appSecret) |
268
|
|
|
{ |
269
|
|
|
// 将参数Key按字典顺序排序 |
270
|
|
|
ksort($parameters); |
271
|
|
|
|
272
|
|
|
$paramArray = []; |
273
|
|
|
foreach ($parameters as $key => $value) { |
274
|
|
|
$paramArray[] = $key . $value; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$string = $appSecret . implode('', $paramArray) . $appSecret; |
278
|
|
|
|
279
|
|
|
return strtoupper(md5($string)); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|