From 9bd6a19ee855d489dd7598e6c5fb580e7be61bbf Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 30 六月 2020 18:11:53 +0800
Subject: [PATCH] 消息包重新规划
---
fanli/src/main/java/com/yeshi/fanli/service/impl/user/SMSServiceImpl.java | 85 +++++++++++++++++++++++++++++++++++++++---
1 files changed, 78 insertions(+), 7 deletions(-)
diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/SMSServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/SMSServiceImpl.java
index 8cd9a9d..0e73af3 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/SMSServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/SMSServiceImpl.java
@@ -1,5 +1,6 @@
package com.yeshi.fanli.service.impl.user;
+import java.math.BigDecimal;
import java.util.Date;
import javax.annotation.Resource;
@@ -8,12 +9,16 @@
import com.yeshi.fanli.dao.mybatis.SMSHistoryMapper;
import com.yeshi.fanli.entity.bus.user.SMSHistory;
-import com.yeshi.fanli.exception.SMSException;
+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 {
@@ -25,20 +30,35 @@
private RedisManager redisManager;
@Override
- public String sendLoginVCode(String phone) throws SMSException {
+ 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 msgCode = StringUtil.getVerifyCode(6);
- System.out.println(msgCode);
+
+ 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);
// 鍙戦�佺煭淇�
- TencentSMSUtil.sendSingleMsg(phone, msg);
+ // 鍙戦�侀棿闅�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());
@@ -49,20 +69,55 @@
}
@Override
- public void sendBindVCode(String phone) throws SMSException {
+ 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.getRandomCode(6);
+ 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);
@@ -78,4 +133,20 @@
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);
+ }
+
}
--
Gitblit v1.8.0