新增:新增修改基础信息 API(优化 Jackson 针对通用枚举接口 IEnum 的序列化和反序列化)

This commit is contained in:
2023-01-09 22:41:09 +08:00
parent 5252c54c48
commit 76fb698a37
21 changed files with 442 additions and 140 deletions

View File

@@ -65,7 +65,7 @@ public class UserInfoVO implements Serializable {
/**
* 性别0未知 1男 2女
*/
@Schema(description = "性别0未知 1男 2女")
@Schema(description = "性别0未知 1男 2女", type = "Integer", allowableValues = {"0", "1", "2"})
private GenderEnum gender;
/**

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.model.request;
import java.io.Serializable;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import top.charles7c.cnadmin.common.enums.GenderEnum;
/**
* 修改基础信息
*
* @author Charles7c
* @since 2023/1/7 23:08
*/
@Data
@Schema(description = "修改基础信息")
public class UpdateBasicInfoRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 昵称
*/
@Schema(description = "昵称")
@NotBlank(message = "昵称不能为空")
@Size(max = 32, message = "昵称长度不能超过 32 个字符")
private String nickname;
/**
* 性别0未知 1男 2女
*/
@Schema(description = "性别0未知 1男 2女", type = "Integer", allowableValues = {"0", "1", "2"})
@NotNull(message = "非法性别")
private GenderEnum gender;
}

View File

@@ -16,6 +16,8 @@
package top.charles7c.cnadmin.system.service;
import org.springframework.web.multipart.MultipartFile;
import top.charles7c.cnadmin.system.model.entity.SysUser;
/**
@@ -36,12 +38,21 @@ public interface UserService {
SysUser getByUsername(String username);
/**
* 修改头像
* 上传头像
*
* @param avatar
* 头像路径
* 头像文件
* @param userId
* 用户ID
* 用户 ID
* @return 新头像路径
*/
void updateAvatar(String avatar, Long userId);
String uploadAvatar(MultipartFile avatar, Long userId);
/**
* 修改信息
*
* @param user
* 用户信息
*/
void update(SysUser user);
}

View File

@@ -16,13 +16,26 @@
package top.charles7c.cnadmin.system.service.impl;
import java.io.File;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties;
import top.charles7c.cnadmin.common.model.dto.LoginUser;
import top.charles7c.cnadmin.common.util.FileUtils;
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.mapper.UserMapper;
import top.charles7c.cnadmin.system.model.entity.SysUser;
import top.charles7c.cnadmin.system.service.UserService;
@@ -38,6 +51,7 @@ import top.charles7c.cnadmin.system.service.UserService;
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
private final LocalStorageProperties localStorageProperties;
@Override
public SysUser getByUsername(String username) {
@@ -45,8 +59,41 @@ public class UserServiceImpl implements UserService {
}
@Override
public void updateAvatar(String avatar, Long userId) {
@Transactional(rollbackFor = Exception.class)
public String uploadAvatar(MultipartFile avatarFile, Long userId) {
// 上传新头像
String avatarPath = localStorageProperties.getPath().getAvatar();
File newAvatarFile = FileUtils.upload(avatarFile, avatarPath, false);
CheckUtils.exIfNull(newAvatarFile, "上传头像失败");
assert newAvatarFile != null;
// 更新用户头像
String newAvatar = newAvatarFile.getName();
userMapper.update(null,
new LambdaUpdateWrapper<SysUser>().set(SysUser::getAvatar, avatar).eq(SysUser::getUserId, userId));
new LambdaUpdateWrapper<SysUser>().set(SysUser::getAvatar, newAvatar).eq(SysUser::getUserId, userId));
// 删除原头像
LoginUser loginUser = LoginHelper.getLoginUser();
String oldAvatar = loginUser.getAvatar();
if (StrUtil.isNotBlank(loginUser.getAvatar())) {
FileUtil.del(avatarPath + oldAvatar);
}
// 更新登录用户信息
loginUser.setAvatar(newAvatar);
LoginHelper.updateLoginUser(loginUser);
return newAvatar;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(SysUser user) {
userMapper.updateById(user);
// 更新登录用户信息
SysUser sysUser = userMapper.selectById(user.getUserId());
LoginUser loginUser = LoginHelper.getLoginUser();
BeanUtil.copyProperties(sysUser, loginUser);
LoginHelper.updateLoginUser(loginUser);
}
}