|
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\Exceptions\InvalidArgumentException; |
|
18
|
|
|
use Overtrue\EasySms\Support\Config; |
|
19
|
|
|
use Overtrue\EasySms\Traits\HasHttpRequest; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class ChuanglanGateway. |
|
23
|
|
|
* |
|
24
|
|
|
* @see https://zz.253.com/v5.html#/api_doc |
|
25
|
|
|
*/ |
|
26
|
|
|
class ChuanglanGateway extends Gateway |
|
27
|
|
|
{ |
|
28
|
|
|
use HasHttpRequest; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* URL模板 |
|
32
|
|
|
*/ |
|
33
|
|
|
const ENDPOINT_URL_TEMPLATE = 'https://%s.253.com/msg/send/json'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 国际短信 |
|
37
|
|
|
*/ |
|
38
|
|
|
const INT_URL = 'http://intapi.253.com/send/json'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 验证码渠道code. |
|
42
|
|
|
*/ |
|
43
|
|
|
const CHANNEL_VALIDATE_CODE = 'smsbj1'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* 会员营销渠道code. |
|
47
|
|
|
*/ |
|
48
|
|
|
const CHANNEL_PROMOTION_CODE = 'smssh1'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param PhoneNumberInterface $to |
|
52
|
|
|
* @param MessageInterface $message |
|
53
|
|
|
* @param Config $config |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
|
|
|
|
|
56
|
|
|
* |
|
57
|
|
|
* @throws GatewayErrorException |
|
58
|
|
|
* @throws InvalidArgumentException |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) |
|
61
|
|
|
{ |
|
62
|
2 |
|
$IDDCode = !empty($to->getIDDCode()) ? $to->getIDDCode() : 86; |
|
63
|
|
|
|
|
64
|
|
|
$params = [ |
|
65
|
2 |
|
'account' => $config->get('account'), |
|
66
|
2 |
|
'password' => $config->get('password'), |
|
67
|
2 |
|
'phone' => $to->getNumber(), |
|
68
|
2 |
|
'msg' => $this->wrapChannelContent($message->getContent($this), $config, $IDDCode), |
|
69
|
2 |
|
]; |
|
70
|
|
|
|
|
71
|
2 |
|
if (86 != $IDDCode) { |
|
72
|
|
|
$params['mobile'] = $to->getIDDCode().$to->getNumber(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
$result = $this->postJson($this->buildEndpoint($config, $IDDCode), $params); |
|
76
|
|
|
|
|
77
|
2 |
|
if (!isset($result['code']) || '0' != $result['code']) { |
|
78
|
2 |
|
throw new GatewayErrorException(json_encode($result, JSON_UNESCAPED_UNICODE), isset($result['code']) ? $result['code'] : 0, $result); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Config $config |
|
86
|
|
|
* @param int $IDDCode |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
* |
|
90
|
|
|
* @throws InvalidArgumentException |
|
91
|
|
|
*/ |
|
92
|
3 |
|
protected function buildEndpoint(Config $config, $IDDCode = 86) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
3 |
|
$channel = $this->getChannel($config, $IDDCode); |
|
95
|
|
|
|
|
96
|
3 |
|
return sprintf(self::ENDPOINT_URL_TEMPLATE, $channel); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Config $config |
|
101
|
|
|
* @param int $IDDCode |
|
102
|
|
|
* |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
* |
|
105
|
|
|
* @throws InvalidArgumentException |
|
106
|
|
|
*/ |
|
107
|
9 |
|
protected function getChannel(Config $config, $IDDCode) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
9 |
|
if (86 != $IDDCode) { |
|
110
|
|
|
return self::INT_URL; |
|
111
|
|
|
} |
|
112
|
9 |
|
$channel = $config->get('channel', self::CHANNEL_VALIDATE_CODE); |
|
113
|
|
|
|
|
114
|
9 |
|
if (!in_array($channel, [self::CHANNEL_VALIDATE_CODE, self::CHANNEL_PROMOTION_CODE])) { |
|
115
|
1 |
|
throw new InvalidArgumentException('Invalid channel for ChuanglanGateway.'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
return $channel; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $content |
|
123
|
|
|
* @param Config $config |
|
124
|
|
|
* @param int $IDDCode |
|
125
|
|
|
* |
|
126
|
|
|
* @return string|string |
|
127
|
|
|
* |
|
128
|
|
|
* @throws InvalidArgumentException |
|
129
|
|
|
*/ |
|
130
|
6 |
|
protected function wrapChannelContent($content, Config $config, $IDDCode) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
6 |
|
$channel = $this->getChannel($config, $IDDCode); |
|
133
|
|
|
|
|
134
|
6 |
|
if (self::CHANNEL_PROMOTION_CODE == $channel) { |
|
135
|
4 |
|
$sign = (string) $config->get('sign', ''); |
|
136
|
4 |
|
if (empty($sign)) { |
|
137
|
1 |
|
throw new InvalidArgumentException('Invalid sign for ChuanglanGateway when using promotion channel'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
3 |
|
$unsubscribe = (string) $config->get('unsubscribe', ''); |
|
141
|
3 |
|
if (empty($unsubscribe)) { |
|
142
|
1 |
|
throw new InvalidArgumentException('Invalid unsubscribe for ChuanglanGateway when using promotion channel'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
$content = $sign.$content.$unsubscribe; |
|
146
|
2 |
|
} |
|
147
|
|
|
|
|
148
|
4 |
|
return $content; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
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.