commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.core.sms.modular.aliyun; |
|
26 |
|
|
27 |
import cn.hutool.core.util.ObjectUtil; |
|
28 |
import com.alibaba.fastjson.JSON; |
|
29 |
import com.alibaba.fastjson.JSONObject; |
|
30 |
import com.aliyuncs.CommonRequest; |
|
31 |
import com.aliyuncs.CommonResponse; |
|
32 |
import com.aliyuncs.DefaultAcsClient; |
|
33 |
import com.aliyuncs.IAcsClient; |
|
34 |
import com.aliyuncs.exceptions.ClientException; |
|
35 |
import com.aliyuncs.profile.DefaultProfile; |
|
36 |
import lombok.extern.slf4j.Slf4j; |
|
37 |
import vip.xiaonuo.core.sms.SmsSender; |
|
38 |
import vip.xiaonuo.core.sms.modular.aliyun.enums.AliyunSmsResultEnum; |
|
39 |
import vip.xiaonuo.core.sms.modular.aliyun.exp.AliyunSmsException; |
|
40 |
import vip.xiaonuo.core.sms.modular.aliyun.msign.MultiSignManager; |
|
41 |
import vip.xiaonuo.core.sms.modular.aliyun.prop.AliyunSmsProperties; |
|
42 |
|
|
43 |
import java.util.Map; |
|
44 |
|
|
45 |
/** |
|
46 |
* 阿里云短信发送服务 |
|
47 |
* |
|
48 |
* @author xuyuxiang |
|
49 |
* @date 2018-07-06-下午2:15 |
|
50 |
*/ |
|
51 |
@Slf4j |
|
52 |
public class AliyunSmsSender implements SmsSender { |
|
53 |
|
|
54 |
private final MultiSignManager multiSignManager; |
|
55 |
|
|
56 |
private final AliyunSmsProperties aliyunSmsProperties; |
|
57 |
|
|
58 |
public AliyunSmsSender(MultiSignManager multiSignManager, AliyunSmsProperties aliyunSmsProperties) { |
|
59 |
this.multiSignManager = multiSignManager; |
|
60 |
this.aliyunSmsProperties = aliyunSmsProperties; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public void sendSms(String phone, String templateCode, Map<String, Object> params) { |
|
65 |
|
|
66 |
log.info(">>> 开始发送阿里云短信,手机号是:" + phone + ",模板号是:" + templateCode + ",参数是:" + params); |
|
67 |
|
|
68 |
// 检验参数是否合法 |
|
69 |
assertSendSmsParams(phone, templateCode, params, aliyunSmsProperties); |
|
70 |
|
|
71 |
// 初始化client profile |
|
72 |
IAcsClient iAcsClient = initClient(); |
|
73 |
|
|
74 |
// 组装请求对象 |
|
75 |
JSONObject smsRes = createSmsRequest(phone, templateCode, params, iAcsClient); |
|
76 |
|
|
77 |
// 如果返回ok则发送成功 |
|
78 |
if (!AliyunSmsResultEnum.OK.getCode().equals(smsRes.getString("Code"))) { |
|
79 |
|
|
80 |
// 返回其他状态码根据情况抛出业务异常 |
|
81 |
String code = AliyunSmsResultEnum.SYSTEM_ERROR.getCode(); |
|
82 |
String errorMessage = AliyunSmsResultEnum.SYSTEM_ERROR.getMessage(); |
|
83 |
for (AliyunSmsResultEnum smsExceptionEnum : AliyunSmsResultEnum.values()) { |
|
84 |
if (smsExceptionEnum.getCode().equals(smsRes.getString("Code"))) { |
|
85 |
code = smsExceptionEnum.getCode(); |
|
86 |
errorMessage = smsExceptionEnum.getMessage(); |
|
87 |
} |
|
88 |
} |
|
89 |
log.error(">>> 发送短信异常!code = " + code + ",message = " + errorMessage); |
|
90 |
throw new AliyunSmsException(code, errorMessage); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
/** |
|
95 |
* 初始化短信发送的客户端 |
|
96 |
* |
|
97 |
* @author xuyuxiang |
|
98 |
* @date 2018/7/6 下午3:57 |
|
99 |
*/ |
|
100 |
private IAcsClient initClient() { |
|
101 |
final String accessKeyId = aliyunSmsProperties.getAccessKeyId(); |
|
102 |
final String accessKeySecret = aliyunSmsProperties.getAccessKeySecret(); |
|
103 |
|
|
104 |
// 创建DefaultAcsClient实例并初始化 |
|
105 |
DefaultProfile profile = DefaultProfile.getProfile(aliyunSmsProperties.getRegionId(), accessKeyId, accessKeySecret); |
|
106 |
return new DefaultAcsClient(profile); |
|
107 |
} |
|
108 |
|
|
109 |
/** |
|
110 |
* 组装请求对象 |
|
111 |
* |
|
112 |
* @author xuyuxiang |
|
113 |
* @date 2018/7/6 下午3:00 |
|
114 |
*/ |
|
115 |
private JSONObject createSmsRequest(String phoneNumber, String templateCode, Map<String, Object> params, IAcsClient acsClient) { |
|
116 |
CommonRequest request = new CommonRequest(); |
|
117 |
request.setSysDomain(aliyunSmsProperties.getSmsDomain()); |
|
118 |
request.setSysVersion(aliyunSmsProperties.getSmsVersion()); |
|
119 |
request.setSysAction(aliyunSmsProperties.getSmsSendAction()); |
|
120 |
|
|
121 |
// 接收短信的手机号码 |
|
122 |
request.putQueryParameter("PhoneNumbers", phoneNumber); |
|
123 |
|
|
124 |
// 短信签名名称。请在控制台签名管理页面签名名称一列查看(必须是已添加、并通过审核的短信签名)。 |
|
125 |
request.putQueryParameter("SignName", this.getSmsSign(phoneNumber)); |
|
126 |
|
|
127 |
// 短信模板ID |
|
128 |
request.putQueryParameter("TemplateCode", templateCode); |
|
129 |
|
|
130 |
// 短信模板变量对应的实际值,JSON格式。 |
|
131 |
request.putQueryParameter("TemplateParam", JSON.toJSONString(params)); |
|
132 |
|
|
133 |
//请求失败这里会抛ClientException异常 |
|
134 |
CommonResponse commonResponse; |
|
135 |
try { |
|
136 |
commonResponse = acsClient.getCommonResponse(request); |
|
137 |
String data = commonResponse.getData(); |
|
138 |
String jsonResult = data.replaceAll("'\'", ""); |
|
139 |
log.info(">>> 获取到发送短信的响应结果!{}", jsonResult); |
|
140 |
return JSON.parseObject(jsonResult); |
|
141 |
} catch (ClientException e) { |
|
142 |
log.error(">>> 初始化阿里云sms异常!可能是accessKey和secret错误!", e); |
|
143 |
throw new AliyunSmsException(AliyunSmsResultEnum.INIT_SMS_CLIENT_ERROR.getCode(), |
|
144 |
AliyunSmsResultEnum.INIT_SMS_CLIENT_ERROR.getMessage()); |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
/** |
|
149 |
* 校验发送短信的参数是否正确 |
|
150 |
* |
|
151 |
* @author xuyuxiang |
|
152 |
* @date 2018/7/6 下午3:19 |
|
153 |
*/ |
|
154 |
private void assertSendSmsParams(String phoneNumber, String templateCode, Map<String, Object> params, |
|
155 |
AliyunSmsProperties aliyunSmsProperties) { |
|
156 |
if (ObjectUtil.hasEmpty(phoneNumber, templateCode, params, aliyunSmsProperties)) { |
|
157 |
log.error(">>> 阿里云短信发送异常!请求参数存在空!"); |
|
158 |
throw new AliyunSmsException(AliyunSmsResultEnum.PARAM_NULL.getCode(), AliyunSmsResultEnum.PARAM_NULL.getMessage()); |
|
159 |
} |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* 获取sms发送的sign标识,参数phone是发送的手机号码 |
|
164 |
* |
|
165 |
* @author xuyuxiang |
|
166 |
* @date 2018/8/13 21:23 |
|
167 |
*/ |
|
168 |
private String getSmsSign(String phone) { |
|
169 |
String signName = aliyunSmsProperties.getSignName(); |
|
170 |
|
|
171 |
// 如果是单个签名就用一个签名发 |
|
172 |
if (!signName.contains(",")) { |
|
173 |
log.info(">>> 发送短信,签名为:" + signName + ",电话为:" + phone); |
|
174 |
return signName; |
|
175 |
} else { |
|
176 |
return multiSignManager.getSign(phone, signName); |
|
177 |
} |
|
178 |
} |
|
179 |
|
|
180 |
} |