优化短信代码
This commit is contained in:
parent
6a63833611
commit
f995f031b4
|
@ -2,8 +2,9 @@ package net.maku.message.sms;
|
||||||
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import com.aliyun.dysmsapi20170525.Client;
|
import com.aliyun.dysmsapi20170525.Client;
|
||||||
import com.aliyun.dysmsapi20170525.models.*;
|
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||||
import com.aliyun.teaopenapi.models.*;
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.maku.framework.common.constant.Constant;
|
import net.maku.framework.common.constant.Constant;
|
||||||
import net.maku.framework.common.exception.FastException;
|
import net.maku.framework.common.exception.FastException;
|
||||||
|
@ -21,6 +22,7 @@ import java.util.Map;
|
||||||
public class AliyunSmsStrategy implements SmsStrategy {
|
public class AliyunSmsStrategy implements SmsStrategy {
|
||||||
private final Client client;
|
private final Client client;
|
||||||
private final SmsConfig smsConfig;
|
private final SmsConfig smsConfig;
|
||||||
|
|
||||||
public AliyunSmsStrategy(SmsConfig smsConfig) {
|
public AliyunSmsStrategy(SmsConfig smsConfig) {
|
||||||
this.smsConfig = smsConfig;
|
this.smsConfig = smsConfig;
|
||||||
|
|
||||||
|
@ -43,7 +45,7 @@ public class AliyunSmsStrategy implements SmsStrategy {
|
||||||
request.setTemplateCode(smsConfig.getTemplateId());
|
request.setTemplateCode(smsConfig.getTemplateId());
|
||||||
request.setPhoneNumbers(mobile);
|
request.setPhoneNumbers(mobile);
|
||||||
// request.setTemplateParam("{\"code\":\"1234\"}");
|
// request.setTemplateParam("{\"code\":\"1234\"}");
|
||||||
if(MapUtil.isNotEmpty(params)){
|
if (MapUtil.isNotEmpty(params)) {
|
||||||
request.setTemplateParam(JsonUtils.toJsonString(params));
|
request.setTemplateParam(JsonUtils.toJsonString(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,11 +54,11 @@ public class AliyunSmsStrategy implements SmsStrategy {
|
||||||
SendSmsResponse response = client.sendSms(request);
|
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());
|
throw new FastException(response.getBody().getMessage());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FastException("短信发送失败:", e);
|
throw new FastException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,9 +38,6 @@ public class HuaweiSmsStrategy implements SmsStrategy {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(String mobile, Map<String, String> params) {
|
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;
|
String templateParas = null;
|
||||||
if (MapUtil.isNotEmpty(params)) {
|
if (MapUtil.isNotEmpty(params)) {
|
||||||
|
@ -63,7 +60,7 @@ public class HuaweiSmsStrategy implements SmsStrategy {
|
||||||
// 使用 https
|
// 使用 https
|
||||||
trustAllHttpsCertificates();
|
trustAllHttpsCertificates();
|
||||||
|
|
||||||
URL realUrl = new URL(url);
|
URL realUrl = new URL(smsConfig.getUrl());
|
||||||
HttpsURLConnection connection = (HttpsURLConnection) realUrl.openConnection();
|
HttpsURLConnection connection = (HttpsURLConnection) realUrl.openConnection();
|
||||||
HostnameVerifier hv = (hostname, session) -> true;
|
HostnameVerifier hv = (hostname, session) -> true;
|
||||||
connection.setHostnameVerifier(hv);
|
connection.setHostnameVerifier(hv);
|
||||||
|
@ -92,7 +89,7 @@ public class HuaweiSmsStrategy implements SmsStrategy {
|
||||||
throw new FastException(IoUtil.read(connection.getErrorStream(), CharsetUtil.CHARSET_UTF_8));
|
throw new FastException(IoUtil.read(connection.getErrorStream(), CharsetUtil.CHARSET_UTF_8));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FastException("短信发送失败:", e);
|
throw new FastException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ public class QiniuSmsStrategy implements SmsStrategy {
|
||||||
throw new FastException(response.error);
|
throw new FastException(response.error);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FastException("短信发送失败:", e);
|
throw new FastException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ public class TencentSmsStrategy implements SmsStrategy {
|
||||||
throw new FastException(sendStatus.getMessage());
|
throw new FastException(sendStatus.getMessage());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FastException("短信发送失败:", e);
|
throw new FastException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user