短信发送接口

This commit is contained in:
张松
2025-02-12 11:34:37 +08:00
parent 59046df4d7
commit 87e8c59cf0
9 changed files with 173 additions and 2 deletions

View File

@@ -10,7 +10,6 @@ import com.czg.account.service.*;
import com.czg.account.vo.LoginVO;
import com.czg.config.RedisCst;
import com.czg.exception.ApiNotPrintException;
import com.czg.exception.CzgException;
import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.service.account.mapper.SysMenuMapper;

View File

@@ -0,0 +1,40 @@
package com.czg.service.account.service.impl;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.SysUser;
import com.czg.account.service.CommonService;
import com.czg.account.service.SysUserService;
import com.czg.config.RedisCst;
import com.czg.exception.ApiNotPrintException;
import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.service.account.util.SmsUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
/**
* @author Administrator
*/
@Service
public class CommonServiceImpl implements CommonService {
@Resource
private SmsUtil smsUtil;
@Resource
private SysUserService sysUserService;
@Resource
private RedisService redisService;
@Override
public Boolean sendSms(String type) {
SysUser sysUser = sysUserService.queryChain().eq(SysUser::getId, StpKit.ADMIN.getLoginIdAsLong()).one();
if (StrUtil.isBlank(sysUser.getPhone())) {
throw new ApiNotPrintException("账号未绑定手机号");
}
int code = RandomUtil.randomInt(100000, 1000000);
redisService.set(RedisCst.SMS_CODE + sysUser.getPhone() + ":" + type, code, 300);
smsUtil.sendCode(sysUser.getPhone(), String.valueOf(code));
return true;
}
}

View File

@@ -0,0 +1,78 @@
package com.czg.service.account.util;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.czg.exception.ApiNotPrintException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 验证码工具类
*
* @author Administrator
*/
@Component
@Slf4j
public class SmsUtil {
/**
* 阿里云key
*/
@Value("${alipay.sms.key}")
private String key;
/**
* 阿里云secret
*/
@Value("${alipay.sms.secret}")
private String secret;
/**
* 短信模板id
*/
@Value("${alipay.sms.templateCode}")
private String templateCode;
public Client createClient() throws Exception {
Config config = new Config();
config.accessKeyId = key;
config.accessKeySecret = secret;
return new Client(config);
}
public void sendCode(String phone, String checkCode) {
try {
Client client = createClient();
// 1.发送短信
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setSignName("银收客")
.setTemplateCode(templateCode)
.setTemplateParam("{\"code\":" + "'" + checkCode + "'" + "}")
.setPhoneNumbers(phone);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
log.info("短信发送请求参数: 手机号: {}, 短信模板: {}, 短信内容: {}", phone, templateCode, checkCode);
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
if (sendSmsResponse.getStatusCode() != 200) {
throw new ApiNotPrintException("短信发送失败");
}
}catch (Exception e) {
log.info("发送短信失败", e);
}
}
public static void main(String[] args) throws Exception {
Config config = new Config();
config.accessKeyId = "LTAI5tPdEfYSZcqHbjCrtPRD";
config.accessKeySecret = "DZjyHBq3nTujF0NMLxnZgsecU8ZCvy";
Client client = new Client(config);
// 1.发送短信
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setSignName("银收客")
.setTemplateCode("SMS_244665149")
.setTemplateParam("{\"code\":" + "'" + "23123" + "'" + "}")
.setPhoneNumbers("19502966242");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
System.out.println(sendSmsResponse);
}
}