SmsMessage.php
4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace domain\system;
use Yii;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use common\helpers\Log as AppLog;
/**
* 基于阿里大鱼的短信通知
* Class SmsMessage
*
*/
class SmsMessage
{
/**
* @param string $mobile
* @param string $signName
* @param string $templateCode
* @param array $templateParam
* @return array
*/
private function send(string $mobile, string $signName, string $templateCode, array $templateParam)
{
AppLog::DEBUG('短信发送结果:' . json_encode($templateParam, JSON_UNESCAPED_UNICODE));
if (YII_ENV_DEV) {
return true;
}
try {
AlibabaCloud::accessKeyClient(Yii::$app->params['sms']["smsKey"], Yii::$app->params['sms']["smsSecret"])->asDefaultClient();
$query = [
'PhoneNumbers' => $mobile,
'SignName' => $signName,
'TemplateCode' => $templateCode,
];
if ($templateParam) {
$query['TemplateParam'] = json_encode($templateParam, JSON_UNESCAPED_UNICODE);
}
$result = AlibabaCloud::rpc()
->regionId("cn-hangzhou") // 指定请求的区域,不指定则使用客户端区域、默认区域
->product('Dysmsapi') // 指定产品
->version('2017-05-25') // 指定产品版本
->action('SendSms') // 指定产品接口
->method('POST') // 指定请求方式
->options([
'query' => $query,
])
->request(); // 发起请求并返回结果对象,请求需要放在设置的最后面
$result = $result->toArray();
AppLog::DEBUG('短信发送结果:' . '<pre>' . print_r($result, 1) . '</pre>');
return $result;
} catch (ClientException $exception) {
AppLog::DEBUG('短信发送出错:' . $exception->getErrorMessage());
return false;
} catch (ServerException $exception) {
AppLog::DEBUG('短信发送出错:' . $exception->getErrorMessage());
return false;
}
}
/**
* @return string
*/
private function getSignName()
{
$smsConfig = Yii::$app->params['sms'];
return isset($smsConfig['smsSignName'])?$smsConfig['smsSignName']:'';
}
/**
* 手机验证码
* 模版内容: 验证码${code},您正在注册成为新用户,感谢您的支持!
* 申请说明: 用户注册验证码
* 模版CODE: SMS_181866843
* @param $phone
* @param $code
* @param $signName
* @return mixed
*/
public function sendRegCode($phone, $code)
{
//return true;
$signName = $this->getSignName();
return $this->send($phone, $signName, "SMS_181866843", ["code" => $code]);
}
/**
* 登录验证码
* 模版内容: 模版CODE: SMS_181866846
* 模版内容: 尊敬的用户,您的登录码为:${code},请勿泄漏于他人!
* @param $phone
* @return mixed
*/
public function sendLoginCode($phone, $code)
{
//return true;
$signName = $this->getSignName();
return $this->send($phone, $signName, "SMS_181866846", ["code" => $code]);
}
/*
* 模版CODE: SMS_181853499
*
* 尊敬的车主,您的爱车正在${maintainer}维修,预计${dateTime}维修完成,预估维修内容:${repairPlan},预估维修费:${repairPrice},维修厂地址:${address},联系电话:${tel}
* */
public function sendSubmitInfo($phone, $postData)
{
//return true;
$signName = $this->getSignName();
return $this->send($phone, $signName, "SMS_181853499", $postData);
}
/*
* 模版CODE: SMS_181862780
* 原来: 您好,${maintainer}已完成您的爱车维修,点击http://gkauto.jiwork.com/site/t?o=${code} 了解维修详情,同时您可以对本次服务进行评分。如有问题请拨打服务监督电话${tel}
* 修改: 您好,${maintainer}已完成您的爱车维修,点击http://gkauto.jiwork.com/site/t?o=${code} 了解维修详情,同时您可以对本次服务进行评分。如有问题请拨打服务监督电话${tel}, 维修厂电话${rtel}
* */
public function sendFinishInfo($phone, $postData)
{
$signName = $this->getSignName();
return $this->send($phone, $signName, "SMS_189240887", $postData);
}
}