using System;using System.Collections.Generic;using System.Linq;using System.Text;using log4net.Core;using System.Linq.Expressions;using System.Web.Mvc;using System.Web.Script.Serialization;using System.Text.RegularExpressions;namespace Model.Common{ /// <summary> /// 異常協助類 封裝 try catch操作和統一調用 增刪改 方法 /// </summary> public class ExceptionHelper { static log4net.ILog log=null; static JavaScriptSerializer JsonSerializer = new JavaScriptSerializer();//json轉換類 public ExceptionHelper(string classname) { log = log4net.LogManager.GetLogger(classname); } /// <summary> /// 無傳回值無參數的方法調用 /// </summary> /// <param name="action"></param> public void Invoke(Action action) { try { action(); } catch (Exception ex) { log.Error(ex.Message); } } /// <summary> /// 有傳回值無參數的方法調用 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="func"></param> /// <returns></returns> public static T Invoke<T>(Func<T> func) { try { return func(); } catch (Exception ex) { log.Error(ex.Message); return default(T); } } /// <summary> /// 公用的擷取非查詢執行結果的方法 /// </summary> /// <param name="op">操作方法</param> /// <param name="nu">newuser對象</param> /// <param name="errormsg">錯誤資訊</param> /// <returns>json結果</returns> public static string GetNoQueryJsonResult<T>(Func<T,int> op, T nu, string errormsg,bool isvalid) { EasyResult er = new EasyResult(); er.success = false; try { if (isvalid) { int i = op(nu); if (i > 0) { er.success = true; } else { er.errorMsg = errormsg; } } else { er.errorMsg = "驗證失敗,請檢查資料"; } } catch (Exception ex) { er.errorMsg = ex.Message; log.Error(ex.Message,ex); } return JsonSerializer.Serialize(er); } } /// <summary> /// 泛型 異常協助類 封裝 try catch操作和統一調用 查詢 方法 /// </summary> /// <typeparam name="ModelType"></typeparam> public class ExceptionHelper<ModelType> { public delegate List<ModelType> FuncEx(int t1, int t2, Dictionary<string, string> t3, out int t4); static log4net.ILog log = null; static JavaScriptSerializer JsonSerializer = new JavaScriptSerializer();//json轉換類 public ExceptionHelper(string classname) { log = log4net.LogManager.GetLogger(classname); } /// <summary> /// 公用的擷取查詢執行結果的方法(分頁) /// </summary> /// <param name="op">操作方法</param> /// <param name="pageIndex">頁碼</param> /// <param name="pageSize">每頁記錄數</param> /// <param name="dicCondition">查詢條件</param> /// <param name="totalcount">總數</param> /// <returns>json資料集合</returns> public static string GetQueryJsonResult(FuncEx op, int pageIndex, int pageSize, Dictionary<string, string> dicCondition, out int totalcount) { totalcount = 0; try { List<ModelType> lsnu = op(pageIndex, pageSize, dicCondition, out totalcount); EasyDataGrid<ModelType> edg = new EasyDataGrid<ModelType>(); edg.total = totalcount;//總記錄數 edg.rows = lsnu;//行資料 string str = JsonSerializer.Serialize(edg); str = Regex.Replace(str, @"\\/Date\((\d+)\)\\/", match => { DateTime dt = new DateTime(1970, 1, 1); dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value)); dt = dt.ToLocalTime(); return dt.ToString("yyyy-MM-dd"); }); return str; } catch (Exception ex) { log.Error(ex.Message, ex); } return ""; } }}
easy ui 協助類
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Model.Common{ /// <summary> /// easyui tree json資料類 /// </summary> public class EasyTreeNode { public EasyTreeNode() { attributes = new Dictionary<string, string>(); children = new List<EasyTreeNode>(); } /// <summary> /// 樹的id /// </summary> public int id { get; set; } /// <summary> /// 樹顯示的文字 /// </summary> public string text { get; set; } /// <summary> /// 其他屬性 <屬性名稱,屬性值> /// </summary> public Dictionary<string, string> attributes { get; set; } /// <summary> /// 子節點 /// </summary> public List<EasyTreeNode> children { get; set; } } /// <summary> /// easyui datagrid json 資料類 /// </summary> public class EasyDataGrid<T> { public EasyDataGrid() { rows = new List<T>(); } /// <summary> /// 總記錄數 /// </summary> public int total { get; set; } /// <summary> /// 行資料 (<欄位名,欄位值>)的集合 /// </summary> public List<T> rows { get; set; } } /// <summary> /// 返回資訊 /// </summary> public class EasyResult { public string errorMsg { get; set; } public bool success { get; set; } }}
需要引用相關dll.