危化品全生命周期管理后端
kongzy
2024-08-22 0c73654f55844e34772732914af8cc1e247aab63
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
package com.gkhy.hazmat.admin.controller.system;
 
import cn.hutool.core.codec.Base64;
import com.gkhy.hazmat.common.annotation.Log;
import com.gkhy.hazmat.common.annotation.RepeatSubmit;
import com.gkhy.hazmat.common.api.CommonResult;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.BusinessType;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.common.utils.StringUtils;
import com.gkhy.hazmat.framework.web.service.SysPasswordService;
import com.gkhy.hazmat.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.Map;
 
@Api(tags = "个人信息前端控制器")
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController {
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysPasswordService passwordService;
 
    @GetMapping
    public CommonResult profile(){
        SysUser user= SecurityUtils.getLoginUser().getUser();
        return CommonResult.success(user);
    }
 
    @RepeatSubmit
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult updateProfile(@RequestBody SysUser user)
    {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        currentUser.setName(user.getName());
        currentUser.setPhone(user.getPhone());
        currentUser.setSex(user.getSex());
        if (StringUtils.isNotEmpty(user.getPhone()) && !userService.checkPhoneUnique(currentUser))
        {
            throw new ApiException("手机号码已存在");
        }
        if (userService.updateUser(currentUser)>0)
        {
            // 更新缓存用户信息
            SecurityUtils.getLoginUser().setUser(currentUser);
            return CommonResult.success();
        }
        throw new ApiException("修改个人信息异常,请联系管理员");
    }
 
    @RepeatSubmit
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "oldPassword", dataType = "String", required = true, value = "旧密码"),
            @ApiImplicitParam(paramType = "body", name = "newPassword", dataType = "String", required = true, value = "新密码")
    })
    @PutMapping("/updatePwd")
    public CommonResult updatePwd(@RequestBody Map<String,String> body)
    {
        String oldPassword=body.get("oldPassword");
        String newPassword=body.get("newPassword");
        if(StringUtils.isBlank(oldPassword)||StringUtils.isBlank(oldPassword)){
            throw new ApiException("旧密码或者新密码不能为空");
        }
        oldPassword=Base64.decodeStr(oldPassword);
        newPassword=Base64.decodeStr(newPassword);
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (!passwordService.matches(currentUser, oldPassword))
        {
            throw new ApiException("修改密码失败,旧密码错误");
        }
        if (!passwordService.matches(currentUser, newPassword))
        {
            throw new ApiException("新密码不能与旧密码相同");
        }
        currentUser.setPassword(SecurityUtils.encryptPassword(newPassword));
        if (userService.resetUserPwd(currentUser))
        {
            // 更新缓存用户密码
           SecurityUtils.getLoginUser().getUser().setPassword(SecurityUtils.encryptPassword(newPassword));
            return CommonResult.success();
        }
        throw new ApiException("修改密码异常,请联系管理员");
    }
 
 
}