mvc3 easyui 异常帮助类 (封装 try catch操作和统一调用 增删改查 方法) 和 easy ui 帮助类

33 篇文章 0 订阅
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.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值