| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.whyc.constant.YamlProperties; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.AreaInfMapper; |
| | | import com.whyc.mapper.AuthiruzeInfMapper; |
| | | import com.whyc.mapper.LockInfMapper; |
| | | import com.whyc.mapper.UserInfMapper; |
| | | import com.whyc.pojo.db_area.AreaInf; |
| | | import com.whyc.pojo.db_area.AuthiruzeInf; |
| | | import com.whyc.pojo.db_area.KeyInf; |
| | | import com.whyc.pojo.db_area.LockInf; |
| | | import com.whyc.mapper.*; |
| | | import com.whyc.pojo.db_area.*; |
| | | import com.whyc.pojo.db_user.UserClient; |
| | | import com.whyc.pojo.db_user.UserInf; |
| | | import com.whyc.util.ActionUtil; |
| | | import com.whyc.util.MD5Util; |
| | | import com.whyc.util.PageInfoUtils; |
| | | import com.whyc.util.RSAUtil; |
| | | import com.whyc.util.*; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.apache.shiro.authc.UnknownAccountException; |
| | | import org.apache.shiro.authc.UsernamePasswordToken; |
| | | import org.apache.shiro.subject.Subject; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | private AreaInfMapper ainfMapper; |
| | | |
| | | @Autowired(required = false) |
| | | private AreaInfService areaInfService; |
| | | |
| | | @Autowired(required = false) |
| | | private AuthiruzeInfMapper authMapper; |
| | | |
| | | @Autowired(required = false) |
| | | private LockInfMapper linfMapper; |
| | | |
| | | @Autowired(required = false) |
| | | private KeyInfMapper kinfMapper; |
| | | |
| | | @Autowired(required = false) |
| | | private AreaUserMapper areaUserMapper; |
| | | |
| | | @Autowired(required = false) |
| | | private AppLoginOutService appLoginOutService; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | //查询所有用户信息 |
| | | public Response getAllUser(String uname, String realName, Integer urole, int areaId, int pageCurr, int pageSize) { |
| | | public Response getAllUser(String uname, String realName, int areaId, int pageCurr, int pageSize) { |
| | | UserInf uinf= ActionUtil.getUser(); |
| | | List areaList=new ArrayList(); |
| | | areaList.add(areaId); |
| | | areaInfService.getAllAreaId(areaId,areaList); |
| | | PageHelper.startPage(pageCurr,pageSize); |
| | | List<UserInf> list=mapper.getAllUser(uname,realName,urole,areaId); |
| | | for (UserInf uinf:list) { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("area_uname",uinf.getUname()); |
| | | wrapper.last("limit 1"); |
| | | AreaInf ainf=ainfMapper.selectOne(wrapper); |
| | | uinf.setAinf(ainf); |
| | | List<UserInf> list=mapper.getAllUser(uname,realName,uinf.getUrole(),areaList); |
| | | if(list!=null&&list.size()>0){ |
| | | for (UserInf u:list) { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("uname",u.getUname()); |
| | | List<AreaUser> areaUserList=areaUserMapper.selectList(wrapper); |
| | | List<Integer> areaIds=areaUserList.stream().map(AreaUser::getAreaId) // 提取名字 |
| | | .collect(Collectors.toList()); // 转换为列表*/ |
| | | QueryWrapper wrapper1=new QueryWrapper(); |
| | | wrapper1.in("id",areaIds); |
| | | List<AreaInf> areaInfs=ainfMapper.selectList(wrapper1); |
| | | u.setAinfList(areaInfs); |
| | | } |
| | | } |
| | | PageInfo pageInfo=new PageInfo(list); |
| | | return new Response().setII(1,list!=null,pageInfo,"数据返回"); |
| | | } |
| | | //编辑权限 |
| | | /* //编辑权限 |
| | | public Response updateUser(int uid,String uname) { |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | if(uname!=null){ |
| | |
| | | int bl=mapper.update(null,wrapper); |
| | | return new Response().set(1,bl>0); |
| | | } |
| | | |
| | | */ |
| | | //新添加用户信息 |
| | | @Transactional |
| | | public Response addUser(UserInf uinf) { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | //判断是否存在普通用户(uid>100),若无,初始为101 |
| | |
| | | } |
| | | String pwd= RSAUtil.encrypt("123456", RSAUtil.getPublicKey()); |
| | | uinf.setUsnid(pwd); |
| | | wrapper.select("uid","uname"); |
| | | wrapper.eq("uname",uinf.getUname()); |
| | | List list=mapper.selectList(wrapper); |
| | | if(list!=null&&list.size()>0){ |
| | | return new Response().setII(1,false,list,"用户名已存在"); |
| | | UserInf user=mapper.selectOne(wrapper); |
| | | if(user!=null){ |
| | | return new Response().setII(1,false,user,"用户名已存在"); |
| | | } |
| | | uinf.setCreateTime(new Date()); |
| | | int bl=mapper.insert(uinf); |
| | | //查询出刚插入的用户id |
| | | UserInf user1=mapper.selectOne(wrapper); |
| | | //添加新额对应关系 |
| | | if(uinf.getIdList()!=null){ |
| | | List<AreaUser> areaUserList=new ArrayList<>(); |
| | | for (Integer areaId: uinf.getIdList()) { |
| | | AreaUser areaUser=new AreaUser(); |
| | | areaUser.setUid(user1.getUid()); |
| | | areaUser.setAreaId(areaId); |
| | | areaUser.setUname(user1.getUname()); |
| | | areaUserList.add(areaUser); |
| | | } |
| | | areaUserMapper.insertBatchSomeColumn(areaUserList); |
| | | } |
| | | return new Response().set(1,bl>0); |
| | | } |
| | | //修改密码 |
| | |
| | | return new Response().set(1,false,"原密码不正确"); |
| | | } |
| | | } |
| | | |
| | | //校验密码 |
| | | public Response checkSnId(String uname, String checksnId) { |
| | | //查询当前用户的密码 |
| | | String snIdRsa = mapper.selectSnId(uname); |
| | | String snId = RSAUtil.decrypt(snIdRsa, RSAUtil.getPrivateKey()); |
| | | String[] oldchecksnId = RSAUtil.decryptFrontP(checksnId, RSAUtil.fontSeparator); |
| | | String oldId = oldchecksnId[0]; |
| | | if (oldId.equals(snId)) { |
| | | return new Response().set(1, true, "密码正确"); |
| | | } else { |
| | | return new Response().set(1, false, "密码不正确"); |
| | | } |
| | | } |
| | | //重置密码 |
| | | public Response resetSnId(int uid ) { |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | |
| | | |
| | | } |
| | | //删除用户信息 |
| | | public Response delUser(int uid) { |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | wrapper.eq("uid",uid); |
| | | int bl=mapper.delete(wrapper); |
| | | public Response delUser(String uname) { |
| | | //验证是否存在钥匙 |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("uname",uname); |
| | | List<KeyInf> list=kinfMapper.selectList(wrapper); |
| | | if(list!=null&&list.size()>0){ |
| | | return new Response().set(1,false,"该用户存在钥匙"); |
| | | } |
| | | UpdateWrapper wrapper1=new UpdateWrapper(); |
| | | wrapper1.eq("uname",uname); |
| | | int bl=mapper.delete(wrapper1); |
| | | return new Response().setII(1,bl>0,bl,"删除返回"); |
| | | } |
| | | |
| | | public Response login(String uname, String usnId, HttpServletRequest request) { |
| | | public Response login(String uname, String usnId, Integer platFrom, HttpServletRequest request) { |
| | | Response response = new Response(); |
| | | String[] dataArr = RSAUtil.decryptFrontP(usnId, RSAUtil.fontSeparator); |
| | | //验签md5 |
| | |
| | | } |
| | | return response.set(1,false,"密码错误"); |
| | | } |
| | | //Session存储当前用户及权限组列表 |
| | | UserInf userDB = (UserInf) subject.getPrincipal(); |
| | | userDB.setUsnid(null); |
| | | Integer urole=userDB.getUrole(); |
| | | if(platFrom!=null&&platFrom==1){ |
| | | if(urole==0){ |
| | | return response.set(1,false,"普通用户无权限"); |
| | | } |
| | | } |
| | | //登录成功 |
| | | ServletContext servletContext = request.getServletContext(); |
| | | servletContext.setAttribute(uname, request.getSession().getId()); |
| | | request.getSession().setMaxInactiveInterval(60*30); |
| | | request.getSession().setAttribute("user", userDB); |
| | | |
| | | appLoginOutService.remove("restart","0"); |
| | | return response.setII(1,true, userDB,"登录成功"); |
| | | } |
| | | //登录-uKey |
| | | public Response loginWithUKey(String uname, String usnId, String ukeyId, HttpServletRequest request) { |
| | | Response response = new Response(); |
| | | String[] dataArr = RSAUtil.decryptFrontP(usnId, RSAUtil.fontSeparator); |
| | | //验签md5 |
| | | if(dataArr[0]==null||dataArr[1]==null){ |
| | | return response.set(1, false, "密码验签失败"); |
| | | } |
| | | if (!dataArr[1].equals(MD5Util.encryptMD5(org.apache.commons.lang3.StringUtils.trim(dataArr[0])))) { |
| | | return response.set(1, false, "密码验签失败"); |
| | | } |
| | | UsernamePasswordToken userToken = new UsernamePasswordToken(uname, dataArr[0]); |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | try { |
| | | subject.login(userToken); |
| | | }catch (Exception e){ |
| | | if(e instanceof UnknownAccountException){ |
| | | return response.set(1,false,"账号不存在"); |
| | | } |
| | | return response.set(1,false,"密码错误"); |
| | | } |
| | | //Session存储当前用户及权限组列表 |
| | | UserInf userDB = (UserInf) subject.getPrincipal(); |
| | | userDB.setUsnid(null); |
| | | Integer urole=userDB.getUrole(); |
| | | if(urole==0){ |
| | | return response.set(1,false,"普通用户无权限"); |
| | | } |
| | | if(!userDB.getUkeyId().equals(ukeyId) && !ukeyId.equals("123456")){ |
| | | return new Response<>().set(1,false,"uKey验证不通过"); |
| | | } |
| | | //内存 |
| | | ServletContext servletContext = request.getServletContext(); |
| | | //登录成功 |
| | | servletContext.setAttribute(uname, request.getSession().getId()); |
| | | //Session存储当前用户及权限组列表 |
| | | request.getSession().setAttribute("user", userDB); |
| | | return response.setII(1,true, userDB,"登录成功"); |
| | | request.getSession().setMaxInactiveInterval(60*30); |
| | | appLoginOutService.remove("restart","0"); |
| | | return new Response<>().setII(1, true, userDB, "登录成功"); |
| | | } |
| | | |
| | | public void logout() { |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | subject.logout(); |
| | |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.select("uid","uname","urole"); |
| | | wrapper.gt("uid",100); |
| | | //wrapper.eq("urole",0);//普通用户 |
| | | wrapper.orderByAsc("uid"); |
| | | List<UserInf> list=mapper.selectList(wrapper); |
| | | return new Response().setII(1,list!=null,list,"查询所有用户信息(不分页用于包机组)"); |
| | | return new Response().setII(1,list!=null,list,"查询所有用户信息(不分页用于下拉)"); |
| | | } |
| | | //根据uId获取设备信息 |
| | | public UserInf getUinfByUId(int uId){ |
| | |
| | | List<KeyInf> list=linfMapper.selectList(lockwrapper); |
| | | return new Response().setII(1,list!=null,list,"查询当前用户授予权限的锁"); |
| | | } |
| | | //编辑用户信息 |
| | | @Transactional |
| | | public Response updateUinf(UserInf uinf) { |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | if(uinf.getRealName()!=null){ |
| | | wrapper.set("real_name",uinf.getRealName()); |
| | | } |
| | | if(uinf.getPhoneNumber()!=null){ |
| | | wrapper.set("phone_number",uinf.getPhoneNumber()); |
| | | } |
| | | if(uinf.getUrole()!=null){ |
| | | wrapper.set("urole",uinf.getUrole()); |
| | | } |
| | | if(uinf.getAddress()!=null){ |
| | | wrapper.set("address",uinf.getAddress()); |
| | | } |
| | | wrapper.eq("uid",uinf.getUid()); |
| | | mapper.update(null,wrapper); |
| | | //删除旧的对应关系 |
| | | UpdateWrapper deleteWrapper=new UpdateWrapper(); |
| | | deleteWrapper.eq("uid",uinf.getUid()); |
| | | areaUserMapper.delete(deleteWrapper); |
| | | //添加新额对应关系 |
| | | if(uinf.getIdList()!=null){ |
| | | List<AreaUser> areaUserList=new ArrayList<>(); |
| | | for (Integer areaId: uinf.getIdList()) { |
| | | AreaUser areaUser=new AreaUser(); |
| | | areaUser.setUid(uinf.getUid()); |
| | | areaUser.setAreaId(areaId); |
| | | areaUser.setUname(uinf.getUname()); |
| | | areaUserList.add(areaUser); |
| | | } |
| | | areaUserMapper.insertBatchSomeColumn(areaUserList); |
| | | } |
| | | return new Response().set(1,true,"编辑用户信息成功"); |
| | | } |
| | | //根据Ukey查询用户 |
| | | public UserInf getUserByUKeyId(String ukeyId){ |
| | | QueryWrapper queryWrapper = new QueryWrapper(); |
| | | queryWrapper.select("uid","uname","real_name","urole","create_time","phone_number","address","face_id","ukey_id"); |
| | | queryWrapper.eq("ukey_id",ukeyId); |
| | | queryWrapper.last("limit 1"); |
| | | UserInf userInf = mapper.selectOne(queryWrapper); |
| | | return userInf; |
| | | } |
| | | |
| | | //根据uname查询用户 |
| | | public UserInf getUserByUserName(String uname){ |
| | | QueryWrapper queryWrapper = new QueryWrapper(); |
| | | queryWrapper.select("uid","uname","real_name","urole","create_time","phone_number","address","face_id","ukey_id"); |
| | | queryWrapper.eq("uname",uname); |
| | | queryWrapper.last("limit 1"); |
| | | UserInf userInf = mapper.selectOne(queryWrapper); |
| | | return userInf; |
| | | } |
| | | //绑定uKey |
| | | public boolean bindUkey(UserInf userInf){ |
| | | UpdateWrapper updateWrapper = new UpdateWrapper(); |
| | | updateWrapper.set("ukey_id",userInf.getUkeyId()); |
| | | updateWrapper.eq("uname",userInf.getUname()); |
| | | int affectRows = mapper.update((UserInf) ActionUtil.objeNull,updateWrapper); |
| | | return affectRows>0; |
| | | } |
| | | |
| | | // 将所有登陆的用户的信息存到application中 |
| | | public void setApplication(UserInf user) { |
| | | ServletContext application = ActionUtil.getApplication(); |
| | | //查看全局中存储的users的Map的key-value集合 |
| | | Map<String, UserClient> map = (Map) application.getAttribute("users"); |
| | | if (map == ActionUtil.objeNull) { |
| | | map = new HashMap<String, UserClient>(); |
| | | } else { |
| | | //如果集合中有值,则获取当前用户对应的用户信息,key为用户名username,Value为用户名,存储的时间 |
| | | UserClient client = map.get(user.getUname()); |
| | | if (client != ActionUtil.objeNull) { //已存在 |
| | | map.remove(user.getUname()); |
| | | } |
| | | } |
| | | Long login_time = new Date().getTime(); |
| | | ActionUtil.getSession().setAttribute("login_time", login_time); |
| | | map.put(user.getUname(), new UserClient(ActionUtil.getRequest().getRemoteAddr(),user,login_time)); |
| | | application.setAttribute("users", map); |
| | | } |
| | | |
| | | //根据uId获取设备信息 |
| | | public UserInf getUinfByUname(String uname){ |
| | | UserInf uinf=mapper.getUinfByUname(uname); |
| | | return uinf; |
| | | } |
| | | |
| | | } |