Prechádzať zdrojové kódy

修复个人信息修改头像点击保存头像地址错误问题

bihuisong 1 mesiac pred
rodič
commit
bf6b304cd8

+ 30 - 45
src/main/java/com/ruoyi/system/controller/SysProfileController.java

@@ -20,7 +20,6 @@ import com.ruoyi.system.service.ISysDeptService;
 import com.ruoyi.system.service.ISysUserService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -35,8 +34,7 @@ import java.util.Arrays;
 @Api(tags = "个人信息控制器")
 @RestController
 @RequestMapping("/user/profile")
-public class SysProfileController extends BaseController
-{
+public class SysProfileController extends BaseController {
     private final ISysUserService userService;
 
     private final TokenService tokenService;
@@ -61,24 +59,23 @@ public class SysProfileController extends BaseController
      */
     @ApiOperation(value = "获取用户个人信息", notes = "获取用户个人信息")
     @GetMapping
-    public AjaxResult profile()
-    {
-        Long[] deptIds = {369L,370L,371L,372L,373L};
+    public AjaxResult profile() {
+        Long[] deptIds = {369L, 370L, 371L, 372L, 373L};
         String username = SecurityUtils.getUsername();
         Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
         SysUser user = userService.selectUserByUserName(username);
         /**图片前缀**/
         String uri = configService.selectConfigByKey("fileUrl");
-        if(StringUtils.isNotEmpty(user.getAvatar())){
-            user.setAvatar(uri+user.getAvatar());
+        if (StringUtils.isNotEmpty(user.getAvatar())) {
+            user.setAvatar(uri + user.getAvatar());
         }
         AjaxResult ajax = AjaxResult.success(user);
         ajax.put("roleGroup", userService.selectUserRoleGroup(username));
         ajax.put("postGroup", userService.selectUserPostGroup(username));
         boolean isItSelf = Arrays.asList(deptIds).contains(deptId);
-        if(isItSelf) {//如果是三县两区本身
+        if (isItSelf) {//如果是三县两区本身
             ajax.put("mapDeptId", deptId);
-        } else if (deptService.thisDeptIdIsParentIdChild(deptId, 369L) > 0){//双辽
+        } else if (deptService.thisDeptIdIsParentIdChild(deptId, 369L) > 0) {//双辽
             ajax.put("mapDeptId", 369L);
         } else if (deptService.thisDeptIdIsParentIdChild(deptId, 370L) > 0) {//伊通
             ajax.put("mapDeptId", 370L);
@@ -88,7 +85,7 @@ public class SysProfileController extends BaseController
             ajax.put("mapDeptId", 372L);
         } else if (deptService.thisDeptIdIsParentIdChild(deptId, 373L) > 0) {//铁西
             ajax.put("mapDeptId", 373L);
-        }else{//四平
+        } else {//四平
             ajax.put("mapDeptId", 365L);
         }
         return ajax;
@@ -100,25 +97,25 @@ public class SysProfileController extends BaseController
     @ApiOperation(value = "修改用户个人信息", notes = "修改用户个人信息")
     @Log(title = "个人信息", businessType = BusinessType.UPDATE)
     @PutMapping
-    public AjaxResult updateProfile(@RequestBody SysUser user)
-    {
+    public AjaxResult updateProfile(@RequestBody SysUser user) {
+        //截取头像
+        int index = user.getAvatar().lastIndexOf("group");
+        if (index != -1) {
+            user.setAvatar(user.getAvatar().substring(index));
+        }
         LoginUser loginUser = SecurityUtils.getLoginUser();
         SysUser sysUser = loginUser.getSysUser();
         user.setUserName(sysUser.getUserName());
         if (StringUtils.isNotEmpty(user.getPhonenumber())
-                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
-        {
+                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
             return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
-        }
-        else if (StringUtils.isNotEmpty(user.getEmail())
-                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
-        {
+        } else if (StringUtils.isNotEmpty(user.getEmail())
+                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
             return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
         }
         user.setUserId(sysUser.getUserId());
         user.setPassword(null);
-        if (userService.updateUserProfile(user) > 0)
-        {
+        if (userService.updateUserProfile(user) > 0) {
             // 更新缓存用户信息
             loginUser.getSysUser().setNickName(user.getNickName());
             loginUser.getSysUser().setPhonenumber(user.getPhonenumber());
@@ -136,24 +133,20 @@ public class SysProfileController extends BaseController
     @ApiOperation(value = "重置密码", notes = "重置密码")
     @Log(title = "个人信息", businessType = BusinessType.UPDATE)
     @PutMapping("/updatePwd")
-    public AjaxResult updatePwd(String oldPassword, String newPassword)
-    {
+    public AjaxResult updatePwd(String oldPassword, String newPassword) {
         String username = SecurityUtils.getUsername();
         SysUser user = userService.selectUserByUserName(username);
         String password = user.getPassword();
-        if (!SecurityUtils.matchesPassword(oldPassword, password))
-        {
+        if (!SecurityUtils.matchesPassword(oldPassword, password)) {
             return AjaxResult.error("修改密码失败,旧密码错误");
         }
-        if (SecurityUtils.matchesPassword(newPassword, password))
-        {
+        if (SecurityUtils.matchesPassword(newPassword, password)) {
             return AjaxResult.error("新密码不能与旧密码相同");
         }
         if (PassUtil.isStringPwd(newPassword).equals("0")) {
             return AjaxResult.error("密码必须大于等于8位,包含特殊字符,数字,大小写字母");
         }
-        if (userService.resetUserPwd(username, SecurityUtils.encryptPassword(newPassword)) > 0)
-        {
+        if (userService.resetUserPwd(username, SecurityUtils.encryptPassword(newPassword)) > 0) {
             // 更新缓存用户密码
             LoginUser loginUser = SecurityUtils.getLoginUser();
             loginUser.getSysUser().setPassword(SecurityUtils.encryptPassword(newPassword));
@@ -169,19 +162,15 @@ public class SysProfileController extends BaseController
     @ApiOperation(value = "头像上传", notes = "头像上传")
     @Log(title = "用户头像", businessType = BusinessType.UPDATE)
     @PostMapping("/avatar")
-    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file)
-    {
-        if (!file.isEmpty())
-        {
+    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) {
+        if (!file.isEmpty()) {
             LoginUser loginUser = SecurityUtils.getLoginUser();
             R<SysFile> fileResult = remoteFileService.upload(file);
-            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
-            {
+            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
                 return AjaxResult.error("文件服务异常,请联系管理员");
             }
             String url = fileResult.getData().getUrl();
-            if (userService.updateUserAvatar(loginUser.getUsername(), url))
-            {
+            if (userService.updateUserAvatar(loginUser.getUsername(), url)) {
                 AjaxResult ajax = AjaxResult.success();
                 /**图片前缀**/
                 String uri = configService.selectConfigByKey("fileUrl");
@@ -202,23 +191,19 @@ public class SysProfileController extends BaseController
     @ApiOperation(value = "app重置密码", notes = "app重置密码")
     @Log(title = "个人信息", businessType = BusinessType.UPDATE)
     @PutMapping("/updateAppPwd")
-    public AjaxResult updateAppPwd(@RequestBody AppResetPwd appResetPwd)
-    {
+    public AjaxResult updateAppPwd(@RequestBody AppResetPwd appResetPwd) {
         SysUser user = userService.selectUserByUserName(appResetPwd.getUn());
         String password = user.getPassword();
-        if (!SecurityUtils.matchesPassword(appResetPwd.getOp(), password))
-        {
+        if (!SecurityUtils.matchesPassword(appResetPwd.getOp(), password)) {
             return AjaxResult.error("修改密码失败,旧密码错误");
         }
-        if (SecurityUtils.matchesPassword(appResetPwd.getNp(), password))
-        {
+        if (SecurityUtils.matchesPassword(appResetPwd.getNp(), password)) {
             return AjaxResult.error("新密码不能与旧密码相同");
         }
         if (PassUtil.isStringPwd(appResetPwd.getNp()).equals("0")) {
             return AjaxResult.error("密码必须大于等于8位,包含特殊字符,数字,大小写字母");
         }
-        if (userService.resetUserPwd(appResetPwd.getUn(), SecurityUtils.encryptPassword(appResetPwd.getNp())) > 0)
-        {
+        if (userService.resetUserPwd(appResetPwd.getUn(), SecurityUtils.encryptPassword(appResetPwd.getNp())) > 0) {
             // 更新缓存用户密码
             LoginUser loginUser = SecurityUtils.getLoginUser();
             loginUser.getSysUser().setPassword(SecurityUtils.encryptPassword(appResetPwd.getNp()));