|
@@ -0,0 +1,157 @@
|
|
|
|
|
+package com.chuanghai.h3c_reporting.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.common.exception.BizCodeEnume;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.common.exception.RRException;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.common.utils.MyQuery;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.common.utils.PageParam;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.common.utils.PageUtils;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.controller.request.AdminLoginRequest;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.controller.request.AdminPasswordUpdateRequest;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.controller.request.UserQueryRequest;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.dto.UserDTO;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.entity.User;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.mapper.UserMapper;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.service.UserService;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.util.JWTUtil;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.util.RSAUtils;
|
|
|
|
|
+import com.chuanghai.h3c_reporting.vo.UserVO;
|
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author 27951
|
|
|
|
|
+ * @version 1.0
|
|
|
|
|
+ * @description: TODO
|
|
|
|
|
+ * @date 2023/3/7 17:42
|
|
|
|
|
+ */
|
|
|
|
|
+@Service("UserService")
|
|
|
|
|
+public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${my-security.private-key}")
|
|
|
|
|
+ private String privateKey;
|
|
|
|
|
+ @Value("${my-security.public-key}")
|
|
|
|
|
+ private String publickey;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public PageUtils queryPage(UserQueryRequest userQueryRequest) {
|
|
|
|
|
+ PageParam pageParam = new PageParam();
|
|
|
|
|
+ BeanUtils.copyProperties(userQueryRequest, pageParam);
|
|
|
|
|
+ QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
+ queryWrapper.and(
|
|
|
|
|
+ e -> e.like(StringUtils.hasText(userQueryRequest.getUserName()), "user_name", userQueryRequest.getUserName())
|
|
|
|
|
+ .like(StringUtils.hasText(userQueryRequest.getName()), "name", userQueryRequest.getName())
|
|
|
|
|
+ .eq(StringUtils.hasText(userQueryRequest.getTime()), "time", userQueryRequest.getTime())
|
|
|
|
|
+ .eq("status", "1")
|
|
|
|
|
+ );
|
|
|
|
|
+ queryWrapper.orderByDesc("time");
|
|
|
|
|
+ IPage<User> page = this.page(
|
|
|
|
|
+ new MyQuery<User>().getPage(pageParam),
|
|
|
|
|
+ queryWrapper
|
|
|
|
|
+ );
|
|
|
|
|
+ return new PageUtils(page);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public UserVO login(AdminLoginRequest request) {
|
|
|
|
|
+ QueryWrapper<User> wrapper = new QueryWrapper<>();
|
|
|
|
|
+ wrapper.eq("user_name", request.getUserName());
|
|
|
|
|
+ wrapper.last("limit 1");
|
|
|
|
|
+ User staff = this.getOne(wrapper);
|
|
|
|
|
+ if (staff == null) {
|
|
|
|
|
+ throw new RRException(BizCodeEnume.ADMIN_LOGIN_FAIL);
|
|
|
|
|
+ }
|
|
|
|
|
+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
|
|
|
|
+ String password;
|
|
|
|
|
+ try {
|
|
|
|
|
+ password = RSAUtils.decrypt(request.getPassword(), RSAUtils.getPrivateKey(privateKey));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ log.error("密码解密失败,错误信息【{}】", e.getCause());
|
|
|
|
|
+ throw new RRException(BizCodeEnume.ADMIN_LOGIN_FAIL, "密码解密失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!encoder.matches(password, staff.getPassword())) {
|
|
|
|
|
+ throw new RRException(BizCodeEnume.ADMIN_LOGIN_FAIL);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 登录成功,生成token
|
|
|
|
|
+ UserDTO dto = UserDTO.builder().id(staff.getId()).adminType(1).build();
|
|
|
|
|
+ String token = JWTUtil.geneJsonWebToken(dto);
|
|
|
|
|
+ // 生成vo
|
|
|
|
|
+ return UserVO.builder()
|
|
|
|
|
+ .token(token)
|
|
|
|
|
+ .tokenTtl(JWTUtil.getExpired())
|
|
|
|
|
+ .userName(staff.getUserName())
|
|
|
|
|
+ .name(staff.getName())
|
|
|
|
|
+ .adminType(staff.getAdminType())
|
|
|
|
|
+ .time(String.valueOf(staff.getTime()))
|
|
|
|
|
+ .status(staff.getStatus())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void mySave(User user) {
|
|
|
|
|
+ String password;
|
|
|
|
|
+ try {
|
|
|
|
|
+ password = RSAUtils.decrypt(user.getPassword(), RSAUtils.getPrivateKey(privateKey));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RRException(BizCodeEnume.UNKNOW_EXCEPTION, "新增用户失败-密码解密错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
|
|
|
+ user.setPassword(passwordEncoder.encode(password));
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.save(user);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RRException(BizCodeEnume.DATA_IS_EXIST);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void updatePassword(AdminPasswordUpdateRequest request) {
|
|
|
|
|
+ String password;
|
|
|
|
|
+ String newPassword;
|
|
|
|
|
+ // 获取当前登录用户信息
|
|
|
|
|
+ User user = this.getById(request.getId());
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ throw new RRException(BizCodeEnume.PERMISSION_DENIED, "无效的用户");
|
|
|
|
|
+ }
|
|
|
|
|
+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 密码解密
|
|
|
|
|
+ password = RSAUtils.decrypt(request.getPassword(), RSAUtils.getPrivateKey(privateKey));
|
|
|
|
|
+ if (encoder.matches(password, user.getPassword())){
|
|
|
|
|
+ newPassword = RSAUtils.decrypt(request.getNewPassword(), RSAUtils.getPrivateKey(privateKey));
|
|
|
|
|
+ // 密码加密存储
|
|
|
|
|
+ BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
|
|
|
+ password = passwordEncoder.encode(newPassword);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ throw new RRException(BizCodeEnume.PARAMETER_ERROR, "密码错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RRException(BizCodeEnume.UNKNOW_EXCEPTION, "修改用户密码失败-密码解密错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ user.setPassword(password);
|
|
|
|
|
+ this.updateById(user);
|
|
|
|
|
+
|
|
|
|
|
+// if (request.getId() != null) { // 修改他人密码
|
|
|
|
|
+// if (user.getAdminType() != 1) {
|
|
|
|
|
+// throw new RRException(BizCodeEnume.PERMISSION_DENIED, "非超级管理员不能修改他人密码");
|
|
|
|
|
+// }
|
|
|
|
|
+// User admin = this.getById(request.getId());
|
|
|
|
|
+// if (admin == null) {
|
|
|
|
|
+// throw new RRException(BizCodeEnume.PARAMETER_ERROR, "用户不存在");
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// admin.setPassword(password);
|
|
|
|
|
+//
|
|
|
|
|
+// this.updateById(admin);
|
|
|
|
|
+// } else { // 修改本人密码
|
|
|
|
|
+// user.setPassword(password);
|
|
|
|
|
+// this.updateById(user);
|
|
|
|
|
+// }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|