1. 程式人生 > >三星管理許可權的登陸

三星管理許可權的登陸

今天帶來的是我自己最近學的easyui的三星管理許可權的一個登陸

要求:不同的使用者登陸擁有的選單樹是不同的。

先上一個邏輯圖縷下思路吧(資料庫表結構可參照下圖)

許可權樹

 

?執行資料庫指令碼
?建立實體類
?建立dao
?新增web的方法
?新增登入介面,跳入前端樹形選單


所謂許可權:指的是系統中的資源,資源包括選單資源(學習情況報表,賬號稽核...)以及按鈕資源
所謂角色:指的是系統中的許可權集合(1)
 

menuTree展示的相關程式碼(利用遞迴)

package com.zking.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zking.entity.TreeNode;
import com.zking.util.BaseJsonDao;
import com.zking.util.JsonUtil;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;

public class MenuTreeDao extends BaseJsonDao{

	/**
	 * 查詢後臺需要屬性展示的選單表資料(所有表字段)
	 * 注意:該資料轉換成json物件,是不符合easyui的tree元件展現的json格式
	 * @param paramMap
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Map<String, Object>> menuList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
	    String sql = "select * from t_easyui_menu where true";
	    //當前節點的id
	    String menuId = JsonUtil.getParamMap(paramMap, "Menuid");
	    if(StringUtils.isNotBlank(menuId)) {
	    	sql += " and parentid in ("+menuId+")";
	    }else {
	    	sql += " and parentid=-1";
	    }
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * 直接查出來的資料來源是不能直接用於展示的,需要轉換成可展示資料
	 * @param map
	 * @param treeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void mapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
		//查出來的資料通過map集合賦值給treeNode實體類
		treeNode.setId(map.get("Menuid").toString());
		treeNode.setText(map.get("Menuname").toString());
		treeNode.setAttributes(map);
		
		Map<String, String[]> paraMap = new HashMap<>();
		//把當前節點的id當作父id,查出所有的子節點
		paraMap.put("Menuid", new String[] {treeNode.getId()});
		List<Map<String, Object>> menuList = this.menuList(paraMap, null);
		List<TreeNode> treeNodeList = new ArrayList<>();
		mapListToTreeNodeList(menuList, treeNodeList);
		//設定子節點
		treeNode.setChildren(treeNodeList);
	}
	
	/**
	 * 
	 * @param list 用來展示的
	 * @param treeNodeList 資料庫查出來的資料
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void mapListToTreeNodeList(List<Map<String, Object>> list,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
		TreeNode treeNode = null;
		for (Map<String, Object> map : list) {
			treeNode = new TreeNode();
			mapToTreeNode(map, treeNode);
			treeNodeList.add(treeNode);
		}
	}

	/**
	 * 這個的方法的返回值才是符合easyui的tree元件所需要的json格式
	 * @param paramMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<TreeNode> menuTreeList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Map<String, Object>> menuList = this.menuList(paramMap, pageBean);
		List<TreeNode> treeNodeList = new ArrayList<>();
		mapListToTreeNodeList(menuList, treeNodeList);
		return treeNodeList;	
	}
}

我們把這個展示選單樹的dao包寫好,在繼續寫登陸相關的程式碼

/**
	 * 三星許可權使用者登陸
	 * @param paramMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> tologin3(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String uid = JsonUtil.getParamMap(paramMap, "uid");
		String upwd = JsonUtil.getParamMap(paramMap, "upwd");
		String sql = "select * from t_easyui_user_version2 where true";
		if(StringUtils.isNotBlank(uid)) {
			sql += " and uid="+uid;
		}
		if(StringUtils.isNotBlank(upwd)) {
			sql += " and upwd="+upwd;
		}
		return super.executeQuery(sql, null);
	}
	
	public List<Map<String, Object>> getMenuidFromUser2(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String zid = JsonUtil.getParamMap(paramMap, "zid");
		String sql = "select * from t_juese where true";
		if(StringUtils.isNotBlank(zid)) {
			sql += " and zid="+zid;
		}
		return super.executeQuery(sql, null);
	}

再來實現action層程式碼(登陸的action)

/**
	 * 三星許可權的登陸
	 * @param req
	 * @param resp
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public String tologin3(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
		List<Map<String, Object>> tologin3 = this.userDao.tologin3(req.getParameterMap(), null);
		Map<String, Object> currentUser = tologin3.get(0);
		if(null != currentUser) {
		 Map<String, String[]> paramMap = new HashMap<>();
		 paramMap.put("zid", new String[] {(String) currentUser.get("zid")});
		 //獲取的中間表資訊
		 List<Map<String, Object>> menuidFromUser = this.userDao.getMenuidFromUser2(paramMap, null);
		 StringBuffer sb = new StringBuffer();
		 for (Map<String, Object> map : menuidFromUser) {
			sb.append(",").append(map.get("menuId"));
		}
		 req.setAttribute("menuId", sb.toString().substring(1));
		}
		return "index";
	}

menuTree的action

package com.zking.Action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.dao.MenuTreeDao;
import com.zking.entity.TreeNode;
import com.zking.framework.ActionSupport;
import com.zking.util.ResponseUtil;

public class MenuTreeAction extends ActionSupport {
  
	private MenuTreeDao menuTreeDao = new MenuTreeDao();
	
	
	public String menuTreeList(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException, JsonProcessingException, IOException {
		List<TreeNode> menuTreeList = this.menuTreeDao.menuTreeList(req.getParameterMap(), null);
		ObjectMapper om = new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(menuTreeList));
		return null;
		
	}

}

完成以上程式碼一個三星管理登陸的許可權就好了