From e6df4123693fca526040115475701bcd3e373904 Mon Sep 17 00:00:00 2001
From: vincentlu <t1341870251@gmail.com>
Date: 星期四, 13 二月 2025 13:36:59 +0800
Subject: [PATCH] #

---
 rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java |   40 +++++
 rsf-server/src/main/java/com/vincent/rsf/server/common/config/ConfigProperties.java   |    5 
 rsf-server/src/main/resources/application.yml                                         |    3 
 rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java               |  337 +++++++++++++++++++++--------------------------
 4 files changed, 195 insertions(+), 190 deletions(-)

diff --git a/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java b/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java
index aa90caf..6886d97 100644
--- a/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java
+++ b/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java
@@ -45,7 +45,7 @@
 	private RedisSupport(String name) {
 		try {
 			boolean bAll = true;
-			String[] configKeys={"host","port","max","min","timeout","index"};
+			String[] configKeys = {"host", "port", "max", "min", "timeout", "index"};
 			for(String key : configKeys){
 				if(!ConfigHelper.contains(name + "." + key)){
 					bAll = false;
@@ -53,19 +53,19 @@
 			}
 			if (bAll) {
 				config = new JedisPoolConfig();
-				config.setMaxIdle(ConfigHelper.getInteger(name+".min"));
-		        config.setMaxWaitMillis(ConfigHelper.getLong(name+".timeout"));
+				config.setMaxIdle(ConfigHelper.getInteger(name + ".min"));
+		        config.setMaxWaitMillis(ConfigHelper.getLong(name + ".timeout"));
 		        // 鍦╞orrow涓�涓猨edis瀹炰緥鏃讹紝鏄惁鎻愬墠杩涜validate鎿嶄綔锛涘鏋滀负true锛屽垯寰楀埌鐨刯edis瀹炰緥鍧囨槸鍙敤鐨勶紱
 		        config.setTestOnBorrow(false);
 		        // 褰撳墠jedis杩炴帴鐨勫簱鍙�
-				index = ConfigHelper.getInteger(name+".index");
+				index = ConfigHelper.getInteger(name + ".index");
 				// 瀹炰緥鍖杍edis姹�
-		        String host = ConfigHelper.getString(name+".host");
-		        int port = ConfigHelper.getInteger(name+".port");
-		        int timeout = ConfigHelper.getInteger(name+".timeout");
-		        String password = ConfigHelper.getString(name+".password");
+		        String host = ConfigHelper.getString(name + ".host");
+		        int port = ConfigHelper.getInteger(name + ".port");
+		        int timeout = ConfigHelper.getInteger(name + ".timeout");
+		        String password = ConfigHelper.getString(name + ".password");
 
-				pool = new JedisPool(config,host , port,timeout,password);
+				pool = new JedisPool(config, host , port, timeout, password);
 				Jedis jedis = this.getJedis();
 				this.pool.returnResource(jedis);
 				initialize = true;
@@ -92,7 +92,129 @@
 		return null;
 	}
 
-	// key - value ----------------------------------------------------------------------------------------------------------
+	// key - object ----------------------------------------------------------------------------------------------------------
+
+	public String set(String flag, String key, Object value) {
+		if(!this.initialize) {
+			return null;
+		}
+		if(null == value) {
+			this.delete(flag, key);
+			return null;
+		}
+		Jedis jedis = this.getJedis();
+		try{
+			return jedis.set((flag + LINK + key).getBytes(), Serialize.serialize(value));
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
+		}
+		return null;
+	}
+
+	public String set(String flag, String key, Object value, Integer seconds) {
+		if(!this.initialize) {
+			return null;
+		}
+		if (null == value) {
+			this.delete(flag, key);
+			return null;
+		}
+		Jedis jedis = this.getJedis();
+		try{
+			return jedis.setex((flag + LINK + key).getBytes(), seconds, Serialize.serialize(value));
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
+		}
+		return null;
+	}
+
+	public <T> T get(String flag, String key) {
+		if(!this.initialize) {
+			return null;
+		}
+		Jedis jedis = this.getJedis();
+		try{
+			byte[] bytes = jedis.get((flag + LINK + key).getBytes());
+			if(bytes == null || bytes.length == 0 ) {
+				return null;
+			}
+			return (T) Serialize.unSerialize(bytes);
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
+		}
+		return null;
+	}
+
+	public Long delete(String flag, String key) {
+		if(!this.initialize) {
+			return null;
+		}
+		Jedis jedis = this.getJedis();
+		try{
+			return jedis.del((flag + LINK + key).getBytes());
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
+		}
+		return null;
+	}
+
+	public Long clear(String flag) {
+		if(!this.initialize) {
+			return null;
+		}
+		Jedis jedis = this.getJedis();
+		this.setValue(flag, "CLEARING", "true");
+		try{
+			Object returnValue = jedis.eval("local keys = redis.call('keys', ARGV[1]) for i=1,#keys,1000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return #keys",0,flag + LINK + "*");
+			return Long.parseLong(String.valueOf(returnValue));
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
+		}
+		return null;
+	}
+
+	// 涓哄凡瀛樺湪鐨刱ey璁剧疆杩囨湡鏃堕棿 - 绉�
+	public void setExpire(String flag, String key, int seconds){
+		if(!this.initialize) {
+			return;
+		}
+		Jedis jedis = this.getJedis();
+		try{
+			jedis.expire((flag + LINK + key).getBytes(), seconds);
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
+		}
+	}
+
+	// 涓哄凡瀛樺湪鐨刱ey璁剧疆杩囨湡鏃堕棿 - 鍏蜂綋鍒版椂闂存埑 锛堢锛�
+	public void setExpireAt(String flag, String key, Date toTime){
+		if(!this.initialize) {
+			return;
+		}
+		Jedis jedis = this.getJedis();
+		try{
+			jedis.expireAt((flag + LINK + key).getBytes(), toTime.getTime()/1000);
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
+		}
+	}
+
+	// 鑾峰彇杩囨湡鍓╀綑鏃堕棿锛堢锛� ttl == -1 娌℃湁璁剧疆杩囨湡鏃堕棿锛� ttl == -2 key涓嶅瓨鍦�
+	public Long getExpire(String flag, String key) {
+		if(!this.initialize) {
+			return null;
+		}
+		Jedis jedis = this.getJedis();
+		try{
+			return jedis.ttl((flag + LINK + key).getBytes());
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
+		}
+		return null;
+	}
+
+
+	// key - string ----------------------------------------------------------------------------------------------------------
 
 	public String setValue(String flag, String key, String value) {
 		if(!this.initialize) {
@@ -107,7 +229,7 @@
 		return null;
 	}
 
-	public String setValue(String flag, String key, String value,Integer seconds) {
+	public String setValue(String flag, String key, String value, Integer seconds) {
 		if(!this.initialize) {
 			return null;
 		}
@@ -180,7 +302,7 @@
 		}
 	}
 
-	public void setValueExpireTime(String flag, String key,Date atTime){
+	public void setValueExpireAt(String flag, String key,Date atTime){
 		if(!this.initialize) {
 			return;
 		}
@@ -282,7 +404,7 @@
 		}
 	}
 
-	public void setMapExpireTime(String name,Date atTime){
+	public void setMapExpireAt(String name,Date atTime){
 		if(!this.initialize) {
 			return;
 		}
@@ -358,7 +480,7 @@
 		}
 	}
 
-	public void setListExpireTime(String name, Date atTime){
+	public void setListExpireAt(String name, Date atTime){
 		if(!this.initialize) {
 			return;
 		}
@@ -370,195 +492,36 @@
 		}
 	}
 
-
-
-
-
-
-
-
-
-
-
-	public Object setObject(String flag,String key, Object value) {
-		if(!this.initialize)return null;
-
-		if(value==null){
-			this.deleteObject(flag, key);
-			return null;
-		}
-		Jedis jedis = this.getJedis();
-		try{
-			String result = jedis.set(("OBJ."+flag+"."+key).getBytes(), Serialize.serialize(value));
-			return value;
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
-		}
-		return value;
-	}
-	public Object setObject(String flag,String key, Object value,Integer seconds) {
-		if(!this.initialize)return null;
-
-		if(value==null){
-			this.deleteObject(flag, key);
-			return null;
-		}
-		Jedis jedis = this.getJedis();
-		try{
-			String result = jedis.setex(("OBJ."+flag+"."+key).getBytes(),seconds,Serialize.serialize(value));
-			return value;
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
-		}
-		return value;
-	}
-	public <T> T getObject(String flag,String key) {
-		if(!this.initialize)return null;
-		Jedis jedis = this.getJedis();
-		try{
-			byte[] bytes = jedis.get(("OBJ."+flag+"."+key).getBytes());
-			if(bytes==null||bytes.length==0)
-				return null;
-			T returnValue = (T) Serialize.unSerialize(bytes);
-			return (T) returnValue;
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
-		}
-		return null;
-	}
-
-	public <T> Long deleteObject(String flag, String key) {
-		if(!this.initialize)return null;
-
-		Jedis jedis = this.getJedis();
-		try{
-			Long returnValue = jedis.del(("OBJ."+flag+"."+key).getBytes());
-			return returnValue;
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
-		}
-		return null;
-	}
-
-
-	public <T> Long clearObject(String flag) {
-		if(!this.initialize)return null;
-
-		this.setObject(flag, "CLEARING", "true");
-		Jedis jedis = this.getJedis();
-		try{
-			//EVAL "return redis.call('del', unpack(redis.call('keys', 'OBJ.*')))" 0
-			//EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 OBJ.*
-			//Object returnValue = jedis.eval("return redis.call('del', unpack(redis.call('keys', ARGV[1])))",0,"OBJ."+flag+"."+"*");
-			Object returnValue = jedis.eval("local keys = redis.call('keys', ARGV[1]) for i=1,#keys,1000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return #keys",0,"OBJ."+flag+"."+"*");
-			return Long.parseLong(String.valueOf(returnValue));
-		}catch(Exception ex){
-			////Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
-		}
-		return null;
-	}
-
-
-	public void setObjectExpire(String flag, String key,int seconds){
-		if(!this.initialize)return;
-		Jedis jedis = this.getJedis();
-		try{
-			jedis.expire(("OBJ."+flag+"."+key).getBytes(), seconds);
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
-		}
-	}
-
-
-	public void setObjectExpireTime(String flag, String key,Date toTime){
-		if(!this.initialize)return;
-		Jedis jedis = this.getJedis();
-		try{
-			jedis.expireAt(("OBJ."+flag+"."+key).getBytes(), toTime.getTime()/1000);
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
-		}
-	}
-
-
-
-
-
-
-	public String[] getKeys(String pattern) {
-		if(!this.initialize)return null;
-
-		Jedis jedis = this.getJedis();
-		try{
-			return jedis.keys(pattern).toArray(new String[]{});
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
-		}
-		return null;
-	}
-
-
+	// count ----------------------------------------------------------------------------------------------------------
 
 	public Long incr(String key) {
-		if(!this.initialize)return null;
+		if(!this.initialize) {
+			return null;
+		}
 		Jedis jedis = this.getJedis();
 		try{
-			Long returnValue = jedis.incr("DI."+key);
-			return returnValue;
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
+            return jedis.incr("COUNT." + key);
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
 		}
 		return null;
 	}
 
 	public Long decr(String key) {
-		if(!this.initialize)return null;
+		if(!this.initialize) {
+			return null;
+		}
 		Jedis jedis = this.getJedis();
 		try{
-			Long returnValue = jedis.decr("DI."+key);
-			return returnValue;
-		}catch(Exception ex){
-			//Utils.trace(ex.getMessage());
-		}finally{
-			if(jedis!=null)
-				this.pool.returnResource(jedis);
+            return jedis.decr("COUNT." + key);
+		} catch (Exception e) {
+			log.error(this.getClass().getSimpleName(), e);
 		}
 		return null;
 	}
 
-
-
 	public static void main(String...strings){
-		Date start = new Date();
-		RedisSupport r = RedisSupport.getRedis("lazy.redis.0");
-		r.clearObject("aaa");
+		RedisSupport redis = RedisSupport.defaultRedisSupport;
 	}
 
 }
diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/common/config/ConfigProperties.java b/rsf-server/src/main/java/com/vincent/rsf/server/common/config/ConfigProperties.java
index 2a0e7ae..2510154 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/common/config/ConfigProperties.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/common/config/ConfigProperties.java
@@ -60,6 +60,11 @@
      */
     private Integer codeLength = 4;
 
+    /**
+     * 楠岃瘉鐮佹湁鏁堟湡 锛� 绉� 锛�
+     */
+    private Integer codeTime = 300;
+
     public List<String> getSuperUserList() {
         return Arrays.stream(superUsername.split(",")).collect(Collectors.toList());
     }
diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java b/rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java
index 0eca7ad..415079f 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java
@@ -1,10 +1,12 @@
 package com.vincent.rsf.server.system.controller;
 
 import com.vincent.rsf.common.enums.SystemModeType;
+import com.vincent.rsf.common.utils.RedisSupport;
 import com.vincent.rsf.common.utils.Utils;
 import com.vincent.rsf.framework.common.BaseRes;
 import com.vincent.rsf.framework.common.Cools;
 import com.vincent.rsf.framework.common.R;
+import com.vincent.rsf.framework.exception.CoolException;
 import com.vincent.rsf.server.common.annotation.OperationLog;
 import com.vincent.rsf.server.common.config.ConfigProperties;
 import com.vincent.rsf.server.common.security.JwtSubject;
@@ -31,6 +33,7 @@
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
+import java.util.Date;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -41,6 +44,8 @@
  */
 @RestController
 public class AuthController extends BaseController {
+
+    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
 
     @Resource
     private ConfigProperties configProperties;
@@ -82,13 +87,45 @@
         if (Cools.isEmpty(email)) {
             return R.parse(BaseRes.PARAM);
         }
+        if (null != userService.getByEmail(email, null)) {
+            return R.error("Email Already Exist");
+        }
+        if (redis.getExpire(EmailType.REGISTER_VERIFY.toString(), email) > (configProperties.getCodeTime() - 60)) {
+            return R.error("Please don't request code too frequently.");
+        }
         String code = Utils.randomNumbers(configProperties.getCodeLength());
-        emailService.sendEmail(email, EmailType.REGISTER_VERIFY, Cools.add("code", code));
+        if (emailService.sendEmail(email, EmailType.REGISTER_VERIFY, Cools.add("code", code))) {
+            redis.setValue(EmailType.REGISTER_VERIFY.toString(), email, code, configProperties.getCodeTime());
+        }
         return R.ok();
     }
 
+    @OperationLog("Register")
     @PostMapping("/register")
     public R register(@RequestBody RegisterParam param, HttpServletRequest request) {
+        if (Cools.isEmpty(param.getUsername(), param.getPassword(), param.getEmail(), param.getCode())) {
+            return R.parse(BaseRes.PARAM);
+        }
+        String cacheCode = redis.getValue(EmailType.REGISTER_VERIFY.toString(), param.getEmail());
+        if (Cools.isEmpty(cacheCode)) {
+            return R.error("The verification code has expired.");
+        }
+        if (!cacheCode.equals(param.getCode())) {
+            return R.error("The verification code is incorrect.");
+        }
+
+
+        User user = new User();
+        user.setUsername(param.getUsername());
+        user.setNickname(param.getUsername());
+        user.setPassword(userService.encodePassword(param.getPassword()));
+        user.setEmail(param.getEmail());
+        user.setEmailVerified(1);
+        user.setStatus(StatusType.ENABLE.val);
+        user.setCreateTime(new Date());
+        if (!userService.save(user)) {
+            throw new CoolException("");
+        }
 
         return R.ok();
     }
@@ -132,7 +169,6 @@
     @PostMapping("/auth/user")
     public R updateInfo(@RequestBody User user) {
         user.setId(getLoginUserId());
-        // 涓嶈兘淇敼鐨勫瓧娈�
         user.setUsername(null);
         user.setPassword(null);
         user.setEmailVerified(null);
diff --git a/rsf-server/src/main/resources/application.yml b/rsf-server/src/main/resources/application.yml
index 8533730..d237769 100644
--- a/rsf-server/src/main/resources/application.yml
+++ b/rsf-server/src/main/resources/application.yml
@@ -33,4 +33,5 @@
   system-mode: OFFLINE
   token-key: KUHSMcYQ4lePt3r6bckz0P13cBJyoonYqInThvQlUnbsFCIcCcZZAbWZ6UNFztYNYPhGdy6eyb8WdIz8FU2Cz396TyTJk3NI2rtXMHBOehRb4WWJ4MdYVVg2oWPyqRQ2
   super-username: root
-  code-length: 6
\ No newline at end of file
+  code-length: 6
+  code-time: 300
\ No newline at end of file

--
Gitblit v1.9.1