ZQN
2024-06-22 b48de0e35c54bd4715f8de2b86bac5a539c27275
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.project.admin.controller.system;
 
import com.project.common.annotation.Log;
import com.project.common.config.ProjectConfig;
import com.project.common.constant.UserConstants;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.domain.entity.SysDept;
import com.project.common.core.domain.entity.SysUser;
import com.project.common.core.domain.model.LoginUser;
import com.project.common.enums.BusinessType;
import com.project.common.utils.SecurityUtils;
import com.project.common.utils.StringUtils;
import com.project.common.utils.file.FileUploadUtils;
import com.project.common.utils.file.MimeTypeUtils;
import com.project.framework.web.service.SysPermissionService;
import com.project.framework.web.service.TokenService;
import com.project.system.domain.SysCompany;
import com.project.system.service.ISysCompanyService;
import com.project.system.service.ISysDeptService;
import com.project.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.Set;
 
/**
 * 个人信息 业务处理
 *
 * @author project
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController
{
    private final ISysUserService userService;
    private final ISysDeptService deptService;
    private final TokenService tokenService;
    private final SysPermissionService permissionService;
    private final ISysCompanyService companyService;
 
 
    /**
     * 个人信息
     */
    @GetMapping
    public AjaxResult profile()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if ("02".equals(user.getUserType())){
            SysCompany company = companyService.getById(userService.getById(user.getUserId()).getDeptId());
            SysDept sysDept = new SysDept();
            sysDept.setDeptName(company.getCompanyName());
            user.setDept(sysDept);
        } else {
            SysDept sysDept = deptService.selectDeptById(userService.getById(user.getUserId()).getDeptId());
            sysDept.setDeptName(deptService.getDeptAllName(sysDept.getDeptId()));
            user.setDept(sysDept);
        }
 
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success(user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        ajax.put("roleGroup", userService.selectUserRoleGroup(user.getUserName()));
        ajax.put("postGroup", userService.selectUserPostGroup(user.getUserName()));
        return ajax;
    }
 
    /**
     * 修改用户
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateProfile(@RequestBody SysUser user)
    {
        LoginUser loginUser = getLoginUser();
        if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
        {
            return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
        }
        if (StringUtils.isNotEmpty(user.getEmail())
                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
        {
            return error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
        }
        int upd = userService.updateUser(user);
        if (upd > 0)
        {
            // 更新缓存用户信息
            loginUser.setUser(user);
            tokenService.setLoginUser(loginUser);
            return success();
        }
        return error("修改个人信息异常,请联系管理员");
    }
 
 
    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePwd")
    public AjaxResult updatePwd(String oldPassword, String newPassword)
    {
        LoginUser loginUser = getLoginUser();
        String userName = loginUser.getUsername();
        String password = loginUser.getPassword();
        if (!SecurityUtils.matchesPassword(oldPassword, password))
        {
            return error("修改密码失败,旧密码错误");
        }
        if (SecurityUtils.matchesPassword(newPassword, password))
        {
            return error("新密码不能与旧密码相同");
        }
        if (userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword), newPassword) > 0)
        {
            // 更新缓存用户密码
            loginUser.getUser().setPassword(SecurityUtils.encryptPassword(newPassword));
            tokenService.setLoginUser(loginUser);
            return success();
        }
        return error("修改密码异常,请联系管理员");
    }
 
    /**
     * 更换手机号
     */
    @Log(title = "更换手机号", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePhone/{phone}")
    public AjaxResult updatePhone(@PathVariable String phone)
    {
        if (StringUtils.isEmpty(phone)){
            return error("请填写手机号");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        user.setPhonenumber(phone);
        if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
        {
            return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
        }
        if (userService.resetPhone(user.getUserId(), phone) > 0)
        {
            // 更新缓存
            loginUser.getUser().setUserName(phone);
            loginUser.getUser().setPhonenumber(phone);
            tokenService.setLoginUser(loginUser);
            return success();
        }
        return error("修改密码异常,请联系管理员");
    }
 
    /**
     * 头像上传
     */
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = getLoginUser();
            String avatar = FileUploadUtils.upload(ProjectConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                // 更新缓存用户头像
                loginUser.getUser().setAvatar(avatar);
                tokenService.setLoginUser(loginUser);
                return ajax;
            }
        }
        return error("上传图片异常,请联系管理员");
    }
}