admin
2021-12-09 f609ca35ee2946acd0ff04b7ac1aa61f75a2e4a1
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
package com.ks.app.service.manager;
 
import com.ks.app.entity.SystemEnum;
import com.ks.app.entity.config.SystemConfigKey;
import com.ks.app.service.inter.config.SystemConfigService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.yeshi.utils.StringUtil;
 
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
 
/**
 * @author hxh
 * @title: VerifyCodeManager
 * @description: 验证码管理
 * @date 2021/11/15 18:38
 */
@Component
public class VerifyCodeManager {
    @Resource
    private RedisTemplate<String, String> redisTemplate;
 
 
    @Resource
    private SystemConfigService systemConfigService;
 
    /**
     * @return void
     * @author hxh
     * @description 发送电话验证码
     * @date 18:40 2021/11/15
     * @param: phone
     * @param: msg 变量为:{验证码}
     **/
    public void sendSMSSuccess(SystemEnum system, String phone, String code) {
 
        redisTemplate.opsForValue().set(String.format("vcp-%s-%s", system.name(), phone), code, 120, TimeUnit.SECONDS);
 
    }
 
    /**
     * @return void
     * @author hxh
     * @description 发送邮箱验证码
     * @date 18:41 2021/11/15
     * @param: email
     * @param: title
     * @param: content
     **/
    public void sendEmailCodeSuccess(String email, String code) {
        redisTemplate.opsForValue().set("v-c-e-" + StringUtil.Md5(email), code, 120, TimeUnit.SECONDS);
    }
 
 
    /**
     * @return boolean
     * @author hxh
     * @description 电话验证码是否正确
     * @date 19:05 2021/11/15
     * @param: phone
     * @param: code
     **/
    public boolean isPhoneCodeRight(SystemEnum system, String phone, String code) {
 
        String value = systemConfigService.getValueCache(system, SystemConfigKey.testAccount);
        //测试账号
        if (!StringUtil.isNullOrEmpty(value)) {
            String[] sts = value.split("#");
            if (sts.length > 1 && sts[0].equalsIgnoreCase(phone) && sts[1].equalsIgnoreCase(code)) {
                return true;
            }
        }
 
 
        String oldCode = redisTemplate.opsForValue().get(String.format("vcp-%s-%s", system.name(), phone));
        return oldCode != null && oldCode.equalsIgnoreCase(code);
//        return true;
    }
 
    /**
     * @return boolean
     * @author hxh
     * @description /邮箱验证码是否正确
     * @date 19:05 2021/11/15
     * @param: email
     * @param: code
     **/
    public boolean isEMailCodeRight(String email, String code) {
        Object oldCode = redisTemplate.opsForValue().get("v-c-e-" + StringUtil.Md5(email));
        return oldCode != null && oldCode.toString().equalsIgnoreCase(code);
    }
 
 
}