|
|
@@ -0,0 +1,108 @@
|
|
|
+package shop.alien.gateway.util;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 短信发送工具类
|
|
|
+ *
|
|
|
+ * 注意:如果需要实际发送短信,需要在pom.xml中添加阿里云短信SDK依赖:
|
|
|
+ * <dependency>
|
|
|
+ * <groupId>com.aliyun</groupId>
|
|
|
+ * <artifactId>dysmsapi20170525</artifactId>
|
|
|
+ * <version>3.0.0</version>
|
|
|
+ * </dependency>
|
|
|
+ *
|
|
|
+ * @author system
|
|
|
+ * @since 2025-01-XX
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class SmsUtil {
|
|
|
+
|
|
|
+ @Value("${ali.sms.accessKeyId:}")
|
|
|
+ private String accessKeyId;
|
|
|
+
|
|
|
+ @Value("${ali.sms.accessKeySecret:}")
|
|
|
+ private String accessKeySecret;
|
|
|
+
|
|
|
+ @Value("${ali.sms.endPoint:}")
|
|
|
+ private String endPoint;
|
|
|
+
|
|
|
+ @Value("${ali.sms.signName:}")
|
|
|
+ private String signName;
|
|
|
+
|
|
|
+ @Value("${ali.sms.templateCode:}")
|
|
|
+ private String templateCode;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送账号密码短信
|
|
|
+ * 短信内容:您的账号{userName},密码是{password}
|
|
|
+ *
|
|
|
+ * @param phone 手机号
|
|
|
+ * @param userName 账号(用户名)
|
|
|
+ * @param password 密码
|
|
|
+ * @return 是否发送成功
|
|
|
+ */
|
|
|
+ public boolean sendAccountPasswordSms(String phone, String userName, String password) {
|
|
|
+ if (!StringUtils.hasText(phone)) {
|
|
|
+ log.warn("手机号为空,无法发送短信");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("SmsUtil.sendAccountPasswordSms?phone={},userName={}", phone, userName);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 构建短信内容:您的账号{userName},密码是{password}
|
|
|
+ String messageContent = String.format("您的账号%s,密码是%s", userName, password);
|
|
|
+
|
|
|
+ // 如果配置为空,只记录日志(测试环境)
|
|
|
+ if (!StringUtils.hasText(accessKeyId) || !StringUtils.hasText(accessKeySecret)) {
|
|
|
+ log.info("短信配置未设置,仅记录日志。短信内容:{}", messageContent);
|
|
|
+ return true; // 测试环境返回成功
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: 如果需要实际发送短信,请取消下面的注释并添加阿里云短信SDK依赖
|
|
|
+ /*
|
|
|
+ Config config = new Config()
|
|
|
+ .setEndpoint(endPoint)
|
|
|
+ .setAccessKeyId(accessKeyId)
|
|
|
+ .setAccessKeySecret(accessKeySecret);
|
|
|
+
|
|
|
+ // 构建发送请求
|
|
|
+ // 注意:这里使用模板方式,如果阿里云有专门的账号密码通知模板,请使用对应的templateCode
|
|
|
+ // 如果没有,可以使用通用模板,将内容作为参数传递
|
|
|
+ SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
|
|
+ .setSignName(signName)
|
|
|
+ .setTemplateCode(templateCode)
|
|
|
+ .setPhoneNumbers(phone)
|
|
|
+ // 根据实际模板参数调整,这里假设模板参数为 {"account":"账号","password":"密码"}
|
|
|
+ .setTemplateParam("{\"account\":\"" + userName + "\",\"password\":\"" + password + "\"}");
|
|
|
+
|
|
|
+ RuntimeOptions runtime = new RuntimeOptions();
|
|
|
+ Client client = new Client(config);
|
|
|
+ SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
|
|
+
|
|
|
+ if ("OK".equals(sendSmsResponse.getBody().getCode())) {
|
|
|
+ log.info("账号密码短信发送成功,phone={}, userName={}", phone, userName);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ log.error("账号密码短信发送失败,phone={}, userName={}, error={}",
|
|
|
+ phone, userName, sendSmsResponse.getBody().getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ */
|
|
|
+
|
|
|
+ // 当前实现:仅记录日志
|
|
|
+ log.info("账号密码短信内容:{},发送至:{}", messageContent, phone);
|
|
|
+ return true;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送账号密码短信异常,phone={}, userName={}, error={}", phone, userName, e.getMessage(), e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|