hdy
2024-03-12 9a80017dfb643f66af44c142f84d8a6d8af42682
提交 | 用户 | 时间
e57a89 1 package com.jcdm.web.controller.system;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.web.bind.annotation.GetMapping;
5 import org.springframework.web.bind.annotation.PostMapping;
6 import org.springframework.web.bind.annotation.PutMapping;
7 import org.springframework.web.bind.annotation.RequestBody;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestParam;
10 import org.springframework.web.bind.annotation.RestController;
11 import org.springframework.web.multipart.MultipartFile;
12 import com.jcdm.common.annotation.Log;
13 import com.jcdm.common.config.MesConfig;
14 import com.jcdm.common.core.controller.BaseController;
15 import com.jcdm.common.core.domain.AjaxResult;
16 import com.jcdm.common.core.domain.entity.SysUser;
17 import com.jcdm.common.core.domain.model.LoginUser;
18 import com.jcdm.common.enums.BusinessType;
19 import com.jcdm.common.utils.SecurityUtils;
20 import com.jcdm.common.utils.StringUtils;
21 import com.jcdm.common.utils.file.FileUploadUtils;
22 import com.jcdm.common.utils.file.MimeTypeUtils;
23 import com.jcdm.framework.web.service.TokenService;
24 import com.jcdm.system.service.ISysUserService;
25
26 /**
27  * 个人信息 业务处理
28  * 
29  * @author jc
30  */
31 @RestController
32 @RequestMapping("/system/user/profile")
33 public class SysProfileController extends BaseController
34 {
35     @Autowired
36     private ISysUserService userService;
37
38     @Autowired
39     private TokenService tokenService;
40
41     /**
42      * 个人信息
43      */
44     @GetMapping
45     public AjaxResult profile()
46     {
47         LoginUser loginUser = getLoginUser();
48         SysUser user = loginUser.getUser();
49         AjaxResult ajax = AjaxResult.success(user);
50         ajax.put("roleGroup", userService.selectUserRoleGroup(loginUser.getUsername()));
51         ajax.put("postGroup", userService.selectUserPostGroup(loginUser.getUsername()));
52         return ajax;
53     }
54
55     /**
56      * 修改用户
57      */
58     @Log(title = "个人信息", businessType = BusinessType.UPDATE)
59     @PutMapping
60     public AjaxResult updateProfile(@RequestBody SysUser user)
61     {
62         LoginUser loginUser = getLoginUser();
63         SysUser currentUser = loginUser.getUser();
64         currentUser.setNickName(user.getNickName());
65         currentUser.setEmail(user.getEmail());
66         currentUser.setPhonenumber(user.getPhonenumber());
67         currentUser.setSex(user.getSex());
68         if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(currentUser))
69         {
70             return error("修改用户'" + loginUser.getUsername() + "'失败,手机号码已存在");
71         }
72         if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(currentUser))
73         {
74             return error("修改用户'" + loginUser.getUsername() + "'失败,邮箱账号已存在");
75         }
76         if (userService.updateUserProfile(currentUser) > 0)
77         {
78             // 更新缓存用户信息
79             tokenService.setLoginUser(loginUser);
80             return success();
81         }
82         return error("修改个人信息异常,请联系管理员");
83     }
84
85     /**
86      * 重置密码
87      */
88     @Log(title = "个人信息", businessType = BusinessType.UPDATE)
89     @PutMapping("/updatePwd")
90     public AjaxResult updatePwd(String oldPassword, String newPassword)
91     {
92         LoginUser loginUser = getLoginUser();
93         String userName = loginUser.getUsername();
94         String password = loginUser.getPassword();
95         if (!SecurityUtils.matchesPassword(oldPassword, password))
96         {
97             return error("修改密码失败,旧密码错误");
98         }
99         if (SecurityUtils.matchesPassword(newPassword, password))
100         {
101             return error("新密码不能与旧密码相同");
102         }
103         newPassword = SecurityUtils.encryptPassword(newPassword);
104         if (userService.resetUserPwd(userName, newPassword) > 0)
105         {
106             // 更新缓存用户密码
107             loginUser.getUser().setPassword(newPassword);
108             tokenService.setLoginUser(loginUser);
109             return success();
110         }
111         return error("修改密码异常,请联系管理员");
112     }
113
114     /**
115      * 头像上传
116      */
117     @Log(title = "用户头像", businessType = BusinessType.UPDATE)
118     @PostMapping("/avatar")
119     public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
120     {
121         if (!file.isEmpty())
122         {
123             LoginUser loginUser = getLoginUser();
124             String avatar = FileUploadUtils.upload(MesConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
125             if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
126             {
127                 AjaxResult ajax = AjaxResult.success();
128                 ajax.put("imgUrl", avatar);
129                 // 更新缓存用户头像
130                 loginUser.getUser().setAvatar(avatar);
131                 tokenService.setLoginUser(loginUser);
132                 return ajax;
133             }
134         }
135         return error("上传图片异常,请联系管理员");
136     }
137 }