jinlin
2023-11-16 86b6785e74bf268885e8663380b3e3c0cb0c4bad
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
package com.zt.life.sys.controller;
 
import java.util.Date;
 
import com.zt.core.security.BCryptPasswordEncoder;
import com.zt.life.sys.model.SysUserRegister;
import com.zt.life.sys.service.SysUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import com.zt.common.annotation.LogOperation;
import com.zt.common.servlet.Result;
import com.zt.common.utils.UUIDUtil;
import com.zt.modules.sys.service.*;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
 
/**
 * 用户管理
 *
 * @author hehz
 * @since 1.0.0 2020-09-01
 */
@RestController
@RequestMapping("/sys")
@Api(tags = "用户管理")
public class SysUserRegisterController {
    @Autowired
    private SysUserRegisterService sysUserRegisterService;
 
    @Autowired
    private SysDeptService sysDeptService;
 
    @Autowired
    private SysParamsService paramsService;
 
    @Autowired
    SysJobUserService sysJobUserService;
    @Autowired
    SysPostUserService sysPostUserService;
 
    @Autowired
    SysMapService sysMapService;
 
    @PostMapping("/userRegister")
    @ApiOperation("新增")
    @LogOperation("用户管理--->新增")
    //@RequiresPermissions("sys:user")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> insert(@RequestBody SysUserRegister entity) {
        // 效验数据
        //ValidatorUtils.validateEntity(entity, AddGroup.class, DefaultGroup.class);
        String result = "OK";
        Integer hasUser = sysUserRegisterService.existUsername(entity.getUserName());
        if (hasUser ==0) {
            String password = encrypt(entity.getPassword());
            entity.setPassword(password);
 
            Long id = UUIDUtil.generateId();
            entity.setUserId(id);
            Date now = new Date();
            entity.setCreateTime(now);
            entity.setUpdateTime(now);
            entity.setCreateBy("10");
            entity.setUpdateBy("10");
            entity.setDelFlag(0);
            sysUserRegisterService.insert(entity);
            // 保存岗位用户关系
//            sysPostUserService.saveOrUpdate(entity.getId(), entity.getPostIdList());
            // 保存职位用户关系
//            sysJobUserService.saveOrUpdate(entity.getId(), entity.getJobIdList());
 
            sysMapService.saveOrUpdate("userTeamggroup",entity.getUserId(),entity.getTeamgroupIds());
            sysMapService.saveOrUpdate("userShip",entity.getUserId(),entity.getShipIds());
        }else{
            result = "该用户名已经被注册,请修改登录用户名!";
        }
 
        return Result.ok(result);
    }
 
    @PutMapping("/userChangePassword")
    @ApiOperation("修改密码")
    @LogOperation("用户管理--->修改密码")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> update(@RequestBody SysUserRegister entity) {
        // 效验数据
        //ValidatorUtils.validateEntity(entity, AddGroup.class, DefaultGroup.class);
        String result = "OK";
        SysUserRegister user = sysUserRegisterService.getByUserName(entity.getUserName());
        if (null == user) {
            result = "该用户不存在!";
        } else {
            if (!matchEncrypt(entity.getOldPassword(), user.getPassword())) {
                result = "旧密码不正确!";
            } else {
                String password = encrypt(entity.getPassword());
                sysUserRegisterService.changePassword(entity.getUserName(), password);
            }
        }
 
        return Result.ok(result);
    }
 
    public String encrypt(String password) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.encode(password);
    }
 
    public boolean matchEncrypt(String password, String passwordHash) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.matches(password, passwordHash);
    }
}