优化controller

This commit is contained in:
luke 2025-06-09 12:50:12 +08:00
parent 5deadeb99e
commit d638a36b0f
55 changed files with 904 additions and 203 deletions

View File

@ -1,11 +1,11 @@
package com.knowledge.base.application.service;
import com.knowledge.base.domain.user.repository.po.User;
import com.knowledge.base.domain.user.repository.po.UserToken;
import com.knowledge.base.domain.user.repository.po.UserRole;
import com.knowledge.base.domain.user.repository.po.UserFile;
import com.knowledge.base.domain.user.repository.po.Role;
import com.knowledge.base.domain.user.repository.po.RoleFileRule;
import com.knowledge.base.infrastructure.north.dto.role.RoleDTO;
import com.knowledge.base.infrastructure.north.dto.role.RoleFileRuleDTO;
import com.knowledge.base.infrastructure.north.dto.role.UserRoleDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserFileDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserTokenDTO;
import java.util.List;
import java.util.Optional;
@ -16,35 +16,35 @@ public interface UserAppService {
boolean verifyPassword(String username, String plainText);
// 用户资料
Optional<User> findByUsername(String username);
Optional<User> findById(Long id);
List<User> findAll();
Optional<UserDTO> findByUsername(String username);
Optional<UserDTO> findById(Long id);
List<UserDTO> findAll();
// Token 相关
UserToken createToken(Long userId, long expireMs);
UserTokenDTO createToken(Long userId, long expireMs);
void removeToken(String token);
boolean isValidToken(String token);
Optional<UserToken> findToken(String token);
Optional<UserTokenDTO> findToken(String token);
// 用户角色
List<UserRole> listUserRoles(Long userId);
List<UserRoleDTO> listUserRoles(Long userId);
boolean addUserRole(Long userId, Long roleId);
boolean removeUserRole(Long userId, Long roleId);
// 用户特殊授权
List<UserFile> listUserFiles(Long userId);
List<UserFileDTO> listUserFiles(Long userId);
boolean addUserFileAuth(Long userId, Long fileId);
boolean removeUserFileAuth(Long userId, Long fileId);
// 角色
List<Role> listRoles();
Optional<Role> getRoleById(Long roleId);
boolean addRole(Role role);
boolean updateRole(Role role);
List<RoleDTO> listRoles();
Optional<RoleDTO> getRoleById(Long roleId);
boolean addRole(RoleDTO role);
boolean updateRole(RoleDTO role);
boolean deleteRole(Long id);
// 角色授权信息
List<RoleFileRule> listRoleFileRules(Long roleId);
boolean addRoleFileRule(RoleFileRule rule);
List<RoleFileRuleDTO> listRoleFileRules(Long roleId);
boolean addRoleFileRule(RoleFileRuleDTO rule);
boolean removeRoleFileRule(Long id);
}

View File

