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.