| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.entity.User; |
| | | import com.moral.api.entity.UserGroup; |
| | | |
| | | import com.moral.api.entity.UserLog; |
| | | import com.moral.api.mapper.MenuMapper; |
| | | import com.moral.api.mapper.UserGroupMapper; |
| | | import com.moral.api.mapper.UserLogMapper; |
| | | import com.moral.api.mapper.UserMapper; |
| | | import com.moral.api.pojo.bo.UserBO; |
| | | import com.moral.api.service.UserService; |
| | | import com.moral.api.utils.OperationLogUtils; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.ResponseCodeEnum; |
| | | import com.moral.util.AESUtils; |
| | |
| | | import com.moral.util.MD5Utils; |
| | | import com.moral.util.RegexUtils; |
| | | import com.moral.util.TokenUtils; |
| | | import com.moral.util.WebUtils; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Autowired |
| | | private UserGroupMapper userGroupMapper; |
| | | |
| | | @Autowired |
| | | private UserLogMapper userLogMapper; |
| | | |
| | | @Value("${AES.KEY}") |
| | | private String AESKey; |
| | | |
| | | |
| | | @Override |
| | | public UserBO selectUserInfo(Map<String, Object> parameters) { |
| | |
| | | result.put("code", ResponseCodeEnum.TOKEN_CREATE_ERROR.getCode()); |
| | | result.put("msg", ResponseCodeEnum.TOKEN_CREATE_ERROR.getMsg()); |
| | | } |
| | | //日志 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | UserLog userLog = new UserLog(); |
| | | userLog.setType(Constants.LOGIN_OPERTATE_TYPE); |
| | | userLog.setIp(WebUtils.getIpAddr(request)); |
| | | userLog.setOperateId(userBo.getId()); |
| | | userLog.setOrganizationId(userBo.getOrganizationId()); |
| | | userLog.setContent("登陆了用户:" + userBo.getAccount()); |
| | | userLogMapper.insert(userLog); |
| | | return result; |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Map<String, Object> addUser(User user, String token) { |
| | | public Map<String, Object> addUser(User user) { |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | String token = request.getHeader("token"); |
| | | |
| | | Map<String, Object> result = new HashMap<>(); |
| | | Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(token); |
| | | |
| | |
| | | } else { |
| | | user.setExpireTime(userExpireTime); |
| | | } |
| | | } else { |
| | | if (orgExpireTime != null) { |
| | | user.setExpireTime(orgExpireTime); |
| | | } |
| | | } |
| | | userMapper.insert(user); |
| | | //日志 |
| | | String content = "添加了用户:" + user.getAccount(); |
| | | OperationLogUtils.insertLog(request, content, Constants.INSERT_OPERATE_TYPE); |
| | | return result; |
| | | } |
| | | |
| | |
| | | //逻辑删除user |
| | | user.setIsDelete(Constants.DELETE); |
| | | userMapper.updateById(user); |
| | | //逻辑删除user_group |
| | | //删除user_group |
| | | UpdateWrapper<UserGroup> deleteWrapper = new UpdateWrapper<>(); |
| | | deleteWrapper.set("is_delete", Constants.DELETE).eq("user_id", userId); |
| | | deleteWrapper.eq("user_id", userId); |
| | | userGroupMapper.delete(deleteWrapper); |
| | | //更新redis |
| | | if (TokenUtils.hHasKey(userId)) { |
| | | String token = TokenUtils.hget(userId).toString(); |
| | | TokenUtils.destoryToken(userId, token); |
| | | } |
| | | userGroupMapper.update(null, deleteWrapper); |
| | | //日志 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | String content = "删除了用户:" + user.getAccount(); |
| | | OperationLogUtils.insertLog(request, content, Constants.DELETE_OPERATE_TYPE); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Map<String, Object> updateUser(User user, String token) { |
| | | public Map<String, Object> updateUser(User user) { |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | String token = request.getHeader("token"); |
| | | |
| | | //更新的属性 |
| | | Map<String, Object> update = JSONObject.parseObject(JSON.toJSONString(user), Map.class); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(token); |
| | | String account = userMapper.selectById(user.getId()).getAccount(); |
| | | User beforeUser = userMapper.selectById(user.getId()); |
| | | Map<String, Object> before = JSONObject.parseObject(JSON.toJSONString(beforeUser), Map.class); |
| | | String account = beforeUser.getAccount(); |
| | | //account不可修改 |
| | | user.setAccount(account); |
| | | |
| | |
| | | } |
| | | //密码校验 |
| | | String password = user.getPassword(); |
| | | //密码解密 |
| | | password = AESUtils.decrypt(password, AESKey); |
| | | if (!RegexUtils.checkPassword(password)) { |
| | | result.put("code", ResponseCodeEnum.PASSWORD_INVALID.getCode()); |
| | | result.put("msg", ResponseCodeEnum.PASSWORD_INVALID.getMsg()); |
| | | return result; |
| | | if (password != null) { |
| | | //密码解密 |
| | | password = AESUtils.decrypt(password, AESKey); |
| | | if (!RegexUtils.checkPassword(password)) { |
| | | result.put("code", ResponseCodeEnum.PASSWORD_INVALID.getCode()); |
| | | result.put("msg", ResponseCodeEnum.PASSWORD_INVALID.getMsg()); |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | //正则校验手机号 |
| | |
| | | } else { |
| | | user.setExpireTime(userExpireTime); |
| | | } |
| | | } else { |
| | | if (orgExpireTime != null) { |
| | | user.setExpireTime(orgExpireTime); |
| | | } |
| | | } |
| | | //更新redis |
| | | String userId = user.getId().toString(); |
| | |
| | | TokenUtils.destoryToken(userId, deleteToken); |
| | | } |
| | | userMapper.updateById(user); |
| | | |
| | | //日志 |
| | | StringBuilder content = new StringBuilder("修改了用户:" + user.getAccount() + "->"); |
| | | for (String key : update.keySet()) { |
| | | Object afterValue = update.get(key); |
| | | if (!key.equals("id") && afterValue != null) { |
| | | //修改前属性值 |
| | | Object beforeValue = before.get(key); |
| | | content.append(key).append(":").append(beforeValue).append("=>").append(afterValue).append(";"); |
| | | } |
| | | } |
| | | OperationLogUtils.insertLog(request, content.toString(), Constants.UPDATE_OPERATE_TYPE); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public Page<User> selectUsers(Map<String, Object> parameters) { |
| | | Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(parameters.get("token").toString()); |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | String token = request.getHeader("token"); |
| | | |
| | | Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(token); |
| | | Map<String, Object> orgInfo = (Map<String, Object>) currentUserInfo.get("organization"); |
| | | QueryWrapper<User> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("organization_id", orgInfo.get("id")) |
| | |
| | | queryWrapper.orderByDesc("create_time"); |
| | | } |
| | | userMapper.selectPage(pageData, queryWrapper); |
| | | |
| | | return pageData; |
| | | } |
| | | |