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