mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-15 03:25:06 +08:00
[update] 新增修改昵称,修改头像,查询昵称与头像接口
This commit is contained in:
@ -1,3 +1,11 @@
|
||||
### 请求 /system/user/profile/get 接口 => 没有权限
|
||||
GET {{userServerUrl}}/system/user/profile/get
|
||||
Authorization: Bearer test245
|
||||
|
||||
### 请求 /system/user/profile/revise-nickname 接口 成功
|
||||
PUT http://localhost:28080/api/system/user/profile/revise-nickname?nickName=yunai111
|
||||
Authorization: Bearer test245
|
||||
|
||||
### 请求 /system/user/profile/get-user-info 接口 成功
|
||||
GET http://localhost:28080/api/system/user/profile/get-user-info?id=245
|
||||
Authorization: Bearer test245
|
@ -1,14 +1,24 @@
|
||||
package cn.iocoder.yudao.userserver.modules.member.controller.user;
|
||||
|
||||
import cn.iocoder.yudao.coreservice.modules.system.controller.user.vo.SysUserCoreProfileRespVo;
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
|
||||
import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
||||
import static cn.iocoder.yudao.userserver.modules.member.enums.MbrErrorCodeConstants.FILE_IS_EMPTY;
|
||||
|
||||
@Api(tags = "用户个人中心")
|
||||
@RestController
|
||||
@ -17,6 +27,9 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@Slf4j
|
||||
public class SysUserProfileController {
|
||||
|
||||
@Resource
|
||||
private MbrUserService userService;
|
||||
|
||||
@GetMapping("/get")
|
||||
@ApiOperation("获得登录用户信息")
|
||||
@PreAuthenticated
|
||||
@ -24,4 +37,27 @@ public class SysUserProfileController {
|
||||
return null;
|
||||
}
|
||||
|
||||
@PutMapping("/revise-nickname")
|
||||
@ApiOperation("修改用户昵称")
|
||||
public CommonResult<Boolean> reviseNickname(@RequestParam("nickName") String nickName) {
|
||||
userService.reviseNickname(getLoginUserId(), nickName);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@PutMapping("/revise-avatar")
|
||||
@ApiOperation("修改用户头像")
|
||||
public CommonResult<String> reviseAvatar(@RequestParam("avatarFile") MultipartFile file) throws IOException {
|
||||
if (file.isEmpty()) {
|
||||
throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
|
||||
}
|
||||
String avatar = userService.reviseAvatar(getLoginUserId(), file.getInputStream());
|
||||
return success(avatar);
|
||||
}
|
||||
|
||||
@GetMapping("/get-user-info")
|
||||
@ApiOperation("获取用户头像与昵称")
|
||||
public CommonResult<SysUserCoreProfileRespVo> getUserInfo(@RequestParam("id") Long id) {
|
||||
return success(userService.getUserInfo(id));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,4 +9,9 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode;
|
||||
*/
|
||||
public interface MbrErrorCodeConstants {
|
||||
|
||||
// 用户相关
|
||||
ErrorCode USER_NOT_EXISTS = new ErrorCode(1004000000, "用户不存在");
|
||||
|
||||
// 文件相关
|
||||
ErrorCode FILE_IS_EMPTY = new ErrorCode(1004000000, "用户不存在");
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package cn.iocoder.yudao.userserver.modules.member.service.user;
|
||||
|
||||
import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.controller.user.vo.SysUserCoreProfileRespVo;
|
||||
import cn.iocoder.yudao.framework.common.validation.Mobile;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 前台用户 Service 接口
|
||||
*
|
||||
@ -44,4 +47,26 @@ public interface MbrUserService {
|
||||
*/
|
||||
MbrUserDO getUser(Long id);
|
||||
|
||||
/**
|
||||
* 修改用户昵称
|
||||
* @param loginUserId 登录用户id
|
||||
* @param nickName 用户新昵称
|
||||
*/
|
||||
void reviseNickname(Long loginUserId, String nickName);
|
||||
|
||||
/**
|
||||
* 修改用户头像
|
||||
* @param loginUserId 登录用户id
|
||||
* @param inputStream 头像文件
|
||||
* @return 头像url
|
||||
*/
|
||||
String reviseAvatar(Long loginUserId, InputStream inputStream);
|
||||
|
||||
/**
|
||||
* 根据用户id,获取用户头像与昵称
|
||||
* @param loginUserId 登录用户id
|
||||
* @return 用户响应实体类
|
||||
*/
|
||||
SysUserCoreProfileRespVo getUserInfo(Long loginUserId);
|
||||
|
||||
}
|
||||
|
@ -1,18 +1,27 @@
|
||||
package cn.iocoder.yudao.userserver.modules.member.service.user.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.iocoder.yudao.coreservice.modules.infra.service.file.InfFileCoreService;
|
||||
import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.controller.user.vo.SysUserCoreProfileRespVo;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper;
|
||||
import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.userserver.modules.member.enums.MbrErrorCodeConstants.USER_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* User Service 实现类
|
||||
*
|
||||
@ -26,6 +35,9 @@ public class MbrUserServiceImpl implements MbrUserService {
|
||||
@Resource
|
||||
private MbrUserMapper userMapper;
|
||||
|
||||
@Resource
|
||||
private InfFileCoreService fileCoreService;
|
||||
|
||||
@Resource
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@ -68,4 +80,53 @@ public class MbrUserServiceImpl implements MbrUserService {
|
||||
return userMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reviseNickname(Long loginUserId, String nickName) {
|
||||
MbrUserDO mbrUserDO = userMapper.selectById(loginUserId);
|
||||
// 仅当新昵称不等于旧昵称时进行修改
|
||||
if (!nickName.equals(mbrUserDO.getNickname())){
|
||||
MbrUserDO user = new MbrUserDO();
|
||||
user.setId(mbrUserDO.getId());
|
||||
user.setNickname(nickName);
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String reviseAvatar(Long loginUserId, InputStream avatarFile) {
|
||||
this.checkUserExists(loginUserId);
|
||||
// 创建文件
|
||||
String avatar = fileCoreService.createFile(IdUtil.fastUUID(), IoUtil.readBytes(avatarFile));
|
||||
// 更新头像路径
|
||||
MbrUserDO userDO = new MbrUserDO();
|
||||
userDO.setId(loginUserId);
|
||||
userDO.setAvatar(avatar);
|
||||
userMapper.updateById(userDO);
|
||||
return avatar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUserCoreProfileRespVo getUserInfo(Long loginUserId) {
|
||||
MbrUserDO mbrUserDO = userMapper.selectById(loginUserId);
|
||||
if (mbrUserDO == null){
|
||||
log.error("用户不存在:{}",loginUserId);
|
||||
throw exception(USER_NOT_EXISTS);
|
||||
}
|
||||
|
||||
SysUserCoreProfileRespVo userRes = new SysUserCoreProfileRespVo();
|
||||
userRes.setNickName(mbrUserDO.getNickname());
|
||||
userRes.setAvatar(mbrUserDO.getAvatar());
|
||||
return userRes;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void checkUserExists(Long id) {
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
MbrUserDO user = userMapper.selectById(id);
|
||||
if (user == null) {
|
||||
throw exception(USER_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user