| | |
| | | package com.gkhy.assess.system.service.impl; |
| | | |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.gkhy.assess.common.api.CommonPage; |
| | | import com.gkhy.assess.common.constant.CacheConstant; |
| | |
| | | import com.gkhy.assess.common.domain.vo.LoginBody; |
| | | import com.gkhy.assess.common.enums.AttachTypeEnum; |
| | | import com.gkhy.assess.common.enums.UserIdentityEnum; |
| | | import com.gkhy.assess.common.enums.UserTypeEnum; |
| | | import com.gkhy.assess.common.exception.ApiException; |
| | | import com.gkhy.assess.common.utils.BeanValidators; |
| | | import com.gkhy.assess.common.utils.JwtTokenUtil; |
| | | import com.gkhy.assess.common.utils.PageUtil; |
| | | import com.gkhy.assess.common.utils.RedisUtils; |
| | | import com.gkhy.assess.system.domain.*; |
| | | import com.gkhy.assess.common.utils.*; |
| | | import com.gkhy.assess.system.domain.SysAgency; |
| | | import com.gkhy.assess.system.domain.SysAttach; |
| | | import com.gkhy.assess.system.domain.SysUser; |
| | | import com.gkhy.assess.system.mapper.SysAgencyMapper; |
| | | import com.gkhy.assess.system.mapper.SysUserMapper; |
| | | import com.gkhy.assess.system.service.SysAgencyService; |
| | | import com.gkhy.assess.system.service.SysAttachService; |
| | | import com.gkhy.assess.system.service.SysConfigService; |
| | | import com.gkhy.assess.system.utils.ShiroUtils; |
| | | import com.gkhy.assess.system.service.SysUserService; |
| | | import com.gkhy.assess.system.utils.ShiroUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.apache.shiro.authc.*; |
| | | import org.apache.shiro.subject.Subject; |
| | |
| | | import javax.validation.Validator; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | |
| | | /** |
| | |
| | | private HttpServletRequest request; |
| | | @Autowired |
| | | private SysAgencyMapper agencyMapper; |
| | | |
| | | @Autowired |
| | | private SysAgencyService agencyService; |
| | | @Autowired |
| | | private SysAttachService attachService; |
| | | |
| | |
| | | @Override |
| | | public AccountVO login(LoginBody loginBody) { |
| | | // 验证码校验 |
| | | // validateCaptcha(loginBody.getUsername(), loginBody.getCode(), loginBody.getUuid()); |
| | | UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginBody.getUsername(), loginBody.getPassword(), false); |
| | | // validateCaptcha(loginBody.getUsername(), loginBody.getCode(), loginBody.getUuid()); |
| | | UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginBody.getUsername(), Base64.decodeStr(loginBody.getPassword()), false); |
| | | Subject subject= SecurityUtils.getSubject(); |
| | | String msg ; |
| | | try { |
| | |
| | | |
| | | String token = JwtTokenUtil.sign(sysUser.getUsername(),sysUser.getPassword()); |
| | | accountVO.setToken(token); |
| | | String key= redisUtils.generateKey(CacheConstant.SYS_USER_TOKEN+":"+JwtTokenUtil.md5Encode(token)); |
| | | // 设置超时时间 |
| | | redisUtils.set(key,token); |
| | | redisUtils.expire(key,JwtTokenUtil.EXPIRATION*2/1000); |
| | | cacheUserToken(sysUser.getUsername(),token); |
| | | return accountVO; |
| | | }catch (UnknownAccountException | IncorrectCredentialsException uae){ |
| | | throw new ApiException("用户名/密码错误,请重新输入"); |
| | | } catch (LockedAccountException lae) { // 账号已被锁定 |
| | | msg = "账号已被锁定"; |
| | | throw new ApiException(msg); |
| | | } catch (AuthenticationException ae) { // 其他身份验证异常 |
| | | msg = "用户认证失败"; |
| | | }catch (AuthenticationException ae) { // 其他身份验证异常 |
| | | msg = "用户认证失败:"+ae.getMessage(); |
| | | throw new ApiException(msg); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 清空旧的登录信息,保证同时只有一个用户再登录,后登录用户会挤掉前一个用户 |
| | | * @param username |
| | | */ |
| | | public void cacheUserToken(String username,String newToken){ |
| | | |
| | | // String userKey = redisUtils.generateKey(CacheConstant.SYS_USER_TOKEN + ":" + username); |
| | | // String oldToken = (String) redisUtils.get(userKey); |
| | | // if (StrUtil.isNotBlank(oldToken)) { |
| | | // String oldTokenkey = redisUtils.generateKey(CacheConstant.SYS_USER_TOKEN + ":" + JwtTokenUtil.md5Encode(oldToken)); |
| | | // redisUtils.del(oldTokenkey); |
| | | // redisUtils.del(userKey); |
| | | // } |
| | | // redisUtils.set(userKey, newToken,(JwtTokenUtil.EXPIRATION*2/1000)+2); |
| | | |
| | | |
| | | String tokenKey= redisUtils.generateKey(CacheConstant.SYS_USER_TOKEN+":"+JwtTokenUtil.md5Encode(newToken)); |
| | | // 设置超时时间 |
| | | redisUtils.set(tokenKey,newToken); |
| | | redisUtils.expire(tokenKey,JwtTokenUtil.EXPIRATION*2/1000); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public void validateCaptcha(String username, String code, String uuid) |
| | | { |
| | | if(StrUtil.isBlank(code)||StrUtil.isBlank(uuid)){ |
| | | if(StringUtils.isBlank(code)||StringUtils.isBlank(uuid)){ |
| | | throw new ApiException("验证码或验证码标识为空"); |
| | | } |
| | | String verifyKey = CacheConstant.CAPTCHA_CODE_KEY +uuid; |
| | | String captcha = (String) redisUtils.get(verifyKey); |
| | | redisUtils.del(verifyKey); |
| | | if (StrUtil.isBlank(captcha)) |
| | | if (StringUtils.isBlank(captcha)) |
| | | { |
| | | throw new ApiException("验证码已失效"); |
| | | } |
| | |
| | | return sysUser; |
| | | } |
| | | |
| | | public void delCacheByUsername(String username){ |
| | | String key=redisUtils.generateKey(CacheConstant.SYS_USER_NAME+":"+username); |
| | | redisUtils.del(key); |
| | | } |
| | | |
| | | @Override |
| | | public SysUser getUserByUsernamePhone(String username) { |
| | | String key=redisUtils.generateKey(CacheConstant.SYS_USER_NAME+":"+username); |
| | |
| | | return sysUser; |
| | | } |
| | | |
| | | public void validatorPassword(String password){ |
| | | if(password.length()<5||password.length()>30){ |
| | | throw new ApiException("密码长度须在5-30之间"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = RuntimeException.class) |
| | | public int agencyRegister(SysUser user) { |
| | | public int addMonitor(SysUser user) { |
| | | //校验用户信息 |
| | | if(!checkUsernameUnique(new SysUser().setUsername(user.getUsername()))){ |
| | | throw new ApiException("用户名已存在"); |
| | |
| | | if(!checkPhoneUnique(new SysUser().setUsername(user.getPhone()))){ |
| | | throw new ApiException("手机号已存在"); |
| | | } |
| | | String originPsword=Base64.decodeStr(user.getPassword()); |
| | | validatorPassword(originPsword); |
| | | user.setPassword(JwtTokenUtil.encryptPassword(user.getUsername(),originPsword,user.getSalt())); |
| | | user.setIdentity(UserIdentityEnum.MONITOR.getCode()); |
| | | boolean b=save(user); |
| | | if(!b){ |
| | | throw new ApiException("创建监管用户信息失败"); |
| | | } |
| | | return 1; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = RuntimeException.class) |
| | | public int addAgency(SysUser user) { |
| | | //校验用户信息 |
| | | if(!checkUsernameUnique(new SysUser().setUsername(user.getUsername()))){ |
| | | throw new ApiException("用户名已存在"); |
| | | } |
| | | if(!checkPhoneUnique(new SysUser().setUsername(user.getPhone()))){ |
| | | throw new ApiException("手机号已存在"); |
| | | } |
| | | String originPsword=Base64.decodeStr(user.getPassword()); |
| | | validatorPassword(originPsword); |
| | | SysAgency agency=user.getAgency(); |
| | | if(ObjectUtil.isNull(agency)){ |
| | | throw new ApiException("机构信息不能为空"); |
| | | } |
| | | |
| | | //查询机构信息是否存在 |
| | | if(ObjectUtil.isNotNull(agencyMapper.checkAgencyNameUnique(agency.getName()))){ |
| | | if(!agencyService.checkAgencyNameUnique(new SysAgency().setName(agency.getName()))){ |
| | | throw new ApiException("机构名称已存在"); |
| | | } |
| | | |
| | | int i=agencyMapper.insert(agency); |
| | | if(i<1){ |
| | | throw new ApiException("保存机构信息失败"); |
| | | } |
| | | |
| | | user.setIdentity(UserIdentityEnum.AGENCY.getCode()); |
| | | user.setAgencyId(agency.getId()); |
| | | user.setUserType(UserIdentityEnum.AGENCY.getCode()); |
| | | user.setPassword(JwtTokenUtil.encryptPassword(user.getUsername(),originPsword,user.getSalt())); |
| | | |
| | | boolean b=save(user); |
| | | if(!b){ |
| | | throw new ApiException("创建机构用户信息失败"); |
| | |
| | | if(!checkPhoneUnique(new SysUser().setUsername(user.getPhone()))){ |
| | | throw new ApiException("手机号已存在"); |
| | | } |
| | | user.setUserType(UserIdentityEnum.EXPERT.getCode()); |
| | | String originPsword=Base64.decodeStr(user.getPassword()); |
| | | validatorPassword(originPsword); |
| | | user.setIdentity(UserIdentityEnum.EXPERT.getCode()); |
| | | user.setCreateBy(ShiroUtils.getSysUser().getUsername()); |
| | | user.setPassword(JwtTokenUtil.encryptPassword(user.getUsername(),originPsword,user.getSalt())); |
| | | boolean b=save(user); |
| | | if(!b){ |
| | | throw new ApiException("创建专家信息失败"); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public SysUser getUserInfoById(Long userId) { |
| | | return baseMapper.getUserInfoById(userId); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = RuntimeException.class) |
| | | public int deleteUserById(Long userId) { |
| | | SysUser user=getUserById(userId); |
| | | if(user==null){ |
| | | throw new ApiException("用户不存在"); |
| | | } |
| | | if(Objects.equals(ShiroUtils.getUserId(), userId)){ |
| | | throw new ApiException("不能删除自己账号"); |
| | | } |
| | | //机构用户,将机构信息设置成删除状态 |
| | | if(user.getUserType().equals(UserIdentityEnum.AGENCY.getCode())){ |
| | | if(user.getIdentity().equals(UserIdentityEnum.AGENCY.getCode())){ |
| | | if(user.getAgencyId()==null){ |
| | | throw new ApiException("获取机构id为空"); |
| | | } |
| | | agencyMapper.deleteAgencyById(user.getAgencyId()); |
| | | } |
| | | delCacheByUsername(user.getUsername()); |
| | | return baseMapper.deleteUserById(userId); |
| | | } |
| | | |
| | | @Override |
| | | public int editMonitor(SysUser user) { |
| | | //校验用户信息 |
| | | if(!checkUsernameUnique(user)){ |
| | | throw new ApiException("用户名已存在"); |
| | | } |
| | | if(!checkPhoneUnique(user)){ |
| | | throw new ApiException("手机号已存在"); |
| | | } |
| | | delCacheByUsername(user.getUsername()); |
| | | user.setPassword(null); |
| | | boolean b=updateById(user); |
| | | if(!b){ |
| | | throw new ApiException("更新监管用户信息失败"); |
| | | } |
| | | return 1; |
| | | } |
| | | |
| | | |
| | |
| | | throw new ApiException("手机号已存在"); |
| | | } |
| | | SysAgency agency=user.getAgency(); |
| | | if(ObjectUtil.isNull(agency)){ |
| | | throw new ApiException("机构信息不能为空"); |
| | | if(ObjectUtil.isNull(agency)||agency.getId()==null){ |
| | | throw new ApiException("机构信息或者id不能为空"); |
| | | } |
| | | |
| | | //查询机构信息是否存在 |
| | | if(ObjectUtil.isNotNull(agency)){ |
| | | if(!agencyService.checkAgencyNameUnique(agency)){ |
| | | throw new ApiException("机构名称已存在"); |
| | | } |
| | | agencyMapper.updateById(agency); |
| | | |
| | | delCacheByUsername(user.getUsername()); |
| | | agencyService.updateById(agency); |
| | | user.setPassword(null); |
| | | boolean b=updateById(user); |
| | | if(!b){ |
| | | throw new ApiException("更新用户失败"); |
| | |
| | | if(!checkPhoneUnique(user)){ |
| | | throw new ApiException("手机号已存在"); |
| | | } |
| | | user.setPassword(null); |
| | | boolean b=updateById(user); |
| | | if(!b){ |
| | | throw new ApiException("更新专家信息失败"); |
| | |
| | | attaches.add(attach); |
| | | } |
| | | attachService.saveBatch(attaches); |
| | | |
| | | delCacheByUsername(user.getUsername()); |
| | | return 1; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean resetUserPwd(SysUser user) { |
| | | return updateById(user); |
| | | String originPsword=Base64.decodeStr(user.getPassword()); |
| | | validatorPassword(originPsword); |
| | | checkUserAllowed(user); |
| | | SysUser oldUser=checkUserDataScope(user.getId()); |
| | | SysUser newUser=new SysUser().setId(user.getId()).setPassword(JwtTokenUtil.encryptPassword(oldUser.getUsername(),originPsword,oldUser.getSalt())); |
| | | newUser.setUpdateBy(ShiroUtils.getSysUser().getUsername()); |
| | | delCacheByUsername(oldUser.getUsername()); |
| | | return updateById(newUser); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void checkUserDataScope(Long userId) { |
| | | public SysUser checkUserDataScope(Long userId) { |
| | | if(userId==null){ |
| | | throw new ApiException("用户id为空!"); |
| | | } |
| | | SysUser user = baseMapper.getUserById(userId); |
| | | if (ObjectUtil.isNull(user)) |
| | | { |
| | | throw new ApiException("用户数据不存在!"); |
| | | } |
| | | return user; |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public boolean changeUserStatus(SysUser user) { |
| | | checkUserAllowed(user); |
| | | checkUserDataScope(user.getId()); |
| | | return updateById(user); |
| | | SysUser existUser=checkUserDataScope(user.getId()); |
| | | SysUser su=new SysUser().setId(user.getId()).setStatus(user.getStatus()); |
| | | su.setUpdateBy(ShiroUtils.getSysUser().getUsername()); |
| | | delCacheByUsername(existUser.getUsername()); |
| | | return updateById(su); |
| | | } |
| | | |
| | | @Override |
| | | public boolean changeApprove(SysUser user) { |
| | | checkUserAllowed(user); |
| | | checkUserDataScope(user.getId()); |
| | | return updateById(user); |
| | | SysUser existUser=checkUserDataScope(user.getId()); |
| | | SysUser su=new SysUser().setId(user.getId()).setState(user.getState()); |
| | | su.setUpdateBy(ShiroUtils.getSysUser().getUsername()); |
| | | delCacheByUsername(existUser.getUsername()); |
| | | return updateById(su); |
| | | } |
| | | } |