admin
2024-10-30 010ef2a907e66efd4702443c06cdd18f8a7ffa5b
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
109
110
111
112
package com.yeshi.buwan.service.imp;
 
import com.github.qcloudsms.SmsSingleSenderResult;
import com.yeshi.buwan.dto.config.TencentSMSConfig;
import com.yeshi.buwan.exception.SMSException;
import com.yeshi.buwan.service.inter.SMSService;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.RedisKeyEnum;
import com.yeshi.buwan.util.RedisManager;
import com.yeshi.buwan.util.StringUtil;
import org.springframework.stereotype.Service;
import org.yeshi.utils.sms.TencentSMSUtil;
 
import javax.annotation.Resource;
 
@Service
public class SMSServiceImpl implements SMSService {
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private DetailSystemConfigService detailSystemConfigService;
 
    private void sendSMSCode(String phone, String msg) throws SMSException {
 
 
        TencentSMSConfig tencentSMSConfig = Constant.tencentSMSConfig;
 
        SmsSingleSenderResult result = TencentSMSUtil.sendSingleMsg(Integer.parseInt(tencentSMSConfig.getAppId()), tencentSMSConfig.getAppKey(), phone, msg);
        if (result == null)
            throw new SMSException(2, "短信发送失败");
        if (result.result == 1025) {
            throw new SMSException(result.result, "今日验证码发送超限,请明日再试");
        } else if (result.result != 0) {// 发送失败
            throw new SMSException(result.result, "短信发送失败");
        }
 
 
    }
 
    @Override
    public void sendBindVCode(String uid, String phone, int codeLength) throws SMSException {
        String limitKey = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSLIMIT, uid + "");
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + 0);
        if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(limitKey)))
            throw new SMSException(1001, "请过60秒再试");
 
        String msgCode = StringUtil.getNumberVerifyCode(codeLength);
        // 验证码模板
        TencentSMSConfig tencentSMSConfig = Constant.tencentSMSConfig;
        String msg = tencentSMSConfig.getContentBind().replace("[签名]", tencentSMSConfig.getSign()).replace("[验证码]",
                msgCode);
        sendSMSCode(phone, msg);
        //保存验证码
        redisManager.cacheCommonString(key, msgCode, 60 * 5);
        //60s后再发送
        redisManager.cacheCommonString(limitKey, "1", 60);
    }
 
    @Override
    public boolean verifyBindVCode(String phone, String code) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + 0);
        String cacheCode = redisManager.getCommonString(key);
        if (cacheCode != null && cacheCode.equalsIgnoreCase(code)) {
            redisManager.removeCommonString(key);
            return true;
        }
        return false;
    }
 
    @Override
    public void sendLoginVCode(String phone, int codeLength, String detailSystemId, int version) throws SMSException {
        String limitKey = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSLIMIT, phone);
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + 1);
        if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(limitKey)))
            throw new SMSException(1001, "请过60秒再试");
 
        String msgCode = StringUtil.getNumberVerifyCode(codeLength);
        // 验证码模板
        TencentSMSConfig tencentSMSConfig = Constant.tencentSMSConfig;
        String msg = tencentSMSConfig.getContentBind().replace("[签名]", detailSystemConfigService.getConfigValueByKey("tencent_sms_sign", detailSystemId, version)).replace("[验证码]",
                msgCode);
        sendSMSCode(phone, msg);
        //保存验证码
        redisManager.cacheCommonString(key, msgCode, 60 * 5);
        //60s后再发送
        redisManager.cacheCommonString(limitKey, "1", 60);
    }
 
    @Override
    public boolean verifyLoginVCode(String phone, String code, String detailSystemId, int version) {
 
        String value = detailSystemConfigService.getConfigValueByKey("test_phone_account", detailSystemId, version);
 
//测试账号
        if (value != null) {
            if (phone.equalsIgnoreCase(value.split("#")[0]) && code.equalsIgnoreCase(value.split("#")[1])) {
                return true;
            }
        }
 
 
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + 1);
        String cacheCode = redisManager.getCommonString(key);
        if (cacheCode != null && cacheCode.equalsIgnoreCase(code)) {
            redisManager.removeCommonString(key);
            return true;
        }
        return false;
    }
}