用户管理一些问题修复
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.xiaowang.yuminailcore.config;
|
||||
|
||||
import io.minio.MinioClient;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "minio")
|
||||
public class MinioConfig {
|
||||
|
||||
private String endpoint;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String bucketName;
|
||||
|
||||
@Bean
|
||||
public MinioClient minioClient() {
|
||||
return MinioClient.builder()
|
||||
.endpoint(endpoint)
|
||||
.credentials(accessKey, secretKey)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.xiaowang.yuminailcore.controller;
|
||||
|
||||
import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.service.MinioService;
|
||||
import com.xiaowang.yuminailcore.service.ObjectStorageFileService;
|
||||
import com.xiaowang.yuminailcore.vo.ObjectStorageFileVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 前台文件控制器
|
||||
* 提供前台用户可以访问的文件相关接口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/file")
|
||||
@RequiredArgsConstructor
|
||||
public class FrontendFileController {
|
||||
|
||||
private final ObjectStorageFileService objectStorageFileService;
|
||||
private final MinioService minioService;
|
||||
|
||||
/**
|
||||
* 获取带签名的图片链接(前台用户可访问)
|
||||
*
|
||||
* @param fileId 文件ID
|
||||
* @param expiryTimeInMinutes 有效期(分钟),默认60分钟
|
||||
* @return 带签名的临时访问链接
|
||||
*/
|
||||
@GetMapping("/get-signed-url/{fileId}")
|
||||
public Result<String> getSignedUrl(@PathVariable Long fileId, @RequestParam(defaultValue = "60") Integer expiryTimeInMinutes) {
|
||||
try {
|
||||
ObjectStorageFileVO fileVO = objectStorageFileService.getObjectStorageFileById(fileId);
|
||||
if (fileVO == null) {
|
||||
return Result.error("文件不存在");
|
||||
}
|
||||
|
||||
// 从fileUrl中提取objectName
|
||||
String fileUrl = fileVO.getFileUrl();
|
||||
// 提取最后一个/后面的部分作为objectName
|
||||
int lastSlashIndex = fileUrl.lastIndexOf("/");
|
||||
if (lastSlashIndex == -1) {
|
||||
return Result.error("文件URL格式错误");
|
||||
}
|
||||
String objectName = fileUrl.substring(lastSlashIndex + 1);
|
||||
|
||||
String signedUrl = minioService.getPresignedUrl(objectName, expiryTimeInMinutes);
|
||||
return Result.success(signedUrl);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Result.error("获取签名链接失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xiaowang.yuminailcore.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface MinioService {
|
||||
|
||||
String uploadFile(MultipartFile file, String objectName) throws Exception;
|
||||
|
||||
InputStream downloadFile(String objectName) throws Exception;
|
||||
|
||||
void deleteFile(String objectName) throws Exception;
|
||||
|
||||
String getFileUrl(String objectName);
|
||||
|
||||
String getPresignedUrl(String objectName, int expiryTimeInMinutes) throws Exception;
|
||||
|
||||
boolean bucketExists(String bucketName) throws Exception;
|
||||
|
||||
void makeBucket(String bucketName) throws Exception;
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package com.xiaowang.yuminailcore.service.impl;
|
||||
|
||||
import com.xiaowang.yuminailcore.config.MinioConfig;
|
||||
import com.xiaowang.yuminailcore.service.MinioService;
|
||||
import io.minio.*;
|
||||
import io.minio.http.Method;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MinioServiceImpl implements MinioService {
|
||||
|
||||
private final MinioClient minioClient;
|
||||
private final MinioConfig minioConfig;
|
||||
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file, String objectName) throws Exception {
|
||||
try {
|
||||
InputStream inputStream = file.getInputStream();
|
||||
long fileSize = file.getSize();
|
||||
String contentType = file.getContentType();
|
||||
|
||||
minioClient.putObject(
|
||||
PutObjectArgs.builder()
|
||||
.bucket(minioConfig.getBucketName())
|
||||
.object(objectName)
|
||||
.stream(inputStream, fileSize, -1)
|
||||
.contentType(contentType)
|
||||
.build()
|
||||
);
|
||||
|
||||
log.info("文件上传成功: {}", objectName);
|
||||
return getFileUrl(objectName);
|
||||
} catch (Exception e) {
|
||||
log.error("文件上传失败: {}", objectName, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream downloadFile(String objectName) throws Exception {
|
||||
return minioClient.getObject(
|
||||
GetObjectArgs.builder()
|
||||
.bucket(minioConfig.getBucketName())
|
||||
.object(objectName)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFile(String objectName) throws Exception {
|
||||
minioClient.removeObject(
|
||||
RemoveObjectArgs.builder()
|
||||
.bucket(minioConfig.getBucketName())
|
||||
.object(objectName)
|
||||
.build()
|
||||
);
|
||||
log.info("文件删除成功: {}", objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileUrl(String objectName) {
|
||||
return minioConfig.getEndpoint() + "/" + minioConfig.getBucketName() + "/" + objectName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPresignedUrl(String objectName, int expiryTimeInMinutes) throws Exception {
|
||||
String url = minioClient.getPresignedObjectUrl(
|
||||
GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.GET)
|
||||
.bucket(minioConfig.getBucketName())
|
||||
.object(objectName)
|
||||
.expiry(expiryTimeInMinutes, TimeUnit.MINUTES)
|
||||
.build()
|
||||
);
|
||||
log.info("生成带签名的临时访问链接: {}, 有效期: {}分钟", objectName, expiryTimeInMinutes);
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean bucketExists(String bucketName) throws Exception {
|
||||
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeBucket(String bucketName) throws Exception {
|
||||
if (!bucketExists(bucketName)) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
log.info("Bucket 创建成功: {}", bucketName);
|
||||
}
|
||||
}
|
||||
}
|
||||
+51
-2
@@ -37,12 +37,19 @@ import java.util.stream.Collectors;
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {
|
||||
|
||||
// 注入用户数据访问层、JWT工具类和店铺服务
|
||||
private final UserMapper userMapper;
|
||||
private final JwtUtil jwtUtil;
|
||||
private final ShopService shopService;
|
||||
|
||||
/**
|
||||
* 创建新用户
|
||||
* @param userDTO 用户数据传输对象
|
||||
* @return 新创建用户的ID
|
||||
*/
|
||||
@Override
|
||||
public Long createUser(UserDTO userDTO) {
|
||||
// 检查账号是否已存在
|
||||
UserDO existingUser = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, userDTO.getAccount())
|
||||
@@ -52,6 +59,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
throw new RuntimeException("账号已存在");
|
||||
}
|
||||
|
||||
// 创建用户实体并设置属性
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setShopId(userDTO.getShopId());
|
||||
userDO.setNickname(userDTO.getNickname());
|
||||
@@ -64,14 +72,17 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO.setVipId(userDTO.getVipId());
|
||||
userDO.setIsDelete(0);
|
||||
userDO.setStatus(1);
|
||||
// 设置默认头像
|
||||
String defaultAvatar = "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png";
|
||||
userDO.setAvatar(defaultAvatar);
|
||||
|
||||
try {
|
||||
// 插入用户数据
|
||||
userMapper.insert(userDO);
|
||||
} catch (Exception e) {
|
||||
// 捕获重复账号异常
|
||||
if (e.getMessage() != null && e.getMessage().contains("Duplicate entry")) {
|
||||
throw new RuntimeException("账号已存在");
|
||||
throw new RuntimeException("账号已被使用");
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
@@ -79,13 +90,21 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
return userDO.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
* @param id 用户ID
|
||||
* @param userDTO 用户数据传输对象
|
||||
* @return 更新是否成功
|
||||
*/
|
||||
@Override
|
||||
public boolean updateUser(Long id, UserDTO userDTO) {
|
||||
// 查询用户是否存在
|
||||
UserDO userDO = userMapper.selectById(id);
|
||||
if (userDO == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 更新账号(如果提供且变更)
|
||||
if (userDTO.getAccount() != null && !userDTO.getAccount().equals(userDO.getAccount())) {
|
||||
UserDO existingUser = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
@@ -98,6 +117,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO.setAccount(userDTO.getAccount());
|
||||
}
|
||||
|
||||
// 更新其他字段(如果提供)
|
||||
if (userDTO.getShopId() != null) {
|
||||
userDO.setShopId(userDTO.getShopId());
|
||||
}
|
||||
@@ -133,11 +153,21 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
return userMapper.updateById(userDO) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
* @param id 用户ID
|
||||
* @return 删除是否成功
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteUser(Long id) {
|
||||
return removeById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取用户信息
|
||||
* @param id 用户ID
|
||||
* @return 用户视图对象
|
||||
*/
|
||||
@Override
|
||||
public UserVO getUserById(Long id) {
|
||||
UserDO userDO = userMapper.selectById(id);
|
||||
@@ -147,6 +177,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
return convertToVO(userDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有用户列表
|
||||
* @return 用户视图对象列表
|
||||
*/
|
||||
@Override
|
||||
public List<UserVO> listAllUsers() {
|
||||
List<UserDO> userDOs = userMapper.selectList(
|
||||
@@ -158,6 +192,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据店铺ID获取用户列表
|
||||
* @param shopId 店铺ID
|
||||
* @return 用户视图对象列表
|
||||
*/
|
||||
@Override
|
||||
public List<UserVO> listUsersByShopId(Long shopId) {
|
||||
List<UserDO> userDOs = userMapper.selectList(
|
||||
@@ -170,6 +209,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户类型获取用户列表
|
||||
* @param type 用户类型
|
||||
* @return 用户视图对象列表
|
||||
*/
|
||||
@Override
|
||||
public List<UserVO> listUsersByType(Integer type) {
|
||||
List<UserDO> userDOs = userMapper.selectList(
|
||||
@@ -182,6 +226,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据账号获取用户信息
|
||||
* @param account 用户账号
|
||||
* @return 用户视图对象
|
||||
*/
|
||||
@Override
|
||||
public UserVO getUserByAccount(String account) {
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
@@ -196,7 +245,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
|
||||
/**
|
||||
* 将 DO 转换为 VO
|
||||
*
|
||||
* @param userDO 用户DO
|
||||
* @param userDO 用户DO
|
||||
* @return 用户VO
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
-- 为t_user表添加status字段
|
||||
-- 执行时间:2026-02-25
|
||||
|
||||
ALTER TABLE `t_user`
|
||||
ADD COLUMN `status` TINYINT UNSIGNED DEFAULT 1 COMMENT '用户状态:1-正常,0-禁用'
|
||||
AFTER `is_delete`;
|
||||
|
||||
-- 为现有用户设置默认状态为正常(1)
|
||||
UPDATE `t_user` SET `status` = 1 WHERE `status` IS NULL;
|
||||
Reference in New Issue
Block a user