| | |
| | | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.oying.domain.BucketStorage; |
| | | import com.oying.modules.security.service.OnlineUserService; |
| | | import com.oying.modules.security.service.UserCacheManager; |
| | | import com.oying.modules.system.domain.Job; |
| | | import com.oying.modules.system.domain.Role; |
| | | import com.oying.modules.system.domain.User; |
| | | import com.oying.modules.system.domain.dto.UserQueryCriteria; |
| | |
| | | import com.oying.modules.system.mapper.UserMapper; |
| | | import com.oying.modules.system.mapper.UserRoleMapper; |
| | | import com.oying.modules.system.service.UserService; |
| | | import com.oying.service.BucketStorageService; |
| | | import com.oying.utils.*; |
| | | import lombok.RequiredArgsConstructor; |
| | | import com.oying.config.properties.FileProperties; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.validation.constraints.NotBlank; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | |
| | | private final RedisUtils redisUtils; |
| | | private final UserCacheManager userCacheManager; |
| | | private final OnlineUserService onlineUserService; |
| | | private final BucketStorageService bucketStorageService; |
| | | |
| | | @Override |
| | | public PageResult<User> queryAll(UserQueryCriteria criteria, Page<Object> page) { |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void create(User resources) { |
| | | resources.setDeptId(resources.getDept().getId()); |
| | | if (userMapper.findByUsername(resources.getUsername()) != null) { |
| | | throw new EntityExistException(User.class, "username", resources.getUsername()); |
| | | } |
| | | if (userMapper.findByEmail(resources.getEmail()) != null) { |
| | | throw new EntityExistException(User.class, "email", resources.getEmail()); |
| | | } |
| | | // if (userMapper.findByEmail(resources.getEmail()) != null) { |
| | | // throw new EntityExistException(User.class, "email", resources.getEmail()); |
| | | // } |
| | | if (userMapper.findByPhone(resources.getPhone()) != null) { |
| | | throw new EntityExistException(User.class, "phone", resources.getPhone()); |
| | | } |
| | | save(resources); |
| | | // 保存用户岗位 |
| | | userJobMapper.insertData(resources.getId(), resources.getJobs()); |
| | | // 保存用户角色 |
| | | userRoleMapper.insertData(resources.getId(), resources.getRoles()); |
| | | } |
| | |
| | | public void update(User resources) throws Exception { |
| | | User user = getById(resources.getId()); |
| | | User user1 = userMapper.findByUsername(resources.getUsername()); |
| | | User user2 = userMapper.findByEmail(resources.getEmail()); |
| | | User user3 = userMapper.findByPhone(resources.getPhone()); |
| | | if (user1 != null && !user.getId().equals(user1.getId())) { |
| | | throw new EntityExistException(User.class, "username", resources.getUsername()); |
| | | } |
| | | if (user2 != null && !user.getId().equals(user2.getId())) { |
| | | throw new EntityExistException(User.class, "email", resources.getEmail()); |
| | | } |
| | | if (user3 != null && !user.getId().equals(user3.getId())) { |
| | | throw new EntityExistException(User.class, "phone", resources.getPhone()); |
| | | } |
| | | if (resources.getEmail() != null) { |
| | | User user2 = userMapper.findByEmail(resources.getEmail()); |
| | | if (user2 != null && !user.getId().equals(user2.getId())) { |
| | | throw new EntityExistException(User.class, "email", resources.getEmail()); |
| | | } |
| | | user.setEmail(resources.getEmail()); |
| | | } |
| | | // 如果用户的角色改变 |
| | | if (!resources.getRoles().equals(user.getRoles())) { |
| | |
| | | redisUtils.del(CacheKey.ROLE_AUTH + resources.getId()); |
| | | redisUtils.del(CacheKey.ROLE_USER + resources.getId()); |
| | | } |
| | | // 修改机构会影响 数据权限 |
| | | if (!Objects.equals(resources.getDept(),user.getDept())) { |
| | | redisUtils.del(CacheKey.DATA_USER + resources.getId()); |
| | | } |
| | | // 如果用户被禁用,则清除用户登录信息 |
| | | if(!resources.getEnabled()){ |
| | | if (!resources.getEnabled()) { |
| | | onlineUserService.kickOutForUsername(resources.getUsername()); |
| | | } |
| | | user.setDeptId(resources.getDept().getId()); |
| | | user.setUsername(resources.getUsername()); |
| | | user.setEmail(resources.getEmail()); |
| | | user.setEnabled(resources.getEnabled()); |
| | | user.setRoles(resources.getRoles()); |
| | | user.setDept(resources.getDept()); |
| | | user.setJobs(resources.getJobs()); |
| | | user.setPhone(resources.getPhone()); |
| | | user.setNickName(resources.getNickName()); |
| | | user.setGender(resources.getGender()); |
| | | saveOrUpdate(user); |
| | | // 清除缓存 |
| | | delCaches(user.getId(), user.getUsername()); |
| | | // 更新用户岗位 |
| | | userJobMapper.deleteByUserId(resources.getId()); |
| | | userJobMapper.insertData(resources.getId(), resources.getJobs()); |
| | | // 更新用户角色 |
| | | userRoleMapper.deleteByUserId(resources.getId()); |
| | | userRoleMapper.insertData(resources.getId(), resources.getRoles()); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public User findByOpenid(String openid) { |
| | | return userMapper.findByOpenid(openid); |
| | | } |
| | | |
| | | @Override |
| | | public User findByRiderOpenId(String riderOpenId) { |
| | | return userMapper.findByRiderOpenId(riderOpenId); |
| | | } |
| | | |
| | | @Override |
| | | public User getLoginData(String userName) { |
| | | return userMapper.findByUsername(userName); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void updateRiderOpenId(User user1) { |
| | | User user = new User(); |
| | | user.setId(user1.getId()); |
| | | user.setRiderOpenId(user1.getRiderOpenId()); |
| | | userMapper.updateById(user); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Map<String, String> updateAvatar(MultipartFile multipartFile) { |
| | | // 文件大小验证 |
| | |
| | | // 验证文件上传的格式 |
| | | String image = "gif jpg png jpeg"; |
| | | String fileType = FileUtil.getExtensionName(multipartFile.getOriginalFilename()); |
| | | if(fileType != null && !image.contains(fileType)){ |
| | | throw new BadRequestException("文件格式错误!, 仅支持 " + image +" 格式"); |
| | | if (fileType != null && !image.contains(fileType)) { |
| | | throw new BadRequestException("文件格式错误!, 仅支持 " + image + " 格式"); |
| | | } |
| | | User user = userMapper.findByUsername(SecurityUtils.getCurrentUsername()); |
| | | String oldPath = user.getAvatarPath(); |
| | | File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar()); |
| | | user.setAvatarPath(Objects.requireNonNull(file).getPath()); |
| | | user.setAvatarName(file.getName()); |
| | | BucketStorage bucketStorage = bucketStorageService.create(null, multipartFile); |
| | | user.setAvatarPath(bucketStorage.getPath()); |
| | | user.setAvatarName(bucketStorage.getRealName()); |
| | | saveOrUpdate(user); |
| | | if (StringUtils.isNotBlank(oldPath)) { |
| | | FileUtil.del(oldPath); |
| | | bucketStorageService.deleteById(oldPath); |
| | | } |
| | | @NotBlank String username = user.getUsername(); |
| | | flushCache(username); |
| | | return new HashMap<String, String>(1) {{ |
| | | put("avatar", file.getName()); |
| | | put("avatar", bucketStorage.getRealName()); |
| | | }}; |
| | | } |
| | | |
| | |
| | | Map<String, Object> map = new LinkedHashMap<>(); |
| | | map.put("用户名", user.getUsername()); |
| | | map.put("角色", roles); |
| | | map.put("机构", user.getDept().getName()); |
| | | map.put("岗位", user.getJobs().stream().map(Job::getName).collect(Collectors.toList())); |
| | | map.put("邮箱", user.getEmail()); |
| | | map.put("状态", user.getEnabled() ? "启用" : "禁用"); |
| | | map.put("手机号码", user.getPhone()); |
| | | map.put("openid", user.getOpenid()); |
| | | map.put("修改密码的时间", user.getPwdResetTime()); |
| | | map.put("创建日期", user.getCreateTime()); |
| | | list.add(map); |