yujian
2020-06-28 1e28ac69827ff7578a418a79bd95aff2c6637f5c
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.yeshi.fanli.service.impl.user;
 
import java.math.BigDecimal;
import java.util.Date;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.SMSHistoryMapper;
import com.yeshi.fanli.entity.bus.user.SMSHistory;
import com.yeshi.fanli.exception.config.SMSException;
import com.yeshi.fanli.service.inter.user.SMSService;
import com.yeshi.fanli.util.AliyunSMSUtil;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TencentSMSUtil;
 
import net.sf.json.JSONObject;
 
@Service
public class SMSServiceImpl implements SMSService {
 
    @Resource
    private SMSHistoryMapper smsHistoryMapper;
 
    @Resource
    private RedisManager redisManager;
 
    @Override
    public String sendLoginVCode(String phone, int codeLength) throws SMSException {
        boolean limit = redisManager.isSmsFrequencyLimit(phone, SMSHistory.TYPE_LOGIN);
        if (limit)
            throw new SMSException(1001, "请过60秒再试");
 
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.emptyKey, StringUtil.Md5("sms-login-tencent-latest-" + phone));
 
        String msgCode = StringUtil.getVerifyCode(codeLength);
        // 验证码模板
        String msg = Constant.smsConfig.getSmsLogin().replace("[签名]", Constant.smsConfig.getSmsSign()).replace("[验证码]",
                msgCode);
        // 发送短信
        // 发送间隔2分钟内的采用阿里云短信发送
        String cacheString = redisManager.getCommonString(key);
        if (StringUtil.isNullOrEmpty(cacheString) || Integer.parseInt(cacheString) < 2) {
            TencentSMSUtil.sendSingleMsg(phone, msg);
            redisManager.increase(key);
            redisManager.expire(key, 120);
        } else {
            redisManager.removeCommonString(key);
            // 发送阿里云短信
            JSONObject data = new JSONObject();
            data.put("code", msgCode);
            AliyunSMSUtil.sendSingleMsg(phone, Constant.smsConfig.getAliyunLoginTemplatecode(), data.toString());
        }
        // 缓存
        redisManager.sendSms(phone, SMSHistory.TYPE_LOGIN);
        redisManager.saveSMSVCode(phone, SMSHistory.TYPE_LOGIN, msgCode);
 
        SMSHistory smsHistory = new SMSHistory();
        smsHistory.setContent(msg);
        smsHistory.setCreateTime(new Date());
        smsHistory.setPhone(phone);
        smsHistory.setType(SMSHistory.TYPE_LOGIN);
        smsHistoryMapper.insertSelective(smsHistory);
        return msgCode;
    }
 
    @Override
    public void sendBindVCode(String phone, int codeLength) throws SMSException {
        boolean limit = redisManager.isSmsFrequencyLimit(phone, SMSHistory.TYPE_BIND);
        if (limit)
            throw new SMSException(1001, "请过60秒再试");
        String msgCode = StringUtil.getVerifyCode(codeLength);
 
        // 验证码模板
        String msg = Constant.smsConfig.getSmsBind().replace("[签名]", Constant.smsConfig.getSmsSign()).replace("[验证码]",
                msgCode);
        // 发送短信
        TencentSMSUtil.sendSingleMsg(phone, msg);
 
        // 缓存
        redisManager.sendSms(phone, SMSHistory.TYPE_BIND);
        redisManager.saveSMSVCode(phone, SMSHistory.TYPE_BIND, msgCode);
 
        SMSHistory smsHistory = new SMSHistory();
        smsHistory.setContent(msg);
        smsHistory.setCreateTime(new Date());
        smsHistory.setPhone(phone);
        smsHistory.setType(SMSHistory.TYPE_BIND);
        smsHistoryMapper.insertSelective(smsHistory);
    }
 
    @Override
    public void sendRemoveVCode(String phone, int codeLength) throws SMSException {
        boolean limit = redisManager.isSmsFrequencyLimit(phone, SMSHistory.TYPE_REMVOE);
        if (limit)
            throw new SMSException(1001, "请过60秒再试");
        String msgCode = StringUtil.getVerifyCode(codeLength);
 
        // 验证码模板
        String msg = Constant.smsConfig.getSmsBind().replace("[签名]", Constant.smsConfig.getSmsSign()).replace("[验证码]",
                msgCode);
        // 发送短信
        TencentSMSUtil.sendSingleMsg(phone, msg);
 
        // 缓存
        redisManager.sendSms(phone, SMSHistory.TYPE_REMVOE);
        redisManager.saveSMSVCode(phone, SMSHistory.TYPE_REMVOE, msgCode);
 
        SMSHistory smsHistory = new SMSHistory();
        smsHistory.setContent(msg);
        smsHistory.setCreateTime(new Date());
        smsHistory.setPhone(phone);
        smsHistory.setType(SMSHistory.TYPE_REMVOE);
        smsHistoryMapper.insertSelective(smsHistory);
    }
    
    @Override
    public void sendExtractVCode(String phone) throws SMSException {
        boolean limit = redisManager.isSmsFrequencyLimit(phone, SMSHistory.TYPE_EXTRACT);
        if (limit)
            throw new SMSException(1001, "请过60秒再试");
        String msgCode = StringUtil.getRandomCode(6);
        String msg = Constant.smsConfig.getSmsExtract().replace("[签名]", Constant.smsConfig.getSmsSign())
                .replace("[验证码]", msgCode);
        // 发送短信
        TencentSMSUtil.sendSingleMsg(phone, msg);
        // 缓存
        redisManager.sendSms(phone, SMSHistory.TYPE_EXTRACT);
        redisManager.saveSMSVCode(phone, SMSHistory.TYPE_EXTRACT, msgCode);
    }
 
    @Override
    public void sendExtractSuccessMsg(String phone, BigDecimal money) throws SMSException {
        String msg = Constant.smsConfig.getSmsExtractSuccess().replace("[签名]", Constant.smsConfig.getSmsSign())
                .replace("[金额]", money.toString());
        // 发送短信
        TencentSMSUtil.sendSingleMsg(phone, msg);
    }
 
    @Override
    public void sendExtractFailMsg(String phone, BigDecimal money) throws SMSException {
        String msg = Constant.smsConfig.getSmsExtractFail().replace("[签名]", Constant.smsConfig.getSmsSign())
                .replace("[金额]", money.toString());
        // 发送短信
        TencentSMSUtil.sendSingleMsg(phone, msg);
    }
 
}