Welcome Huihui's Code World ! !
接下来看看由辉辉所写的关于OA项目的相关操作吧
表数据
表分析
所谓的权限管理就是不同的人管理不同的事,拥有着管理不同事情的不同权力。那么第一张表--权限表,这张表中存放的是用户的角色编号以及可看的权限界面。第二张表--用户表,其中不同的用户有不同的角色id。第三表--权限内容表,其中就是所有的页面
sql语句分析
权限管理,首先就是围绕着权限表来的,但是我们要的内容是在其他表中的。所以这个时候我们肯定是需要拿到两张或以上的表来操作的。那么我们首先是需要通过id把t_oa_permission和t_oa_role_permission连接起来,那么还有rid呢,我们也需要连接用户表吗?其实不然,我们在登录的时候就以及把用户的信息保存在session中了,那么我们是不是也可以拿到保存到session中的rid呢?显然是可以的,在下文会有代码展示!
SELECT * FROM t_oa_permission WHERE id IN ( SELECT pid FROM t_oa_role_permission WHERE rid = rid )
①权限管理:不同身份的人看到的左侧菜单不一样,所拥有的权限不一样
②个人中心:
拿到当前登录的用户的个人信息,当用户进入到个人中心界面点击修改的时候,需要他先输入原密码,只有原密码正确,才能够进行后续的修改信息的操作【还没有输入密码以及密码输入不正确时,用户的信息为可读状态】修改完成后,会进行一个判断,修改之后的密码不能够与原密码一致
修改头像,如果用户没有头像的话,那么便会显示一个默认的头像,如果用户有头像了,那么显示的便是用户本身的头像,且头像的上面都附有一个相机图标的遮罩,点击这个相机图标的遮罩,便可以触发一个图像上传的机制,可以选择本地的图片作为自己的头像。
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="/common/head.jsp"%>
main.js
var element,layer,util,$; layui.use(['element', 'layer', 'util'], function(){ element = layui.element ,layer = layui.layer ,util = layui.util ,$ = layui.$; //左侧树形菜单 $.ajax({ url:"permission.action?methodName=menus", dataType:'json', method: 'post', success: function(data) { console.info(data) var content=' '; $.each(data,function(i,n){ content+='
'; content+=' '+n.text+' '; if(n.hasChildren){ var children =n.children; content+=' '; }) $("#menu").html(content); element.render('menu'); } }) }); // //右侧选项卡 function openTab(title,content,id){ var $node=$('li[lay-id="'+id+'"]'); console.info($node); //新增一个Tab项 if($node.length==0){ element.tabAdd('demo', { title : title, //用于演示 content :"", id :id }) } //切换到指定Tab项 element.tabChange('demo', id); //切换到:用户管理 }'; $.each(children,function(idx,nodes){ content+='
'; } content+='- '+nodes.text+'
'; // content+='- '+nodes.text+'
'; }) content+='
后端代码
实体类
package com.wh.entity; public class Permission { private long id; private String name; private String description; private String url; private long pid; private int ismenu; private long displayno; public Permission() { // TODO Auto-generated constructor stub } public Permission(long id, String name, String description, String url, long pid, int ismenu, long displayno) { super(); this.id = id; this.name = name; this.description = description; this.url = url; this.pid = pid; this.ismenu = ismenu; this.displayno = displayno; } public Permission(String name, String description, String url, long pid, int ismenu, long displayno) { super(); this.name = name; this.description = description; this.url = url; this.pid = pid; this.ismenu = ismenu; this.displayno = displayno; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public long getPid() { return pid; } public void setPid(long pid) { this.pid = pid; } public int getIsmenu() { return ismenu; } public void setIsmenu(int ismenu) { this.ismenu = ismenu; } public long getDisplayno() { return displayno; } public void setDisplayno(long displayno) { this.displayno = displayno; } @Override public String toString() { return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid=" + pid + ", ismenu=" + ismenu + ", displayno=" + displayno + "]"; } }
dao
package com.wh.dao; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.wh.entity.Permission; import com.wh.entity.User; import com.zking.util.BaseDao; import com.zking.util.BuildTree; import com.zking.util.PageBean; import com.zking.util.TreeVo; public class PermissionDao extends BaseDao
{ //查询所有 public List list( Permission permission,long rid, PageBean pageBean) throws Exception { String sql="SELECT * FROM t_oa_permission where id in (select pid from t_oa_role_permission where rid="+rid+") "; System.out.println(sql); return super.executeQuery(sql, Permission.class, pageBean); } //将原数据转换成有层级关系的数据(借助自定义 工具类TreeVo完成) public List > menus( Permission permission,long rid, PageBean pageBean) throws Exception { List > menus = new ArrayList<>(); List list = this.list(permission,rid, pageBean); for (Permission p : list) { TreeVo tv = new TreeVo<>(); tv.setId(p.getId()+""); tv.setParentId(p.getPid()+""); tv.setText(p.getName()); Map map = new HashMap<>(); map.put("self", p); tv.setAttributes(map); menus.add(tv); } // return menus; return BuildTree.buildList(menus, "-1");//-1是顶级节点的id } } servlet
package com.wh.web; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wh.dao.PermissionDao; import com.wh.entity.Permission; import com.wh.entity.User; import com.zking.framework.ActionSupport; import com.zking.framework.ModelDriver; import com.zking.util.PageBean; import com.zking.util.ResponseUtil; import com.zking.util.StringUtils; import com.zking.util.TreeVo; public class PermissionAction extends ActionSupport implements ModelDriver
{ private Permission permission = new Permission(); private PermissionDao pd = new PermissionDao(); public void menus(HttpServletRequest req, HttpServletResponse resp) { User u = (User) req.getSession().getAttribute("user"); long rid = u.getRid(); try { List > menus = pd.menus(null,rid, null); ResponseUtil.writeJson(resp, menus); } catch (Exception e) { e.printStackTrace(); } } @Override public Permission getModel() { return permission; } }
前端代码
personalCenter.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="common/head.jsp"%>
个人中心 个人中心
后端代码
实体类
package com.wh.entity; public class User { private long id; private String name; private String loginName; private String pwd; private long rid; private String profile; public User() { // TODO Auto-generated constructor stub } public User(String name, String loginName, String pwd, long rid, String profile) { super(); this.name = name; this.loginName = loginName; this.pwd = pwd; this.rid = rid; this.profile = profile; } public User(long id, String name, String loginName, String pwd, long rid, String profile) { super(); this.id = id; this.name = name; this.loginName = loginName; this.pwd = pwd; this.rid = rid; this.profile = profile; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public long getRid() { return rid; } public void setRid(long rid) { this.rid = rid; } public String getProfile() { return profile; } public void setProfile(String profile) { this.profile = profile; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", loginName=" + loginName + ", pwd=" + pwd + ", rid=" + rid + ", profile=" + profile + "]"; } }
dao
package com.wh.dao; import java.util.List; import java.util.Map; import com.wh.entity.User; import com.zking.util.BaseDao; import com.zking.util.PageBean; import com.zking.util.StringUtils; /** * dao层 * @author W * */ public class UserDao extends BaseDao
{ // 修改个人信息 public int upd(User user) throws Exception { String sql = "update t_oa_user set name=?,loginName=?, pwd=? where id=?"; return super.executeUpdate(sql,user,new String[] {"name","loginName","pwd","id"}); } //头像上传 public int updProfile(User user) throws Exception { String sql = "update t_oa_user set profile= ? where id=?"; return super.executeUpdate(sql,user,new String[] {"profile","id"}); } } servlet
package com.wh.web; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wh.dao.UserDao; import com.wh.entity.User; import com.zking.framework.ActionSupport; import com.zking.framework.ModelDriver; import com.zking.util.PageBean; import com.zking.util.R; import com.zking.util.ResponseUtil; import com.zking.util.StringUtils; import sun.java2d.opengl.OGLContext; public class UserAction extends ActionSupport implements ModelDriver
{ private User user = new User(); private UserDao ud = new UserDao(); // 头像上传 public void updProfile(HttpServletRequest req, HttpServletResponse resp) { try { User u = (User) req.getSession().getAttribute("user"); user.setId(u.getId()); // 影响行数 int add = ud.updProfile(user); if (add > 0) { ResponseUtil.writeJson(resp, R.ok(0, "修改成功")); } else { ResponseUtil.writeJson(resp, R.error(0, "修改失败")); } } catch (Exception e) { e.printStackTrace(); try { ResponseUtil.writeJson(resp, R.error(0, "用户数据查询失败")); } catch (Exception e2) { e2.printStackTrace(); } } } // 修改个人信息 public void upd(HttpServletRequest req, HttpServletResponse resp) { try { User u = (User) req.getSession().getAttribute("user"); user.setId(u.getId()); // 影响行数 int upd = ud.upd(user); if (upd > 0) { ResponseUtil.writeJson(resp, R.ok(0, "用户数据修改成功")); } else { ResponseUtil.writeJson(resp, R.error(1, "用户数据修改失败")); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); try { ResponseUtil.writeJson(resp, R.error(1, "用户数据修改失败")); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } @Override public User getModel() { return user; } }
好啦,今天的分享就到这了,希望能够帮到你呢!