1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huying\Sms\RongLian; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
6
|
|
|
use Huying\Sms\AbstractProvider; |
7
|
|
|
use Huying\Sms\Message; |
8
|
|
|
use Huying\Sms\ProviderException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 容联短信平台接口实现 |
13
|
|
|
* |
14
|
|
|
* Class Provider |
15
|
|
|
*/ |
16
|
|
|
class Provider extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Rest URL |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $restUrl = 'https://app.cloopen.com:8883'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 接口版本 |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $softVersion = '2013-12-26'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 主账户 ID |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $accountSid; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 主账号授权令牌 |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $authToken; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 应用 ID |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $appId; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 当前时间戳 |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $timestamp; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 返回短信接口必须的参数 |
62
|
|
|
* @param $key |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
8 |
View Code Duplication |
protected function getRequiredOptions($key) |
|
|
|
|
66
|
|
|
{ |
67
|
8 |
|
if ($key == self::PROVIDER_OPTIONS) { |
68
|
|
|
return [ |
69
|
8 |
|
'accountSid', |
70
|
8 |
|
'authToken', |
71
|
8 |
|
'appId', |
72
|
8 |
|
]; |
73
|
4 |
|
} elseif ($key == self::MESSAGE_OPTIONS) { |
74
|
|
|
return [ |
75
|
4 |
|
'data', |
76
|
4 |
|
'recipients', |
77
|
4 |
|
'template_id', |
78
|
4 |
|
]; |
79
|
|
|
} else { |
80
|
|
|
return []; // @codeCoverageIgnore |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* 获取接收使用的时间戳 |
86
|
|
|
* |
87
|
|
|
* 每次调用返回值是相同的 |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
4 |
|
public function getTimestamp() |
92
|
|
|
{ |
93
|
4 |
|
if ($this->timestamp) { |
94
|
4 |
|
return $this->timestamp; |
95
|
|
|
} else { |
96
|
4 |
|
return $this->timestamp = date('YmdHis'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* 返回请求链接 |
102
|
|
|
* |
103
|
|
|
* @param Message $message |
104
|
|
|
* @return string |
105
|
|
|
* @throws \RuntimeException |
106
|
|
|
*/ |
107
|
4 |
|
protected function getUrl(Message $message) |
108
|
|
|
{ |
109
|
4 |
|
return $this->restUrl.'/'.$this->softVersion |
110
|
4 |
|
.'/Accounts/'.$this->accountSid |
111
|
4 |
|
.'/SMS/TemplateSMS?sig='.md5($this->accountSid.$this->authToken.$this->getTimestamp()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* 返回请求的方法 |
116
|
|
|
* |
117
|
|
|
* @return string HTTP 方法 |
118
|
|
|
*/ |
119
|
4 |
|
protected function getRequestMethod() |
120
|
|
|
{ |
121
|
4 |
|
return static::METHOD_POST; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* 返回请求短信接口时的 headers |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
4 |
|
protected function getRequestHeaders() |
130
|
|
|
{ |
131
|
|
|
return [ |
132
|
4 |
|
'Accept' => 'application/json;', |
133
|
4 |
|
'Content-Type' => 'application/json;charset=utf-8;', |
134
|
4 |
|
'Authorization' => base64_encode($this->accountSid.':'.$this->getTimestamp()), |
135
|
4 |
|
]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* 返回请求短信接口时的 payload |
140
|
|
|
* |
141
|
|
|
* @param Message $message |
142
|
|
|
* @return string |
143
|
|
|
* @throws \RuntimeException |
144
|
|
|
*/ |
145
|
4 |
|
protected function getRequestPayload(Message $message) |
146
|
|
|
{ |
147
|
4 |
|
$recipients = implode(',', $message->getRecipients()); |
148
|
4 |
|
$templateId = (string) $message->getTemplateId(); |
149
|
4 |
|
$data = $message->getData(); |
150
|
4 |
|
array_walk($data, function (&$item) { |
151
|
4 |
|
$item = (string) $item; |
152
|
4 |
|
}); |
153
|
4 |
|
$data = array_values($data); |
154
|
|
|
|
155
|
4 |
|
return json_encode([ |
156
|
4 |
|
'to' => $recipients, |
157
|
4 |
|
'appId' => $this->appId, |
158
|
4 |
|
'templateId' => $templateId, |
159
|
4 |
|
'datas' => $data, |
160
|
4 |
|
]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* 处理短信接口的返回结果 |
165
|
|
|
* |
166
|
|
|
* @param ResponseInterface|GuzzleException $response |
167
|
|
|
* @return array |
168
|
|
|
* @throws ProviderException |
169
|
|
|
* @throws GuzzleException |
170
|
|
|
*/ |
171
|
4 |
|
protected function handleResponse($response) |
172
|
|
|
{ |
173
|
4 |
|
if ($response instanceof GuzzleException) { |
174
|
|
|
throw $response; |
175
|
|
|
} |
176
|
4 |
|
$parsedResponse = self::parseJson($response->getBody()); |
177
|
4 |
|
if ($parsedResponse['statusCode'] != '000000') { |
178
|
2 |
|
throw new ProviderException($parsedResponse['statusMsg'], $parsedResponse['statusCode'], $parsedResponse); |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
return $parsedResponse; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* 获取短信供应商名称 |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
2 |
|
public function getName() |
190
|
|
|
{ |
191
|
2 |
|
return 'RongLian'; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.