优化短信代码

This commit is contained in:
阿沐 2022-08-24 11:02:47 +08:00
parent 6a63833611
commit f995f031b4
4 changed files with 11 additions and 12 deletions

View File

@ -2,8 +2,9 @@ package net.maku.message.sms;
import cn.hutool.core.map.MapUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.exception.FastException;
@ -21,6 +22,7 @@ import java.util.Map;
public class AliyunSmsStrategy implements SmsStrategy {
private final Client client;
private final SmsConfig smsConfig;
public AliyunSmsStrategy(SmsConfig smsConfig) {
this.smsConfig = smsConfig;
@ -43,7 +45,7 @@ public class AliyunSmsStrategy implements SmsStrategy {
request.setTemplateCode(smsConfig.getTemplateId());
request.setPhoneNumbers(mobile);
// request.setTemplateParam("{\"code\":\"1234\"}");
if(MapUtil.isNotEmpty(params)){
if (MapUtil.isNotEmpty(params)) {
request.setTemplateParam(JsonUtils.toJsonString(params));
}
@ -52,11 +54,11 @@ public class AliyunSmsStrategy implements SmsStrategy {
SendSmsResponse response = client.sendSms(request);
// 发送失败
if(!Constant.OK.equalsIgnoreCase(response.getBody().getCode())) {
if (!Constant.OK.equalsIgnoreCase(response.getBody().getCode())) {
throw new FastException(response.getBody().getMessage());
}
} catch (Exception e) {
throw new FastException("短信发送失败:", e);
throw new FastException(e.getMessage());
}
}
}

View File

@ -38,9 +38,6 @@ public class HuaweiSmsStrategy implements SmsStrategy {
@Override
public void send(String mobile, Map<String, String> params) {
// APP接入地址(在控制台"应用管理"页面获取)+接口访问URI
String url = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1";
// 有参数则设置
String templateParas = null;
if (MapUtil.isNotEmpty(params)) {
@ -63,7 +60,7 @@ public class HuaweiSmsStrategy implements SmsStrategy {
// 使用 https
trustAllHttpsCertificates();
URL realUrl = new URL(url);
URL realUrl = new URL(smsConfig.getUrl());
HttpsURLConnection connection = (HttpsURLConnection) realUrl.openConnection();
HostnameVerifier hv = (hostname, session) -> true;
connection.setHostnameVerifier(hv);
@ -92,7 +89,7 @@ public class HuaweiSmsStrategy implements SmsStrategy {
throw new FastException(IoUtil.read(connection.getErrorStream(), CharsetUtil.CHARSET_UTF_8));
}
} catch (Exception e) {
throw new FastException("短信发送失败:", e);
throw new FastException(e.getMessage());
}
}

View File

@ -34,7 +34,7 @@ public class QiniuSmsStrategy implements SmsStrategy {
throw new FastException(response.error);
}
} catch (Exception e) {
throw new FastException("短信发送失败:", e);
throw new FastException(e.getMessage());
}
}
}

View File

@ -70,7 +70,7 @@ public class TencentSmsStrategy implements SmsStrategy {
throw new FastException(sendStatus.getMessage());
}
} catch (Exception e) {
throw new FastException("短信发送失败:", e);
throw new FastException(e.getMessage());
}
}
}