@ -1,12 +1,21 @@
package com.knowledge.base.application.service;
import com.knowledge.base.domain.user.repository.po.*;
import com.google.common.collect.Lists;
import com.knowledge.base.domain.user.model.UserDO;
import com.knowledge.base.domain.user.service.iface.UserDomainService;
import com.knowledge.base.infrastructure.converter.*;
import com.knowledge.base.infrastructure.north.dto.role.RoleDTO;
import com.knowledge.base.infrastructure.north.dto.role.RoleFileRuleDTO;
import com.knowledge.base.infrastructure.north.dto.role.UserRoleDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserFileDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserTokenDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@ -18,7 +27,7 @@ public class UserAppServiceImpl implements UserAppService {
public boolean registerUser(String username, String password, List<Long> roleIds) {
boolean ok = userDomainService.registerUser(username, password);
if (!ok) return false;
Optional<User> userOpt = userDomainService.findByUsername(username);
Optional<UserDO> userOpt = userDomainService.findByUsername(username);
if (userOpt.isEmpty()) return false;
Long userId = userOpt.get().getId();
for (Long roleId : roleIds) {
@ -33,23 +42,25 @@ public class UserAppServiceImpl implements UserAppService {
}
@Override
public Optional<User> findByUsername(String username) {
return userDomainService.findByUsername(username);
public Optional<UserDTO> findByUsername(String username) {
return Optional.ofNullable(UserDtoConverter.toDTO(userDomainService.findByUsername(username).get()));
}
@Override
public Optional<User> findById(Long id) {
return userDomainService.findById(id);
public Optional<UserDTO> findById(Long id) {
return Optional.ofNullable(UserDtoConverter.toDTO(userDomainService.findById(id).get()));
}
@Override
public List<User> findAll() {
return userDomainService.findAllUsers();
public List<UserDTO> findAll() {
return Optional.ofNullable(userDomainService.findAllUsers()).orElse(Lists.newArrayList()).stream()
.map(e -> UserDtoConverter.toDTO(e))
.collect(Collectors.toList());
}
@Override
public UserToken createToken(Long userId, long expireMs) {
return userDomainService.createToken(userId, expireMs);
public UserTokenDTO createToken(Long userId, long expireMs) {
return UserTokenDtoConverter.toDTO(userDomainService.createToken(userId, expireMs));
}
@Override
@ -63,13 +74,15 @@ public class UserAppServiceImpl implements UserAppService {
}
@Override
public Optional<UserToken> findToken(String token) {
return userDomainService.findToken(token);
public Optional<UserTokenDTO> findToken(String token) {
return Optional.ofNullable(UserTokenDtoConverter.toDTO(userDomainService.findToken(token).get()));
}
@Override
public List<UserRole> listUserRoles(Long userId) {
return userDomainService.findRolesByUserId(userId);
public List<UserRoleDTO> listUserRoles(Long userId) {
return Optional.ofNullable(userDomainService.findRolesByUserId(userId)).orElse(Lists.newArrayList()).stream()
.map(e -> UserRoleDtoConverter.toDTO(e))
.collect(Collectors.toList());
}
@Override
@ -83,8 +96,10 @@ public class UserAppServiceImpl implements UserAppService {
}
@Override
public List<UserFile> listUserFiles(Long userId) {
return userDomainService.findUserFiles(userId);
public List<UserFileDTO> listUserFiles(Long userId) {
return Optional.ofNullable(userDomainService.findUserFiles(userId)).orElse(Lists.newArrayList()).stream()
.map(e -> UserFileDtoConverter.toDTO(e))
.collect(Collectors.toList());
}
@Override
@ -98,23 +113,25 @@ public class UserAppServiceImpl implements UserAppService {
}
@Override
public List<Role> listRoles() {
return userDomainService.findAllRoles();
public List<RoleDTO> listRoles() {
return Optional.ofNullable(userDomainService.findAllRoles()).orElse(Lists.newArrayList()).stream()
.map(e -> RoleDtoConverter.toDTO(e))
.collect(Collectors.toList());
}
@Override
public Optional<Role> getRoleById(Long roleId) {
return userDomainService.findRoleById(roleId);
public Optional<RoleDTO> getRoleById(Long roleId) {
return Optional.ofNullable(RoleDtoConverter.toDTO(userDomainService.findRoleById(roleId).get()));
}
@Override
public boolean addRole(Role role) {
return userDomainService.addRole(role);
public boolean addRole(RoleDTO role) {
return userDomainService.addRole(RoleDtoConverter.toDO(role));
}
@Override
public boolean updateRole(Role role) {
return userDomainService.updateRole(role);
public boolean updateRole(RoleDTO role) {
return userDomainService.updateRole(RoleDtoConverter.toDO(role));
}
@Override
@ -123,13 +140,15 @@ public class UserAppServiceImpl implements UserAppService {
}
@Override
public List<RoleFileRule> listRoleFileRules(Long roleId) {
return userDomainService.findRoleFileRules(roleId);
public List<RoleFileRuleDTO> listRoleFileRules(Long roleId) {
return Optional.ofNullable(userDomainService.findRoleFileRules(roleId)).orElse(Lists.newArrayList()).stream()
.map(e -> RoleFileRuleDtoConverter.toDTO(e))
.collect(Collectors.toList());
}
@Override
public boolean addRoleFileRule(RoleFileRule rule) {
return userDomainService.addRoleFileRule(rule);
public boolean addRoleFileRule(RoleFileRuleDTO rule) {
return userDomainService.addRoleFileRule(RoleFileRuleDtoConverter.toDO(rule));
}
@Override

View File

@ -0,0 +1,18 @@
package com.knowledge.base.domain.doc.model;
import lombok.Data;
import java.time.LocalDateTime;
/**
* @author Luke.ye
* @date 2025/6/9 10:51
*/
@Data
public class FileDO {
public Long id;
public String fileName;
public String filePath;
public LocalDateTime addTime;
public LocalDateTime updateTime;
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.domain.doc.repository.converter;
import com.knowledge.base.domain.doc.repository.po.File;
import com.knowledge.base.domain.doc.model.FileDO;
import org.springframework.beans.BeanUtils;
public class FileDomainConverter {
public static FileDO toDO(File po) {
if (po == null) return null;
FileDO doObj = new FileDO();
BeanUtils.copyProperties(po, doObj);
return doObj;
}
public static File toPO(FileDO doObj) {
if (doObj == null) return null;
File po = new File();
BeanUtils.copyProperties(doObj, po);
return po;
}
}

View File

@ -0,0 +1,11 @@
package com.knowledge.base.domain.user.model;
/**
* @author Luke.ye
* @date 2025/6/9 10:37
*/
public class RoleDO {
public Long id;
public String roleCode;
public String roleName;
}

View File

@ -0,0 +1,15 @@
package com.knowledge.base.domain.user.model;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:39
*/
@Data
public class RoleFileRuleDO {
public Long id;
public Long roleId;
public String filePattern;
public String remark;
}

View File

@ -0,0 +1,16 @@
package com.knowledge.base.domain.user.model;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:36
*/
@Data
public class UserDO {
public Long id;
public String username;
public String password;
// 领域层需要的字段可适当扩展
}

View File

@ -0,0 +1,14 @@
package com.knowledge.base.domain.user.model;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:38
*/
@Data
public class UserFileDO {
public Long id;
public Long userId;
public Long fileId;
}

View File

@ -0,0 +1,14 @@
package com.knowledge.base.domain.user.model;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:38
*/
@Data
public class UserRoleDO {
public Long id;
public Long userId;
public Long roleId;
}

View File

@ -0,0 +1,18 @@
package com.knowledge.base.domain.user.model;
import lombok.Data;
import java.time.LocalDateTime;
/**
* @author Luke.ye
* @date 2025/6/9 10:40
*/
@Data
public class UserTokenDO {
public Long id;
public Long userId;
public String token;
public LocalDateTime loginAt;
public LocalDateTime expiredAt;
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.domain.user.repository.converter;
import com.knowledge.base.domain.user.repository.po.Role;
import com.knowledge.base.domain.user.model.RoleDO;
import org.springframework.beans.BeanUtils;
public class RoleDomainConverter {
public static RoleDO toDO(Role po) {
if (po == null) return null;
RoleDO doObj = new RoleDO();
BeanUtils.copyProperties(po, doObj);
return doObj;
}
public static Role toPO(RoleDO doObj) {
if (doObj == null) return null;
Role po = new Role();
BeanUtils.copyProperties(doObj, po);
return po;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.domain.user.repository.converter;
import com.knowledge.base.domain.user.repository.po.RoleFileRule;
import com.knowledge.base.domain.user.model.RoleFileRuleDO;
import org.springframework.beans.BeanUtils;
public class RoleFileRuleDomainConverter {
public static RoleFileRuleDO toDO(RoleFileRule po) {
if (po == null) return null;
RoleFileRuleDO doObj = new RoleFileRuleDO();
BeanUtils.copyProperties(po, doObj);
return doObj;
}
public static RoleFileRule toPO(RoleFileRuleDO doObj) {
if (doObj == null) return null;
RoleFileRule po = new RoleFileRule();
BeanUtils.copyProperties(doObj, po);
return po;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.domain.user.repository.converter;
import com.knowledge.base.domain.user.repository.po.User;
import com.knowledge.base.domain.user.model.UserDO;
import org.springframework.beans.BeanUtils;
public class UserDomainConverter {
public static UserDO toDO(User po) {
if (po == null) return null;
UserDO doObj = new UserDO();
BeanUtils.copyProperties(po, doObj);
return doObj;
}
public static User toPO(UserDO doObj) {
if (doObj == null) return null;
User po = new User();
BeanUtils.copyProperties(doObj, po);
return po;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.domain.user.repository.converter;
import com.knowledge.base.domain.user.repository.po.UserFile;
import com.knowledge.base.domain.user.model.UserFileDO;
import org.springframework.beans.BeanUtils;
public class UserFileDomainConverter {
public static UserFileDO toDO(UserFile po) {
if (po == null) return null;
UserFileDO doObj = new UserFileDO();
BeanUtils.copyProperties(po, doObj);
return doObj;
}
public static UserFile toPO(UserFileDO doObj) {
if (doObj == null) return null;
UserFile po = new UserFile();
BeanUtils.copyProperties(doObj, po);
return po;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.domain.user.repository.converter;
import com.knowledge.base.domain.user.repository.po.UserRole;
import com.knowledge.base.domain.user.model.UserRoleDO;
import org.springframework.beans.BeanUtils;
public class UserRoleDomainConverter {
public static UserRoleDO toDO(UserRole po) {
if (po == null) return null;
UserRoleDO doObj = new UserRoleDO();
BeanUtils.copyProperties(po, doObj);
return doObj;
}
public static UserRole toPO(UserRoleDO doObj) {
if (doObj == null) return null;
UserRole po = new UserRole();
BeanUtils.copyProperties(doObj, po);
return po;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.domain.user.repository.converter;
import com.knowledge.base.domain.user.repository.po.UserToken;
import com.knowledge.base.domain.user.model.UserTokenDO;
import org.springframework.beans.BeanUtils;
public class UserTokenDomainConverter {
public static UserTokenDO toDO(UserToken po) {
if (po == null) return null;
UserTokenDO doObj = new UserTokenDO();
BeanUtils.copyProperties(po, doObj);
return doObj;
}
public static UserToken toPO(UserTokenDO doObj) {
if (doObj == null) return null;
UserToken po = new UserToken();
BeanUtils.copyProperties(doObj, po);
return po;
}
}

View File

@ -1,14 +1,15 @@
package com.knowledge.base.domain.user.service.iface;
import com.knowledge.base.domain.user.model.RoleDO;
import com.knowledge.base.domain.user.repository.po.Role;
import java.util.List;
import java.util.Optional;
public interface RoleDomainSupport {
Optional<Role> findById(Long id);
List<Role> findAll();
boolean addRole(Role role);
boolean updateRole(Role role);
Optional<RoleDO> findById(Long id);
List<RoleDO> findAll();
boolean addRole(RoleDO role);
boolean updateRole(RoleDO role);
boolean deleteRole(Long id);
}

View File

@ -1,11 +1,12 @@
package com.knowledge.base.domain.user.service.iface;
import com.knowledge.base.domain.user.model.RoleFileRuleDO;
import com.knowledge.base.domain.user.repository.po.RoleFileRule;
import java.util.List;
public interface RoleFileRuleDomainSupport {
List<RoleFileRule> findByRoleId(Long roleId);
boolean addRoleFileRule(RoleFileRule rule);
List<RoleFileRuleDO> findByRoleId(Long roleId);
boolean addRoleFileRule(RoleFileRuleDO rule);
boolean removeRoleFileRule(Long ruleId);
}

View File

@ -1,13 +1,14 @@
package com.knowledge.base.domain.user.service.iface;
import com.knowledge.base.domain.user.model.UserTokenDO;
import com.knowledge.base.domain.user.repository.po.UserToken;
import java.util.Optional;
public interface UserAuthDomainSupport {
boolean verifyPassword(String username, String plainText);
UserToken createToken(Long userId, long expireMs);
UserTokenDO createToken(Long userId, long expireMs);
boolean isValidToken(String token);
void removeToken(String token);
Optional<UserToken> findToken(String token);
Optional<UserTokenDO> findToken(String token);
}

View File

@ -1,5 +1,6 @@
package com.knowledge.base.domain.user.service.iface;
import com.knowledge.base.domain.user.model.*;
import com.knowledge.base.domain.user.repository.po.*;
import java.util.List;
@ -7,37 +8,37 @@ import java.util.Optional;
public interface UserDomainService {
// 用户资料
Optional<User> findByUsername(String username);
Optional<User> findById(Long id);
List<User> findAllUsers();
Optional<UserDO> findByUsername(String username);
Optional<UserDO> findById(Long id);
List<UserDO> findAllUsers();
boolean registerUser(String username, String plainPassword);
// 认证/Token
Optional<UserToken> findToken(String token);
Optional<UserTokenDO> findToken(String token);
boolean verifyPassword(String username, String plainText);
UserToken createToken(Long userId, long expireMs);
UserTokenDO createToken(Long userId, long expireMs);
boolean isValidToken(String token);
void removeToken(String token);
// 角色
List<Role> findAllRoles();
Optional<Role> findRoleById(Long id);
boolean addRole(Role role);
boolean updateRole(Role role);
List<RoleDO> findAllRoles();
Optional<RoleDO> findRoleById(Long id);
boolean addRole(RoleDO role);
boolean updateRole(RoleDO role);
boolean deleteRole(Long id);
// 用户-角色
List<UserRole> findRolesByUserId(Long userId);
List<UserRoleDO> findRolesByUserId(Long userId);
boolean addUserRole(Long userId, Long roleId);
boolean removeUserRole(Long userId, Long roleId);
// 角色-文件规则
List<RoleFileRule> findRoleFileRules(Long roleId);
boolean addRoleFileRule(RoleFileRule rule);
List<RoleFileRuleDO> findRoleFileRules(Long roleId);
boolean addRoleFileRule(RoleFileRuleDO rule);
boolean removeRoleFileRule(Long ruleId);
// 用户特殊授权
List<UserFile> findUserFiles(Long userId);
List<UserFileDO> findUserFiles(Long userId);
boolean addUserFileAuth(Long userId, Long fileId);
boolean removeUserFileAuth(Long userId, Long fileId);
}

View File

@ -1,11 +1,12 @@
package com.knowledge.base.domain.user.service.iface;
import com.knowledge.base.domain.user.model.UserFileDO;
import com.knowledge.base.domain.user.repository.po.UserFile;
import java.util.List;
public interface UserFileDomainSupport {
List<UserFile> findByUserId(Long userId);
List<UserFileDO> findByUserId(Long userId);
boolean addUserFileAuth(Long userId, Long fileId);
boolean removeUserFileAuth(Long userId, Long fileId);
}

View File

@ -1,15 +1,16 @@
package com.knowledge.base.domain.user.service.iface;
import com.knowledge.base.domain.user.model.UserDO;
import com.knowledge.base.domain.user.repository.po.User;
import java.util.Optional;
import java.util.List;
public interface UserProfileDomainSupport {
Optional<User> findByUsername(String username);
Optional<User> findById(Long id);
List<User> findAll();
Optional<UserDO> findByUsername(String username);
Optional<UserDO> findById(Long id);
List<UserDO> findAll();
boolean registerUser(String username, String plainPassword);
boolean updateUser(User user);
boolean updateUser(UserDO user);
boolean deleteUser(Long id);
}

View File

@ -1,11 +1,12 @@
package com.knowledge.base.domain.user.service.iface;
import com.knowledge.base.domain.user.model.UserRoleDO;
import com.knowledge.base.domain.user.repository.po.UserRole;
import java.util.List;
public interface UserRoleDomainSupport {
List<UserRole> findByUserId(Long userId);
List<UserRoleDO> findByUserId(Long userId);
boolean addUserRole(Long userId, Long roleId);
boolean removeUserRole(Long userId, Long roleId);
}

View File

@ -1,13 +1,16 @@
package com.knowledge.base.domain.user.service.impl;
import com.google.common.collect.Lists;
import com.knowledge.base.domain.user.model.RoleDO;
import com.knowledge.base.domain.user.repository.converter.RoleDomainConverter;
import com.knowledge.base.domain.user.repository.iface.RoleRepository;
import com.knowledge.base.domain.user.repository.po.Role;
import com.knowledge.base.domain.user.service.iface.RoleDomainSupport;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@ -16,23 +19,27 @@ public class RoleDomainSupportImpl implements RoleDomainSupport {
private final RoleRepository roleRepository;
@Override
public Optional<Role> findById(Long id) {
return Optional.ofNullable(roleRepository.findById(id));
public Optional<RoleDO> findById(Long id) {
return Optional.ofNullable(
RoleDomainConverter.toDO(roleRepository.findById(id))
);
}
@Override
public List<Role> findAll() {
return roleRepository.findAll();
public List<RoleDO> findAll() {
return Optional.ofNullable(roleRepository.findAll()).orElse(Lists.newArrayList()).stream()
.map(e -> RoleDomainConverter.toDO(e))
.collect(Collectors.toList());
}
@Override
public boolean addRole(Role role) {
return roleRepository.save(role);
public boolean addRole(RoleDO role) {
return roleRepository.save(RoleDomainConverter.toPO(role));
}
@Override
public boolean updateRole(Role role) {
return roleRepository.update(role);
public boolean updateRole(RoleDO role) {
return roleRepository.update(RoleDomainConverter.toPO(role));
}
@Override

View File

@ -1,12 +1,16 @@
package com.knowledge.base.domain.user.service.impl;
import com.google.common.collect.Lists;
import com.knowledge.base.domain.user.model.RoleFileRuleDO;
import com.knowledge.base.domain.user.repository.converter.RoleFileRuleDomainConverter;
import com.knowledge.base.domain.user.repository.iface.RoleFileRuleRepository;
import com.knowledge.base.domain.user.repository.po.RoleFileRule;
import com.knowledge.base.domain.user.service.iface.RoleFileRuleDomainSupport;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@ -15,13 +19,15 @@ public class RoleFileRuleDomainSupportImpl implements RoleFileRuleDomainSupport
private final RoleFileRuleRepository roleFileRuleRepository;
@Override
public List<RoleFileRule> findByRoleId(Long roleId) {
return roleFileRuleRepository.findByRoleId(roleId);
public List<RoleFileRuleDO> findByRoleId(Long roleId) {
return Optional.ofNullable(roleFileRuleRepository.findByRoleId(roleId)).orElse(Lists.newArrayList()).stream()
.map(e -> RoleFileRuleDomainConverter.toDO(e))
.collect(Collectors.toList());
}
@Override
public boolean addRoleFileRule(RoleFileRule rule) {
return roleFileRuleRepository.save(rule);
public boolean addRoleFileRule(RoleFileRuleDO rule) {
return roleFileRuleRepository.save(RoleFileRuleDomainConverter.toPO(rule));
}
@Override

View File

@ -1,5 +1,7 @@
package com.knowledge.base.domain.user.service.impl;
import com.knowledge.base.domain.user.model.UserTokenDO;
import com.knowledge.base.domain.user.repository.converter.UserTokenDomainConverter;
import com.knowledge.base.domain.user.repository.iface.UserRepository;
import com.knowledge.base.domain.user.repository.iface.UserTokenRepository;
import com.knowledge.base.domain.user.repository.po.User;
@ -29,7 +31,7 @@ public class UserAuthDomainSupportImpl implements UserAuthDomainSupport {
}
@Override
public UserToken createToken(Long userId, long expireMs) {
public UserTokenDO createToken(Long userId, long expireMs) {
String token = UUID.randomUUID().toString().replace("-", "");
UserToken userToken = new UserToken();
userToken.setUserId(userId);
@ -37,7 +39,7 @@ public class UserAuthDomainSupportImpl implements UserAuthDomainSupport {
userToken.setLoginAt(LocalDateTime.now());
userToken.setExpiredAt(LocalDateTime.now().plusSeconds(expireMs / 1000));
userTokenRepository.save(userToken);
return userToken;
return UserTokenDomainConverter.toDO(userToken);
}
@Override
@ -52,7 +54,7 @@ public class UserAuthDomainSupportImpl implements UserAuthDomainSupport {
}
@Override
public Optional<UserToken> findToken(String token) {
return userTokenRepository.findByToken(token);
public Optional<UserTokenDO> findToken(String token) {
return Optional.ofNullable(UserTokenDomainConverter.toDO(userTokenRepository.findByToken(token).get()));
}
}

View File

@ -1,5 +1,6 @@
package com.knowledge.base.domain.user.service.impl;
import com.knowledge.base.domain.user.model.*;
import com.knowledge.base.domain.user.repository.po.*;
import com.knowledge.base.domain.user.service.iface.*;
import lombok.RequiredArgsConstructor;
@ -21,17 +22,17 @@ public class UserDomainServiceImpl implements UserDomainService {
// 用户资料
@Override
public Optional<User> findByUsername(String username) {
public Optional<UserDO> findByUsername(String username) {
return userProfileDomainSupport.findByUsername(username);
}
@Override
public Optional<User> findById(Long id) {
public Optional<UserDO> findById(Long id) {
return userProfileDomainSupport.findById(id);
}
@Override
public List<User> findAllUsers() {
public List<UserDO> findAllUsers() {
return userProfileDomainSupport.findAll();
}
@ -43,7 +44,7 @@ public class UserDomainServiceImpl implements UserDomainService {
// 认证/Token
@Override
public Optional<UserToken> findToken(String token) {
public Optional<UserTokenDO> findToken(String token) {
return userAuthDomainSupport.findToken(token);
}
@ -52,7 +53,7 @@ public class UserDomainServiceImpl implements UserDomainService {
return userAuthDomainSupport.verifyPassword(username, plainText);
}
@Override
public UserToken createToken(Long userId, long expireMs) {
public UserTokenDO createToken(Long userId, long expireMs) {
return userAuthDomainSupport.createToken(userId, expireMs);
}
@Override
@ -66,21 +67,21 @@ public class UserDomainServiceImpl implements UserDomainService {
// 角色
@Override
public List<Role> findAllRoles() {
public List<RoleDO> findAllRoles() {
return roleDomainSupport.findAll();
}
@Override
public Optional<Role> findRoleById(Long id) {
public Optional<RoleDO> findRoleById(Long id) {
return roleDomainSupport.findById(id);
}
@Override
public boolean addRole(Role role) {
public boolean addRole(RoleDO role) {
return roleDomainSupport.addRole(role);
}
@Override
public boolean updateRole(Role role) {
public boolean updateRole(RoleDO role) {
return roleDomainSupport.updateRole(role);
}
@ -91,7 +92,7 @@ public class UserDomainServiceImpl implements UserDomainService {
// 用户-角色
@Override
public List<UserRole> findRolesByUserId(Long userId) {
public List<UserRoleDO> findRolesByUserId(Long userId) {
return userRoleDomainSupport.findByUserId(userId);
}
@Override
@ -105,11 +106,11 @@ public class UserDomainServiceImpl implements UserDomainService {
// 角色-文件规则
@Override
public List<RoleFileRule> findRoleFileRules(Long roleId) {
public List<RoleFileRuleDO> findRoleFileRules(Long roleId) {
return roleFileRuleDomainSupport.findByRoleId(roleId);
}
@Override
public boolean addRoleFileRule(RoleFileRule rule) {
public boolean addRoleFileRule(RoleFileRuleDO rule) {
return roleFileRuleDomainSupport.addRoleFileRule(rule);
}
@Override
@ -119,7 +120,7 @@ public class UserDomainServiceImpl implements UserDomainService {
// 用户特殊授权
@Override
public List<UserFile> findUserFiles(Long userId) {
public List<UserFileDO> findUserFiles(Long userId) {
return userFileDomainSupport.findByUserId(userId);
}
@Override

View File

@ -1,5 +1,8 @@
package com.knowledge.base.domain.user.service.impl;
import com.google.common.collect.Lists;
import com.knowledge.base.domain.user.model.UserFileDO;
import com.knowledge.base.domain.user.repository.converter.UserFileDomainConverter;
import com.knowledge.base.domain.user.repository.iface.UserFileRepository;
import com.knowledge.base.domain.user.repository.po.UserFile;
import com.knowledge.base.domain.user.service.iface.UserFileDomainSupport;
@ -7,6 +10,8 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@ -15,8 +20,10 @@ public class UserFileDomainSupportImpl implements UserFileDomainSupport {
private final UserFileRepository userFileRepository;
@Override
public List<UserFile> findByUserId(Long userId) {
return userFileRepository.findByUserId(userId);
public List<UserFileDO> findByUserId(Long userId) {
return Optional.ofNullable(userFileRepository.findByUserId(userId)).orElse(Lists.newArrayList()).stream()
.map(e -> UserFileDomainConverter.toDO(e))
.collect(Collectors.toList());
}
@Override

View File

@ -1,15 +1,17 @@
package com.knowledge.base.domain.user.service.impl;
import com.google.common.collect.Lists;
import com.knowledge.base.domain.user.model.UserDO;
import com.knowledge.base.domain.user.repository.converter.UserDomainConverter;
import com.knowledge.base.domain.user.repository.iface.UserRepository;
import com.knowledge.base.domain.user.repository.po.User;
import com.knowledge.base.domain.user.service.iface.UserProfileDomainSupport;
import com.knowledge.base.infrastructure.util.crypto.SM3Util;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@ -18,18 +20,20 @@ public class UserProfileDomainSupportImpl implements UserProfileDomainSupport {
private final UserRepository userRepository;
@Override
public Optional<User> findByUsername(String username) {
return userRepository.findByUsername(username);
public Optional<UserDO> findByUsername(String username) {
return Optional.ofNullable(UserDomainConverter.toDO(userRepository.findByUsername(username).get()));
}
@Override
public Optional<User> findById(Long id) {
return userRepository.findById(id);
public Optional<UserDO> findById(Long id) {
return Optional.ofNullable(UserDomainConverter.toDO(userRepository.findById(id).get()));
}
@Override
public List<User> findAll() {
return userRepository.findAll();
public List<UserDO> findAll() {
return Optional.ofNullable(userRepository.findAll()).orElse(Lists.newArrayList()).stream()
.map(e -> UserDomainConverter.toDO(e))
.collect(Collectors.toList());
}
@Override
@ -37,13 +41,13 @@ public class UserProfileDomainSupportImpl implements UserProfileDomainSupport {
if (userRepository.findByUsername(username).isPresent()) return false;
User user = new User();
user.setUsername(username);
user.setPassword(DigestUtils.md5DigestAsHex(plainPassword.getBytes(StandardCharsets.UTF_8)));
user.setPassword(SM3Util.digest(plainPassword));
return userRepository.save(user);
}
@Override
public boolean updateUser(User user) {
return userRepository.update(user);
public boolean updateUser(UserDO user) {
return userRepository.update(UserDomainConverter.toPO(user));
}
@Override

View File

@ -1,5 +1,8 @@
package com.knowledge.base.domain.user.service.impl;
import com.google.common.collect.Lists;
import com.knowledge.base.domain.user.model.UserRoleDO;
import com.knowledge.base.domain.user.repository.converter.UserRoleDomainConverter;
import com.knowledge.base.domain.user.repository.iface.UserRoleRepository;
import com.knowledge.base.domain.user.repository.po.UserRole;
import com.knowledge.base.domain.user.service.iface.UserRoleDomainSupport;
@ -7,6 +10,8 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@ -15,8 +20,10 @@ public class UserRoleDomainSupportImpl implements UserRoleDomainSupport {
private final UserRoleRepository userRoleRepository;
@Override
public List<UserRole> findByUserId(Long userId) {
return userRoleRepository.findByUserId(userId);
public List<UserRoleDO> findByUserId(Long userId) {
return Optional.ofNullable(userRoleRepository.findByUserId(userId)).orElse(Lists.newArrayList()).stream()
.map(e -> UserRoleDomainConverter.toDO(e))
.collect(Collectors.toList());
}
@Override

View File

@ -0,0 +1,30 @@
package com.knowledge.base.infrastructure.converter;
import com.knowledge.base.domain.doc.model.FileDO;
import com.knowledge.base.infrastructure.north.dto.doc.FileDTO;
import org.springframework.beans.BeanUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FileDtoConverter {
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static FileDO toDO(FileDTO dto) {
if (dto == null) return null;
FileDO doObj = new FileDO();
BeanUtils.copyProperties(dto, doObj);
if (dto.addTime != null) doObj.addTime = LocalDateTime.parse(dto.addTime, dtf);
if (dto.updateTime != null) doObj.updateTime = LocalDateTime.parse(dto.updateTime, dtf);
return doObj;
}
public static FileDTO toDTO(FileDO doObj) {
if (doObj == null) return null;
FileDTO dto = new FileDTO();
BeanUtils.copyProperties(doObj, dto);
dto.addTime = doObj.addTime == null ? null : dtf.format(doObj.addTime);
dto.updateTime = doObj.updateTime == null ? null : dtf.format(doObj.updateTime);
return dto;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.infrastructure.converter;
import com.knowledge.base.domain.user.model.RoleDO;
import com.knowledge.base.infrastructure.north.dto.role.RoleDTO;
import org.springframework.beans.BeanUtils;
public class RoleDtoConverter {
public static RoleDO toDO(RoleDTO dto) {
if (dto == null) return null;
RoleDO doObj = new RoleDO();
BeanUtils.copyProperties(dto, doObj);
return doObj;
}
public static RoleDTO toDTO(RoleDO doObj) {
if (doObj == null) return null;
RoleDTO dto = new RoleDTO();
BeanUtils.copyProperties(doObj, dto);
return dto;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.infrastructure.converter;
import com.knowledge.base.domain.user.model.RoleFileRuleDO;
import com.knowledge.base.infrastructure.north.dto.role.RoleFileRuleDTO;
import org.springframework.beans.BeanUtils;
public class RoleFileRuleDtoConverter {
public static RoleFileRuleDO toDO(RoleFileRuleDTO dto) {
if (dto == null) return null;
RoleFileRuleDO doObj = new RoleFileRuleDO();
BeanUtils.copyProperties(dto, doObj);
return doObj;
}
public static RoleFileRuleDTO toDTO(RoleFileRuleDO doObj) {
if (doObj == null) return null;
RoleFileRuleDTO dto = new RoleFileRuleDTO();
BeanUtils.copyProperties(doObj, dto);
return dto;
}
}

View File

@ -0,0 +1,25 @@
package com.knowledge.base.infrastructure.converter;
import com.knowledge.base.domain.user.model.UserDO;
import com.knowledge.base.infrastructure.north.dto.user.UserDTO;
import org.springframework.beans.BeanUtils;
/**
* @author Luke.ye
* @date 2025/6/9 09:44
*/
public class UserDtoConverter {
public static UserDO toDO(UserDTO dto) {
if (dto == null) return null;
UserDO doObj = new UserDO();
BeanUtils.copyProperties(dto, doObj);
return doObj;
}
public static UserDTO toDTO(UserDO doObj) {
if (doObj == null) return null;
UserDTO dto = new UserDTO();
BeanUtils.copyProperties(doObj, dto);
return dto;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.infrastructure.converter;
import com.knowledge.base.domain.user.model.UserFileDO;
import com.knowledge.base.infrastructure.north.dto.user.UserFileDTO;
import org.springframework.beans.BeanUtils;
public class UserFileDtoConverter {
public static UserFileDO toDO(UserFileDTO dto) {
if (dto == null) return null;
UserFileDO doObj = new UserFileDO();
BeanUtils.copyProperties(dto, doObj);
return doObj;
}
public static UserFileDTO toDTO(UserFileDO doObj) {
if (doObj == null) return null;
UserFileDTO dto = new UserFileDTO();
BeanUtils.copyProperties(doObj, dto);
return dto;
}
}

View File

@ -0,0 +1,20 @@
package com.knowledge.base.infrastructure.converter;
import com.knowledge.base.domain.user.model.UserRoleDO;
import com.knowledge.base.infrastructure.north.dto.role.UserRoleDTO;
import org.springframework.beans.BeanUtils;
public class UserRoleDtoConverter {
public static UserRoleDO toDO(UserRoleDTO dto) {
if (dto == null) return null;
UserRoleDO doObj = new UserRoleDO();
BeanUtils.copyProperties(dto, doObj);
return doObj;
}
public static UserRoleDTO toDTO(UserRoleDO doObj) {
if (doObj == null) return null;
UserRoleDTO dto = new UserRoleDTO();
BeanUtils.copyProperties(doObj, dto);
return dto;
}
}

View File

@ -0,0 +1,30 @@
package com.knowledge.base.infrastructure.converter;
import com.knowledge.base.domain.user.model.UserTokenDO;
import com.knowledge.base.infrastructure.north.dto.user.UserTokenDTO;
import org.springframework.beans.BeanUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class UserTokenDtoConverter {
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static UserTokenDO toDO(UserTokenDTO dto) {
if (dto == null) return null;
UserTokenDO doObj = new UserTokenDO();
BeanUtils.copyProperties(dto, doObj);
if (dto.loginAt != null) doObj.loginAt = LocalDateTime.parse(dto.loginAt, dtf);
if (dto.expiredAt != null) doObj.expiredAt = LocalDateTime.parse(dto.expiredAt, dtf);
return doObj;
}
public static UserTokenDTO toDTO(UserTokenDO doObj) {
if (doObj == null) return null;
UserTokenDTO dto = new UserTokenDTO();
BeanUtils.copyProperties(doObj, dto);
dto.loginAt = doObj.loginAt == null ? null : dtf.format(doObj.loginAt);
dto.expiredAt = doObj.expiredAt == null ? null : dtf.format(doObj.expiredAt);
return dto;
}
}

View File

@ -2,38 +2,41 @@ package com.knowledge.base.infrastructure.north.controller;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;
import com.knowledge.base.application.service.UserAppService;
import com.knowledge.base.infrastructure.cache.FileCacheService;
import com.knowledge.base.infrastructure.north.dto.SearchReq;
import com.google.common.collect.Maps;
import com.knowledge.base.infrastructure.north.dto.user.UserFileDTO;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;
/**
* @author Luke.ye
* @date 2025/5/20 16:56
*/
@RestController
@RequestMapping("/search")
public class SearchController {
@RequestMapping("/api/v1/file")
@RequiredArgsConstructor
public class FileQueryController {
private static final Logger LOGGER = LoggerFactory.getLogger(FileQueryController.class);
private static final Logger LOGGER = LoggerFactory.getLogger(SearchController.class);
private final UserAppService userAppService;
private final RestHighLevelClient esClient;
@ -43,11 +46,7 @@ public class SearchController {
@Value("${search.default-page-size:10}")
private int defaultPageSize;
public SearchController(RestHighLevelClient esClient) {
this.esClient = esClient;
}
@PostMapping
@PostMapping("/search")
public Map<String, Object> search(@RequestBody SearchReq requestBody) throws IOException {
int page = requestBody.getPage() != null && requestBody.getPage() > 0 ? requestBody.getPage() : 1;
int size = requestBody.getSize() != null && requestBody.getSize() > 0 ? requestBody.getSize() : defaultPageSize;
@ -174,7 +173,9 @@ public class SearchController {
}
// 查询指定用户被授权文件
@GetMapping("/listUserFiles")
public ResponseEntity<List<UserFileDTO>> listUserFiles(@RequestParam Long userId) {
return ResponseEntity.ok(userAppService.listUserFiles(userId));
}
}

View File

@ -1,5 +1,7 @@
package com.knowledge.base.infrastructure.north.controller;
import com.knowledge.base.application.service.UserAppService;
import com.knowledge.base.domain.doc.service.iface.DocumentImporter;
import com.knowledge.base.infrastructure.cache.FileCacheService;
import com.knowledge.base.infrastructure.util.SafeIdUtil;
import lombok.RequiredArgsConstructor;
@ -13,18 +15,50 @@ import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@RestController
@RequestMapping("/api/v1/doc")
@RequiredArgsConstructor
public class DocMaintenanceController {
public class FileWriteController {
private static final Logger logger = LoggerFactory.getLogger(FileWriteController.class);
private final RestHighLevelClient esClient;
private final FileCacheService fileCacheService;
private static final String INDEX_NAME = "documents";
private static final Logger logger = LoggerFactory.getLogger(DocMaintenanceController.class);
private final UserAppService userAppService;
private List<DocumentImporter> importers;
@PostMapping("/addUserFileAuth")
public ResponseEntity<?> addUserFileAuth(@RequestParam Long userId, @RequestParam Long fileId) {
return ResponseEntity.ok(userAppService.addUserFileAuth(userId, fileId));
}
@PostMapping("/removeUserFileAuth")
public ResponseEntity<?> removeUserFileAuth(@RequestParam Long userId, @RequestParam Long fileId) {
return ResponseEntity.ok(userAppService.removeUserFileAuth(userId, fileId));
}
@GetMapping("/import/{type}")
public ResponseEntity<String> importByType(@PathVariable String type) {
for (DocumentImporter importer : importers) {
if (importer.getType().equalsIgnoreCase(type)) {
try {
importer.importDocuments();
return ResponseEntity.ok(type + " 文件导入成功");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("导入失败: " + e.getMessage());
}
}
}
return ResponseEntity.badRequest().body("不支持的类型: " + type);
}
/**
* 1. 根据文件名同时删除 ES 和缓存Redis/本地的记录

View File

@ -1,31 +0,0 @@
package com.knowledge.base.infrastructure.north.controller;
import com.knowledge.base.domain.doc.service.iface.DocumentImporter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/import")
public class ImportController {
@Autowired
private List<DocumentImporter> importers;
@GetMapping("/{type}")
public ResponseEntity<String> importByType(@PathVariable String type) {
for (DocumentImporter importer : importers) {
if (importer.getType().equalsIgnoreCase(type)) {
try {
importer.importDocuments();
return ResponseEntity.ok(type + " 文件导入成功");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("导入失败: " + e.getMessage());
}
}
}
return ResponseEntity.badRequest().body("不支持的类型: " + type);
}
}

View File

@ -0,0 +1,37 @@
package com.knowledge.base.infrastructure.north.controller;
import com.knowledge.base.application.service.UserAppService;
import com.knowledge.base.infrastructure.north.dto.role.RoleDTO;
import com.knowledge.base.infrastructure.north.dto.role.RoleFileRuleDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/v1/role")
@RequiredArgsConstructor
public class RoleQueryController {
private final UserAppService userAppService;
@GetMapping("/list-roles")
public ResponseEntity<List<RoleDTO>> listAllRoles() {
return ResponseEntity.ok(userAppService.listRoles());
}
@GetMapping("/get")
public ResponseEntity<?> getRoleById(@RequestParam Long id) {
Optional<RoleDTO> roleOpt = userAppService.getRoleById(id);
return roleOpt.<ResponseEntity<?>>map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
@GetMapping("/list-role-file-rules")
public ResponseEntity<List<RoleFileRuleDTO>> listRoleFileRules(@RequestParam Long roleId) {
return ResponseEntity.ok(userAppService.listRoleFileRules(roleId));
}
}

View File

@ -0,0 +1,41 @@
package com.knowledge.base.infrastructure.north.controller;
import com.knowledge.base.application.service.UserAppService;
import com.knowledge.base.infrastructure.north.dto.role.RoleDTO;
import com.knowledge.base.infrastructure.north.dto.role.RoleFileRuleDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/v1/role")
@RequiredArgsConstructor
public class RoleWriteController {
private final UserAppService userAppService;
@PostMapping("/add")
public ResponseEntity<?> addRole(@RequestBody RoleDTO role) {
return ResponseEntity.ok(userAppService.addRole(role));
}
@PostMapping("/update")
public ResponseEntity<?> updateRole(@RequestBody RoleDTO role) {
return ResponseEntity.ok(userAppService.updateRole(role));
}
@PostMapping("/delete")
public ResponseEntity<?> deleteRole(@RequestParam Long id) {
return ResponseEntity.ok(userAppService.deleteRole(id));
}
@PostMapping("/add-role-file-rule")
public ResponseEntity<?> addRoleFileRule(@RequestBody RoleFileRuleDTO rule) {
return ResponseEntity.ok(userAppService.addRoleFileRule(rule));
}
@PostMapping("/remove-role-file-rule")
public ResponseEntity<?> removeRoleFileRule(@RequestParam Long id) {
return ResponseEntity.ok(userAppService.removeRoleFileRule(id));
}
}

View File

@ -0,0 +1,56 @@
package com.knowledge.base.infrastructure.north.controller;
import com.knowledge.base.application.service.UserAppService;
import com.knowledge.base.infrastructure.north.dto.role.UserRoleDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserTokenDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@RestController
@RequestMapping("/api/v1/user")
@RequiredArgsConstructor
public class UserQueryController {
private final UserAppService userAppService;
@GetMapping("/check")
public ResponseEntity<?> check(
@CookieValue(value = "token", required = false) String cookieToken,
@RequestHeader(value = "Authorization", required = false) String headerToken
) {
String token = headerToken != null ? headerToken : cookieToken;
if (userAppService.isValidToken(token)) {
return ResponseEntity.ok(Map.of("code", 0, "msg", "校验成功"));
}
return ResponseEntity.status(401).body(Map.of("code", 401, "msg", "未授权"));
}
@GetMapping("/username")
public ResponseEntity<?> getUsernameByToken(
@CookieValue(value = "token", required = false) String cookieToken,
@RequestHeader(value = "Authorization", required = false) String headerToken
) {
String token = headerToken != null ? headerToken : cookieToken;
Optional<UserTokenDTO> userTokenOpt = userAppService.findToken(token);
if (userTokenOpt.isPresent()) {
Long userId = userTokenOpt.get().getUserId();
Optional<UserDTO> userOpt = userAppService.findById(userId);
if (userOpt.isPresent()) {
return ResponseEntity.ok(Map.of("username", userOpt.get().getUsername()));
}
}
return ResponseEntity.status(401).body(Map.of("msg", "无效token"));
}
@GetMapping("/list-user-roles")
public ResponseEntity<List<UserRoleDTO>> listUserRoles(@RequestParam Long userId) {
return ResponseEntity.ok(userAppService.listUserRoles(userId));
}
}

View File

@ -1,10 +1,10 @@
package com.knowledge.base.infrastructure.north.controller;
import com.knowledge.base.application.service.UserAppService;
import com.knowledge.base.domain.user.repository.po.User;
import com.knowledge.base.domain.user.repository.po.UserToken;
import com.knowledge.base.infrastructure.config.ConstantConfig;
import com.knowledge.base.infrastructure.config.DynamicConfig;
import com.knowledge.base.infrastructure.north.dto.user.UserDTO;
import com.knowledge.base.infrastructure.north.dto.user.UserTokenDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -17,7 +17,7 @@ import java.util.Optional;
@RestController
@RequestMapping("/api/v1/user")
@RequiredArgsConstructor
public class UserController {
public class UserWriteController {
private final UserAppService userAppService;
private final DynamicConfig dynamicConfig;
@ -27,14 +27,14 @@ public class UserController {
String username = body.getOrDefault("username", "").trim();
String password = body.getOrDefault("password", "").trim();
Optional<User> userOpt = userAppService.findByUsername(username);
Optional<UserDTO> userOpt = userAppService.findByUsername(username);
if (userOpt.isEmpty() || !userAppService.verifyPassword(userOpt.get().getUsername(), password)) {
return ResponseEntity.status(401).body(Map.of("message", "用户名或密码错误"));
}
User user = userOpt.get();
UserDTO user = userOpt.get();
long expireMs = dynamicConfig.getTokenExpireTime();
UserToken token = userAppService.createToken(user.getId(), expireMs);
UserTokenDTO token = userAppService.createToken(user.getId(), expireMs);
Cookie cookie = new Cookie(ConstantConfig.COOKIE_KEY, token.getToken());
cookie.setPath("/");
@ -70,32 +70,14 @@ public class UserController {
return ResponseEntity.ok(Map.of("msg", "已登出"));
}
@GetMapping("/check")
public ResponseEntity<?> check(
@CookieValue(value = ConstantConfig.COOKIE_KEY, required = false) String cookieToken,
@RequestHeader(value = "Authorization", required = false) String headerToken
) {
String token = headerToken != null ? headerToken : cookieToken;
if (userAppService.isValidToken(token)) {
return ResponseEntity.ok(Map.of("code", 0, "msg", "校验成功"));
}
return ResponseEntity.status(401).body(Map.of("code", 401, "msg", "未授权"));
@PostMapping("/add-user-role")
public ResponseEntity<?> addUserRole(@RequestParam Long userId, @RequestParam Long roleId) {
return ResponseEntity.ok(userAppService.addUserRole(userId, roleId));
}
@GetMapping("/username")
public ResponseEntity<?> getUsernameByToken(
@CookieValue(value = ConstantConfig.COOKIE_KEY, required = false) String cookieToken,
@RequestHeader(value = "Authorization", required = false) String headerToken
) {
String token = headerToken != null ? headerToken : cookieToken;
Optional<UserToken> userTokenOpt = userAppService.findToken(token);
if (userTokenOpt.isPresent()) {
Long userId = userTokenOpt.get().getUserId();
Optional<User> userOpt = userAppService.findById(userId);
if (userOpt.isPresent()) {
return ResponseEntity.ok(Map.of("username", userOpt.get().getUsername()));
}
}
return ResponseEntity.status(401).body(Map.of("msg", "无效token"));
@PostMapping("/remove-user-role")
public ResponseEntity<?> removeUserRole(@RequestParam Long userId, @RequestParam Long roleId) {
return ResponseEntity.ok(userAppService.removeUserRole(userId, roleId));
}
}

View File

@ -0,0 +1,16 @@
package com.knowledge.base.infrastructure.north.dto.doc;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:50
*/
@Data
public class FileDTO {
public Long id;
public String fileName;
public String filePath;
public String addTime; // 建议前端用字符串格式
public String updateTime;
}

View File

@ -0,0 +1,14 @@
package com.knowledge.base.infrastructure.north.dto.role;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:37
*/
@Data
public class RoleDTO {
public Long id;
public String roleCode;
public String roleName;
}

View File

@ -0,0 +1,15 @@
package com.knowledge.base.infrastructure.north.dto.role;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:39
*/
@Data
public class RoleFileRuleDTO {
public Long id;
public Long roleId;
public String filePattern;
public String remark;
}

View File

@ -0,0 +1,15 @@
package com.knowledge.base.infrastructure.north.dto.role;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:37
*/
@Data
public class UserRoleDTO {
public Long id;
public Long userId;
public Long roleId;
}

View File

@ -0,0 +1,14 @@
package com.knowledge.base.infrastructure.north.dto.user;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:36
*/
@Data
public class UserDTO {
public Long id;
public String username;
}

View File

@ -0,0 +1,14 @@
package com.knowledge.base.infrastructure.north.dto.user;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:38
*/
@Data
public class UserFileDTO {
public Long id;
public Long userId;
public Long fileId;
}

View File

@ -0,0 +1,16 @@
package com.knowledge.base.infrastructure.north.dto.user;
import lombok.Data;
/**
* @author Luke.ye
* @date 2025/6/9 10:40
*/
@Data
public class UserTokenDTO {
public Long id;
public Long userId;
public String token;
public String loginAt; // 建议用字符串时间方便前端展示
public String expiredAt;
}

View File

@ -37,3 +37,6 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.logic-delete-field=deleted
mybatis-plus.global-config.db-config.id-type=auto
# Redis 开关控制
knowledge.base.redis.enable=false

View File

@ -37,3 +37,6 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.logic-delete-field=deleted
mybatis-plus.global-config.db-config.id-type=auto
# Redis 开关控制
knowledge.base.redis.enable=false