|
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 YunxinGateway. |
|
22
|
|
|
* |
|
23
|
|
|
* @author her-cat <[email protected]> |
|
24
|
|
|
* |
|
25
|
|
|
* @see https://dev.yunxin.163.com/docs/product/%E7%9F%AD%E4%BF%A1/%E7%9F%AD%E4%BF%A1%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97 |
|
26
|
|
|
*/ |
|
27
|
|
|
class YunxinGateway extends Gateway |
|
28
|
|
|
{ |
|
29
|
|
|
use HasHttpRequest; |
|
30
|
|
|
|
|
31
|
|
|
const ENDPOINT_TEMPLATE = 'https://api.netease.im/%s/%s.action'; |
|
32
|
|
|
|
|
33
|
|
|
const ENDPOINT_ACTION = 'sendCode'; |
|
34
|
|
|
|
|
35
|
|
|
const SUCCESS_CODE = 200; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Send a short message. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to |
|
41
|
|
|
* @param \Overtrue\EasySms\Contracts\MessageInterface $message |
|
42
|
|
|
* @param \Overtrue\EasySms\Support\Config $config |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
|
|
|
|
|
45
|
|
|
* |
|
46
|
|
|
* @throws GatewayErrorException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) |
|
49
|
|
|
{ |
|
50
|
|
|
$data = $message->getData($this); |
|
51
|
|
|
|
|
52
|
|
|
$action = isset($data['action']) ? $data['action'] : self::ENDPOINT_ACTION; |
|
53
|
|
|
|
|
54
|
|
|
$endpoint = $this->buildEndpoint('sms', $action); |
|
55
|
|
|
|
|
56
|
|
|
switch ($action) { |
|
57
|
|
|
case 'sendCode': |
|
58
|
|
|
$params = $this->buildSendCodeParams($to, $message, $config); |
|
59
|
|
|
break; |
|
60
|
|
|
case 'verifyCode': |
|
61
|
|
|
$params = $this->buildVerifyCodeParams($to, $message); |
|
62
|
|
|
break; |
|
63
|
|
|
default: |
|
64
|
|
|
throw new GatewayErrorException(sprintf('action: %s not supported', $action), 0); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$headers = $this->buildHeaders($config); |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
|
|
$result = $this->post($endpoint, $params, $headers); |
|
71
|
|
|
|
|
72
|
|
|
if (!isset($result['code']) || self::SUCCESS_CODE !== $result['code']) { |
|
73
|
|
|
$code = isset($result['code']) ? $result['code'] : 0; |
|
74
|
|
|
$error = isset($result['msg']) ? $result['msg'] : json_encode($result, JSON_UNESCAPED_UNICODE); |
|
75
|
|
|
throw new GatewayErrorException($error, $code); |
|
76
|
|
|
} |
|
77
|
|
|
} catch (\Exception $e) { |
|
78
|
|
|
throw new GatewayErrorException($e->getMessage(), $e->getCode()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $resource |
|
86
|
|
|
* @param $function |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function buildEndpoint($resource, $function) |
|
91
|
|
|
{ |
|
92
|
|
|
return sprintf(self::ENDPOINT_TEMPLATE, $resource, strtolower($function)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the request headers. |
|
97
|
|
|
* |
|
98
|
|
|
* @param Config $config |
|
99
|
|
|
* |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function buildHeaders(Config $config) |
|
103
|
|
|
{ |
|
104
|
|
|
$headers = [ |
|
105
|
|
|
'AppKey' => $config->get('app_key'), |
|
106
|
|
|
'Nonce' => md5(uniqid('easysms')), |
|
107
|
|
|
'CurTime' => (string) time(), |
|
108
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8', |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
|
|
$headers['CheckSum'] = sha1("{$config->get('app_secret')}{$headers['Nonce']}{$headers['CurTime']}"); |
|
112
|
|
|
|
|
113
|
|
|
return $headers; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param PhoneNumberInterface $to |
|
118
|
|
|
* @param MessageInterface $message |
|
119
|
|
|
* @param Config $config |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
public function buildSendCodeParams(PhoneNumberInterface $to, MessageInterface $message, Config $config) |
|
124
|
|
|
{ |
|
125
|
|
|
$data = $message->getData($this); |
|
126
|
|
|
$template = $message->getTemplate($this); |
|
127
|
|
|
|
|
128
|
|
|
return [ |
|
129
|
|
|
'mobile' => $to->getUniversalNumber(), |
|
130
|
|
|
'authCode' => array_key_exists('code', $data) ? $data['code'] : '', |
|
131
|
|
|
'deviceId' => array_key_exists('device_id', $data) ? $data['device_id'] : '', |
|
132
|
|
|
'templateid' => is_string($template) ? $template : '', |
|
133
|
|
|
'codeLen' => $config->get('code_length', 4), |
|
134
|
|
|
'needUp' => $config->get('need_up', false), |
|
135
|
|
|
]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param PhoneNumberInterface $to |
|
140
|
|
|
* @param MessageInterface $message |
|
141
|
|
|
* |
|
142
|
|
|
* @return array |
|
143
|
|
|
* |
|
144
|
|
|
* @throws GatewayErrorException |
|
145
|
|
|
*/ |
|
146
|
|
|
public function buildVerifyCodeParams(PhoneNumberInterface $to, MessageInterface $message) |
|
147
|
|
|
{ |
|
148
|
|
|
$data = $message->getData($this); |
|
149
|
|
|
|
|
150
|
|
|
if (!array_key_exists('code', $data)) { |
|
151
|
|
|
throw new GatewayErrorException('"code" cannot be empty', 0); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return [ |
|
155
|
|
|
'mobile' => $to->getUniversalNumber(), |
|
156
|
|
|
'code' => $data['code'], |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.