1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class JuHeAgent |
7
|
|
|
* |
8
|
|
|
* @property string $key |
9
|
|
|
* @property string $times |
10
|
|
|
*/ |
11
|
|
|
class JuHeAgent extends Agent implements TemplateSms |
12
|
|
|
{ |
13
|
|
|
public function sendSms($to, $content, $tempId, array $data) |
14
|
|
|
{ |
15
|
|
|
$this->sendTemplateSms($to, $tempId, $data); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function sendTemplateSms($to, $tempId, array $data) |
19
|
|
|
{ |
20
|
|
|
$sendUrl = 'http://v.juhe.cn/sms/send'; |
21
|
|
|
$tplValue = ''; |
22
|
|
|
foreach ($data as $key => $value) { |
23
|
|
|
if (preg_match('/[#&=]/', $value)) { |
24
|
|
|
$value = urlencode($value); |
25
|
|
|
} |
26
|
|
|
$split = !$tplValue ? '' : '&'; |
27
|
|
|
$tplValue .= "$split#$key#=$value"; |
28
|
|
|
} |
29
|
|
|
$tplValue = !$tplValue ?: urlencode($tplValue); |
30
|
|
|
$smsConf = [ |
31
|
|
|
'key' => $this->key, |
32
|
|
|
'mobile' => $to, |
33
|
|
|
'tpl_id' => $tempId, |
34
|
|
|
'tpl_value' => $tplValue, |
35
|
|
|
'dtype' => 'json', |
36
|
|
|
]; |
37
|
|
|
$result = $this->curl($sendUrl, $smsConf); |
38
|
|
|
$this->setResult($result); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function voiceVerify($to, $code, $tempId, array $data) |
42
|
|
|
{ |
43
|
|
|
$url = 'http://op.juhe.cn/yuntongxun/voice'; |
44
|
|
|
$params = [ |
45
|
|
|
'valicode' => $code, |
46
|
|
|
'to' => $to, |
47
|
|
|
'playtimes' => $this->times ?: 3, |
48
|
|
|
'key' => $this->key, |
49
|
|
|
'dtype' => 'json', |
50
|
|
|
]; |
51
|
|
|
$result = $this->curl($url, $params); |
52
|
|
|
$this->setResult($result); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
protected function setResult($result) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if ($result['request']) { |
58
|
|
|
$this->result(Agent::INFO, $result['response']); |
59
|
|
|
$result = json_decode($result['response'], true); |
60
|
|
|
$this->result(Agent::SUCCESS, $result['error_code'] === 0); |
61
|
|
|
$this->result(Agent::CODE, $result['error_code']); |
62
|
|
|
} else { |
63
|
|
|
$this->result(Agent::INFO, 'request failed'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
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.