| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.whyc.mapper.UserInfMapper; |
| | | import com.whyc.pojo.Response; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.pojo.db_user.UserInf; |
| | | import com.whyc.util.MD5Util; |
| | | import com.whyc.util.RSAUtil; |
| | |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | public class UserInfService { |
| | |
| | | public Response updateUser(int uid,String uname, int udownloadRole) { |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | wrapper.set("udownload_role",udownloadRole); |
| | | wrapper.set("uname",uname); |
| | | if(uname!=null){ |
| | | wrapper.set("uname",uname); |
| | | } |
| | | wrapper.eq("uid",uid); |
| | | int bl=mapper.update(null,wrapper); |
| | | return new Response().set(1,bl>0); |
| | |
| | | int flag=mapper.update(null,wrapper); |
| | | return new Response().set(1,flag>0,flag>0?"修改成功":"修改失败"); |
| | | }else { |
| | | return new Response().set(1,false,"原密码不择行情"); |
| | | return new Response().set(1,false,"原密码不正确"); |
| | | } |
| | | } |
| | | //重置密码 |
| | | public Response resetSnId(int uid ) { |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | wrapper.set("usnid",RSAUtil.encrypt("123456",RSAUtil.getPublicKey())); |
| | | wrapper.eq("uid",uid); |
| | | int flag=mapper.update(null,wrapper); |
| | | return new Response().set(1,flag>0,flag>0?"修改成功":"修改失败"); |
| | | |
| | | } |
| | | //删除用户信息 |
| | | public Response delUser(int uid) { |
| | |
| | | //Session存储当前用户及权限组列表 |
| | | UserInf userDB = (UserInf) subject.getPrincipal(); |
| | | userDB.setUsnid(null); |
| | | request.getSession().setAttribute("user", userDB); |
| | | return response.setII(1,true, userDB,"登录成功"); |
| | | } |
| | | |
| | |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | subject.logout(); |
| | | } |
| | | //将用户添加至100~1000管理员 |
| | | public Response improveRole(int uid) { |
| | | //判断表是否存在 |
| | | String tableName = mapper.existTable(); |
| | | if(tableName==null){ |
| | | ///创建100~1000的id表,不存在则创建 |
| | | mapper.createNumber(); |
| | | //插入默认数据 |
| | | String sql=" INSERT INTO temp_numbers(unumber) " + |
| | | " VALUES "; |
| | | for(int i=101;i<=1000;i++){ |
| | | sql+="("+i+")"; |
| | | if(i!=1000){ |
| | | sql+=","; |
| | | } |
| | | } |
| | | mapper.setUnumber(sql); |
| | | } |
| | | String minUid=mapper.getIn1000(); |
| | | if(minUid==null){ |
| | | return new Response().set(1,false,"管理员个数超过上限"); |
| | | } |
| | | UpdateWrapper wrapper =new UpdateWrapper(); |
| | | wrapper.set("uid",minUid); |
| | | wrapper.eq("uid",uid); |
| | | int flag= mapper.update(null,wrapper); |
| | | return new Response().set(1,flag>0,flag>0?"身份变更成功":"身份变更失败"); |
| | | } |
| | | //将管理员变成普通用户 |
| | | public Response dropRole(int uid, HttpServletRequest request) { |
| | | //查询最大的uid |
| | | String maxUid=mapper.getMaxUid(); |
| | | if(maxUid==null){ |
| | | maxUid="1001"; |
| | | } |
| | | //根据用户id查询用户信息 |
| | | QueryWrapper qWrapper=new QueryWrapper(); |
| | | qWrapper.eq("uid",uid); |
| | | qWrapper.last("limit 1"); |
| | | UserInf uinf=mapper.selectOne(qWrapper); |
| | | //编辑 |
| | | UpdateWrapper wrapper =new UpdateWrapper(); |
| | | wrapper.set("uid",Integer.valueOf(maxUid)+1); |
| | | wrapper.eq("uid",uid); |
| | | int flag= mapper.update(null,wrapper); |
| | | //如果是普通用户自己强退,将用户名对应的sessionId变更 |
| | | ServletContext servletContext = request.getServletContext(); |
| | | servletContext.setAttribute(uinf.getUname(),"123456"); |
| | | return new Response().set(1,flag>0,flag>0?"身份变更成功":"身份变更失败"); |
| | | } |
| | | |
| | | public Response getUserNameList() { |
| | | QueryWrapper<UserInf> query = Wrappers.query(); |
| | | query.select("uname"); |
| | | List<String> userNameList = mapper.selectList(query).stream().map(UserInf::getUname).collect(Collectors.toList()); |
| | | return new Response().set(1,userNameList); |
| | | } |
| | | |
| | | //登录检测 |
| | | public Response checkUserWebSocket(HttpSession httpSession){ |
| | | Response model = new Response(); |
| | | try { |
| | | UserInf user = (UserInf) httpSession.getAttribute("user"); |
| | | if(user!=null){ |
| | | String sessionId = (String) httpSession.getServletContext().getAttribute(user.getUname()); |
| | | if(httpSession.getId().equals(sessionId)){ |
| | | model.set(1,user,null); |
| | | }else{ |
| | | if(sessionId.equals("123456")){ |
| | | model.set(1,false,"身份权限变更,请重新登录"); |
| | | }else{ |
| | | model.set(1,false,"不同主机登录"); |
| | | } |
| | | //用户在其他主机登录,强迫用户在本机的session失效 |
| | | httpSession.invalidate(); |
| | | } |
| | | } |
| | | else { |
| | | model.set(1,false,"用户信息失效,请重新登录"); |
| | | } |
| | | }catch (Exception e){ |
| | | model.set(1,false,"登录信息失效,重新登录"); |
| | | } |
| | | return model; |
| | | } |
| | | } |