liuxm
2024-05-06 2661ce7da0b5544d5574cfad50cba9a9a643254c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package com.dy.pmsBase.user;
 
import com.dy.common.aop.SsoAop;
import com.dy.common.aop.SsoPowerAop;
import com.dy.common.util.MD5;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pmsGlobal.aop.Log;
import com.dy.pmsGlobal.pojoBa.BaRole;
import com.dy.pmsGlobal.pojoBa.BaUser;
import com.dy.pmsGlobal.util.Constant;
import com.mysql.cj.util.StringUtils;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Objects;
 
/**
 * 用户管理
 */
@Slf4j
@RestController
@RequestMapping(path = "user")
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked")
public class UserCtrl {
 
    @Autowired
    private UserSv sv;
 
    @Value("${user.defaultTrueRandomFalsePassword:true}")
    private Boolean defaultTrueRandomFalsePassword;
 
    @Value("${user.defaultPassword:ABC123}")
    private String defaultPassword;
 
    /**
     * 客户端请求得到默认密码
     * @return 默认密码
     */
    @GetMapping(path = "defaultPassword")
    public BaseResponse<String> defaultPassword(){
        if(defaultTrueRandomFalsePassword){
            return BaseResponseUtils.buildSuccess(defaultPassword) ;
        }else{
            String password = this.sv.getStringRandom(6) ;
            return BaseResponseUtils.buildSuccess(password) ;
        }
    }
 
