1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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);
    }
}