package com.ruoyi.message.sms.provider.aliyun;
|
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
|
import com.ruoyi.message.MessageChannel;
|
import com.ruoyi.message.SendResult;
|
import com.ruoyi.message.sms.AbstractSmsSender;
|
import com.ruoyi.message.sms.SmsSendRequest;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
/**
|
* 阿里云短信发送方({@code aliyun} 通道)。
|
* <p>
|
* 对接特征:阿里云 dysmsapi SDK,{@code SignName + TemplateCode + TemplateParam},
|
* 成功判定为响应 {@code Code == "OK"}。
|
* <p>
|
* 说明:该通道目前只在 {@code BaseSmsaccountServiceImpl}(短信账号/模板管理)里使用,
|
* 业务发送流程未走它。收口后可由工厂统一调度。
|
*/
|
@Slf4j
|
@Component
|
public class AliYunSmsSender extends AbstractSmsSender {
|
|
/**
|
* 渠道 key
|
*/
|
public static final String KEY = "aliyun";
|
|
private final AliYunSmsApi aliYunSmsApi;
|
|
public AliYunSmsSender(AliYunSmsApi aliYunSmsApi) {
|
this.aliYunSmsApi = aliYunSmsApi;
|
}
|
|
@Override
|
public String key() {
|
return KEY;
|
}
|
|
@Override
|
public String description() {
|
return "阿里云短信(dysmsapi,模板化)";
|
}
|
|
@Override
|
protected SendResult doSend(SmsSendRequest request) {
|
//阿里云短信不走内容直发,而是 模板编码 + 模板参数
|
SendSmsRequestAdapter adapter = new SendSmsRequestAdapter(request);
|
if (adapter.templateParam == null) {
|
return SendResult.fail(KEY, MessageChannel.SMS, "短信模板参数为空", null);
|
}
|
|
SendSmsResponseBody body = aliYunSmsApi.sendSms(
|
adapter.phone, adapter.templateCode, adapter.templateParam);
|
if (body == null) {
|
return SendResult.fail(KEY, MessageChannel.SMS, "短信发送失败", null);
|
}
|
log.info("【{}】阿里云短信发送结果:code={}, message={}", KEY, body.getCode(), body.getMessage());
|
if ("OK".equals(body.getCode())) {
|
return SendResult.success(KEY, MessageChannel.SMS, body.getMessage())
|
.withCode(body.getCode())
|
.withMessage(body.getBizId());
|
}
|
return SendResult.fail(KEY, MessageChannel.SMS, body.getMessage(), body.getMessage())
|
.withCode(body.getCode());
|
}
|
|
/**
|
* 绕过基类「内容不能为空」的校验:阿里云用模板参数,不用 content 直发。
|
*/
|
@Override
|
public SendResult send(SmsSendRequest request) {
|
if (request == null || request.getPhone() == null || request.getPhone().trim().isEmpty()) {
|
return SendResult.fail(KEY, MessageChannel.SMS, "手机号为空", null);
|
}
|
try {
|
return doSend(request);
|
} catch (Exception e) {
|
log.error("【{}】阿里云短信发送异常:{}", KEY, e.getMessage(), e);
|
return SendResult.fail(KEY, MessageChannel.SMS, "短信发送异常:" + e.getMessage(), null);
|
}
|
}
|
|
/**
|
* 小适配器:把统一入参映射成阿里云需要的三个字段。
|
* <p>
|
* 兼容旧调用方({@code BaseSmsRequest})传参习惯:手机号走 {@code phone},
|
* 模板编码/参数走 {@code templateCode} / {@code templateParam}。
|
*/
|
private static final class SendSmsRequestAdapter {
|
private final String phone;
|
private final String templateCode;
|
private final String templateParam;
|
|
private SendSmsRequestAdapter(SmsSendRequest request) {
|
this.phone = request.getPhone();
|
this.templateCode = request.getTemplateCode();
|
this.templateParam = request.getTemplateParam() != null
|
? request.getTemplateParam()
|
: request.getContent();
|
}
|
}
|
}
|