    /**
     * 客户端请求得到所有用户数据
     * @return 所有用户数据
     */
    @PostMapping(path = "some", consumes = MediaType.APPLICATION_JSON_VALUE)
    //@SsoAop() //只有登录验证,没有权限验证
    @SsoPowerAop(power = "10100010") //登录与权限同时验证
    @Log("查询用户")
    public BaseResponse<QueryResultVo<List<BaUser>>> some(@RequestBody QueryVo vo) {
        try {
            QueryResultVo<List<BaUser>> res = this.sv.selectSome(vo);
            return BaseResponseUtils.buildSuccess(res);
        } catch (Exception e) {
            log.error("查询用户异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
    /**
     * 得到一个用户数据
     * @return 一个用户数据
     */
    //@GetMapping(path = "one", consumes = MediaType.TEXT_PLAIN_VALUE)//指前端向后传的参数类型
    @GetMapping(path = "one")
    //@SsoAop() //只有登录验证,没有权限验证
    @SsoPowerAop(power = "10100010") //登录与权限同时验证
    @Log("查询单个用户")
    public BaseResponse<BaUser> one(String id) {
        try {
            return BaseResponseUtils.buildSuccess(this.sv.selectById(Long.parseLong(id)));
        } catch (Exception e) {
            log.error("查询一个用户数据异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
    /**
     * 保存用户信息
     * @param po
     * @param bindingResult
     * @return
     */
    @PostMapping(path = "save", consumes = MediaType.APPLICATION_JSON_VALUE)
    //@SsoAop() //只有登录验证,没有权限验证
    @SsoPowerAop(power = "10100011") //登录与权限同时验证
    @Log("保存用户信息")
    public BaseResponse<Boolean> save(@RequestBody @Valid BaUser po, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.hasErrors()) {
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        po.id = null;
        Long id;
        try {
            po.supperAdmin = Constant.no.byteValue() ;
            po.disabled = false ;//默认不禁用
            po.deleted = false;//默认不删除
            if (!StringUtils.isNullOrEmpty(po.password)) {
                /*
                如果前端进行了base64加密
                po.password = new String(Base64.getDecoder().decode(po.password)) ;
                */
                po.password = MD5.encrypt(po.password);//进行加密码
            } else {
                po.password = MD5.encrypt(defaultPassword);
            }
            id = this.sv.save(po);
        } catch (Exception e) {
            log.error("保存用户异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
        if (id <= 0) {
            return BaseResponseUtils.buildFail("数据库存储失败");
        } else {
            return BaseResponseUtils.buildSuccess(true);
        }
    }
 
    /**
     * 更新用户信息
     * @param po
     * @param bindingResult
     * @return
     */
    @PostMapping(path = "update", consumes = MediaType.APPLICATION_JSON_VALUE)
    //@SsoAop() //只有登录验证,没有权限验证
    @SsoPowerAop(power = "10100011") //登录与权限同时验证
    @Log("更新用户信息")
    public BaseResponse<Boolean> update(@RequestBody @Validated BaUser po, BindingResult bindingResult) {
        int count;
        try {
            if (bindingResult != null && bindingResult.hasErrors()) {
                return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
            }
            if (po.id == null) {
                return BaseResponseUtils.buildFail("无数据实体ID");
            }
            po.password = null;//设置为null,不做更新
            po.supperAdmin = null;//设置为null,不做更新
            po.deleted = null;//设置为null,不做更新
            count = this.sv.update(po);
        } catch (Exception e) {
            log.error("修改用户异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
        if (count <= 0) {
            return BaseResponseUtils.buildFail("数据库存储失败");
        } else {
            return BaseResponseUtils.buildSuccess(true);
        }
    }
 
    /**
     * 禁用或启用用户信息
     * @param po
     * @return
     */
    @PostMapping(path="disabled")
    @SsoPowerAop(power = "10100011")
    @Log("禁用或启用用户信息")
    public BaseResponse<BaRole> disabled(@RequestBody BaUser po){
        try {
            return BaseResponseUtils.buildSuccess(sv.disabled(po.id,po.disabled));
        }catch (Exception e){
            log.error("禁用或启用用户信息异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @GetMapping(path = "delete")
    @SsoPowerAop(power = "10100011") //登录与权限同时验证
    @Log("删除用户")
    public BaseResponse<Boolean> delete(Long id) {
        int count;
        try {
            BaUser po = new BaUser();
            po.id = id;
            po.deleted = true;
            count = this.sv.update(po);
        } catch (Exception e) {
            log.error("删除用户异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
        if (count <= 0) {
            return BaseResponseUtils.buildFail("数据库存储失败");
        } else {
            return BaseResponseUtils.buildSuccess(true);
        }
    }
 
    /**
     * 修改密码
     * @param id 用户ID
     * @return 是否成功
     */
    //@GetMapping(path = "changePassword", consumes = MediaType.TEXT_PLAIN_VALUE)
    @GetMapping(path = "changePassword")
    @SsoAop()
    @Log("修改密码")
    public BaseResponse<Boolean> changePassword(String id, String oldPassword, String newPassword) {
        int count;
        try {
            if (id == null) {
            return BaseResponseUtils.buildFail("id不能为空");
            }
            if (StringUtils.isNullOrEmpty(oldPassword)) {
                return BaseResponseUtils.buildFail("旧密码不能为空");
            }
            if (StringUtils.isNullOrEmpty(newPassword)) {
                return BaseResponseUtils.buildFail("新密码不能为空");
            }
            /*
            如果前端进行了base64加密
            oldPassword = new String(Base64.getDecoder().decode(oldPassword)) ;
            newPassword = new String(Base64.getDecoder().decode(newPassword)) ;
            */
            oldPassword = MD5.encrypt(oldPassword);//进行加密码
            newPassword = MD5.encrypt(newPassword);//进行加密码
 
            Long idLg = Long.parseLong(id) ;
 
            BaUser po = this.sv.selectById(idLg);
            if (Objects.isNull(po)) {
                return BaseResponseUtils.buildFail("未得到用户,请求失败");
            } else {
                if (!po.password.equalsIgnoreCase(oldPassword)) {
                    return BaseResponseUtils.buildFail("旧密码不正确,请求失败");
                } else {
                    count = this.sv.changePassword(idLg, newPassword);
                }
            }
        } catch (Exception e) {
            log.error("修改密码异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
        if (count <= 0) {
            return BaseResponseUtils.buildFail("数据库存储失败");
        } else {
            return BaseResponseUtils.buildSuccess(true);
        }
    }
 
 
    /**
     * 重置密码
     * @param vo form表单对象
     * @return 是否成功
     */
    @PostMapping(path = "resetPassword", consumes = MediaType.APPLICATION_JSON_VALUE)
    //@SsoAop() //只有登录验证,没有权限验证
    @SsoPowerAop(power = "10100011") //登录与权限同时验证
    @Log("重置密码")
    public BaseResponse<Boolean> resetPassword(@RequestBody @Validated ResetPasswordVo vo, BindingResult bindingResult) {
        int count;
        try {
            if (bindingResult != null && bindingResult.hasErrors()) {
                return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
            }
            String password = MD5.encrypt(vo.password);//进行加密码
            Long idLg = Long.parseLong(vo.id);
             BaUser po = this.sv.selectById(idLg);
            if (Objects.isNull(po)) {
                return BaseResponseUtils.buildFail("未得到用户,请求失败");
            } else {
                count = this.sv.changePassword(idLg, password);
            }
        } catch (Exception e) {
            log.error("保存用户异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
        if (count <= 0) {
            return BaseResponseUtils.buildFail("数据库存储失败");
        } else {
            return BaseResponseUtils.buildSuccess("重置密码成功");
        }
    }
 
}