SMSUtils.java
9.15 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.subsidy.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.subsidy.common.configure.AliyunSmsProperties;
import com.subsidy.common.exception.HttpException;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* 短信发送工具类
*/
@Component
public class SMSUtils {
@Autowired
private AliyunSmsProperties aliyunSmsProperties;
public SendSmsResponse send(String phone, String code) {
try {
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunSmsProperties.getAccessKeyId(), aliyunSmsProperties.getAccessKeySecret());
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", aliyunSmsProperties.getProduct(), aliyunSmsProperties.getDomain());
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(phone);
//必填:短信签名-可在短信控制台中找到
request.setSignName("有课互联");
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode("SMS_229640297");
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam("{\"code\":\""+ code +"\"}");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse != null && sendSmsResponse.getCode().equals("OK")) {
return sendSmsResponse;
}
throw new Exception(sendSmsResponse.getMessage());
} catch (Exception e) {
e.printStackTrace();
throw new HttpException(70011);
}
}
/**
* 发送验证码短信
* @param templateId 短信模板id
* @param telephone 手机号
* @return
*/
public String sendVerifySMS(String templateId,String telephone,String param){
try {
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunSmsProperties.getAccessKeyId(), aliyunSmsProperties.getAccessKeySecret());
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", aliyunSmsProperties.getProduct(), aliyunSmsProperties.getDomain());
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(telephone);
//必填:短信签名-可在短信控制台中找到
request.setSignName("有课互联");
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateId);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam("{\"code\":\""+ param +"\"}");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse != null && sendSmsResponse.getCode().equals("OK")) {
return ConstantUtils.SUCCESS_SEND_OUT;
}
throw new Exception(sendSmsResponse.getMessage());
} catch(Exception e) {
e.printStackTrace();
return ConstantUtils.FAIL_SEND_OUT;
}
}
public String sendWarning(String templateId,String telephone,String param){
try {
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunSmsProperties.getAccessKeyId(), aliyunSmsProperties.getAccessKeySecret());
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", aliyunSmsProperties.getProduct(), aliyunSmsProperties.getDomain());
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(telephone);
//必填:短信签名-可在短信控制台中找到
request.setSignName("有课互联科技");
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateId);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam("{\"ids\":\""+ param +"\"}");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse != null && sendSmsResponse.getCode().equals("OK")) {
return ConstantUtils.SUCCESS_SEND_OUT;
}
throw new Exception(sendSmsResponse.getMessage());
} catch(Exception e) {
e.printStackTrace();
return ConstantUtils.FAIL_SEND_OUT;
}
}
/**
* 发送通知短信
* @param templateId 短信模板id
* @param telephone 手机号
* @return
*/
public static String sendNoticeSMS(String templateId,String telephone,String param){
DefaultProfile profile = DefaultProfile.getProfile(ConstantUtils.REGION_ID, ConstantUtils.ACCESS_KEY_ID, ConstantUtils.SECRET);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId",ConstantUtils.REGION_ID);
request.putQueryParameter("PhoneNumbers",telephone);
request.putQueryParameter("SignName",ConstantUtils.TECH_NAME);
request.putQueryParameter("TemplateCode",templateId);
// if (StringUtils.isEmpty(param)){
// request.putQueryParameter("TemplateParam","{ \"code\":\""+verifyCode +"\"}");
// }
request.putQueryParameter("TemplateParam",param);
CommonResponse response = null;
try {
response = client.getCommonResponse(request);
if(response != null) {
JSONObject data = JSON.parseObject(response.getData());
if(data.get("Code").equals("OK")) {
return ConstantUtils.SUCCESS_SEND_OUT;
}
throw new HttpException(1000, data.get("Message").toString());
}
return ConstantUtils.FAIL_SEND_OUT;
} catch(Exception e) {
e.printStackTrace();
return ConstantUtils.FAIL_SEND_OUT;
}
}
//public static void main(String[] args) {
// String param = "{ \"course\":\""+"语文课"+ "\",\"time\":\""+"2020-01-02" +"\",\"address\":\""+"上海市" +"\"}";
// sendNoticeSMS("SMS_215336604","15201936167,18201963812",param);
//}
public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
//拼音小写
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//不带声调
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//format.setVCharType();
//要转换的中文,格式,转换之后的拼音的分隔符,遇到不能转换的是否保留 wo,shi,zhong,guo,ren,,hello
System.out.println(PinyinHelper.toHanYuPinyinString("我是中国人", format, "", false));
}
}