fix: 修复super管理员获取菜单时因companyId为null抛异常的问题

- 将getCurrentCompanyId()调用延迟到switch分支内部
- super角色不需要公司上下文,直接获取所有菜单
- admin/user角色按需获取公司ID
This commit is contained in:
小蟑螂
2026-03-29 02:41:54 +08:00
parent 6a1b6d09c0
commit 87961c3679
28 changed files with 98 additions and 1860 deletions
@@ -35,6 +35,16 @@ public interface SysFileService {
*/
List<SysFileUploadVO> batchUploadFiles(List<MultipartFile> files, String category);
/**
* 批量上传文件(指定所属公司)
*
* @param files 上传的文件列表
* @param category 文件分类
* @param companyId 所属公司ID(可为空)
* @return 上传结果
*/
List<SysFileUploadVO> batchUploadFiles(List<MultipartFile> files, String category, Long companyId);
/**
* 批量获取URL签名(用于前端上传到OSS)
*
@@ -54,7 +54,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
throw new BusinessException("文件列表不能为空");
}
Long companyId = currentUserContext.getCurrentCompanyId();
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
Long userId = currentUserContext.getCurrentUserId();
List<SysFileUploadVO> result = new ArrayList<>();
@@ -88,7 +88,6 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
// 检查是否有其他公司已有相同哈希的文件
LambdaQueryWrapper<SysFilePO> otherCompanyWrapper = new LambdaQueryWrapper<>();
otherCompanyWrapper.eq(SysFilePO::getFileHash, fileHash)
.ne(SysFilePO::getCompanyId, companyId)
.isNull(SysFilePO::getDeleteTime)
.last("LIMIT 1");
SysFilePO otherCompanyFile = this.getOne(otherCompanyWrapper);
@@ -167,130 +166,61 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
@Override
@Transactional
public List<SysFileUploadVO> batchUploadFiles(List<MultipartFile> files, String category) {
if (files == null || files.isEmpty()) {
throw new BusinessException("文件列表不能为空");
}
Long companyId = currentUserContext.getCurrentCompanyId();
Long userId = currentUserContext.getCurrentUserId();
List<SysFileUploadVO> result = new ArrayList<>();
for (MultipartFile file : files) {
if (file.isEmpty()) {
continue;
}
// 计算文件哈希
String fileHash = FileHashUtils.calculateFileHash(file);
// 检查当前公司是否已有相同哈希的文件
LambdaQueryWrapper<SysFilePO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysFilePO::getCompanyId, companyId)
.eq(SysFilePO::getFileHash, fileHash)
.isNull(SysFilePO::getDeleteTime)
.last("LIMIT 1");
SysFilePO existingFile = this.getOne(wrapper);
if (existingFile != null) {
// 当前公司已有相同文件,先验证文件是否真的存在可访问
if (verifyFileExists(existingFile)) {
// 如果文件已存在但category不同,更新category
if (category != null && !category.equals(existingFile.getCategory())) {
existingFile.setCategory(category);
this.updateById(existingFile);
}
result.add(convertToUploadVO(existingFile));
continue;
} else {
// 文件不存在,删除无效记录,继续处理
this.removeById(existingFile.getId());
}
}
// 检查是否有其他公司已有相同哈希的文件
LambdaQueryWrapper<SysFilePO> otherCompanyWrapper = new LambdaQueryWrapper<>();
otherCompanyWrapper.eq(SysFilePO::getFileHash, fileHash)
.ne(SysFilePO::getCompanyId, companyId)
.isNull(SysFilePO::getDeleteTime)
.last("LIMIT 1");
SysFilePO otherCompanyFile = this.getOne(otherCompanyWrapper);
String storagePath = null;
String storageType = "local"; // 上传接口始终使用本地存储
if (otherCompanyFile != null) {
// 其他公司已有相同文件,先验证文件是否真的存在可访问
if (verifyFileExists(otherCompanyFile)) {
// 文件存在,复制存储路径和类型
storagePath = otherCompanyFile.getStoragePath();
storageType = otherCompanyFile.getStorageType();
// 如果是本地存储,需要重新上传到当前公司的存储路径
if ("local".equals(storageType)) {
String relativePath = generateStoragePath(file.getOriginalFilename(), fileHash);
storagePath = "./" + relativePath;
saveFileToLocal(storagePath, file);
}
// 如果是OSS存储,直接使用相同的存储路径(OSS文件可以跨公司共享)
// 创建当前公司的文件记录
SysFilePO sysFile = new SysFilePO();
BeanUtils.copyProperties(otherCompanyFile, sysFile);
sysFile.setId(null);
sysFile.setCompanyId(companyId);
sysFile.setStoragePath(storagePath);
sysFile.setStorageType(storageType);
sysFile.setFilename(file.getOriginalFilename());
sysFile.setFileFormat(getFileExtension(file.getOriginalFilename()).toLowerCase());
sysFile.setMimeType(file.getContentType());
sysFile.setFileSize(file.getSize());
sysFile.setFileHash(fileHash);
sysFile.setCategory(category); // 设置文件分类
sysFile.setUploadBy(userId);
sysFile.setUploadTime(LocalDateTime.now());
sysFile.setUpdateTime(null);
sysFile.setUpdateBy(null);
sysFile.setDeleteTime(null);
sysFile.setDeleteBy(null);
this.save(sysFile);
result.add(convertToUploadVO(sysFile));
} else {
// 文件不存在,删除无效记录,继续处理(当作新文件处理)
}
}
// 如果没有找到有效的其他公司文件,或者文件不存在,则作为新文件处理
if (storagePath == null) {
// 没有任何公司有相同文件,生成新的存储路径并上传(仅本地存储)
String relativePath = generateStoragePath(file.getOriginalFilename(), fileHash);
storagePath = "./" + relativePath;
saveFileToLocal(storagePath, file);
// 创建新记录
SysFilePO sysFile = new SysFilePO();
sysFile.setCompanyId(companyId);
sysFile.setStorageType(storageType);
sysFile.setStoragePath(storagePath);
sysFile.setFilename(file.getOriginalFilename());
sysFile.setFileFormat(getFileExtension(file.getOriginalFilename()).toLowerCase());
sysFile.setMimeType(file.getContentType());
sysFile.setFileSize(file.getSize());
sysFile.setFileHash(fileHash);
sysFile.setCategory(category); // 设置文件分类
sysFile.setUploadBy(userId);
this.save(sysFile);
result.add(convertToUploadVO(sysFile));
}
}
return result;
return batchUploadFiles(files, category, null);
}
@Override
@Transactional
public List<SysFileUploadVO> batchUploadFiles(List<MultipartFile> files, String category, Long overrideCompanyId) {
if (files == null || files.isEmpty()) {
throw new BusinessException("文件列表不能为空");
}
Long companyId = overrideCompanyId != null ? overrideCompanyId : (currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null);
Long userId = currentUserContext.getCurrentUserId();
List<SysFileUploadVO> result = new ArrayList<>();
for (MultipartFile file : files) {
if (file.isEmpty()) { continue; }
String fileHash = FileHashUtils.calculateFileHash(file);
// 查询当前公司是否已有相同哈希的文件
LambdaQueryWrapper<SysFilePO> wrapper = new LambdaQueryWrapper<>();
if (companyId != null) {
wrapper.eq(SysFilePO::getCompanyId, companyId);
} else {
wrapper.isNull(SysFilePO::getCompanyId);
}
wrapper.eq(SysFilePO::getFileHash, fileHash)
.isNull(SysFilePO::getDeleteTime)
.last("LIMIT 1");
SysFilePO existingFile = this.getOne(wrapper);
if (existingFile != null && verifyFileExists(existingFile)) {
result.add(convertToUploadVO(existingFile));
continue;
}
String storageType = "local";
String storagePath = "./" + generateStoragePath(file.getOriginalFilename(), fileHash);
saveFileToLocal(storagePath, file);
SysFilePO sysFile = new SysFilePO();
sysFile.setCompanyId(companyId);
sysFile.setStorageType(storageType);
sysFile.setStoragePath(storagePath);
sysFile.setFilename(file.getOriginalFilename());
sysFile.setFileFormat(getFileExtension(file.getOriginalFilename()).toLowerCase());
sysFile.setMimeType(file.getContentType());
sysFile.setFileSize(file.getSize());
sysFile.setFileHash(fileHash);
sysFile.setCategory(category);
sysFile.setUploadBy(userId);
sysFile.setUploadTime(LocalDateTime.now());
this.save(sysFile);
result.add(convertToUploadVO(sysFile));
}
return result;
}
@Override
public BatchSignUrlVO batchSignUrl(BatchSignUrlDTO dto) {
Long companyId = currentUserContext.getCurrentCompanyId();
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
List<BatchSignUrlVO.FileSignInfo> fileSignInfos = new ArrayList<>();
for (BatchSignUrlDTO.FileItem item : dto.getFiles()) {
@@ -402,7 +332,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
throw new BusinessException("文件列表不能为空");
}
Long companyId = currentUserContext.getCurrentCompanyId();
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
Long userId = currentUserContext.getCurrentUserId();
List<SysFileVO> result = new ArrayList<>();
@@ -555,7 +485,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
throw new BusinessException("文件ID不能为空");
}
Long companyId = currentUserContext.getCurrentCompanyId();
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
// 查询文件记录
SysFilePO sysFile = this.getById(fileId);
@@ -563,8 +493,8 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
throw new BusinessException("文件不存在");
}
// 验证文件是否属于当前公司
if (!sysFile.getCompanyId().equals(companyId)) {
// 验证文件是否属于当前公司(company_id为null的文件如用户头像等,不做公司校验)
if (sysFile.getCompanyId() != null && !sysFile.getCompanyId().equals(companyId)) {
throw new BusinessException("无权访问该文件");
}
@@ -603,7 +533,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
@Override
public PageResultVO<SysFileVO> getFilePage(PageQueryDTO<SysFileFilterDTO> pageQueryDTO) {
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
// 创建分页对象
Page<SysFilePO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
@@ -662,9 +592,9 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
@Override
public void deleteFile(Long id) {
Long companyId = currentUserContext.getCurrentCompanyId();
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
SysFilePO file = this.getById(id);
if (file == null || !file.getCompanyId().equals(companyId)) {
if (file == null || (file.getCompanyId() != null && !file.getCompanyId().equals(companyId))) {
throw new BusinessException("文件不存在");
}
// 删除数据库记录(物理文件由OSS生命周期或定时任务清理)
@@ -673,9 +603,9 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
@Override
public void updateFile(Long id, SysFileUpdateDTO dto) {
Long companyId = currentUserContext.getCurrentCompanyId();
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
SysFilePO file = this.getById(id);
if (file == null || !file.getCompanyId().equals(companyId)) {
if (file == null || (file.getCompanyId() != null && !file.getCompanyId().equals(companyId))) {
throw new BusinessException("文件不存在");
}
// 更新元信息
@@ -71,18 +71,17 @@ public class UserMenuServiceImpl implements UserMenuService {
// 获取用户角色
String userRole = user.getRole();
// 获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
List<MenuVO> menus;
switch (userRole) {
case "super":
// 超级管理员获取所有系统菜单
// 超级管理员获取所有系统菜单,不需要公司上下文
menus = menuService.getAllMenus();
break;
case "admin":
// 公司管理员获取公司所有菜单
Long companyId = currentUserContext.getCurrentCompanyId();
if (companyId == null) {
menus = List.of();
} else {
@@ -91,12 +90,13 @@ public class UserMenuServiceImpl implements UserMenuService {
break;
case "user":
// 普通用户,根据公司成员角色判断
CompanyMemberPO member = companyMemberService.getByUserIdAndCompanyId(userId, companyId);
Long companyIdForUser = currentUserContext.getCurrentCompanyId();
CompanyMemberPO member = companyMemberService.getByUserIdAndCompanyId(userId, companyIdForUser);
if (member == null) {
menus = List.of();
} else if (member.getRole() == CompanyMemberRole.owner) {
// owner 角色获取公司所有菜单
menus = companyMenuService.getCompanyMenusForRoute(companyId);
menus = companyMenuService.getCompanyMenusForRoute(companyIdForUser);
} else {
// member 角色根据职位获取菜单
if (member.getPositionId() == null) {
@@ -22,8 +22,8 @@ import top.xyquan.ecomdmbbackend.modules.org.company.model.po.CompanyPO;
import top.xyquan.ecomdmbbackend.modules.sys.auth.model.vo.LoginVO;
import top.xyquan.ecomdmbbackend.modules.sys.user.model.vo.UserVO;
import top.xyquan.ecomdmbbackend.modules.sys.user.service.UserService;
import top.xyquan.ecomdmbbackend.modules.media.service.MediaImageService;
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
import top.xyquan.ecomdmbbackend.modules.sys.file.service.SysFileService;
import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO;
import top.xyquan.ecomdmbbackend.utils.JwtUtils;
import top.xyquan.ecomdmbbackend.utils.SecurityUtils;
import top.xyquan.ecomdmbbackend.utils.AccessTokenPayload;
@@ -42,18 +42,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
private final PasswordEncoder passwordEncoder;
private final JwtUtils jwtUtils;
private final MediaImageService mediaImageService;
private final SysFileService sysFileService;
private final top.xyquan.ecomdmbbackend.modules.org.companymember.service.CompanyMemberService companyMemberService;
private final top.xyquan.ecomdmbbackend.modules.org.departmentmember.service.DepartmentMemberService departmentMemberService;
private final top.xyquan.ecomdmbbackend.modules.org.company.service.CompanyService companyService;
public UserServiceImpl(PasswordEncoder passwordEncoder, JwtUtils jwtUtils, MediaImageService mediaImageService,
public UserServiceImpl(PasswordEncoder passwordEncoder, JwtUtils jwtUtils, SysFileService sysFileService,
@Lazy top.xyquan.ecomdmbbackend.modules.org.companymember.service.CompanyMemberService companyMemberService,
top.xyquan.ecomdmbbackend.modules.org.departmentmember.service.DepartmentMemberService departmentMemberService,
top.xyquan.ecomdmbbackend.modules.org.company.service.CompanyService companyService) {
this.passwordEncoder = passwordEncoder;
this.jwtUtils = jwtUtils;
this.mediaImageService = mediaImageService;
this.sysFileService = sysFileService;
this.companyMemberService = companyMemberService;
this.departmentMemberService = departmentMemberService;
this.companyService = companyService;
@@ -218,11 +218,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
// 处理头像图片ID
if (updateUserInfoDTO.getAvatarImageId() != null) {
MediaImageVO avatarImage = mediaImageService.getImageById(updateUserInfoDTO.getAvatarImageId());
if (avatarImage != null) {
user.setAvatarImageId(updateUserInfoDTO.getAvatarImageId());
user.setAvatar(avatarImage.getUrl());
}
user.setAvatarImageId(updateUserInfoDTO.getAvatarImageId());
user.setAvatar(sysFileService.getFileAccessUrl(updateUserInfoDTO.getAvatarImageId()));
}
if (StringUtils.hasText(updateUserInfoDTO.getPhone())) {
@@ -293,8 +290,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
}
// 上传头像到媒体模块(支持哈希校验和共享)
MediaImageVO mediaImageVO = mediaImageService.uploadImageWithType(file, "avatar");
String avatarUrl = mediaImageVO.getUrl();
List<SysFileUploadVO> uploaded = sysFileService.batchUploadFiles(java.util.List.of(file), "avatar");
String avatarUrl = uploaded.isEmpty() ? null : sysFileService.getFileAccessUrl(uploaded.get(0).getId());
// 更新用户头像信息
user.setAvatar(avatarUrl);