From e4194cdf4d29b9b5415c5c456463523fa0662c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 14 May 2025 14:18:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BD=BF=E7=94=A8=E9=98=BF?= =?UTF-8?q?=E9=87=8C=E4=BA=91=E5=B9=B3=E5=8F=B0=E5=8F=91=E9=80=81=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E7=9F=AD=E4=BF=A1=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=20--pom.xml=20=E6=B7=BB=E5=8A=A0=E7=9F=AD=E4=BF=A1=E5=8F=91?= =?UTF-8?q?=E9=80=81sdk=20--Constant.java=20=E6=B7=BB=E5=8A=A0=E5=8F=91?= =?UTF-8?q?=E9=80=81=E7=9F=AD=E4=BF=A1=E7=9A=84=E5=8F=82=E6=95=B0=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=20--ALiYunSmsService.java=20=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E4=BA=91=E5=AE=9E=E7=8E=B0=E7=9F=AD=E4=BF=A1=E6=96=B9=E6=B3=95?= =?UTF-8?q?=20--SmsService.java=20=E7=9F=AD=E4=BF=A1=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/manager-api/pom.xml | 15 ++++ .../xiaozhi/common/constant/Constant.java | 40 ++++++++++ .../modules/sms/service/SmsService.java | 17 +++++ .../sms/service/imp/ALiYunSmsService.java | 76 +++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 main/manager-api/src/main/java/xiaozhi/modules/sms/service/SmsService.java create mode 100644 main/manager-api/src/main/java/xiaozhi/modules/sms/service/imp/ALiYunSmsService.java diff --git a/main/manager-api/pom.xml b/main/manager-api/pom.xml index d08e32f2..c4234647 100644 --- a/main/manager-api/pom.xml +++ b/main/manager-api/pom.xml @@ -28,6 +28,8 @@ 1.6.2 33.0.0-jre 4.20.0 + 4.1.0 + 3.4.0 @@ -37,6 +39,7 @@ jakarta ${shiro.version} + org.apache.shiro shiro-spring @@ -201,6 +204,18 @@ com.fasterxml.jackson.datatype jackson-datatype-jsr310 + + + com.aliyun + dysmsapi20170525 + ${the-latest-version} + + + + com.squareup.okio + okio + ${okio-version} + diff --git a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java index 78154b5c..171d1a21 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java +++ b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java @@ -146,6 +146,46 @@ public interface Constant { } } + /** + * 系统短信 + */ + enum SysMSMParam { + /** + * 阿里云授权keyID + */ + ALIYUN_SMS_ACCESS_KEY_ID("aliyun.sms.access_key_id"), + /** + * 阿里云授权密钥 + */ + ALIYUN_SMS_ACCESS_KEY_SECRET("aliyun.sms.access_key_secret"), + /** + * + */ + ALIYUN_SMS_SIGN_NAME("aliyun.sms.sign_name"), + /** + * 已删除 + */ + ALIYUN_SMS_SMS_CODE_TEMPLATE_CODE("aliyun.sms.sms_code_template_code"), + /** + * 已删除 + */ + SYSTEM_SMS_MAX_SEND_COUNT("system.sms.max_send_count"), + /** + * 是否开启手机注册 + */ + SYSTEM_ENABLE_MOBILE_REGISTER("system.enable_mobile_register"); + + private String value; + + SysMSMParam(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + /** * 数据状态 */ diff --git a/main/manager-api/src/main/java/xiaozhi/modules/sms/service/SmsService.java b/main/manager-api/src/main/java/xiaozhi/modules/sms/service/SmsService.java new file mode 100644 index 00000000..c33ad945 --- /dev/null +++ b/main/manager-api/src/main/java/xiaozhi/modules/sms/service/SmsService.java @@ -0,0 +1,17 @@ +package xiaozhi.modules.sms.service; + +/** + * 短信服务的方法定义接口 + * + * @author zjy + * @since 2025-05-12 + */ +public interface SmsService { + + /** + * 发送验证码短信 + * @param phone 手机号码 + * @param VerificationCode 验证码 + */ + void sendVerificationCodeSms(String phone, String VerificationCode) ; +} diff --git a/main/manager-api/src/main/java/xiaozhi/modules/sms/service/imp/ALiYunSmsService.java b/main/manager-api/src/main/java/xiaozhi/modules/sms/service/imp/ALiYunSmsService.java new file mode 100644 index 00000000..0a73bf68 --- /dev/null +++ b/main/manager-api/src/main/java/xiaozhi/modules/sms/service/imp/ALiYunSmsService.java @@ -0,0 +1,76 @@ +package xiaozhi.modules.sms.service.imp; + +import com.aliyun.dysmsapi20170525.Client; +import com.aliyun.dysmsapi20170525.models.SendSmsRequest; +import com.aliyun.dysmsapi20170525.models.SendSmsResponse; +import com.aliyun.tea.TeaException; +import com.aliyun.teaopenapi.models.Config; +import com.aliyun.teautil.Common; +import com.aliyun.teautil.models.RuntimeOptions; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import xiaozhi.common.constant.Constant; +import xiaozhi.modules.sms.service.SmsService; +import xiaozhi.modules.sys.service.SysParamsService; + +@Service +@AllArgsConstructor +@Slf4j +public class ALiYunSmsService implements SmsService { + private final SysParamsService sysParamsService; + + @Override + public void sendVerificationCodeSms(String phone, String VerificationCode) { + String SignName = sysParamsService.getValue(Constant.SysMSMParam + .ALIYUN_SMS_SIGN_NAME.getValue(),true); + String TemplateCode = sysParamsService.getValue(Constant.SysMSMParam + .ALIYUN_SMS_SMS_CODE_TEMPLATE_CODE.getValue(),true); + try { + Client client = createClient(); + SendSmsRequest sendSmsRequest = new SendSmsRequest() + .setSignName(SignName) + .setTemplateCode(TemplateCode) + .setPhoneNumbers(phone) + .setTemplateParam(String.format("{\"code\":\"%s\"}", VerificationCode)); + RuntimeOptions runtime = new RuntimeOptions(); + try { + // 复制代码运行请自行打印 API 的返回值 + SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime); + System.out.println(sendSmsResponse.getBody().getRequestId()); + System.out.println(sendSmsResponse.getBody().getRequestId()); + } catch (TeaException error) { + // 错误 message + log.error(error.getMessage()); + // 诊断地址 + log.error(error.getData().get("Recommend").toString()); + log.error(Common.assertAsString(error.message)); + } + } catch (Exception e) { + // 错误 message + log.error(e.getMessage()); + throw new RuntimeException(e); + } + + } + + + /** + * 创建阿里云连接 + * @return 返回连接对象 + * @throws Exception + */ + private Client createClient() throws Exception { + String ACCESS_KEY_ID = sysParamsService.getValue(Constant.SysMSMParam + .ALIYUN_SMS_ACCESS_KEY_ID.getValue(),true); + String ACCESS_KEY_SECRET = sysParamsService.getValue(Constant.SysMSMParam + .ALIYUN_SMS_ACCESS_KEY_SECRET.getValue(),true); + Config config = new Config() + .setAccessKeyId(ACCESS_KEY_ID) + .setAccessKeySecret(ACCESS_KEY_SECRET); + // 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com + config.endpoint = "dysmsapi.aliyuncs.com"; + + return new Client(config); + } +}