xin
2025-07-08 3660c62eca4ec4bf3fda22607abdf6d702f06abd
oying-system/src/main/java/com/oying/modules/system/service/impl/UserServiceImpl.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.domain.BucketStorage;
import com.oying.modules.security.service.OnlineUserService;
import com.oying.modules.security.service.UserCacheManager;
import com.oying.modules.system.domain.Job;
@@ -12,6 +13,7 @@
import com.oying.modules.system.mapper.UserMapper;
import com.oying.modules.system.mapper.UserRoleMapper;
import com.oying.modules.system.service.UserService;
import com.oying.service.BucketStorageService;
import com.oying.utils.*;
import lombok.RequiredArgsConstructor;
import com.oying.config.properties.FileProperties;
@@ -20,9 +22,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -43,6 +45,7 @@
    private final RedisUtils redisUtils;
    private final UserCacheManager userCacheManager;
    private final OnlineUserService onlineUserService;
    private final BucketStorageService bucketStorageService;
    @Override
    public PageResult<User> queryAll(UserQueryCriteria criteria, Page<Object> page) {
@@ -112,7 +115,7 @@
            redisUtils.del(CacheKey.ROLE_USER + resources.getId());
        }
        // 如果用户被禁用,则清除用户登录信息
        if(!resources.getEnabled()){
        if (!resources.getEnabled()) {
            onlineUserService.kickOutForUsername(resources.getUsername());
        }
        user.setUsername(resources.getUsername());
@@ -210,22 +213,22 @@
        // 验证文件上传的格式
        String image = "gif jpg png jpeg";
        String fileType = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
        if(fileType != null && !image.contains(fileType)){
            throw new BadRequestException("文件格式错误!, 仅支持 " + image +" 格式");
        if (fileType != null && !image.contains(fileType)) {
            throw new BadRequestException("文件格式错误!, 仅支持 " + image + " 格式");
        }
        User user = userMapper.findByUsername(SecurityUtils.getCurrentUsername());
        String oldPath = user.getAvatarPath();
        File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar());
        user.setAvatarPath(Objects.requireNonNull(file).getPath());
        user.setAvatarName(file.getName());
        BucketStorage bucketStorage = bucketStorageService.create(null, multipartFile);
        user.setAvatarPath(bucketStorage.getPath());
        user.setAvatarName(bucketStorage.getRealName());
        saveOrUpdate(user);
        if (StringUtils.isNotBlank(oldPath)) {
            FileUtil.del(oldPath);
            bucketStorageService.deleteById(oldPath);
        }
        @NotBlank String username = user.getUsername();
        flushCache(username);
        return new HashMap<String, String>(1) {{
            put("avatar", file.getName());
            put("avatar", bucketStorage.getRealName());
        }};
    }