yl-backend/src/main/java/com/guwan/backend/service/impl/AliyunSmsServiceImpl.java

55 lines
1.8 KiB
Java

package com.guwan.backend.service.impl;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import com.guwan.backend.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
@Slf4j
public class AliyunSmsServiceImpl implements SmsService {
@Value("${aliyun.sms.access-key-id}")
private String accessKeyId;
@Value("${aliyun.sms.access-key-secret}")
private String accessKeySecret;
@Value("${aliyun.sms.sign-name}")
private String signName;
@Value("${aliyun.sms.template-code}")
private String templateCode;
private Client createClient() throws Exception {
Config config = new Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret)
.setEndpoint("dysmsapi.aliyuncs.com");
return new Client(config);
}
@Override
public void sendVerificationCode(String phone, String code) {
try {
Client client = createClient();
SendSmsRequest request = new SendSmsRequest()
.setPhoneNumbers(phone)
.setSignName(signName)
.setTemplateCode(templateCode)
.setTemplateParam("{\"code\":\"" + code + "\"}");
client.sendSms(request);
log.info("短信验证码发送成功:{} -> {}", phone, code);
} catch (Exception e) {
log.error("短信发送失败:" + e.getMessage(), e);
throw new RuntimeException("短信发送失败", e);
}
}
@Override
public void sendNotification(String phone, String content) {
// 实现通知短信发送
}
}