SmsUtil.java
1.62 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
package com.infoloop.tianting.utils;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.infoloop.tianting.config.SmsConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import java.util.Map;
@Slf4j
@RequiredArgsConstructor
public class SmsUtil {
private final SmsConfig smsConfig;
private final IAcsClient client;
public boolean sendSms(String templateId, Map<String, Object> templateParam, String... mobiles) {
try {
final var smsRequest = new SendSmsRequest();
smsRequest.setSysMethod(MethodType.POST);
smsRequest.setPhoneNumbers(mobiles.length == 1 ? mobiles[0] : String.join(",", mobiles));
smsRequest.setSignName(smsConfig.getSignName());
smsRequest.setTemplateCode(templateId);
if (MapUtils.isNotEmpty(templateParam)) {
final var templateParamJson = JsonUtil.writeAsJson(templateParam);
log.info("send sms template param: {}", templateParamJson);
smsRequest.setTemplateParam(templateParamJson);
}
final var sendSmsResponse = client.getAcsResponse(smsRequest);
log.info("send sms response: {}", JsonUtil.writeAsJson(sendSmsResponse));
return sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK");
} catch (ClientException e) {
log.error("send sms error", e);
return false;
}
}
}