本章主要講述Web權限管理系統的項目架構,及開發中需要的基本類和相關的CSS,JS文件。
1.1系統結構
本系統搭建開發工具為Visual Studio 2012,采用ASP.NET MVC 4.0技術開發。系統的框架圖如下所示:
特別說明:系統需要用到的CSS文件在Content目錄下,公有的JS文件在Scripts目錄下。其下載鏈接為:http://files.cnblogs.com/files/wlandwl/CSS-JS.zip
系統頁面前台展示主要運用EasyUI1.4.3的展示控件及其擴展控件,引用到Content目錄。系統后台管理主要通過區域的方式開發,運用區域管理可以模塊化的開發系統的功能,有助於中大型系統在后期的開發和維護。
1.2系統共有類
1.2.1數據表對應Model
AccountInfo.cs,主要管理賬戶的基本信息,及可以訪問的目錄信息,頁面信息。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Models 7 { 8 /// <summary> 9 /// 用戶信息 10 /// </summary> 11 public class AccountInfo 12 { 13 public string OperatorId { get; set; } //用戶ID 14 public string OperatorName { get; set; } //用戶名字 15 public string AliasName { get; set; } //別名 16 public string Sex { get; set; } //性別 17 public int IsOnStaff { get; set; } 18 public string OperatorGroupId { get; set; } //用戶組ID 19 public string OperatorGroupName { get; set; } //用戶組名稱 20 public IList<Catalog> NavigationList { get; set; } //用戶能夠訪問的一級導航列表 21 public IList<Catalog> RightList { get; set; } //用戶權限列表 22 } 23 }
Catalog.cs,主要是用於目錄結構信息管理。

1 using System.Collections.Generic; 2 3 namespace Models 4 { 5 /// <summary> 6 /// 欄目 7 /// </summary> 8 public class Catalog 9 { 10 public int CatalogId { get; set; } 11 public int ParentId { get; set; } 12 public string CatalogName { get; set; } 13 public string PictureUrl { get; set; } 14 public string Remark { get; set; } 15 public int ShowNo { get; set; } 16 public int IsAvailable { get; set; } 17 public IList<Catalog> Childern { get; set; } 18 public IList<Page> PageList { get; set; } 19 public Catalog() 20 { 21 Childern = new List<Catalog>(); 22 PageList = new List<Page>(); 23 } 24 } 25 }
OperatorGroup.cs,主要是用於分組信息的管理。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace Models 7 { 8 /// <summary> 9 /// 用戶組 10 /// </summary> 11 public class OperatorGroup 12 { 13 public int GroupId { get; set; } 14 public string GroupName { get; set; } 15 public int OrderNum { get; set; } //排序值 16 public int ParentId { get; set; } //父節點 17 public int State { get; set; } //是否啟用 18 19 } 20 }
Page.cs,主要是用於頁面信息的管理。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Models 7 { 8 /// <summary> 9 /// 頁面 10 /// </summary> 11 public class Page 12 { 13 public int CategoryId { get; set; } //一級分類ID 14 public string CategoryName { get; set; } //一級分類名稱 15 public decimal SubcategoryId { get; set; } //二級分類ID 16 public string SubcategoryName { get; set; } //二級分類名稱 17 public decimal PageIndex { get; set; } 18 public string PageName { get; set; } 19 public string PageUrl { get; set; } 20 public string ProcedureName { get; set; } //對應的存儲過程的名稱 21 public string ReportRdlcName { get; set; } //報表名稱(對應的rdlc的名稱) 22 public string Title { get; set; } //報表標題 23 public string Subtitle { get; set; } //副標題 24 public string LeftHeader { get; set; } //左側頁眉顯示信息 25 public string MiddelHeader { get; set; } //中間頁眉顯示信息 26 public string RightHeader { get; set; } //右側頁眉顯示信息 27 public string LeftFooter { get; set; } //左側頁腳顯示信息 28 public string MiddleFooter { get; set; } //中間頁腳顯示信息 29 public string RightFooter { get; set; } //右側頁腳顯示信息 30 public int ShowNum { get; set; } //顯示順序,排序值 31 public int IsAvailable { get; set; } //是否可用 32 public string Operate { get; set; } 33 } 34 }
TreeModel.cs,主要是為了生成一課權限管理樹,定義的節點類。
為了生成節點信息的安全和高效,引用了DotNetOpenAuth.Messaging動態鏈接庫,需要下載才可以加入到項目中。

using System; using System.Collections.Generic; using System.Data; using System.Linq; using DotNetOpenAuth.Messaging; namespace Models { /// <summary> /// 樹模型 /// </summary> public class TreeModel { public decimal id { get; set; } public string text { get; set; } public string state { get; set; } public IList<TreeModel> children { get; set; } public object attributes { get; set; } /// <summary> /// 從數據行生成樹節點列表 /// </summary> /// <param name="rows">行數組</param> /// <param name="idName">id對應的數據庫列</param> /// <param name="textName">text對應的數據庫列</param> /// <param name="parentIdName">父節點ID對應的數據庫列</param> /// <param name="needRoot">是否需要顯示根節點</param> /// <param name="attributes">自定義屬性</param> /// <returns></returns> public static IList<TreeModel> BuildTreeNodeList(DataRowCollection rows, string idName, string textName, string parentIdName, bool needRoot, params string[] attributes) { IList<TreeModel> tree = new List<TreeModel>(); IList<TreeModel> resultList = new List<TreeModel>(); if (rows.Count > 0) { foreach (DataRow dr in rows) { var node = new TreeModel { id = DBNull.Value.Equals(dr[idName]) ? 0 : Convert.ToDecimal(dr[idName]), text = DBNull.Value.Equals(dr[textName]) ? "" : Convert.ToString(dr[textName]), state = "open", children = new List<TreeModel>() }; int isavailable = DBNull.Value.Equals(dr["isavailable"]) ? 0 : Convert.ToInt32(dr["isavailable"]); int showno = DBNull.Value.Equals(dr["showno"]) ? 0 : Convert.ToInt32(dr["showno"]); string remark = DBNull.Value.Equals(dr["remark"]) ? "" : Convert.ToString(dr["remark"]); string picUrl = DBNull.Value.Equals(dr["picurl"]) ? "" : Convert.ToString(dr["picurl"]); node.attributes = new CatalogAttributes { isavailable = isavailable, remark = remark, showno = showno, picurl = picUrl }; decimal parentId = DBNull.Value.Equals(dr[parentIdName]) ? 0 : Convert.ToDecimal(dr[parentIdName]); bool hasFound = FoundTreeNode(resultList, node, parentId); if (!hasFound) { resultList.Add(node); } } } if (needRoot) { var rootNode = new TreeModel { id = 0, text = "欄目樹", state = "open", children = new List<TreeModel>() }; rootNode.children.AddRange(resultList); tree.Add(rootNode); return tree; } return resultList; } public static IList<TreeModel> BuildTreeNodeListIncludeAll(DataRowCollection rows, string idName, string textName, string parentIdName) { IList<TreeModel> resultList = new List<TreeModel>(); var root = new TreeModel { id = 0, text = "所有欄目", state = "open", children = new List<TreeModel>() }; resultList.Add(root); if (rows.Count > 0) { foreach (DataRow dr in rows) { var node = new TreeModel { id = DBNull.Value.Equals(dr[idName]) ? 0 : Convert.ToDecimal(dr[idName]), text = DBNull.Value.Equals(dr[textName]) ? "" : Convert.ToString(dr[textName]), state = "open", children = new List<TreeModel>() }; decimal parentId = DBNull.Value.Equals(dr[parentIdName]) ? 0 : Convert.ToDecimal(dr[parentIdName]); bool hasFound = FoundTreeNode(resultList, node, parentId); if (!hasFound) { resultList.Add(node); } } } return resultList; } /// <summary> /// 在節點列表中查找父節點位置,並將新的節點插入 /// </summary> /// <param name="nodeList">原始節點列表</param> /// <param name="newNode">新節點</param> /// <param name="parentId">父節點ID</param> /// <returns></returns> public static bool FoundTreeNode(IList<TreeModel> nodeList, TreeModel newNode, decimal parentId) { bool hasFound = false; var parent = nodeList.FirstOrDefault(n => n.id == parentId); if (parent != null) { parent.children.Add(newNode); hasFound = true; } else { //在孩子節點中查找 foreach (TreeModel node in nodeList) { IList<TreeModel> children = node.children; //孩子節點 if (children != null && children.Count > 0) { hasFound = FoundTreeNode(children, newNode, parentId); if (hasFound) break; } } } return hasFound; } } public class CustomAttributes { public string url { get; set; } public string pageName { get; set; } } public class CatalogAttributes { public string remark { get; set; } //備注 public int showno { get; set; } //排序值 public int isavailable { get; set; } //狀態 public string picurl { get; set; } //圖標路徑 } }
1.2.1數據庫處理類
數據庫模板類主要是把常用sql語句的增,刪,改,查及事務集中起來。便於開發中通過類的實例化調用。
該類位於目錄OdbcDbAcess文件夾中。文件名稱:SqlHelper.cs。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data; 6 using System.Configuration; 7 using System.Data.SqlClient; 8 9 namespace OdbcDbAccess 10 { 11 public class SqlHelper 12 { 13 /// <summary> 14 /// **************************** 15 /// 功能:數據庫連接處理類 16 /// 作者:王令 17 /// 時間:2015-7-10 18 /// 郵箱:1129137758@qq.com 19 /// **************************** 20 21 private static SqlCommand CreateCommand(SqlConnection conn) 22 { 23 var comm = conn.CreateCommand(); 24 comm.CommandTimeout = 600; 25 return comm; 26 } 27 28 /// <summary> 29 /// 執行單一數據返回查詢 30 /// </summary> 31 /// <typeparam name="T">返回類型</typeparam> 32 /// <param name="connName">配置文件中連接字符串的名稱</param> 33 /// <param name="sql">查詢語句</param> 34 /// <returns></returns> 35 public static T ExecuteScalar<T>(string connName, string sql) 36 { 37 object result = null; 38 SqlConnection conn = new SqlConnection(connName); 39 { 40 var comm = CreateCommand(conn); 41 conn.Open(); 42 comm.CommandText = sql; 43 result = comm.ExecuteScalar(); 44 conn.Close(); 45 } 46 if (result != null && result != DBNull.Value) 47 return (T)Convert.ChangeType(result, typeof(T)); 48 return default(T); 49 } 50 51 /// <summary> 52 /// 查詢結果,返回多行數據【sql語句】 53 /// </summary> 54 /// <param name="sql">sql查詢語句</param> 55 56 public static DataSet ExecuteQuery(string connectionString, string sql) 57 { 58 SqlConnection con = new SqlConnection(connectionString); 59 string cmdText = sql; 60 SqlDataAdapter da = new SqlDataAdapter(cmdText, con); ///創建SqlDataAdapter 61 DataSet dss = new DataSet(); 62 try 63 { 64 con.Open(); 65 da.Fill(dss, "table1"); 66 con.Close(); 67 68 } 69 catch (Exception ex) 70 { ///拋出異常 71 throw new Exception(ex.Message, ex); 72 } 73 finally 74 { ///關閉連接 75 con.Close(); 76 } 77 return dss; 78 } 79 80 /// <summary> 81 /// 執行事務操作 82 /// </summary> 83 /// <param name="sql">sql查詢語句</param> 84 public static int ExecuteTran(string connectionString, string sql) 85 { 86 try 87 { 88 using (var conn = new SqlConnection(connectionString)) 89 { 90 var comm = conn.CreateCommand(); 91 conn.Open(); 92 var tran = conn.BeginTransaction(); 93 comm.Transaction = tran; 94 try 95 { 96 comm.CommandText = sql; 97 comm.ExecuteNonQuery(); 98 tran.Commit(); 99 return 1; 100 } 101 catch (Exception) 102 { 103 tran.Rollback(); 104 conn.Close(); 105 return 0; 106 throw; 107 108 } 109 } 110 } 111 catch (Exception ex) 112 { 113 throw new Exception(ex.Message, ex); 114 } 115 } 116 117 118 /// <summary> 119 /// 數據集刪除,修改,添加【sql語句】 120 /// </summary> 121 /// <param name="sql">sql操作語句</param> 122 123 public static int ExecuteNonQuery(string connectionString, string sql) 124 { ///創建連接 125 SqlConnection con = new SqlConnection(connectionString); 126 ///創建SQL語句 127 string cmdText = sql; 128 ///創建SqlCommand 129 SqlCommand cmd = new SqlCommand(cmdText, con); 130 131 int result = -1; 132 try 133 { ///打開連接 134 con.Open(); 135 ///操作數據 136 result = cmd.ExecuteNonQuery(); 137 } 138 catch (Exception ex) 139 { ///拋出異常 140 throw new Exception(ex.Message, ex); 141 } 142 finally 143 { ///關閉連接 144 con.Close(); 145 } 146 147 return result; 148 149 } 150 151 /// <summary> 152 /// 查詢結果,返回多行數據【存儲過程】 153 /// </summary> 154 /// <param name="storage">存儲過程</param> 155 156 public static DataSet ExecuteQueryWithParameter(string connectionString, string storage) 157 { 158 SqlConnection con = new SqlConnection(connectionString); 159 string cmdText = storage; 160 SqlDataAdapter da = new SqlDataAdapter(cmdText, con); ///創建SqlDataAdapter 161 ///設置執行方式為存儲過程 162 da.SelectCommand.CommandType = CommandType.StoredProcedure; 163 164 165 DataSet dss = new DataSet(); 166 try 167 { 168 con.Open(); 169 da.Fill(dss, "table1"); 170 con.Close(); 171 172 } 173 catch (Exception ex) 174 { ///拋出異常 175 throw new Exception(ex.Message, ex); 176 } 177 finally 178 { ///關閉連接 179 con.Close(); 180 } 181 return dss; 182 } 183 } 184 }
讀取webconfig中的數據庫連接字符串封裝在ConnectionHelper.cs中。

1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data.Odbc; 5 using System.Linq; 6 using System.Text; 7 8 namespace OdbcDbAccess 9 { 10 public class ConnectionHelper 11 { 12 public static string GeSqlDbConnectionStr() 13 { 14 return ConfigurationManager.ConnectionStrings["sqlSeverCenter"].ConnectionString; 15 } 16 17 } 18 }
1.2.2數據集轉換類
數據集轉換類,主要是為了把得到的數據如DataSet封裝成指定的格式數據,用於與前台網頁數據作特定的交互。
該類位於OdbcDbAcess文件夾中。文件名稱:DataTransfor.cs。

1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Text; 5 6 7 namespace OdbcDbAccess 8 { 9 /// <summary> 10 /// **************************** 11 /// 功能:數據轉換類 12 /// 作者:王令 13 /// 時間:2015-7-10 14 /// 郵箱:1129137758@qq.com 15 /// **************************** 16 /// 17 public class DataTransfor 18 { 19 /// <summary> 20 /// 數據轉換 21 /// </summary> 22 /// <typeparam name="T">數據類型</typeparam> 23 /// <param name="dataSet">數據集,其字段順序必需與T的屬性順序一直</param> 24 /// <returns></returns> 25 public static IList<T> DataSetTransfor<T>(DataSet dataSet) where T : class,new() 26 { 27 IList<T> resultList = new List<T>(); //結果數據集合 28 //獲取T的屬性集合 29 Type tType = new T().GetType(); 30 var propertyArray = tType.GetProperties(); 31 32 if (dataSet != null && dataSet.Tables.Count > 0) 33 { 34 DataTable table = dataSet.Tables[0]; 35 foreach (DataRow dr in table.Rows) 36 { 37 T item = new T(); 38 //為屬性設值 39 for (int i = 0; i < propertyArray.Length; i++) 40 { 41 if (!DBNull.Value.Equals(dr[i])) 42 { 43 var propertyInfo = propertyArray[i]; //獲取屬性 44 Type propertyType = propertyInfo.PropertyType; 45 object value = Convert.ChangeType(dr[i], propertyType); //將DataSet中的值轉換為屬性同類型的值 46 propertyInfo.SetValue(item, value, null); //為屬性設置值 47 } 48 } 49 resultList.Add(item); 50 } 51 } 52 return resultList; 53 } 54 55 56 /// <summary> 57 /// 數據轉換--指定部分屬性 58 /// </summary> 59 /// <typeparam name="T">數據類型</typeparam> 60 /// <param name="dataSet">數據集,其字段順序必需與T的屬性propertyArray順序一直</param> 61 /// <param name="propertyArray">指定類的屬性數組</param> 62 /// <returns></returns> 63 public static IList<T> DataSetTransfor<T>(DataSet dataSet, string[] propertyArray) where T : class,new() 64 { 65 IList<T> resultList = new List<T>(); //結果數據集合 66 67 //獲取T的數據類型 68 Type tType = new T().GetType(); 69 70 if (dataSet != null && dataSet.Tables.Count > 0) 71 { 72 DataTable table = dataSet.Tables[0]; 73 foreach (DataRow dr in table.Rows) 74 { 75 var item = new T(); 76 //為屬性設值 77 for (int i = 0; i < propertyArray.Length; i++) 78 { 79 var propertyInfo = tType.GetProperty(propertyArray[i]); //獲取屬性 80 81 if (!DBNull.Value.Equals(dr[i])) 82 { 83 string propertyTypeName = propertyInfo.PropertyType.FullName.Trim(); 84 object value; 85 switch (propertyTypeName) 86 { 87 case "System.DateTime": 88 value = Convert.ToDateTime(dr[i]); 89 break; 90 case "System.Decimal": 91 value = Convert.ToDecimal(dr[i]); 92 break; 93 case "System.Double": 94 value = Convert.ToDouble(dr[i]); 95 break; 96 case "System.Int32": 97 value = Convert.ToInt32(dr[i]); 98 break; 99 default: 100 value = Convert.ToString(dr[i]); 101 break; 102 } 103 propertyInfo.SetValue(item, value, null); //為屬性設置值 104 } 105 106 } 107 resultList.Add(item); 108 } 109 } 110 return resultList; 111 } 112 113 114 /// <summary> 115 /// 數據轉換--指定部分屬性 116 /// </summary> 117 /// <typeparam name="T">數據類型</typeparam> 118 /// <param name="dataSet">數據集,其字段順序必需與T的屬性propertyArray順序一直</param> 119 /// <param name="columnIndexArray">DataSet中的字段對應的下表數組,其長度和PropertyArray相同</param> 120 /// <param name="propertyArray">指定類的屬性數組</param> 121 /// <returns></returns> 122 public static IList<T> DataSetTransfor<T>(DataSet dataSet, int[] columnIndexArray, string[] propertyArray) where T : class,new() 123 { 124 IList<T> resultList = new List<T>(); //結果數據集合 125 126 //獲取T的數據類型 127 Type tType = new T().GetType(); 128 129 if (dataSet != null && dataSet.Tables.Count > 0) 130 { 131 if (columnIndexArray != null && propertyArray != null && columnIndexArray.Length == propertyArray.Length) 132 { 133 DataTable table = dataSet.Tables[0]; 134 foreach (DataRow dr in table.Rows) 135 { 136 var item = new T(); 137 for (int i = 0; i < columnIndexArray.Length; i++) 138 { 139 int index = columnIndexArray[i]; 140 if (!DBNull.Value.Equals(dr[index])) 141 { 142 //為屬性設值 143 var propertyInfo = tType.GetProperty(propertyArray[i]); //獲取屬性 144 object value = Convert.ChangeType(dr[index], propertyInfo.PropertyType); //將DataSet中的值轉換為屬性同類型的值 145 propertyInfo.SetValue(item, value, null); //為屬性設置值 146 } 147 } 148 resultList.Add(item); 149 } 150 } 151 } 152 return resultList; 153 } 154 155 156 /// <summary> 157 /// 將List中的數據,封裝為Combobox的Html 158 /// </summary> 159 /// <typeparam name="T">數據類型</typeparam> 160 /// <param name="dataList">數據列表</param> 161 /// <param name="valueProperty">value綁定的屬性名稱,多個用“,”分隔</param> 162 /// <param name="textProperty">text綁定的屬性名稱</param> 163 /// <param name="containAll">是否包含"全部"選項</param> 164 /// <returns></returns> 165 public static string ListToComboboxHtml<T>(IList<T> dataList, string valueProperty, string textProperty, bool containAll) where T : class, new() 166 { 167 var htmlStr = new StringBuilder(); 168 169 if (dataList != null && dataList.Count > 0) 170 { 171 if (containAll && dataList.Count > 1) 172 { 173 htmlStr.Append("<option value=\"-1\">全部</option>"); 174 } 175 176 string[] valuePropertyArray = valueProperty.Split(','); 177 178 //獲取T的數據類型 179 Type tType = new T().GetType(); 180 foreach (T item in dataList) 181 { 182 var textPropertyInfo = tType.GetProperty(textProperty); //獲取Text屬性 183 184 htmlStr.Append("<option value=\""); 185 for (int i = 0; i < valuePropertyArray.Length; i++) 186 { 187 string s = valuePropertyArray[i]; 188 var valuePropertyInfo = tType.GetProperty(s); //獲取Value屬性 189 htmlStr.Append(valuePropertyInfo.GetValue(item, null)); 190 if (i != valuePropertyArray.Length - 1) 191 { 192 htmlStr.Append("_"); 193 } 194 } 195 196 htmlStr.Append("\">"); 197 htmlStr.Append(textPropertyInfo.GetValue(item, null)); 198 htmlStr.Append("</option>"); 199 } 200 } 201 return htmlStr.ToString(); 202 } 203 /// <summary> 204 /// 將List中的數據,封裝為datagrid body的Html 205 /// </summary> 206 /// <typeparam name="T">數據類型</typeparam> 207 /// <param name="dataList">數據列表</param> 208 /// <param name="propertyArray">屬性數組,其順序和前台顯示順序一致</param> 209 /// <returns></returns> 210 public static string ListToTableHtml<T>(IList<T> dataList, string[] propertyArray) 211 where T : class, new() 212 { 213 var htmlStr = new StringBuilder(); 214 215 if (dataList != null && dataList.Count > 0) 216 { 217 //獲取T的數據類型 218 Type tType = new T().GetType(); 219 foreach (T item in dataList) 220 { 221 htmlStr.Append("<tr>"); 222 for (int i = 0; i < propertyArray.Length; i++) 223 { 224 htmlStr.Append("<td>"); 225 htmlStr.Append(tType.GetProperty(propertyArray[i]).GetValue(item, null)); 226 htmlStr.Append("</td>"); 227 } 228 htmlStr.Append("</tr>"); 229 } 230 } 231 return htmlStr.ToString(); 232 } 233 /// <summary> 234 /// 數據轉換-2015-4-17添加 235 /// </summary> 236 /// <typeparam name="T">數據類型</typeparam> 237 /// <param name="dataSet">數據集,所有數據轉換為string</param> 238 public static IList<T> DataSetTransforString<T>(DataSet dataSet) where T : class,new() 239 { 240 IList<T> resultList = new List<T>(); //結果數據集合 241 242 //獲取T的屬性集合 243 Type tType = new T().GetType(); 244 var propertyArray = tType.GetProperties(); 245 246 if (dataSet != null && dataSet.Tables.Count > 0) 247 { 248 DataTable table = dataSet.Tables[0]; 249 var temp = dataSet.Tables[0].Columns.Count; 250 foreach (DataRow dr in table.Rows) 251 { 252 T item = new T(); 253 //為屬性設值 254 for (int i = 0; i < temp; i++) 255 { 256 var propertyInfo = propertyArray[i]; //獲取屬性 257 Type propertyType = propertyInfo.PropertyType; 258 object value = DBNull.Value.Equals(dr[i]) ? "" : dr[i].ToString(); //將DataSet中的值轉換為屬性同類型的值 259 propertyInfo.SetValue(item, value, null); //為屬性設置值 260 } 261 resultList.Add(item); 262 } 263 } 264 return resultList; 265 } 266 267 268 269 270 271 } 272 }
1.2.3會話管理類
會話管理類是權限管理系統的核心類,它主要是根據登錄者的id和密碼獲取相應信息,同時把登錄者可以點擊的目錄,訪問的網頁存在session中。供系統自動配置出用戶的可訪問信息。
該類位於BaseBag文件夾中。文件名稱:SessionManage.cs。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using Models; 7 using OdbcDbAccess; 8 using System.Data; 9 using LogInfo; 10 11 namespace Session 12 { 13 /// <summary> 14 /// **************************** 15 /// 功能:會話管理類 16 /// 作者:王令 17 /// 時間:2015-7-18 18 /// 郵箱:1129137758@qq.com 19 /// **************************** 20 21 public class SessionManage 22 { 23 24 /// <summary> 25 /// Web虛擬目錄路徑 26 /// </summary> 27 public static string AppPath 28 { 29 get 30 { 31 return HttpContext.Current.Request.ApplicationPath; 32 } 33 } 34 35 36 /// <summary> 37 /// 當前訪問的Web域名 38 /// </summary> 39 public static string WebRoot 40 { 41 get 42 { 43 string url = HttpContext.Current.Request.Url.AbsoluteUri; 44 url = url.Remove(0, 7); 45 url = "http://" + url.Substring(0, url.IndexOf('/')) + AppPath; 46 return url.ToLower(); 47 } 48 } 49 50 51 /// <summary> 52 /// 用戶認證信息KEY 53 /// </summary> 54 private const string UserInfoKey = "USER_INFO_KEY"; 55 56 57 /// <summary> 58 /// 當前登陸用戶信息 59 /// </summary> 60 public static AccountInfo CurrentUser 61 { 62 get 63 { 64 if (HttpContext.Current.Session[UserInfoKey] == null) 65 return null; 66 67 return (AccountInfo)HttpContext.Current.Session[UserInfoKey]; 68 } 69 set { HttpContext.Current.Session[UserInfoKey] = value; } 70 } 71 72 73 /// <summary> 74 /// 用戶權限驗證 75 /// </summary> 76 /// <returns></returns> 77 public static bool CheckRight() 78 { 79 try 80 { 81 if (CurrentUser == null) 82 { 83 //當前用戶信息是否為空,為空,驗證失敗 84 return false; 85 } 86 else 87 { 88 string fUrl = HttpContext.Current.Request.Url.AbsoluteUri.Trim(); //用戶當前請求的地址 89 if (fUrl.EndsWith("/")) 90 { 91 fUrl = fUrl.Substring(0, fUrl.Length - 1); 92 } 93 else 94 { 95 if (fUrl.Contains("?")) 96 { 97 fUrl = fUrl.Substring(0, fUrl.LastIndexOf("?", System.StringComparison.Ordinal)); 98 } 99 } 100 fUrl = fUrl.Substring(0, fUrl.LastIndexOf("/", System.StringComparison.Ordinal) + 1);//只取到控制器名稱,具體的ActionName不在考慮,因為不同的Action都會形成不同的URL 101 fUrl = fUrl.Trim('/'); 102 string[] url = fUrl.Split('/'); 103 string righturl = ""; 104 for (int i = 3; i < url.Length; i++) 105 { 106 righturl += url[i] + "/"; 107 } 108 string strSql = "select PageUrl from pageinfo,rightlist where pageinfo.PageUrl like '" + righturl + "%' and pageinfo.pageid=rightlist.pageid and operatorgroupid='" + CurrentUser.OperatorGroupId + "' "; 109 110 DataSet dataSet = SqlHelper.ExecuteQuery(ConnectionHelper.GeSqlDbConnectionStr(), strSql); 111 if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0) 112 { 113 return true; 114 } 115 else 116 { 117 return false; 118 } 119 } 120 } 121 catch (Exception ex) 122 { 123 Log.SaveErrorLog(ex.ToString(), "用戶權限判斷出錯!"); 124 return false; 125 } 126 } 127 128 129 } 130 131 }
1.2.4頁面權限檢測控制器
頁面權限檢測控制器主要是每訪問一個頁面信息時,校驗該用戶是否具有訪問該頁面的權限。定義為BaseControl.cs控制器,其他的控制器均繼承該控制器。

1 using System; 2 using Session; 3 using OdbcDbAccess; 4 using System.Data; 5 using Models; 6 using System.Web; 7 using System.Web.Mvc; 8 using System.Collections.Generic; 9 using System.Configuration; 10 using System.IO; 11 using System.Web.Caching; 12 13 namespace Controllers 14 { 15 /// <summary> 16 /// **************************** 17 /// 功能:頁面權限檢測 18 /// 作者:王令 19 /// 時間:2015-7-15 20 /// 郵箱:1129137758@qq.com 21 /// **************************** 22 23 public class BaseController : Controller 24 { 25 /// <summary> 26 /// sql sever連接字符串 27 /// </summary> 28 protected static string SqlSeverConnectionName = ConfigurationManager.ConnectionStrings["sqlSeverCenter"].ConnectionString; 29 30 /// <summary> 31 /// 控制器初始化,判斷用戶權限以及登陸是否過期 32 /// </summary> 33 /// <param name="requestContext"></param> 34 protected override void Initialize(System.Web.Routing.RequestContext requestContext) 35 { 36 base.Initialize(requestContext); 37 if (!SessionManage.CheckRight()) 38 { 39 Response.Write("<script> window.parent.location.href = '/Login/Login';</script>"); 40 } 41 } 42 43 } 44 }
1.3系統模板頁
由於登錄到首頁后,其他頁面均是在生成的iframe標簽中展示,這些頁面都需要引用一些相同的CSS文件,JS文件,為了網頁的統一管理,開發了一個共享頁面,作為其他頁面的模板頁。
模板頁位於Views/Shared目錄中,文件名稱為:_BaseLayout.cshtml。引用它的頁面通過 @RenderBody()的方式即可把html代碼渲染在網頁中。

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta name="viewport" content="width=device-width" /> 5 <link type="text/css" rel="stylesheet" href="~/Content/easyui143/themes/gray/easyui.css" /> 6 <link type="text/css" rel="stylesheet" href="~/Content/easyui143/themes/icon.css" /> 7 <link type="text/css" rel="stylesheet" href="~/Content/easyui143/themes/color.css" /> 8 <style type="text/css"> 9 .panel-title { 10 text-align: center; 11 } 12 13 .datagrid-header-inner { 14 width: 100%; 15 } 16 17 .datagrid-htable, .datagrid-btable, .datagrid-ftable { 18 width: 100%; 19 } 20 </style> 21 22 <style type="text/css"> 23 .panel-title { 24 text-align: center; 25 } 26 27 .panel-title { 28 line-height: 22px; 29 letter-spacing: 1px; 30 } 31 32 33 .datagrid-header-inner { 34 width: 100%; 35 } 36 37 .datagrid-htable, .datagrid-btable, .datagrid-ftable { 38 width: 100%; 39 } 40 41 span.datagrid-row-expander.datagrid-row-expand { 42 display: block !important; 43 } 44 45 span.datagrid-row-expander.datagrid-row-collapse { 46 display: block !important; 47 } 48 49 div[id^='ddv-'] table { 50 font-size: smaller; 51 border-right: 1px groove rgba(248, 243, 243, 0.27); 52 border-bottom: 1px groove rgba(248, 243, 243, 0.27); 53 width: 1000px; 54 margin-bottom: 20px; 55 } 56 57 div[id^='ddv-'] table td, div[id^='ddv-'] table th { 58 line-height: 20px; 59 border-left: 1px groove rgba(248, 243, 243, 0.27); 60 border-top: 1px groove rgba(248, 243, 243, 0.27); 61 border-bottom: none; 62 border-right: none; 63 64 padding-left: 10px; 65 color: #808080; 66 font-weight: 100; 67 } 68 69 div[id^='ddv-'] .easyui-tabs .tabtitle { 70 color: lightblue; 71 } 72 </style> 73 @RenderSection("style", required: false) 74 <title>@ViewBag.Title</title> 75 </head> 76 77 <body> 78 <div style="margin: 10px 0 10px 10px; font-weight: bold; font-size: 14px; font-family: 宋體,Arial,Helvetica,sans-serif" id="titleDiv"> 79 <input type="hidden" value="true" id="firstLoadFlag" /> 80 </div> 81 82 @RenderBody() 83 84 <script src="@Url.Content("~/Scripts/jquery-1.7.1.js")"></script> 85 <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script> 86 <script src="@Url.Content("~/Scripts/jquery.validate.js")"></script> 87 <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"></script> 88 <script type="text/javascript" src="@Url.Content("~/Content/easyui143/jquery.easyui.min.js")"></script> 89 <script type="text/javascript" src="@Url.Content("~/Content/easyui143/locale/easyui-lang-zh_CN.js")"></script> 90 <script src="@Url.Content("~/Scripts/CustomJs/common.js")"></script> 91 <script src="@Url.Content("~/Content/easyui143/datagrid-detailview.js")" type="text/javascript"></script> 92 @RenderSection("scripts", required: false) 93 94 </body> 95 96 </html>
總結:本章主要介紹了項目的基本架構,常用的基本信息類。通過前前三章的介紹,已經完成了項目開發的前期准備工作,第四章開始,就以模塊化的方式講述每個模塊的開發流程。