学校组
This commit is contained in:
@@ -2,17 +2,23 @@ package com.yunhai.bschclassmanage.controller.api;
|
||||
|
||||
import com.yunhai.bschclassmanage.config.Config;
|
||||
import com.yunhai.bschclassmanage.controller.BaseController;
|
||||
import com.yunhai.bschclassmanage.enums.UserType;
|
||||
import com.yunhai.bschclassmanage.utils.RedisUtil;
|
||||
import com.yunhai.bschclassmanage.utils.UserContext;
|
||||
import com.yunhai.platform.utils.exception.DException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 缓存查看接口
|
||||
@@ -23,12 +29,25 @@ import java.util.concurrent.TimeUnit;
|
||||
@CrossOrigin(originPatterns = {"*"}, allowCredentials = "true")
|
||||
public class CacheController extends BaseController {
|
||||
|
||||
/** 匹配全部 key 的通配符,禁止用于清空全部 */
|
||||
private static final String ALL_KEYS_PATTERN = "*";
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
/**
|
||||
* 超管校验:非超级管理员直接抛异常阻断
|
||||
*/
|
||||
private void denyIfNotSuperAdmin() {
|
||||
boolean isSuperAdmin = UserType.SUPER_ADMINISTRATOR.getId().equals(UserContext.getType());
|
||||
if (!isSuperAdmin) {
|
||||
throw new DException("无权限:仅超级管理员可执行缓存操作");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 key 查看缓存内容
|
||||
* 返回 key 是否存在、数据类型、剩余过期时间(秒)以及对应的值
|
||||
@@ -38,22 +57,18 @@ public class CacheController extends BaseController {
|
||||
*/
|
||||
@GetMapping("get")
|
||||
public Map<String, Object> getByKey(@RequestParam String key) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("key", key);
|
||||
|
||||
denyIfNotSuperAdmin();
|
||||
if (StringUtils.isBlank(key)) {
|
||||
result.put("exists", false);
|
||||
result.put("msg", "key不能为空");
|
||||
return result;
|
||||
throw new DException("key不能为空");
|
||||
}
|
||||
|
||||
Boolean exists = redisTemplate.hasKey(key);
|
||||
if (exists == null || !exists) {
|
||||
result.put("exists", false);
|
||||
result.put("msg", "缓存不存在或已过期");
|
||||
return result;
|
||||
throw new DException("缓存不存在或已过期");
|
||||
}
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("key", key);
|
||||
result.put("exists", true);
|
||||
String type = redisTemplate.type(key).name().toLowerCase();
|
||||
result.put("type", type);
|
||||
@@ -90,7 +105,8 @@ public class CacheController extends BaseController {
|
||||
* @param pattern 匹配模式,默认 * 表示所有 key
|
||||
*/
|
||||
@GetMapping("keys")
|
||||
public Object keys(@RequestParam(required = false, defaultValue = "*") String pattern) {
|
||||
public Set<String> keys(@RequestParam(required = false, defaultValue = "*") String pattern) {
|
||||
denyIfNotSuperAdmin();
|
||||
return redisUtil.keys(pattern);
|
||||
}
|
||||
|
||||
@@ -103,14 +119,19 @@ public class CacheController extends BaseController {
|
||||
*/
|
||||
@PostMapping("delete")
|
||||
public Map<String, Object> delete(@RequestParam String key) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
denyIfNotSuperAdmin();
|
||||
if (StringUtils.isBlank(key)) {
|
||||
result.put("success", false);
|
||||
result.put("msg", "key不能为空");
|
||||
return result;
|
||||
throw new DException("key不能为空");
|
||||
}
|
||||
String[] keys = key.split(",");
|
||||
long count = redisTemplate.delete(java.util.Arrays.asList(keys));
|
||||
List<String> keys = Arrays.stream(key.split(","))
|
||||
.map(String::trim)
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.collect(Collectors.toList());
|
||||
if (keys.isEmpty()) {
|
||||
throw new DException("key不能为空");
|
||||
}
|
||||
long count = redisTemplate.delete(keys);
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", true);
|
||||
result.put("deleted", count);
|
||||
result.put("msg", "已清除 " + count + " 个 key");
|
||||
@@ -125,43 +146,19 @@ public class CacheController extends BaseController {
|
||||
*/
|
||||
@PostMapping("deletePattern")
|
||||
public Map<String, Object> deletePattern(@RequestParam String pattern) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
if (StringUtils.isBlank(pattern) || "*".equals(pattern.trim())) {
|
||||
result.put("success", false);
|
||||
result.put("msg", "禁止用 * 清空全部,请使用 clearAll 接口或指定明确前缀");
|
||||
return result;
|
||||
denyIfNotSuperAdmin();
|
||||
if (StringUtils.isBlank(pattern) || ALL_KEYS_PATTERN.equals(pattern.trim())) {
|
||||
throw new DException("禁止用 * 清空全部,请指定明确的 key 前缀");
|
||||
}
|
||||
Set<String> keys = redisUtil.keys(pattern);
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
Set<String> keys = redisUtil.keys(pattern.trim());
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
result.put("success", true);
|
||||
result.put("deleted", 0);
|
||||
result.put("msg", "未匹配到任何 key");
|
||||
return result;
|
||||
throw new DException("未匹配到任何 key");
|
||||
}
|
||||
redisTemplate.delete(keys);
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", true);
|
||||
result.put("deleted", keys.size());
|
||||
result.put("msg", "已清除 " + keys.size() + " 个 key");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空当前 Redis 数据库(仅当前 db,不影响其他 db)
|
||||
* ⚠️ 高危操作:会删除当前库下所有缓存,请谨慎调用
|
||||
*/
|
||||
@PostMapping("clearAll")
|
||||
public Map<String, Object> clearAll() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
redisTemplate.getConnectionFactory().getConnection().flushDb();
|
||||
result.put("success", true);
|
||||
result.put("msg", "当前数据库已清空");
|
||||
} catch (Exception e) {
|
||||
log.error("清空缓存失败", e);
|
||||
result.put("success", false);
|
||||
result.put("msg", "清空失败:" + e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
+6
-4
@@ -22,7 +22,7 @@ public class SyncGroupController {
|
||||
|
||||
@PostMapping("AddGroup")
|
||||
public void AddGroup(@RequestBody String jsonString) {
|
||||
log.info("jsonString="+jsonString);
|
||||
log.info("AddGroup jsonString="+jsonString);
|
||||
//判断 2,"学校组" 4, "班级组"
|
||||
SyncGroupDTO group = OtherUtil.jsonParseObject(jsonString, SyncGroupDTO.class);
|
||||
syncGroupService.syncAddOrUpdate(group);
|
||||
@@ -30,19 +30,21 @@ public class SyncGroupController {
|
||||
|
||||
@PostMapping("UpdateGroup")
|
||||
public void UpdateGroup(@RequestBody String jsonString) {
|
||||
log.info("jsonString="+jsonString);
|
||||
log.info("UpdateGroup jsonString="+jsonString);
|
||||
SyncGroupDTO group = OtherUtil.jsonParseObject(jsonString, SyncGroupDTO.class);
|
||||
syncGroupService.syncAddOrUpdate(group);
|
||||
}
|
||||
|
||||
@PostMapping("DeleteGroup")
|
||||
public void DeleteGroup(@RequestBody String jsonString) {
|
||||
log.info("jsonString="+jsonString);
|
||||
log.info("DeleteGroup jsonString="+jsonString);
|
||||
}
|
||||
|
||||
@PostMapping("SyncGroup")
|
||||
public void SyncGroup(@RequestBody String jsonString) {
|
||||
log.info("jsonString="+jsonString);
|
||||
log.info("SyncGroup jsonString="+jsonString);
|
||||
SyncGroupDTO group = OtherUtil.jsonParseObject(jsonString, SyncGroupDTO.class);
|
||||
syncGroupService.syncAddOrUpdate(group);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,5 +43,7 @@ public class SyncUserController {
|
||||
@PostMapping("SyncUser")
|
||||
public void SyncUser(@RequestBody String jsonString) {
|
||||
log.info("SyncUser jsonString="+jsonString);
|
||||
List<SyncUserDTO> userList = OtherUtil.jsonParseList(jsonString, SyncUserDTO.class);
|
||||
syncUserService.syncAddOrUpdate(userList);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,10 +107,12 @@ public class ComSchTreeService implements IComSchTreeService {
|
||||
|
||||
|
||||
public List<BoaGroup> getSchTree(Integer schId) {
|
||||
String cacheKey = RedisConstants.SCHOOL_TREE + "_" + schId;
|
||||
String json = redisUtil.get(cacheKey,String.class);
|
||||
List<BoaGroup> treeList = OtherUtil.jsonParseArrayTree(json, BoaGroup.class);
|
||||
return treeList;
|
||||
BoaGroup group = YschBaseOpenApi.getGroupById(schId,true);
|
||||
return Collections.singletonList(group);
|
||||
// String cacheKey = RedisConstants.SCHOOL_TREE + "_" + schId;
|
||||
// String json = redisUtil.get(cacheKey,String.class);
|
||||
// List<BoaGroup> treeList = OtherUtil.jsonParseArrayTree(json, BoaGroup.class);
|
||||
// return treeList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user