1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/easy-sms. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overtrue\EasySms\Gateways; |
13
|
|
|
|
14
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
15
|
|
|
use Overtrue\EasySms\Contracts\PhoneNumberInterface; |
16
|
|
|
use Overtrue\EasySms\Exceptions\GatewayErrorException; |
17
|
|
|
use Overtrue\EasySms\Support\Config; |
18
|
|
|
use Overtrue\EasySms\Traits\HasHttpRequest; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class YunzhixunGateway. |
22
|
|
|
* |
23
|
|
|
* @author her-cat <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @see http://docs.ucpaas.com/doku.php?id=%E7%9F%AD%E4%BF%A1:sendsms |
26
|
|
|
*/ |
27
|
|
|
class YunzhixunGateway extends Gateway |
28
|
|
|
{ |
29
|
|
|
use HasHttpRequest; |
30
|
|
|
|
31
|
|
|
const SUCCESS_CODE = '000000'; |
32
|
|
|
|
33
|
|
|
const FUNCTION_SEND_SMS = 'sendsms'; |
34
|
|
|
|
35
|
|
|
const FUNCTION_BATCH_SEND_SMS = 'sendsms_batch'; |
36
|
|
|
|
37
|
|
|
const ENDPOINT_TEMPLATE = 'https://open.ucpaas.com/ol/%s/%s'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Send a short message. |
41
|
|
|
* |
42
|
|
|
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to |
43
|
|
|
* @param \Overtrue\EasySms\Contracts\MessageInterface $message |
44
|
|
|
* @param \Overtrue\EasySms\Support\Config $config |
45
|
|
|
* |
46
|
|
|
* @return array |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @throws GatewayErrorException |
49
|
|
|
*/ |
50
|
|
|
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) |
51
|
|
|
{ |
52
|
|
|
$data = $message->getData($this); |
53
|
|
|
|
54
|
|
|
$function = isset($data['mobiles']) ? self::FUNCTION_BATCH_SEND_SMS : self::FUNCTION_SEND_SMS; |
55
|
|
|
|
56
|
|
|
$endpoint = $this->buildEndpoint('sms', $function); |
57
|
|
|
|
58
|
|
|
$params = $this->buildParams($to, $message, $config); |
59
|
|
|
|
60
|
|
|
return $this->execute($endpoint, $params); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $resource |
65
|
|
|
* @param $function |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
protected function buildEndpoint($resource, $function) |
70
|
|
|
{ |
71
|
|
|
return sprintf(self::ENDPOINT_TEMPLATE, $resource, $function); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param PhoneNumberInterface $to |
76
|
|
|
* @param MessageInterface $message |
77
|
|
|
* @param Config $config |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
protected function buildParams(PhoneNumberInterface $to, MessageInterface $message, Config $config) |
82
|
|
|
{ |
83
|
|
|
$data = $message->getData($this); |
84
|
|
|
|
85
|
|
|
return [ |
86
|
|
|
'sid' => $config->get('sid'), |
87
|
|
|
'token' => $config->get('token'), |
88
|
|
|
'appid' => $config->get('app_id'), |
89
|
|
|
'templateid' => $message->getTemplate($this), |
90
|
|
|
'uid' => isset($data['uid']) ? $data['uid'] : '', |
91
|
|
|
'param' => isset($data['params']) ? $data['params'] : '', |
92
|
|
|
'mobile' => isset($data['mobiles']) ? $data['mobiles'] : $to->getNumber(), |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $endpoint |
98
|
|
|
* @param $params |
99
|
|
|
* |
100
|
|
|
* @return array |
|
|
|
|
101
|
|
|
* |
102
|
|
|
* @throws GatewayErrorException |
103
|
|
|
*/ |
104
|
|
|
protected function execute($endpoint, $params) |
105
|
|
|
{ |
106
|
|
|
try { |
107
|
|
|
$result = $this->postJson($endpoint, $params); |
108
|
|
|
|
109
|
|
View Code Duplication |
if (!isset($result['code']) || self::SUCCESS_CODE !== $result['code']) { |
|
|
|
|
110
|
|
|
$code = isset($result['code']) ? $result['code'] : 0; |
111
|
|
|
$error = isset($result['msg']) ? $result['msg'] : json_encode($result, JSON_UNESCAPED_UNICODE); |
112
|
|
|
|
113
|
|
|
throw new GatewayErrorException($error, $code); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $result; |
117
|
|
|
} catch (\Exception $e) { |
118
|
|
|
throw new GatewayErrorException($e->getMessage(), $e->getCode()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.