1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dongdavid\Notify\sender; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
6
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
7
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
8
|
|
|
use Dongdavid\Notify\Sender; |
9
|
|
|
|
10
|
|
|
class AliSms extends Sender |
11
|
|
|
{ |
12
|
|
|
public function send($data) |
13
|
|
|
{ |
14
|
|
|
AlibabaCloud::accessKeyClient($this->config['accessKeyId'], $this->config['accessKeySecret']) |
15
|
|
|
->regionId('cn-hangzhou') |
16
|
|
|
->asDefaultClient(); |
17
|
|
|
$query = [ |
18
|
|
|
'RegionId' => 'cn-hangzhou', |
19
|
|
|
'SignName' => $this->config['SignName'], |
20
|
|
|
'TemplateCode' => $this->config['template_code'], |
21
|
|
|
'PhoneNumbers' => $data['phone'], |
22
|
|
|
'TemplateParam' => json_encode($data['template_param'], JSON_UNESCAPED_UNICODE), |
23
|
|
|
]; |
24
|
|
|
try { |
25
|
|
|
$result = AlibabaCloud::rpc() |
26
|
|
|
->product('Dysmsapi') |
27
|
|
|
// ->scheme('https') // https | http |
28
|
|
|
->version('2017-05-25') |
29
|
|
|
->action('SendSms') |
30
|
|
|
->method('POST') |
31
|
|
|
->host('dysmsapi.aliyuncs.com') |
32
|
|
|
->options([ |
33
|
|
|
'query' => $query, |
34
|
|
|
]) |
35
|
|
|
->request(); |
36
|
|
|
|
37
|
|
|
return $result->toArray(); |
38
|
|
|
} catch (ClientException $e) { |
39
|
|
|
throw new \Exception('短信发送失败-client:'.$e->getErrorMessage()); |
40
|
|
|
} catch (ServerException $e) { |
41
|
|
|
throw new \Exception('短信发送失败-server:'.$e->getErrorMessage()); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function checkConfig() |
46
|
|
|
{ |
47
|
|
|
// TODO: Implement checkConfig() method. |
48
|
|
|
if (!isset($this->config['accessKeyId']) || !$this->config['accessKeyId']) { |
49
|
|
|
throw new \Exception('缺失accessKeyId'); |
50
|
|
|
} |
51
|
|
|
if (!isset($this->config['accessKeySecret']) || !$this->config['accessKeySecret']) { |
52
|
|
|
throw new \Exception('缺失accessKeySecret'); |
53
|
|
|
} |
54
|
|
|
if (!isset($this->config['SignName'])) { |
55
|
|
|
throw new \Exception('缺失短信签名'); |
56
|
|
|
} |
57
|
|
|
if (empty($this->config['template_code'])) { |
58
|
|
|
throw new \Exception('模版ID不能为空'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function checkMsgFormate($msg) |
63
|
|
|
{ |
64
|
|
|
// TODO: Implement checkMsgFormate() method. |
65
|
|
|
if (empty($msg['phone'])) { |
66
|
|
|
throw new \Exception('手机号码不能为空'); |
67
|
|
|
} |
68
|
|
|
if (!(isset($msg['checkPhone']) && false === $msg['checkPhone'])) { |
69
|
|
|
$phonePreg = '#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#'; |
70
|
|
|
if (!preg_match($phonePreg, $msg['phone'])) { |
71
|
|
|
throw new \Exception('手机号码格式不正确'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|