From a98fdc67a6028428fa02dbf5563ef7132ea0c390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=A4=A9?= <2982176321@qq.com> Date: Wed, 13 Oct 2021 18:35:23 +0800 Subject: [PATCH] =?UTF-8?q?[uodate]=E6=96=B0=E5=A2=9E=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=98=B5=E7=A7=B0/=E4=BF=AE=E6=94=B9=E5=A4=B4=E5=83=8F/?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=EF=BC=88?= =?UTF-8?q?=E5=A4=B4=E5=83=8F+=E6=98=B5=E7=A7=B0=EF=BC=89=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/user/SysUserController.http | 8 ++++ .../controller/user/SysUserController.java | 16 +++++++ .../user/SysUserProfileController.java | 10 +++++ .../system/service/user/SysUserService.java | 27 +++++++++--- .../service/user/SysUserServiceImpl.java | 42 +++++++++++++++++++ .../src/main/resources/application-local.yaml | 15 +++---- .../service/user/SysUserServiceImplTest.java | 22 ++++++++++ .../views/system/user/profile/userAvatar.vue | 4 +- 8 files changed, 129 insertions(+), 15 deletions(-) diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.http b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.http index 87283759db..e288bc8299 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.http +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.http @@ -1,3 +1,11 @@ ### 请求 /system/user/page 接口 => 没有权限 GET {{baseUrl}}/system/user/page?pageNo=1&pageSize=10 Authorization: Bearer test104 # 使用测试账号 + +### +PUT {{baseUrl}}/system/user/update-nick-name +Authorization: Bearer {{token}} + +{ + "nickname": "芋艿" +} \ No newline at end of file diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.java index df54e79801..df347ebd6d 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.controller.user; import cn.hutool.core.collection.CollUtil; +import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; @@ -32,6 +33,7 @@ import java.util.*; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @Api(tags = "用户") @RestController @@ -171,4 +173,18 @@ public class SysUserController { return success(userService.importUsers(list, updateSupport)); } + @PutMapping("/update-nick-name") + @ApiOperation("修改用户昵称") + @PreAuthorize("@ss.hasPermission('system:user:update')") + public CommonResult updateNickName(@Valid @RequestBody SysUserProfileUpdateReqVO reqVO) { + userService.updateNickName(getLoginUserId(), reqVO.getNickname()); + return success(true); + } + + @GetMapping("/get-user-info") + @ApiOperation("获取用户信息:头像+昵称") + @PreAuthorize("@ss.hasPermission('system:user:query')") + public CommonResult getUserInfo(){ + return success(userService.getUserInfo(getLoginUserId())); + } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java index 8fb29bbedf..2c51c47d79 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java @@ -99,4 +99,14 @@ public class SysUserProfileController { return success(true); } + @PutMapping("/avatar") + @ApiOperation("修改用户头像") + public CommonResult updateAvatar(@RequestParam("avatarFile") MultipartFile file) throws IOException{ + if (file.isEmpty()) { + throw ServiceExceptionUtil.exception(FILE_IS_EMPTY); + } + userService.updateAvatar(getLoginUserId(),file.getInputStream()); + return success(true); + } + } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserService.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserService.java index 16c815ace2..77e5db6e8c 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserService.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserService.java @@ -1,15 +1,10 @@ package cn.iocoder.yudao.adminserver.modules.system.service.user; import cn.hutool.core.collection.CollUtil; +import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.*; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVO; import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO; -import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.SysUserCreateReqVO; -import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.SysUserExportReqVO; -import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.SysUserImportExcelVO; -import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.SysUserImportRespVO; -import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.SysUserPageReqVO; -import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.SysUserUpdateReqVO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; @@ -166,4 +161,24 @@ public interface SysUserService { */ SysUserImportRespVO importUsers(List importUsers, boolean isUpdateSupport); + /** + * 修改用户昵称 + * @param loginUserId 用户id + * @param newNickname 用户新昵称 + */ + void updateNickName(Long loginUserId, String newNickname); + + /** + * 修改用户头像 + * @param loginUserId 用户id + * @param inputStream 文件 + */ + void updateAvatar(Long loginUserId, InputStream inputStream); + + /** + * 获取当前登录用户头像与昵称 + * @return 用户实体类,仅包含头像于昵称两项 + * @param loginUserId 用户id + */ + SysUserDO getUserInfo(Long loginUserId); } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImpl.java index 9c344e92dd..9c42dca626 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImpl.java @@ -370,4 +370,46 @@ public class SysUserServiceImpl implements SysUserService { return respVO; } + @Override + public void updateNickName(Long loginUserId, String newNickname) { + this.checkUserExists(loginUserId); + + SysUserDO sysUserDO = new SysUserDO(); + sysUserDO.setId(loginUserId); + sysUserDO.setNickname(newNickname); + userMapper.updateById(sysUserDO); + } + + @Override + public void updateAvatar(Long loginUserId, InputStream avatarFile) { + SysUserDO sysUserDO = userMapper.selectById(loginUserId); + if (sysUserDO == null){ + throw exception(USER_NOT_EXISTS); + } + // 判断是否第一次上传头像 + if (sysUserDO.getAvatar().isEmpty()) { + this.updateUserAvatar(loginUserId,avatarFile); + return; + } + + // 删除旧文件 + String url = sysUserDO.getAvatar(); + String AvatarName = url.substring(url.lastIndexOf("/")+1); + fileService.deleteFile(AvatarName); + + // 存储文件 + String avatar = fileService.createFile(IdUtil.fastUUID(), IoUtil.readBytes(avatarFile)); + sysUserDO.setAvatar(avatar); + userMapper.updateById(sysUserDO); + } + + @Override + public SysUserDO getUserInfo(Long loginUserId) { + SysUserDO sysUserDO = userMapper.selectById(loginUserId); + + SysUserDO user = new SysUserDO(); + user.setNickname(sysUserDO.getNickname()); + user.setAvatar(sysUserDO.getAvatar()); + return user; + } } diff --git a/yudao-admin-server/src/main/resources/application-local.yaml b/yudao-admin-server/src/main/resources/application-local.yaml index 53ab065605..a65c4818b3 100644 --- a/yudao-admin-server/src/main/resources/application-local.yaml +++ b/yudao-admin-server/src/main/resources/application-local.yaml @@ -44,22 +44,23 @@ spring: datasource: master: name: ruoyi-vue-pro - url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT - driver-class-name: com.mysql.jdbc.Driver - username: root - password: 123456 - slave: # 模拟从库,可根据自己需要修改 - name: ruoyi-vue-pro - url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT + url: jdbc:mysql://127.0.0.1:3308/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 +# slave: # 模拟从库,可根据自己需要修改 +# name: ruoyi-vue-pro +# url: jdbc:mysql://127.0.0.1:3308/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT +# driver-class-name: com.mysql.jdbc.Driver +# username: root +# password: 123456 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 redis: host: 127.0.0.1 # 地址 port: 6379 # 端口 database: 0 # 数据库索引 + password: 123456 --- #################### 定时任务相关配置 #################### diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImplTest.java index 95833e076c..f1b50350c8 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImplTest.java @@ -576,6 +576,28 @@ public class SysUserServiceImplTest extends BaseDbUnitTest { verify(passwordEncoder, times(1)).matches(eq(oldPassword), eq(user.getPassword())); } + @Test + public void test_update_nick_name() { + // 准备数据 + SysUserDO user = randomSysUserDO(); + userMapper.insert(user); + System.out.println("=====>"+user); + // 调用测试 + userService.updateNickName(user.getId(), randomString()); + + System.out.println(userMapper.selectById(user.getId())); + } + + @Test + public void test_get_user_info() { + // 准备数据 + SysUserDO user = randomSysUserDO(); + userMapper.insert(user); + // 调用测试 + System.out.println(userService.getUserInfo(user.getId())); + } + + // ========== 随机对象 ========== @SafeVarargs diff --git a/yudao-admin-ui/src/views/system/user/profile/userAvatar.vue b/yudao-admin-ui/src/views/system/user/profile/userAvatar.vue index edc15e36e0..8a483cc958 100644 --- a/yudao-admin-ui/src/views/system/user/profile/userAvatar.vue +++ b/yudao-admin-ui/src/views/system/user/profile/userAvatar.vue @@ -123,7 +123,7 @@ export default { uploadImg() { this.$refs.cropper.getCropBlob(data => { let formData = new FormData(); - formData.append("avatarfile", data); + formData.append("avatarFile", data); uploadAvatar(formData).then(response => { this.open = false; this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl; @@ -164,4 +164,4 @@ export default { line-height: 110px; border-radius: 50%; } - \ No newline at end of file +