package com.everyday.word.service.impl;
|
|
import com.everyday.word.config.RedisKeyEnum;
|
import com.everyday.word.entity.SystemEnum;
|
import com.everyday.word.entity.config.SystemConfigKeyEnum;
|
import com.everyday.word.exception.SMSException;
|
import com.everyday.word.service.RedisManager;
|
import com.everyday.word.service.inter.SMSService;
|
import com.everyday.word.service.inter.config.SystemConfigService;
|
import org.springframework.stereotype.Service;
|
import org.yeshi.utils.StringUtil;
|
import org.yeshi.utils.sms.TencentSMSUtil;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
|
/**
|
* @author hxh
|
* @title: SMSServiceImpl
|
* @description: 短信服务
|
* @date 2025/2/11 17:04
|
*/
|
@Service
|
public class SMSServiceImpl implements SMSService {
|
|
@Resource
|
private RedisManager redisManager;
|
|
@Resource
|
private SystemConfigService systemConfigService;
|
|
/**
|
* @return boolean
|
* @author hxh
|
* @description 是否触发验证码发送频率
|
* @date 17:17 2025/2/11
|
* @param: phone
|
* @param: sence
|
**/
|
private boolean isSmsFrequencyLimit(String phone, int sence) {
|
String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMS, phone + "-" + sence);
|
String value = redisManager.getCommonString(key);
|
if (StringUtil.isNullOrEmpty(value)) {
|
return false;
|
} else {
|
return true;
|
}
|
}
|
|
private String getVerifyCode(int length) {
|
String sts = "0123456789";
|
String code = "";
|
|
for (int i = 0; i < length; ++i) {
|
int p = (int) (Math.random() * 10.0D);
|
code = code + sts.charAt(p);
|
}
|
|
return code;
|
}
|
|
private void sendSmsSuccess(String phone, int type) {
|
String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMS, phone + "-" + type);
|
redisManager.cacheCommonString(key, "1", 10);
|
}
|
|
|
private void saveSMSVCode(String phone, int type, String code) {
|
String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type);
|
// 保存2分钟
|
redisManager.cacheCommonString(key, code, 120);
|
}
|
|
|
@Override
|
public String sendVerifyCode(SystemEnum system, String phone, int codeLength, int sence) throws SMSException {
|
boolean limit = isSmsFrequencyLimit(phone, sence);
|
if (limit) {
|
throw new SMSException(1001, "请过60秒再试");
|
}
|
String msgCode = getVerifyCode(codeLength);
|
String smsAlias = systemConfigService.getValueCache(SystemConfigKeyEnum.smsAlias, system);
|
if (StringUtil.isNullOrEmpty(smsAlias)) {
|
throw new SMSException(1001, "短信签名为空");
|
}
|
String smsTemplate = systemConfigService.getValueCache(SystemConfigKeyEnum.smsAlias, system);
|
if (StringUtil.isNullOrEmpty(smsTemplate)) {
|
throw new SMSException(1001, "短信模版为空");
|
}
|
// 验证码模板
|
String msg = smsTemplate.replace("[签名]", smsAlias).replace("[验证码]",
|
msgCode);
|
// 发送短信
|
String appId = systemConfigService.getValueCache(SystemConfigKeyEnum.smsAppId, system);
|
String appKey = systemConfigService.getValueCache(SystemConfigKeyEnum.smsAppKey, system);
|
TencentSMSUtil.sendSingleMsg(Integer.parseInt(appId), appKey, phone, msg);
|
// 缓存
|
sendSmsSuccess(phone, sence);
|
saveSMSVCode(phone, sence, msgCode);
|
return msgCode;
|
}
|
|
@Override
|
public String getVerifyCode(SystemEnum system, String phone, int sence) {
|
String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + sence);
|
return redisManager.getCommonString(key);
|
}
|
}
|