1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class SendCloudAgent |
7
|
|
|
* |
8
|
|
|
* @property string $appId |
9
|
|
|
* @property string $appKey |
10
|
|
|
*/ |
11
|
|
|
class QcloudAgent extends Agent implements TemplateSms, ContentSms, VoiceCode, ContentVoice |
12
|
|
|
{ |
13
|
|
|
protected $sendSms = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms'; |
14
|
|
|
protected $sendVoiceCode = 'https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoice'; |
15
|
|
|
protected $sendVoicePrompt = 'https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoiceprompt'; |
16
|
|
|
protected $random; |
17
|
|
|
|
18
|
|
|
public function formatMobile(array $list) |
19
|
|
|
{ |
20
|
|
|
$list = array_map(function ($value) { |
21
|
|
|
return [ |
22
|
|
|
'nationcode' => $value['nation'], |
23
|
|
|
'mobile' => $value['number'], |
24
|
|
|
]; |
25
|
|
|
}, array_filter($list, function ($value) { |
26
|
|
|
return is_array($value); |
27
|
|
|
})); |
28
|
|
|
|
29
|
|
|
return count($list) === 1 ? array_pop($list) : array_values($list); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function sendContentSms($to, $content) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$params = [ |
35
|
|
|
'type' => 0, // 0:普通短信 1:营销短信 |
|
|
|
|
36
|
|
|
'msg' => $content, |
37
|
|
|
'tel' => $to, |
38
|
|
|
'time' => time(), |
39
|
|
|
]; |
40
|
|
|
$this->random = $this->getRandom(); |
41
|
|
|
$sendUrl = "{$this->sendSms}?sdkappid={$this->appId}&random={$this->random}"; |
42
|
|
|
$this->request($sendUrl, $params); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function sendTemplateSms($to, $tempId, array $data) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$params = [ |
48
|
|
|
'tel' => $to, |
49
|
|
|
'tpl_id' => $tempId, |
50
|
|
|
'params' => array_values($data), |
51
|
|
|
'time' => time(), |
52
|
|
|
]; |
53
|
|
|
$this->random = $this->getRandom(); |
54
|
|
|
$sendUrl = "{$this->sendSms}?sdkappid={$this->appId}&random={$this->random}"; |
55
|
|
|
$this->request($sendUrl, $params); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function sendVoiceCode($to, $code) |
59
|
|
|
{ |
60
|
|
|
$params = [ |
61
|
|
|
'tel' => $to, |
62
|
|
|
'msg' => $code, |
63
|
|
|
]; |
64
|
|
|
$sendUrl = "{$this->sendVoiceCode}?sdkappid={$this->appId}&random={$this->random}"; |
65
|
|
|
$this->request($sendUrl, $params); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function sendContentVoice($to, $content) |
69
|
|
|
{ |
70
|
|
|
$params = [ |
71
|
|
|
'tel' => $to, |
72
|
|
|
'prompttype' => 2, |
73
|
|
|
'promptfile' => $content, |
74
|
|
|
]; |
75
|
|
|
$sendUrl = "{$this->sendVoicePrompt}?sdkappid={$this->appId}&random={$this->random}"; |
76
|
|
|
$this->request($sendUrl, $params); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function request($sendUrl, array $params) |
80
|
|
|
{ |
81
|
|
|
$params['sig'] = $this->genSign($params); |
82
|
|
|
$params = $this->params($params); |
83
|
|
|
$result = $this->curlPost($sendUrl, [], [ |
84
|
|
|
CURLOPT_POSTFIELDS => json_encode($params), |
85
|
|
|
]); |
86
|
|
|
$this->setResult($result); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function genSign($params) |
90
|
|
|
{ |
91
|
|
|
$mobileStr = null; |
|
|
|
|
92
|
|
|
if (array_key_exists('mobile', $params['tel'])) { |
93
|
|
|
$mobileStr = $params['tel']['mobile']; |
94
|
|
|
} else { |
95
|
|
|
$mobileStr = implode(',', array_map(function ($value) { |
96
|
|
|
return $value['mobile']; |
97
|
|
|
}, $params['tel'])); |
98
|
|
|
} |
99
|
|
|
$signature = "appkey={$this->appKey}&random={$this->random}&time={$params['time']}&mobile={$mobileStr}"; |
100
|
|
|
|
101
|
|
|
return hash('sha256', $signature, false); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function setResult($result) |
105
|
|
|
{ |
106
|
|
|
if ($result['request']) { |
107
|
|
|
$this->result(Agent::INFO, $result['response']); |
108
|
|
|
$result = json_decode($result['response'], true); |
109
|
|
|
if (isset($result['result'])) { |
110
|
|
|
$this->result(Agent::SUCCESS, $result['result'] === 0); |
111
|
|
|
$this->result(Agent::CODE, $result['result']); |
112
|
|
|
} elseif (isset($result['ErrorCode'])) { |
113
|
|
|
$this->result(Agent::CODE, $result['ErrorCode']); |
114
|
|
|
$this->result(Agent::INFO, $result['ErrorInfo']); |
115
|
|
|
} |
116
|
|
|
} else { |
117
|
|
|
$this->result(Agent::INFO, 'request failed'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getRandom() |
122
|
|
|
{ |
123
|
|
|
return rand(100000, 999999); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.