diff --git a/tamguo-modules-core/src/main/java/com/tamguo/config/redis/CacheService.java b/tamguo-modules-core/src/main/java/com/tamguo/config/redis/CacheService.java index 8a8c07d..445b490 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/config/redis/CacheService.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/config/redis/CacheService.java @@ -14,19 +14,23 @@ import redis.clients.jedis.ShardedJedis; /** * 缓存中心 - * + * */ @Service("cacheService") public class CacheService { + // 定义 Redis 键的前缀 private final static String REDIS_PRE_KEY = "TAMGUO:"; + // 创建对象序列化工具 private SerializeTranscoder objectSerialize = new ObjectUtil(); @Autowired private RedisXMLConfigure redisXMLConfigure; /** - * - * @Title: get @Description: @param @return String 返回类型 @throws + * 获取指定键的值 + * + * @param key 键 + * @return 值 */ public String get(String key) { key = getPreKey(key); @@ -40,8 +44,10 @@ public class CacheService { } /** - * - * @Title: set @Description: @param @return void 返回类型 @throws + * 设置指定键的值 + * + * @param key 键 + * @param value 值 */ public void set(String key, String value) { key = getPreKey(key); @@ -55,16 +61,11 @@ public class CacheService { } /** - * - * set 设置带过期时间的字符缓存 - * - * @param key - * @param value - * @param time - * 过期时间,秒 - * @description - * @exception @since - * 1.0.0 + * 设置指定键的值,并指定过期时间(单位:秒) + * + * @param key 键 + * @param value 值 + * @param time 过期时间(单位:秒) */ public void set(String key, String value, int time) { key = getPreKey(key); @@ -79,10 +80,10 @@ public class CacheService { } /** - * redis中存放对象 - * - * @param key 对象key - * @param value 可序列化的对象 + * 存储对象到 Redis + * + * @param key 键 + * @param value 对象 */ public void setObject(String key, Object value) { key = getPreKey(key); @@ -93,16 +94,16 @@ public class CacheService { } catch (Exception ex) { ex.printStackTrace(); } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } } /** - * 设置过期时间存储对象 - * - * @param key 对象key - * @param value 对象值 - * @param time 过期时间 秒 + * 存储对象到 Redis,并设置过期时间(单位:秒) + * + * @param key 键 + * @param value 对象 + * @param time 过期时间(单位:秒) */ public void setObject(String key, Object value, int time) { key = getPreKey(key); @@ -113,15 +114,15 @@ public class CacheService { } catch (Exception ex) { ex.printStackTrace(); } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } } /** - * 获取存储的对象 - * - * @param key 对象key - * @return 存储的对象 + * 获取 Redis 中存储的对象 + * + * @param key 键 + * @return 对象 */ public Object getObject(String key) { key = getPreKey(key); @@ -141,10 +142,10 @@ public class CacheService { } /** - * 删除一个对象 - * - * @param key 对象key值 - * @return + * 删除 Redis 中存储的对象 + * + * @param key 键 + * @return 是否删除成功 */ public boolean deleteObject(String key) { key = getPreKey(key); @@ -155,15 +156,16 @@ public class CacheService { } catch (Exception ex) { ex.printStackTrace(); } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } return false; } /** - * - * @Title: isExist @Description: 判断key是否存在 @param @return boolean - * 返回类型 @throws + * 判断指定键是否存在 + * + * @param key 键 + * @return 是否存在 */ public boolean isExist(String key) { key = getPreKey(key); @@ -179,10 +181,22 @@ public class CacheService { return false; } + /** + * 判断指定键是否不存在 + * + * @param key 键 + * @return 是否不存在 + */ public boolean notExist(String key) { - return !isExist(key); + return!isExist(key); } + /** + * 删除指定键的值 + * + * @param key 键 + * @return 是否删除成功 + */ public boolean delete(String key) { key = getPreKey(key); ShardedJedis conn = null; @@ -198,11 +212,11 @@ public class CacheService { } /** - * 关于 redis list的操作 将 值 value 插入到列表 key 的表尾(最右边)。 - * - * @param key - * @param value - * @return + * 将值插入到列表的末尾 + * + * @param key 键 + * @param value 值 + * @return 列表的长度 */ public long putToListEnd(String key, String value) { key = getPreKey(key); @@ -217,28 +231,25 @@ public class CacheService { } /** - * 将value插入集合key的尾部, 并设置过期时间 - * - * @author zhangxin - * @param key - * @param value - * @param seconds - * @param score - * @return long 被成功添加的新成员的数量,不包括那些被更新的、已经存在的成员 + * 将值插入到有序集合中,并设置过期时间 + * + * @param key 键 + * @param value 值 + * @param seconds 过期时间(单位:秒) + * @param score 分数 + * @return 添加的新成员数量 */ public long addToSortedSetAndExpire(String key, String value, int seconds, double score) { return addToSortedSet(key, value, seconds, true, score); } - - + /** - * 将value插入集合key的尾部 增加value的score - * - * @author zhangxin - * @param key - * @param value - * @param score - * @return long 被成功添加的新成员的分数 + * 将值插入到有序集合中,并增加其分数 + * + * @param key 键 + * @param value 值 + * @param score 分数 + * @return 添加的新成员的分数 */ public double addToSortedSetScore(String key, String value, double score) { key = getPreKey(key); @@ -248,47 +259,28 @@ public class CacheService { Double zincrby = conn.zincrby(key, score, value); return zincrby; } finally { - redisXMLConfigure.closeConnection(conn); - } - } - - /** - * 获取member的Score - * @param key - * @param value - * @return - */ - public Double getMemberScore(String key, String member) { - key = getPreKey(key); - ShardedJedis conn = null; - try { - conn = redisXMLConfigure.getConnection(); - Double zscore = conn.zscore(key, member); - return zscore == null ? 0 : zscore; - } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } } - /** - * 将value插入集合key的尾部, 不设置过期时间 - * - * @author zhangxin - * @param key - * @param value - * @param score - * @return long 被成功添加的新成员的数量,不包括那些被更新的、已经存在的成员 + * 将值插入到有序集合中 + * + * @param key 键 + * @param value 值 + * @param score 分数 + * @return 添加的新成员数量 */ public long addToSortedSet(String key, String value, double score) { return addToSortedSet(key, value, -1, false, score); } - - + /** - * 判断member在集合里是否存在 - * - * @return isExist 存在 true 不存在 + * 判断有序集合中是否存在指定成员 + * + * @param key 键 + * @param member 成员 + * @return 是否存在 */ public boolean isExistSortedSet(String key, String member) { key = getPreKey(key); @@ -296,16 +288,18 @@ public class CacheService { try { conn = redisXMLConfigure.getConnection(); Long zrank = conn.zrank(key, member); - return zrank != null; + return zrank!= null; } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } } - + /** - * 删除member - * - * @return isExist 存在 true 不存在 + * 删除有序集合中的指定成员 + * + * @param key 键 + * @param member 成员 + * @return 是否删除成功 */ public boolean delSortedSetMember(String key, String[] member) { key = getPreKey(key); @@ -320,9 +314,14 @@ public class CacheService { } /** - * 将value插入集合key的尾部, 对于setExpire为false的情况, seconds无效 - * - * @return 被成功添加的新成员的数量,不包括那些被更新的、已经存在的成员 + * 将值插入到有序集合中,并设置过期时间(可选) + * + * @param key 键 + * @param value 值 + * @param seconds 过期时间(单位:秒) + * @param setExpire 是否设置过期时间 + * @param score 分数 + * @return 添加的新成员数量 */ private long addToSortedSet(String key, String value, int seconds, boolean setExpire, double score) { key = getPreKey(key); @@ -335,19 +334,17 @@ public class CacheService { } return addNum; } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } } /** - * 按score降序分页获取有序集合中内容 - * - * @author zhangxin - * @param key - * @param pageNo - * 首页从1开始 - * @param pageSize - * @return Set + * 按分数降序分页获取有序集合中的内容 + * + * @param key 键 + * @param pageNo 页码(从 1 开始) + * @param pageSize 每页大小 + * @return 有序集合的内容 */ public Set getSortedSetByPage(String key, int pageNo, int pageSize) { key = getPreKey(key); @@ -365,11 +362,17 @@ public class CacheService { } catch (Exception ex) { ex.printStackTrace(); } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } return null; } + /** + * 获取列表的表头元素 + * + * @param key 键 + * @return 列表的表头元素 + */ public List getListHead(String key) { key = getPreKey(key); ShardedJedis conn = null; @@ -386,12 +389,12 @@ public class CacheService { } /** - * 存储map - * - * @param key 键值 - * @param field map field - * @param value map value - * @return if filed exist return 0 else return 1 + * 存储 Map 到 Redis + * + * @param key 键 + * @param field Map 的字段 + * @param value Map 的值 + * @return 如果字段已存在返回 0,否则返回 1 */ public Long hset(String key, String field, String value) { key = getPreKey(key); @@ -400,10 +403,17 @@ public class CacheService { conn = redisXMLConfigure.getConnection(); return conn.hset(key, field, value); } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } } + /** + * 存储 Map 到 Redis + * + * @param key 键 + * @param values Map + * @return 设置结果 + */ public String hset(String key, Map values) { key = getPreKey(key); ShardedJedis conn = null; @@ -411,10 +421,18 @@ public class CacheService { conn = redisXMLConfigure.getConnection(); return conn.hmset(key, values); } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } } + /** + * 存储 Map 到 Redis,并设置过期时间 + * + * @param key 键 + * @param values Map + * @param time 过期时间(单位:秒) + * @return 设置结果 + */ public String hset(String key, Map values, int time) { key = getPreKey(key); ShardedJedis conn = null; @@ -429,28 +447,28 @@ public class CacheService { } /** - * 得到map中存储的field值 - * - * @param key 键值 - * @param field map field - * @return + * 获取 Map 中指定字段的值 + * + * @param key 键 + * @param field 字段 + * @return 值 */ public String hget(String key, String field) { key = getPreKey(key); ShardedJedis conn = null; try { - conn = redisXMLConfigure.getConnection(); + conn = redisXML Configure.getConnection(); return conn.hget(key, field); } finally { - redisXMLConfigure.closeConnection(conn); + redisXML Configure.closeConnection(conn); } } /** - * 名称为key的string减1操作 - * - * @param key - * @return + * 将名称为 key 的字符串减 1 + * + * @param key 键 + * @return 减 1 后的结果 */ public Long decr(String key) { key = getPreKey(key); @@ -462,24 +480,30 @@ public class CacheService { redisXMLConfigure.closeConnection(conn); } } - + /** - * 名称为key的string加1操作 - * - * @param key - * @return + * 将名称为 key 的字符串加 1 + * + * @param key 键 + * @return 加 1 后的结果 */ public Long incr(String key) { key = getPreKey(key); ShardedJedis conn = null; try { - conn = redisXMLConfigure.getConnection(); + conn = redisXML Configure.getConnection(); return conn.incr(key); } finally { redisXMLConfigure.closeConnection(conn); } } + /** + * 获取 Redis 键的完整路径 + * + * @param key 键 + * @return 完整路径 + */ private String getPreKey(String key) { String temp_pre = redisXMLConfigure.getPreKey(); if (null == temp_pre) { @@ -487,5 +511,4 @@ public class CacheService { } return temp_pre + key; } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/config/redis/PoolConfigBean.java b/tamguo-modules-core/src/main/java/com/tamguo/config/redis/PoolConfigBean.java index c1b9bc1..92407c1 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/config/redis/PoolConfigBean.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/config/redis/PoolConfigBean.java @@ -1,13 +1,28 @@ package com.tamguo.config.redis; +/** + * PoolConfigBean 类,用于配置 Redis 连接池 + */ public class PoolConfigBean { + // 最大活跃连接数 private int max_active; + // 最大空闲连接数 private int max_idle; + // 最大等待时间(毫秒) private long max_wait; + /** + * 无参构造函数 + */ public PoolConfigBean() { } + /** + * 全参构造函数 + * @param max_active 最大活跃连接数 + * @param max_idle 最大空闲连接数 + * @param max_wait 最大等待时间(毫秒) + */ public PoolConfigBean(int max_active, int max_idle, long max_wait) { super(); this.max_active = max_active; @@ -15,33 +30,60 @@ public class PoolConfigBean { this.max_wait = max_wait; } + /** + * 获取最大活跃连接数 + * @return 最大活跃连接数 + */ public int getMax_active() { return max_active; } + /** + * 设置最大活跃连接数 + * @param max_active 最大活跃连接数 + */ public void setMax_active(int max_active) { this.max_active = max_active; } + /** + * 获取最大空闲连接数 + * @return 最大空闲连接数 + */ public int getMax_idle() { return max_idle; } + /** + * 设置最大空闲连接数 + * @param max_idle 最大空闲连接数 + */ public void setMax_idle(int max_idle) { this.max_idle = max_idle; } + /** + * 获取最大等待时间(毫秒) + * @return 最大等待时间(毫秒) + */ public long getMax_wait() { return max_wait; } + /** + * 设置最大等待时间(毫秒) + * @param max_wait 最大等待时间(毫秒) + */ public void setMax_wait(long max_wait) { this.max_wait = max_wait; } + /** + * 重写 toString 方法,返回对象的字符串表示 + * @return 对象的字符串表示 + */ @Override public String toString() { return "PoolConfig [max_active=" + max_active + ", max_idle=" + max_idle + ", max_wait=" + max_wait + "]"; } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/config/redis/RedisServerNodeBean.java b/tamguo-modules-core/src/main/java/com/tamguo/config/redis/RedisServerNodeBean.java index d8b4297..a26bb81 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/config/redis/RedisServerNodeBean.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/config/redis/RedisServerNodeBean.java @@ -1,11 +1,26 @@ package com.tamguo.config.redis; +/** + * RedisServerNodeBean 类,用于表示 Redis 服务器节点的信息 + */ public class RedisServerNodeBean { + // Redis 服务器的 IP 地址 private String ip; + // Redis 服务器的端口号 private int port; + // 是否需要身份验证 private boolean needAuth; + // 身份验证密码 private String auth; + /** + * 构造函数,用于创建 RedisServerNodeBean 对象 + * + * @param ip Redis 服务器的 IP 地址 + * @param port Redis 服务器的端口号 + * @param needAuth 是否需要身份验证 + * @param auth 身份验证密码 + */ public RedisServerNodeBean(String ip, int port, boolean needAuth, String auth) { this.ip = ip; this.port = port; @@ -13,41 +28,85 @@ public class RedisServerNodeBean { this.auth = auth; } + /** + * 获取 Redis 服务器的 IP 地址 + * + * @return Redis 服务器的 IP 地址 + */ public String getIp() { return ip; } + /** + * 设置 Redis 服务器的 IP 地址 + * + * @param ip Redis 服务器的 IP 地址 + */ public void setIp(String ip) { this.ip = ip; } + /** + * 获取 Redis 服务器的端口号 + * + * @return Redis 服务器的端口号 + */ public int getPort() { return port; } + /** + * 设置 Redis 服务器的端口号 + * + * @param port Redis 服务器的端口号 + */ public void setPort(int port) { this.port = port; } + /** + * 获取是否需要身份验证 + * + * @return 是否需要身份验证 + */ public boolean isNeedAuth() { return needAuth; } + /** + * 设置是否需要身份验证 + * + * @param needAuth 是否需要身份验证 + */ public void setNeedAuth(boolean needAuth) { this.needAuth = needAuth; } + /** + * 获取身份验证密码 + * + * @return 身份验证密码 + */ public String getAuth() { return auth; } + /** + * 设置身份验证密码 + * + * @param auth 身份验证密码 + */ public void setAuth(String auth) { this.auth = auth; } + /** + * 重写 toString 方法,返回 RedisServerNodeBean 对象的字符串表示 + * + * @return RedisServerNodeBean 对象的字符串表示 + */ @Override public String toString() { return "RedisServer [ip=" + ip + ", port=" + port + ", needAuth=" + needAuth + ", auth=" + auth + "]"; } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/config/redis/RedisXMLConfigure.java b/tamguo-modules-core/src/main/java/com/tamguo/config/redis/RedisXMLConfigure.java index 0fe52c0..f233da6 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/config/redis/RedisXMLConfigure.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/config/redis/RedisXMLConfigure.java @@ -18,158 +18,187 @@ import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; +/** + * RedisXMLConfigure 类,用于从 XML 配置文件中读取 Redis 服务器节点信息,并创建 ShardedJedisPool + */ @Component("redisConfigure") public class RedisXMLConfigure implements InitializingBean { - private static final Logger logger = Logger.getLogger(RedisXMLConfigure.class); - private static String preKey; - private static Document document = null; - private ShardedJedisPool shardedJedisPool; + private static final Logger logger = Logger.getLogger(RedisXMLConfigure.class); // 日志记录器 + private static String preKey; // Redis 键的前缀 + private static Document document = null; // XML 文档对象 + private ShardedJedisPool shardedJedisPool; // ShardedJedis 连接池 + /** + * 在所有属性设置后执行的初始化方法 + * 从 XML 配置文件中读取 Redis 服务器节点信息,并创建 ShardedJedisPool + * @throws Exception 可能抛出的异常 + */ @Override public void afterPropertiesSet() throws Exception { - XMLConfiguration xmlConfiguration = new XMLConfiguration(); - String REDIS_PATH = "redis.xml"; - InputStream stream = null; + XMLConfiguration xmlConfiguration = new XMLConfiguration(); // 创建 XML 配置对象 + String REDIS_PATH = "redis.xml"; // XML 配置文件路径 + InputStream stream = null; // 输入流对象 + try { - stream = this.getClass().getClassLoader().getResourceAsStream(REDIS_PATH); - if (stream == null) { - logger.error("load redis.xml failed!!!" + REDIS_PATH); - throw new RuntimeException("load redis.xml failed"); + stream = this.getClass().getClassLoader().getResourceAsStream(REDIS_PATH); // 获取 XML 配置文件的输入流 + if (stream == null) { // 如果输入流为空 + logger.error("load redis.xml failed!!!" + REDIS_PATH); // 记录错误日志 + throw new RuntimeException("load redis.xml failed"); // 抛出运行时异常 } - logger.info("Redis XML config path:" + REDIS_PATH); - if (xmlConfiguration.readConfigFile(stream)) { - document = xmlConfiguration.getDocument(); + logger.info("Redis XML config path:" + REDIS_PATH); // 记录日志 + if (xmlConfiguration.readConfigFile(stream)) { // 如果成功读取 XML 配置文件 + document = xmlConfiguration.getDocument(); // 获取 XML 文档对象 } else { - logger.error("load redis.xml failed!!!"); + logger.error("load redis.xml failed!!!"); // 记录错误日志 } } finally { - if (null != stream) - stream.close(); + if (null!= stream) // 如果输入流不为空 + stream.close(); // 关闭输入流 } - //初始化参数 - initPreKey(); - PoolConfigBean pcb = initPoolConfigBean(); - List rsnbs = initRedisServerNodeBeans(); - //实现shardedJedisPool + + // 初始化参数 + initPreKey(); // 初始化 Redis 键的前缀 + PoolConfigBean pcb = initPoolConfigBean(); // 初始化连接池配置对象 + List rsnbs = initRedisServerNodeBeans(); // 初始化 Redis 服务器节点信息列表 + + // 创建 JedisPoolConfig 对象 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); - //no maxActive config + // 设置最大空闲连接数 jedisPoolConfig.setMaxIdle(pcb.getMax_idle()); + // 设置最大等待时间(毫秒) jedisPoolConfig.setMaxWaitMillis(pcb.getMax_wait()); - shardedJedisPool = new ShardedJedisPool(jedisPoolConfig,getJedisShardInfo(rsnbs)); - if(shardedJedisPool == null){ - throw new RuntimeException("config redis.xml error"); + // 创建 ShardedJedisPool 对象 + shardedJedisPool = new ShardedJedisPool(jedisPoolConfig, getJedisShardInfo(rsnbs)); + if (shardedJedisPool == null) { // 如果 ShardedJedisPool 对象为空 + throw new RuntimeException("config redis.xml error"); // 抛出运行时异常 } } /** - * 初始化jedis参数 + * 初始化连接池配置对象 + * @return 连接池配置对象 */ private PoolConfigBean initPoolConfigBean() { - PoolConfigBean poolConfigBean = new PoolConfigBean(); - Element poolElement = (Element) document.getElementsByTagName("pool").item(0); - int max_active = poolElement.hasAttribute("maxActive") ? Integer.parseInt(poolElement.getAttribute("maxActive")) : -1; - int max_idle = poolElement.hasAttribute("maxIdle") ? Integer.parseInt(poolElement.getAttribute("maxIdle")) : -1; - long max_wait = poolElement.hasAttribute("maxWait") ? Long.parseLong(poolElement.getAttribute("maxWait")) : -1; - poolConfigBean.setMax_active(max_active); - poolConfigBean.setMax_idle(max_idle); - poolConfigBean.setMax_wait(max_wait); + PoolConfigBean poolConfigBean = new PoolConfigBean(); // 创建连接池配置对象 + Element poolElement = (Element) document.getElementsByTagName("pool").item(0); // 获取 pool 元素 + + int max_active = poolElement.hasAttribute("maxActive")? Integer.parseInt(poolElement.getAttribute("maxActive")) : -1; // 获取最大活跃连接数 + int max_idle = poolElement.hasAttribute("maxIdle")? Integer.parseInt(poolElement.getAttribute("maxIdle")) : -1; // 获取最大空闲连接数 + long max_wait = poolElement.hasAttribute("maxWait")? Long.parseLong(poolElement.getAttribute("maxWait")) : -1; // 获取最大等待时间(毫秒) + + poolConfigBean.setMax_active(max_active); // 设置最大活跃连接数 + poolConfigBean.setMax_idle(max_idle); // 设置最大空闲连接数 + poolConfigBean.setMax_wait(max_wait); // 设置最大等待时间(毫秒) + return poolConfigBean; } /** - * 解析配置redis的server列表 + * 初始化 Redis 服务器节点信息列表 + * @return Redis 服务器节点信息列表 */ private List initRedisServerNodeBeans() { - List redisServers = new ArrayList(); - NodeList serverElements = document.getElementsByTagName("server"); - int serverLen = serverElements.getLength(); - if (serverLen < 1) { - logger.error("redis.servers.server must have one !"); - return null; + List redisServers = new ArrayList(); // 创建 Redis 服务器节点信息列表 + + NodeList serverElements = document.getElementsByTagName("server"); // 获取所有 server 元素 + int serverLen = serverElements.getLength(); // 获取 server 元素的数量 + + if (serverLen < 1) { // 如果 server 元素的数量小于 1 + logger.error("redis.servers.server must have one!"); // 记录错误日志 + return null; // 返回空列表 } - for (int i = 0; i < serverLen; i++) { - Element serverElement = (Element) serverElements.item(i); - String temp_ip = serverElement.hasAttribute("ip") ? serverElement.getAttribute("ip") : null; - if (temp_ip == null) { - logger.error("redis.servers.server.ip must be supplied!"); - return null; + + for (int i = 0; i < serverLen; i++) { // 遍历所有 server 元素 + Element serverElement = (Element) serverElements.item(i); // 获取当前 server 元素 + + String temp_ip = serverElement.hasAttribute("ip")? serverElement.getAttribute("ip") : null; // 获取 IP 地址 + if (temp_ip == null) { // 如果 IP 地址为空 + logger.error("redis.servers.server.ip must be supplied!"); // 记录错误日志 + return null; // 返回空列表 } - String temp_port = serverElement.hasAttribute("port") ? serverElement.getAttribute("port") : "6379"; - String temp_needAuth = serverElement.hasAttribute("needAuth") ? serverElement.getAttribute("needAuth") : "false"; - String temp_auth = null; - // need auth + String temp_port = serverElement.hasAttribute("port")? serverElement.getAttribute("port") : "6379"; // 获取端口号 + String temp_needAuth = serverElement.hasAttribute("needAuth")? serverElement.getAttribute("needAuth") : "false"; // 获取是否需要身份验证 + String temp_auth = null; // 身份验证密码 + + // 需要身份验证 if ("true".equals(temp_needAuth)) { - temp_auth = serverElement.hasAttribute("auth") ? serverElement.getAttribute("auth") : null; - if (null == temp_auth) { - logger.error("since needAuth is true,auth must be supplied!"); - return null; + temp_auth = serverElement.hasAttribute("auth")? serverElement.getAttribute("auth") : null; // 获取身份验证密码 + if (null == temp_auth) { // 如果身份验证密码为空 + logger.error("since needAuth is true, auth must be supplied!"); // 记录错误日志 + return null; // 返回空列表 } } - RedisServerNodeBean rs = null; + RedisServerNodeBean rs = null; // 创建 Redis 服务器节点对象 try { - rs = new RedisServerNodeBean(temp_ip, Integer.parseInt(temp_port), Boolean.parseBoolean(temp_needAuth), temp_auth); - } catch (NumberFormatException e) { - logger.error("port must be a number!\n" + e.getMessage()); - return null; + rs = new RedisServerNodeBean(temp_ip, Integer.parseInt(temp_port), Boolean.parseBoolean(temp_needAuth), temp_auth); // 创建 Redis 服务器节点对象 + } catch (NumberFormatException e) { // 如果端口号转换为整数时发生异常 + logger.error("port must be a number!\n" + e.getMessage()); // 记录错误日志 + return null; // 返回空列表 } - redisServers.add(rs); + redisServers.add(rs); // 将 Redis 服务器节点对象添加到列表中 } return redisServers; } /** - * 转换自定义配置为JedisShardInfo对象 - * @param redisServers - * @return + * 将自定义配置转换为 JedisShardInfo 对象列表 + * @param redisServers Redis 服务器节点信息列表 + * @return JedisShardInfo 对象列表 */ private List getJedisShardInfo(List redisServers) { - if(redisServers == null){ - logger.error("redisServers must not be empty null"); - return null; + if (redisServers == null) { // 如果 Redis 服务器节点信息列表为空 + logger.error("redisServers must not be empty null"); // 记录错误日志 + return null; // 返回空列表 } - int serverLen = redisServers.size(); - if (serverLen < 1) { - logger.error("redisServers must not be empty "); - return null; + int serverLen = redisServers.size(); // 获取 Redis 服务器节点的数量 + if (serverLen < 1) { // 如果 Redis 服务器节点的数量小于 1 + logger.error("redisServers must not be empty "); // 记录错误日志 + return null; // 返回空列表 } - List servers = new ArrayList(serverLen); - for (int i = 0; i < serverLen; i++) { - RedisServerNodeBean redisServer = redisServers.get(i); - JedisShardInfo jedisShardInfo = new JedisShardInfo(redisServer.getIp(), redisServer.getPort()); - if (redisServer.isNeedAuth()) { - jedisShardInfo.setPassword(redisServer.getAuth()); + List servers = new ArrayList(serverLen); // 创建 JedisShardInfo 对象列表 + + for (int i = 0; i < serverLen; i++) { // 遍历 Redis 服务器节点信息列表 + RedisServerNodeBean redisServer = redisServers.get(i); // 获取当前 Redis 服务器节点对象 + JedisShardInfo jedisShardInfo = new JedisShardInfo(redisServer.getIp(), redisServer.getPort()); // 创建 JedisShardInfo 对象 + if (redisServer.isNeedAuth()) { // 如果需要身份验证 + jedisShardInfo.setPassword(redisServer.getAuth()); // 设置身份验证密码 } - servers.add(jedisShardInfo); + servers.add(jedisShardInfo); // 将 JedisShardInfo 对象添加到列表中 } return servers; } - - /* - * 初始化redis的key前缀 + + /** + * 初始化 Redis 键的前缀 */ private void initPreKey() { - Element preKeyElement = (Element) document.getElementsByTagName("preKey").item(0); - preKey = preKeyElement.hasAttribute("value") ? preKeyElement.getAttribute("value") : ""; + Element preKeyElement = (Element) document.getElementsByTagName("preKey").item(0); // 获取 preKey 元素 + preKey = preKeyElement.hasAttribute("value")? preKeyElement.getAttribute("value") : ""; // 获取前缀值 } + /** + * 获取 Redis 键的前缀 + * @return Redis 键的前缀 + */ public String getPreKey() { return preKey; } + /** - * 从jedis连接池获得一个连接 - * @return + * 从 Jedis 连接池获取一个连接 + * @return Jedis 连接 */ public ShardedJedis getConnection() { return shardedJedisPool.getResource(); } + /** - * 把连接放回jedis连接池 - * @param resource + * 将连接放回 Jedis 连接池 + * @param resource Jedis 连接 */ public void closeConnection(ShardedJedis resource) { resource.close(); } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/BookCategoryEntity.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/BookCategoryEntity.java index 885f04d..0e0b159 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/BookCategoryEntity.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/BookCategoryEntity.java @@ -8,31 +8,35 @@ import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import lombok.Data; +/** + * BookCategoryEntity 类,用于表示书籍分类信息 + */ @Data @TableName(value="b_book_category") -public class BookCategoryEntity extends Model{ +public class BookCategoryEntity extends Model { private static final long serialVersionUID = 1L; - + + // 定义字段 @TableId - private String id; - private String parentId; - private String parentIds; - private String treeSort; - private String treeSorts; - private String treeLeaf; - private String treeLevel; - private String treeNames; - private String name; - private String seoTitle; - private String seoKeywords; - private String seoDescription; - private Date createDate; - private Date updateDate; + private String id; // 分类 ID + private String parentId; // 父分类 ID + private String parentIds; // 父分类 ID 列表 + private String treeSort; // 树排序 + private String treeSorts; // 树排序列表 + private String treeLeaf; // 是否叶子节点 + private String treeLevel; // 树层次 + private String treeNames; // 树名称 + private String name; // 分类名称 + private String seoTitle; // SEO 标题 + private String seoKeywords; // SEO 关键词 + private String seoDescription; // SEO 描述 + private Date createDate; // 创建日期 + private Date updateDate; // 更新日期 + // 重写 pkVal 方法,返回 ID 作为主键值 @Override protected Serializable pkVal() { return getId(); } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/BookEntity.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/BookEntity.java index c13b405..1c62cf4 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/BookEntity.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/BookEntity.java @@ -10,33 +10,39 @@ import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import lombok.Data; +/** + * BookEntity 类,用于表示书籍信息 + */ @Data @TableName(value="b_book") -public class BookEntity extends Model{ +public class BookEntity extends Model { private static final long serialVersionUID = 1L; - + + // 定义字段 @TableId - private String id; - private String categoryId; - private String bookImage; - private String owner; - private String name; - private String seoTitle; - private String seoKeywords; - private String seoDescription; - private Date createDate; - private Date updateDate; - + private String id; // 书籍 ID + private String categoryId; // 书籍分类 ID + private String bookImage; // 书籍图片 + private String owner; // 书籍所有者 + private String name; // 书籍名称 + private String seoTitle; // SEO 标题 + private String seoKeywords; // SEO 关键词 + private String seoDescription; // SEO 描述 + private Date createDate; // 创建日期 + private Date updateDate; // 更新日期 + + // 定义关联字段 @TableField(exist=false) - private List categoryIds; + private List categoryIds; // 分类 ID 列表 @TableField(exist=false) - private String memberName; + private String memberName; // 会员名称 @TableField(exist=false) - private String categoryName; + private String categoryName; // 分类名称 + // 重写 pkVal 方法,返回 ID 作为主键值 @Override protected Serializable pkVal() { return getId(); } -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/DocumentEntity.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/DocumentEntity.java index d8bce1e..b756bae 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/DocumentEntity.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/DocumentEntity.java @@ -13,46 +13,51 @@ import com.baomidou.mybatisplus.annotations.TableName; import com.tamguo.modules.book.model.enums.DocumentStatusEnum; import lombok.Data; +/** + * DocumentEntity 类,用于表示文档信息 + */ @Data @TableName(value="b_document") -public class DocumentEntity extends Model{ +public class DocumentEntity extends Model { private static final long serialVersionUID = 1L; - + + // 定义字段 @TableId - private String id; - private String batchNo; - private String parentId; - private String bookId; - private String owner; - private String name; - @JSONField(serialzeFeatures= SerializerFeature.WriteEnumUsingToString) - private DocumentStatusEnum status; + private String id; // 文档 ID + private String batchNo; // 批次号 + private String parentId; // 父文档 ID + private String bookId; // 所属书籍 ID + private String owner; // 所有者 + private String name; // 文档名称 + @JSONField(serialzeFeatures=SerializerFeature.WriteEnumUsingToString) + private DocumentStatusEnum status; // 文档状态 @TableField(value="is_open") - private String isOpen; - private Date createDate; - private Date updateDate; - - private String content; - private String markdown; - + private String isOpen; // 是否公开 + private Date createDate; // 创建日期 + private Date updateDate; // 更新日期 + + // 定义关联字段 + private String content; // 文档内容 + private String markdown; // Markdown 内容 + @TableField(exist=false) - private Integer level; + private Integer level; // 层级 @TableField(exist=false) - private String rootId; + private String rootId; // 根文档 ID @TableField(exist=false) - private boolean leaf; + private boolean leaf; // 是否叶子节点 @TableField(exist=false) - private List children; + private List children; // 子文档列表 @TableField(exist=false) - private String cover; - + private String cover; // 封面 + @TableField(exist=false) - private List fileUploads; + private List fileUploads; // 文件上传列表 + // 重写 pkVal 方法,返回 ID 作为主键值 @Override protected Serializable pkVal() { return getId(); } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileEntity.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileEntity.java index 40fe810..b7f969f 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileEntity.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileEntity.java @@ -4,16 +4,29 @@ import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import lombok.Data; +/** + * FileEntity 类,用于表示文件实体 + */ @Data @TableName(value="b_file_entity") public class FileEntity { - @TableId("file_id") + // 文件 ID + @TableId("file_id") private String fileId; - + + // 文件 MD5 值 private String fileMd5; + + // 文件路径 private String filePath; + + // 文件类型 private String fileContentType; + + // 文件扩展名 private String fileExtension; + + // 文件大小(字节数) private Long fileSize; -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileUploadEntity.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileUploadEntity.java index 4921c8e..63b0560 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileUploadEntity.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileUploadEntity.java @@ -12,33 +12,51 @@ import com.tamguo.modules.book.model.enums.BizTypeEnum; import com.tamguo.modules.book.model.enums.FileUploadStatusEnum; import lombok.Data; +/** + * FileUploadEntity 类,用于表示文件上传信息 + */ @Data @TableName(value="b_file_upload") -public class FileUploadEntity extends Model{ - +public class FileUploadEntity extends Model { + private static final long serialVersionUID = 1L; - + + // 文件上传 ID private String id; + // 文件 ID private String fileId; + // 文件名 private String fileName; + // 文件类型 private String fileType; - private String bizKey; - @JSONField(serialzeFeatures= SerializerFeature.WriteEnumUsingToString) + // 业务键 + private String bizKey; + // 业务类型 + @JSONField(serialzeFeatures = SerializerFeature.WriteEnumUsingToString) private BizTypeEnum bizType; - @JSONField(serialzeFeatures= SerializerFeature.WriteEnumUsingToString) + // 文件上传状态 + @JSONField(serialzeFeatures = SerializerFeature.WriteEnumUsingToString) private FileUploadStatusEnum status; + // 创建人 private String createBy; + // 创建日期 private Date createDate; + // 更新人 private String updateBy; + // 更新日期 private Date updateDate; + // 备注 private String remarks; + // 文件路径(不存在数据库中,通过查询关联表得到) @TableField(exist=false) private String filePath; + // 文件大小(字节数)(不存在数据库中,通过查询关联表得到) @TableField(exist=false) private Long fileSize; + + // 重写 pkVal 方法,返回 ID 作为主键值 @Override protected Serializable pkVal() { return getId(); } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/BizTypeEnum.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/BizTypeEnum.java index e3258dd..c52f156 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/BizTypeEnum.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/BizTypeEnum.java @@ -1,32 +1,50 @@ package com.tamguo.modules.book.model.enums; -import java.io.Serializable; -import com.baomidou.mybatisplus.enums.IEnum; +import java.io.Serializable; // 导入 Serializable 接口 +import com.baomidou.mybatisplus.enums.IEnum; // 导入 IEnum 接口 /** - * 用户状态 + * 用户状态枚举类,实现了 IEnum 接口 */ public enum BizTypeEnum implements IEnum { - DOCUMENT("document", "文档"); + // 文档类型 + DOCUMENT("document", "文档"); - private String value; - private String desc; + private String value; // 枚举值 + private String desc; // 枚举描述 + /** + * 构造函数,用于初始化枚举值和描述 + * @param value 枚举值 + * @param desc 枚举描述 + */ BizTypeEnum(final String value, final String desc) { this.value = value; this.desc = desc; } + /** + * 获取枚举值 + * @return 枚举值 + */ public Serializable getValue() { return this.value; } + /** + * 获取枚举描述 + * @return 枚举描述 + */ public String getDesc(){ return this.desc; } - + + /** + * 重写 toString 方法,返回枚举值 + * @return 枚举值 + */ @Override public String toString() { - return this.value; + return this.value; } -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/DocumentStatusEnum.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/DocumentStatusEnum.java index 582e08b..41781c5 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/DocumentStatusEnum.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/DocumentStatusEnum.java @@ -4,30 +4,53 @@ import java.io.Serializable; import com.baomidou.mybatisplus.enums.IEnum; /** - * 用户状态 + * 文档状态枚举类 */ public enum DocumentStatusEnum implements IEnum { - NORMAL("normal", "正常"), - HISTORY("history", "历史版本"); + /** + * 正常状态 + */ + NORMAL("normal", "正常"), + /** + * 历史版本状态 + */ + HISTORY("history", "历史版本"); - private String value; - private String desc; + private String value; // 状态值 + private String desc; // 状态描述 + /** + * 构造函数,用于初始化状态值和描述 + * @param value 状态值 + * @param desc 状态描述 + */ DocumentStatusEnum(final String value, final String desc) { this.value = value; this.desc = desc; } + /** + * 获取状态值 + * @return 状态值 + */ public Serializable getValue() { return this.value; } + /** + * 获取状态描述 + * @return 状态描述 + */ public String getDesc(){ return this.desc; } - + + /** + * 重写 toString 方法,返回状态值 + * @return 状态值 + */ @Override public String toString() { - return this.value; + return this.value; } -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/FileUploadStatusEnum.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/FileUploadStatusEnum.java index ff95cc3..f180b2e 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/FileUploadStatusEnum.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/enums/FileUploadStatusEnum.java @@ -4,30 +4,53 @@ import java.io.Serializable; import com.baomidou.mybatisplus.enums.IEnum; /** - * 用户状态 + * 文件上传状态枚举类 */ public enum FileUploadStatusEnum implements IEnum { - NORMAL("normal", "正常"), - DELETE("delete", "删除"); + /** + * 正常状态 + */ + NORMAL("normal", "正常"), + /** + * 删除状态 + */ + DELETE("delete", "删除"); - private String value; - private String desc; + private String value; // 状态值 + private String desc; // 状态描述 + /** + * 构造函数,用于初始化状态值和描述 + * @param value 状态值 + * @param desc 状态描述 + */ FileUploadStatusEnum(final String value, final String desc) { this.value = value; this.desc = desc; } + /** + * 获取状态值 + * @return 状态值 + */ public Serializable getValue() { return this.value; } + /** + * 获取状态描述 + * @return 状态描述 + */ public String getDesc(){ return this.desc; } - + + /** + * 重写 toString 方法,返回状态值 + * @return 状态值 + */ @Override public String toString() { - return this.value; + return this.value; } -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/BookServiceImpl.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/BookServiceImpl.java index 1e9c942..e0b88af 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/BookServiceImpl.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/BookServiceImpl.java @@ -11,22 +11,30 @@ import com.tamguo.modules.book.dao.BookMapper; import com.tamguo.modules.book.model.BookEntity; import com.tamguo.modules.book.service.IBookService; +/** + * BookServiceImpl 类,实现了 IBookService 接口 + */ @Service(value="bbookServiceImpl") -public class BookServiceImpl extends ServiceImpl implements IBookService{ +public class BookServiceImpl extends ServiceImpl implements IBookService { + /** + * 保存书籍信息 + * + * @param book 书籍实体 + */ @Transactional(readOnly=false) @Override public void saveBook(BookEntity book) { - book.setCreateDate(new Date()); - book.setUpdateDate(new Date()); - book.setSeoDescription(book.getName()); - book.setSeoKeywords(book.getName()); - book.setSeoTitle(book.getName()); - if(StringUtils.isEmpty(book.getId())) { - this.insert(book); - }else { - this.updateById(book); + book.setCreateDate(new Date()); // 设置创建日期为当前日期 + book.setUpdateDate(new Date()); // 设置更新日期为当前日期 + book.setSeoDescription(book.getName()); // 设置 SEO 描述为书籍名称 + book.setSeoKeywords(book.getName()); // 设置 SEO 关键词为书籍名称 + book.setSeoTitle(book.getName()); // 设置 SEO 标题为书籍名称 + + if (StringUtils.isEmpty(book.getId())) { // 如果书籍 ID 为空 + this.insert(book); // 执行插入操作 + } else { + this.updateById(book); // 执行更新操作 } } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/DocumentServiceImpl.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/DocumentServiceImpl.java index bb950b5..5eb219f 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/DocumentServiceImpl.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/DocumentServiceImpl.java @@ -12,14 +12,23 @@ import com.tamguo.modules.book.model.DocumentEntity; import com.tamguo.modules.book.model.enums.DocumentStatusEnum; import com.tamguo.modules.book.service.IDocumentService; +/** + * DocumentServiceImpl 类,实现了 IDocumentService 接口 + */ @Service -public class DocumentServiceImpl extends ServiceImpl implements IDocumentService{ +public class DocumentServiceImpl extends ServiceImpl implements IDocumentService { - @Transactional(readOnly=false) + /** + * 修改文档 + * + * @param document 文档实体 + */ + @Transactional(readOnly = false) @Override public void modify(DocumentEntity document) { - DocumentEntity entity = this.selectById(document.getId()); - if("yes".equals(document.getCover())) { + DocumentEntity entity = this.selectById(document.getId()); // 根据 ID 获取文档实体 + + if ("yes".equals(document.getCover())) { // 覆盖修改 entity.setContent(document.getContent()); entity.setMarkdown(document.getMarkdown()); @@ -28,47 +37,51 @@ public class DocumentServiceImpl extends ServiceImpl implements Serializable { private static final long serialVersionUID = 1L; - + + // 用户名 private String username; + // 昵称 private String nickName; + // 密码 private String password; + // 头像 private String avatar; + // 手机号 private String mobile; + // 邮箱 private String email; + // 积分 private Integer point; + // 余额 private BigDecimal amount; + // 最后登录时间 private Date lastLoginTime; + // 发布文章数 private Integer paperNum; + // 提问数 private Integer questionNum; + // 下载数 private Integer downNum; + // 点击数 private Integer hitsNum; + // 验证码(数据库中不存在该字段) @TableField(exist=false) private String verifyCode; + // 当前密码(数据库中不存在该字段) @TableField(exist=false) private String nowPassword; - } \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/member/model/condition/MemberCondition.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/member/model/condition/MemberCondition.java index 5d2e855..994f66f 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/member/model/condition/MemberCondition.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/member/model/condition/MemberCondition.java @@ -1,43 +1,99 @@ package com.tamguo.modules.member.model.condition; +/** + * MemberCondition 类,用于封装会员查询条件 + */ public class MemberCondition { - + + // 当前页码 private Integer pageNo; + // 每页显示数量 private Integer pageSize; + // 手机号码 private String mobile; + // 用户名 private String username; + // 昵称 private String nickName; - + + /** + * 获取手机号码 + * @return 手机号码 + */ public String getMobile() { return mobile; } + + /** + * 设置手机号码 + * @param mobile 手机号码 + */ public void setMobile(String mobile) { this.mobile = mobile; } + + /** + * 获取用户名 + * @return 用户名 + */ public String getUsername() { return username; } + + /** + * 设置用户名 + * @param username 用户名 + */ public void setUsername(String username) { this.username = username; } + + /** + * 获取昵称 + * @return 昵称 + */ public String getNickName() { return nickName; } + + /** + * 设置昵称 + * @param nickName 昵称 + */ public void setNickName(String nickName) { this.nickName = nickName; } + + /** + * 获取当前页码 + * @return 当前页码 + */ public Integer getPageNo() { return pageNo; } + + /** + * 设置当前页码 + * @param pageNo 当前页码 + */ public void setPageNo(Integer pageNo) { this.pageNo = pageNo; } + + /** + * 获取每页显示数量 + * @return 每页显示数量 + */ public Integer getPageSize() { return pageSize; } + + /** + * 设置每页显示数量 + * @param pageSize 每页显示数量 + */ public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } - -} +} \ No newline at end of file diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/member/service/impl/MemberService.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/member/service/impl/MemberService.java index 438a5c8..33ff565 100644 --- a/tamguo-modules-core/src/main/java/com/tamguo/modules/member/service/impl/MemberService.java +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/member/service/impl/MemberService.java @@ -239,49 +239,532 @@ public class MemberService extends ServiceImpl imple if(!entity.getPassword().equals(new Sha256Hash(member.getPassword()).toHex())) { return Result.result(501, null, "旧密码错误!"); } - if(!cacheService.isExist(SystemConstant.ALIYUN_MOBILE_SMS_PREFIX + member.getMobile())){ - return Result.result(502, null, "验证码错误"); - } - entity.setPassword(new Sha256Hash(member.getNowPassword()).toHex()); - return Result.result(0, null, "修改成功"); - } + package com.tamguo.modules.member.service.impl; - @SuppressWarnings("unchecked") - @Transactional(readOnly=true) - @Override - public Page listData(MemberCondition condition) { - Page page = new Page<>(condition.getPageNo(), condition.getPageSize()); - Condition query = Condition.create(); - if(!StringUtils.isEmpty(condition.getMobile())) { - query.eq("mobile", condition.getMobile()); - } - if(!StringUtils.isEmpty(condition.getNickName())) { - query.like("nick_name", condition.getNickName()); - } - if(!StringUtils.isEmpty(condition.getUsername())) { - query.eq("username", condition.getUsername()); - } - return this.selectPage(page, query); - } +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; +import java.util.UUID; +import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.crypto.hash.Sha256Hash; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; - @Transactional(readOnly=false) - @Override - public void reward(String id ,String bookId , Integer rewardPoint, BigDecimal rewardMoney) { - MemberEntity member = memberMapper.selectById(id); - - // 更新记录 - member.setPoint(member.getPoint() + rewardPoint); - member.setAmount(member.getAmount().add(rewardMoney)); - this.updateById(member); - - BookEntity book = iBookService.selectById(bookId); - - // 发送短信 - try { - iSmsService.sendRewardSms(member.getMobile(), member.getUsername(), book.getName(), rewardPoint, rewardMoney); - } catch (ClientException e) { - e.printStackTrace(); - } - } - -} +import com.aliyuncs.exceptions.ClientException; +import com.baomidou.mybatisplus.mapper.Condition; +import com.baomidou.mybatisplus.plugins.Page; +import com.baomidou.mybatisplus.service.impl.ServiceImpl; +import com.tamguo.common.utils.Result; +import com.tamguo.common.utils.SystemConstant; +import com.tamguo.config.redis.CacheService; +import com.tamguo.modules.book.model.BookEntity; +import com.tamguo.modules.book.service.IBookService; +import com.tamguo.modules.member.dao.MemberMapper; +import com.tamguo.modules.member.model.MemberEntity; +import com.tamguo.modules.member.model.condition.MemberCondition; +import com.tamguo.modules.member.service.IMemberService; +import com.tamguo.modules.sys.service.ISmsService; + +/** + * MemberServiceImpl 类,实现了 IMemberService 接口 + */ + @Service + public class MemberServiceImpl extends ServiceImpl implements IMemberService { + + // 注入 MemberMapper + @Autowired + private MemberMapper memberMapper; + + // 注入 CacheService + @Autowired + private CacheService cacheService; + + // 注入 ISmsService + @Autowired + private ISmsService iSmsService; + + // 注入 IBookService + @Autowired + private IBookService iBookService; + + /** + * 登录方法 + * + * @param username 用户名 + * @param password 密码 + * @return 登录结果 + */ + @Override + public Result login(String username, String password) { + // 创建 MemberEntity 对象 + MemberEntity condition = new MemberEntity(); + // 设置用户名 + condition.setUsername(username); + // 从数据库中查询该用户 + MemberEntity member = memberMapper.selectOne(condition); + // 如果用户不存在 + if (member == null) { + // 返回错误结果,提示用户名 或 password 有误 + return Result.result(201, member, "用户名或密码有误,请重新输入或找回密码"); + } + // 获取该用户的登录失败次数 + Integer loginFailureCount = this.getLoginFailureCount(member); + // 如果密码错误 + if (!new Sha256Hash(password).toHex().equals(member.getPassword())) { + // 登录失败次数加 1 + loginFailureCount++; + // 更新登录失败次数 + this.updateLoginFailureCount(member, loginFailureCount); + // 返回错误结果,提示用户名 或 password 有误 + return Result.result(202, member, "用户名或密码有误,请重新输入或找回密码"); + } + // 更新登录失败次数为 0 + this.updateLoginFailureCount(member, 0); + // 返回正确结果,提示登录成功 + return Result.result(200, member, "登录成功"); + } + + /** + * 更新登录失败次数 + * + * @param member 会员实体 + * @param loginFailureCount 登录失败次数 + */ + public void updateLoginFailureCount(MemberEntity member, Integer loginFailureCount) { + // 将登录失败次数存储到缓存中,key 为 LOGIN_FAILURE_COUNT + member.getId() + cacheService.setObject(SystemConstant.LOGIN_FAILURE_COUNT + member.getId(), loginFailureCount, 2 * 60 * 60); + } + + /** + * 获取登录失败次数 + * + * @param member 会员实体 + * @return 登录失败次数 + */ + public Integer getLoginFailureCount(MemberEntity member) { + // 如果 member 为空 + if (member == null) { + // 返回 0 + return 0; + } + // 如果缓存中不存在登录失败次数 + if (!cacheService.isExist(SystemConstant.LOGIN_FAILURE_COUNT + member.getId())) { + // 返回 0 + return 0; + } + // 从缓存中获取登录失败次数 + return (Integer) cacheService.getObject(SystemConstant.LOGIN_FAILURE_COUNT + member.getId()); + } + + /** + * 检查用户名是否存在 + * + * @param username 用户名 + * @return 检查结果 + */ + @Override + public Result checkUsername(String username) { + // 创建 MemberEntity 对象 + MemberEntity condition = new MemberEntity(); + // 设置用户名 + condition.setUsername(username); + // 从数据库中查询该用户 + MemberEntity member = memberMapper.selectOne(condition); + // 如果用户存在 + if (member!= null) { + // 返回错误结果,提示该用户名已经存在 + return Result.result(201, null, "该用户名已经存在"); + } + // 返回正确结果,提示该用户名可用 + return Result.result(200, null, "该用户名可用"); + } + + /** + * 检查手机号是否存在 + * + * @param mobile 手机号 + * @return 检查结果 + */ + @Override + public Result checkMobile(String mobile) { + // 创建 MemberEntity 对象 + MemberEntity condition = new MemberEntity(); + // 设置手机号 + condition.setMobile(mobile); + // 从数据库中查询该用户 + MemberEntity member = memberMapper.selectOne(condition); + // 如果用户存在 + if (member!= null) { + // 返回错误结果,提示该手机号已经存在 + return Result.result(201, null, "该手机号已经存在"); + } + // 返回正确结果,提示该手机号可用 + return Result.result(200, null, "该手机号可用"); + } + + /** + * 注册方法 + * + * @param member 会员实体 + * @return 注册结果 + */ + @Transactional(readOnly = false) + @Override + public Result register(MemberEntity member) { + // 创建 MemberEntity 对象 + MemberEntity condition = new MemberEntity(); + // 设置用户名 + condition.setUsername(member.getUsername()); + // 从数据库中查询该用户 + MemberEntity m = memberMapper.selectOne(condition); + // 如果用户已存在 + if (m!= null) { + // 返回错误结果,提示该用户已经存在 + return Result.result(201, null, "该用户已经存在"); + } + // 创建 MemberEntity 对象 + condition = new MemberEntity(); + // 设置手机号 + condition.setMobile(member.getMobile()); + // 从数据库中查询该用户 + m = memberMapper.selectOne(condition); + // 如果手机号已存在 + if (m!= null) { + // 返回错误结果,提示该手机号已经存在 + return Result.result(202, null, "该手机号已经存在"); + } + // 如果缓存中不存在验证码 + if (!cacheService.isExist(SystemConstant.ALIYUN_MOBILE_SMS_PREFIX + member.getMobile())) { + // 返回错误结果,提示验证码错误 + return Result.result(203, null, "验证码错误"); + } + // 从缓存中获取验证码 + String code = (String) cacheService.getObject(SystemConstant.ALIYUN_MOBILE_SMS_PREFIX + member.getMobile()); + // 如果验证码错误 + if (!code.equals(member.getVerifyCode())) { + // 返回错误结果,提示验证码错误 + return Result.result(204, null, "验证码错误"); + } + // 创建 MemberEntity 对象 + MemberEntity entity = new MemberEntity(); + // 设置头像 + entity.setAvatar(SystemConstant.DEFAULT_MEMBER_AVATAR); + // 设置手机号 + entity.setMobile(member.getMobile()); + // 设置密码 + entity.setPassword(new Sha256Hash(member.getPassword()).toHex()); + // 设置用户名 + entity.setUsername(member.getUsername()); + // 设置昵称 + entity.setNickName(member.getUsername()); + // 设置邮箱 + entity.setEmail(member.getEmail()); + // 插入会员信息 + memberMapper.insert(entity); + // 返回正确结果,提示注册成功 + return Result.result(200, entity, "注册成功"); + } + + /** + * 检查帐号是否存在 + * + * @param account 帐号 + * @return 检查结果 + */ + @SuppressWarnings("unchecked") + @Override + public Result checkAccount(String account) { + // 如果帐号为空 + if (StringUtils.isEmpty(account)) { + // 返回错误结果,提示帐号不存在 + return Result.result(201, null, "帐号不存在!"); + } + // 创建 Condition 对象 + Condition query = Condition.create(); + // 添加条件,根据帐号查询会员信息 + query.eq("user_name", account).or().eq("mobile", account); + // 从数据库中查询会员信息 + List members = memberMapper.selectList(query); + // 如果没有查询到会员信息 + if (members.size() == 0) { + // 返回错误结果,提示帐号不存在 + return Result.result(201, null, "帐号不存在!"); + } + // 返回正确结果,提示帐号存在,并返回会员信息 + return Result.result(200, members.get(0), "该帐号存在"); + } + + /** + * 确认帐号是否存在 + * + * @param account 帐号 + * @param veritycode 验证码 + * @return 确认结果 + */ + @SuppressWarnings("unchecked") + @Override + public Result confirmAccount(String account, String veritycode) { + // 如果帐号为空 + if (StringUtils.isEmpty(account)) { + // 返回错误结果,提示帐号不存在 + return Result.result(201, null, "帐号不存在!"); + } + // 创建 Condition 对象 + Condition query = Condition.create(); + // 添加条件,根据帐号查询会员信息 + query.eq("username", account).or().eq("mobile", account); + // 从数据库中查询会员信息 + List members = memberMapper.selectList(query); + // 如果没有查询到会员信息 + if (members.size() == 0) { + // 返回错误结果,提示帐号不存在 + return Result.result(201, null, "帐号不存在!"); + } + // 返回正确结果,提示帐号存在,并返回会员信息 + return Result.result(200, members.get(0), "该帐号存在"); + } + + /** + * 安全检查 + * + * @param username 用户名 + * @param isEmail 是否为邮箱 + * @param vcode 验证码 + * @return 安全检查结果 + */ + @Override + public Result securityCheck(String username, String isEmail, String vcode) { + // 创建 MemberEntity 对象 + MemberEntity condition = new MemberEntity(); + // 设置用户名 + condition.setUsername(username); + // 从数据库中查询该用户 + MemberEntity member = memberMapper.selectOne(condition); + // 如果用户不存在 + if (member == null) { + // 返回错误结果,提示用户不存在 + return Result.result(201, member, "用户不存在"); + } + // 如果是邮箱验证 + if ("1".equals(isEmail)) { + // 如果缓存中不存在验证码 + if (!cacheService.isExist(SystemConstant.ALIYUN_MAIL_FIND_PASSWORD_PREFIX + member.getEmail())) { + // 返回错误结果,提示验证码错误 + return Result.result(201, member, "验证码错误"); + } + // 从缓存中获取验证码 + String code = (String) cacheService.getObject(SystemConstant.ALIYUN_MAIL_FIND_PASSWORD_PREFIX + member.getEmail()); + // 如果验证码错误 + if (!code.equals(vcode)) { + // 返回错误结果,提示验证码错误 + return Result.result(202, member, "验证码错误"); + } + } else { + // 如果缓存中不存在验证码 + if (!cacheService.isExist(SystemConstant.ALIYUN_MOBILE_SMS_PREFIX + member.getMobile())) { + // 返回错误结果,提示验证码错误 + return Result.result(203, member, "验证码错误"); + } + // 从缓存中获取验证码 + String code = (String) cacheService.getObject(SystemConstant.ALIYUN_MOBILE_SMS_PREFIX + member.getMobile()); + // 如果验证码错误 + if (!code.equals(vcode)) { + // 返回错误结果,提示验证码错误 + return Result.result(204, member, "验证码错误"); + } + } + // 创建随机字符串 + String key = UUID.randomUUID().toString(); + // 将用户名存储到缓存中,key 为 SECURITY_CHECK_PREFIX + key + cacheService.setObject(SystemConstant.SECURITY_CHECK_PREFIX + key, username, 2 * 60 * 60); + // 返回正确结果,提示安全验证通过,并返回随机字符串 + return Result.result(200, key, "安全验证通过"); + } + + /** + * 重置密码 + * + * @param resetPasswordKey 重置密码 key + * @param username 用户名 + * @param password 新密码 + * @param verifypwd 确认密码 + * @return 重置密码结果 + */ + @Override + public Result resetPassword(String resetPasswordKey, String username, String password, String verifypwd) { + // 如果缓存中存在重置密码 key + if (cacheService.isExist(SystemConstant.SECURITY_CHECK_PREFIX + resetPasswordKey)) { + // 创建 MemberEntity 对象 + MemberEntity condition = new MemberEntity(); + // 设置用户名 + condition.setUsername(username); + // 从数据库中查询该用户 + MemberEntity member = memberMapper.selectOne(condition); + // 如果密码和确认密码不一致 + if (!password.equals(verifypwd)) { + // 返回错误结果,提示密码不一致 + return Result.result(201, null, "密码不一致"); + } + // 设置密码 + member.setPassword(new Sha256Hash(password).toHex()); + // 更新会员信息 + memberMapper.updateById(member); + } + // 返回正确结果,提示更新成功 + return Result.result(200, null, "更新成功"); + } + + /** + * 更新会员信息 + * + * @param member 会员实体 + */ + @Transactional(readOnly = false) + @Override + public void updateMember(MemberEntity member) { + // 获取会员信息 + MemberEntity entity = memberMapper.selectById(member.getId()); + // 设置头像 + entity.setAvatar(member.getAvatar()); + // 设置邮箱 + entity.setEmail(member.getEmail()); + // 设置手机号 + entity.setMobile(member.getMobile()); + // 设置昵称 + entity.setNickName(member.getNickName()); + // 更新会员信息 + memberMapper.updateById(entity); + } + + /** + * 根据 uid 查询会员信息 + * + * @param uid uid + * @return 会员信息 + */ + @Transactional(readOnly = true) + @Override + public MemberEntity findByUid(String uid) { + // 根据 uid 查询会员信息 + return memberMapper.selectById(uid); + } + + /** + * 根据用户名查询会员信息 + * + * @param username 用户名 + * @return 会员信息 + */ + @SuppressWarnings("unchecked") + @Transactional(readOnly = true) + @Override + public MemberEntity findByUsername(String username) { + // 创建 Condition 对象 + Condition query = Condition.create(); + // 添加条件,根据用户名查询会员信息 + query.eq("username", username).or().eq("mobile", username).or().eq("email", username); + // 从数据库中查询会员信息 + return this.selectOne(query); + } + + /** + * 更新会员最后登录时间 + * + * @param uid uid + */ + @Transactional(readOnly = false) + @Override + public void updateLastLoginTime(String uid) { + // 获取会员信息 + MemberEntity member = memberMapper.selectById(uid); + // 设置最后登录时间为当前时间 + member.setLastLoginTime(new Date()); + // 更新会员信息 + memberMapper.updateById(member); + } + + /** + * 获取当前会员信息 + * + * @param id id + * @return 会员信息 + */ + @Override + public MemberEntity findCurrMember(String id) { + // 获取会员信息 + MemberEntity member = memberMapper.selectById(id); + // 将密码设置为 null + member.setPassword(null); + // 返回会员信息 + return member; + } + + /** + * 更新密码 + * + * @param member 会员实体 + * @return 更新结果 + */ + @Transactional(readOnly = false) + @Override + public Result updatePwd(MemberEntity member) { + // 获取会员信息 + MemberEntity entity = memberMapper.selectById(member.getId()); + // 如果旧密码错误 + if (!entity.getPassword().equals(new Sha256Hash(member.getPassword()).toHex())) { + // 返回错误结果,提示旧密码错误 + return Result.result(501, null, "旧密码错误!"); + } + // 如果缓存中不存在验证码 + if (!cacheService.isExist(SystemConstant.ALIYUN_MOBILE_SMS_PREFIX + member.getMobile())){ + // 如果缓存服务中不存在指定的验证码前缀与会员手机号的组合 + return Result.result(502, null, "验证码错误"); + } + entity.setPassword(new Sha256Hash(member.getNowPassword()).toHex()); +// 设置实体的密码为会员当前密码的 Sha256 哈希值的十六进制表示 + return Result.result(0, null, "修改成功"); + } + + @SuppressWarnings("unchecked") + @Transactional(readOnly=true) + @Override + public Page listData(MemberCondition condition) { + // 创建分页对象 + Page page = new Page<>(condition.getPageNo(), condition.getPageSize()); + Condition query = Condition.create(); + // 如果条件中的手机号不为空 + if(!StringUtils.isEmpty(condition.getMobile())) { + query.eq("mobile", condition.getMobile()); + } + // 如果条件中的昵称不为空 + if(!StringUtils.isEmpty(condition.getNickName())) { + query.like("nick_name", condition.getNickName()); + } + // 如果条件中的用户名不为空 + if(!StringUtils.isEmpty(condition.getUsername())) { + query.eq("username", condition.getUsername()); + } + return this.selectPage(page, query); + } + + @Transactional(readOnly=false) + @Override + public void reward(String id,String bookId, Integer rewardPoint, BigDecimal rewardMoney) { + MemberEntity member = memberMapper.selectById(id); + // 获取会员实体 + // 更新会员的积分和金额 + member.setPoint(member.getPoint() + rewardPoint); + member.setAmount(member.getAmount().add(rewardMoney)); + this.updateById(member); + + BookEntity book = iBookService.selectById(bookId); + // 获取书籍实体 + + // 发送短信 + try { + iSmsService.sendRewardSms(member.getMobile(), member.getUsername(), book.getName(), rewardPoint, rewardMoney); + } catch (ClientException e) { + e.printStackTrace(); + } + }