示例#1
0
        /// <summary>
        ///  action前执行校验
        /// </summary>
        /// <param name="filterContext"></param>
        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            var gcb = new ReResult();

            try
            {
                ControllerActionDescriptor actioninfo = (ControllerActionDescriptor)filterContext.ActionDescriptor;
                HttpRequest requestinfo = filterContext.HttpContext.Request;
                //logger.SetRequest(ArgumentMapping.GetRequestParams());
                //logger.Type = "Control";
                //logger.Url = requestinfo.GetDisplayUrl();
                //logger.Path = actioninfo.ControllerName + "/" + actioninfo.ActionName;
                //logger.Method = requestinfo.Method;
                //logger.RequestMark = requestinfo.Headers.ContainsKey("_ladder_client_mark_") ? requestinfo.Headers["_ladder_client_mark_"].ToString() : Core.GenUuid();
                //filterContext.HttpContext.Items["__requestmark__"] = logger.RequestMark;
                //filterContext.HttpContext.Items["___loggerforrequest____"] = logger;
                //Logs.Write(logger, LogOption.Request);

                //获取当前请求controller中方法的特性
                var permission = actioninfo.ControllerTypeInfo.GetCustomAttribute <LoginAttribute>();
                //验证当前请求action是否需要验证登录 是的话 验证是否登录 未登陆的话进入执行方法
                if (!permission.IsBlank())
                {
                    if (!permission.is_login)
                    {
                        //返回固定状态 登陆失效
                        filterContext.Result = new ValidErrorResult(new { code = "401", msg = "未登录或登陆超时" });
                        //状态码返回401,身份未验证
                        filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                    }
                }
                else
                {
                    permission = actioninfo.MethodInfo.GetCustomAttribute <LoginAttribute>();
                    //验证当前请求action是否需要验证登录 是的话 验证是否登录 未登陆的话进入执行方法
                    if (!permission.IsBlank() && !permission.is_login)
                    {
                        //返回固定状态 登陆失效
                        filterContext.Result = new ValidErrorResult(new { code = "401", msg = "未登录或登陆超时" });
                        //状态码返回401,身份未验证
                        filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                    }
                }
            }
            catch (Exception e)
            {
                // 记录日志
                // Logs.WriteLine("FilterError", $"{logger.RequestMark}----拦截异常", e.Message);
                gcb.Message = "系统未知异常,请联系管理员";
                // 返回结果
                filterContext.Result = new ValidErrorResult(new ReResult(500, e.Message).setData(e.StackTrace));
                filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            }
        }
示例#2
0
        /// <summary>
        /// 新增方法
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="rec">参数</param>
        /// <returns></returns>
        public ReResult Inserts(string tablename, List <Record> rec, bool transaction = false)
        {
            var re = new ReResult();

            if (rec.IsBlank())
            {
                return(re.setCode(400, "未添加有效数据"));
            }
            var list = rec.Select(x => new Curd(tablename, x, Operation.Insert)).ToList();

            return(SaveBeans(list, transaction, re));
        }
示例#3
0
        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="cox">条件</param>
        /// <returns></returns>
        public ReResult Delete(string tablename)
        {
            var re = new ReResult();

            if (tablename.IsBlank())
            {
                return(re.setCode(400, "未添加有效数据"));
            }
            return(SaveBeans(new List <Curd>()
            {
                new Curd(tablename, new Record(), Operation.Clear)
            }, false, re));
        }
示例#4
0
        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="cox">条件</param>
        /// <returns></returns>
        public ReResult Delete(string tablename, Record cox, Record rec)
        {
            var re = new ReResult();

            if (cox.IsBlank())
            {
                return(re.setCode(400, "未添加有效数据"));
            }
            return(SaveBeans(new List <Curd>()
            {
                new Curd(tablename, cox, Operation.Delete, rec)
            }, false, re));
        }
示例#5
0
        /// <summary>
        /// 新增方法
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="rec">参数</param>
        /// <returns></returns>
        public ReResult Insert(string tablename, Record rec)
        {
            var re = new ReResult();

            if (rec.IsBlank())
            {
                return(re.setCode(400, "未添加有效数据"));
            }
            return(SaveBeans(new List <Curd>()
            {
                new Curd(tablename, rec, Operation.Insert)
            }, false, re));
        }
示例#6
0
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="rec">操作值</param>
        /// <param name="transaction">是否启用事务</param>
        /// <param name="rex">返回值</param>
        /// <returns></returns>
        public ReResult SaveBeans(List <Curd> rec, bool transaction = false, ReResult rex = null)
        {
            rex = rex ?? new ReResult();
            var list = new List <Record>();
            var con  = Connection();

            if (con.code != 200)
            {
                return(rex.setCode(con.code, con.msg));
            }
            MySqlTransaction trans = null;

            if (transaction)
            {
                trans = conn.BeginTransaction();
            }
            try
            {
                var sqls = new List <string>();
                rec.ForEach(x =>
                {
                    if (x.op == Operation.Insert && !x.list.IsBlank())
                    {
                        sqls.AddRange(x.getSqlList());
                    }
                    else
                    {
                        var ss = x.getSql();
                        if (!ss.IsBlank())
                        {
                            sqls.Add(ss);
                        }
                    }
                });

                foreach (var sql in sqls)
                {
                    if (sql.IsBlank())
                    {
                        if (transaction && trans != null)
                        {
                            trans.Rollback();
                        }
                        list.Add(new Record("success", "false").Put("message", "sql语句错误"));
                        return(rex.setCode(400, "sql语句错误").setData(list));
                    }
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    //执行ExecuteReader()返回一个MySqlDataReader对象
                    var i = cmd.ExecuteNonQuery();
                    if (i > 0)
                    {
                        list.Add(new Record("Success", "true").Put("Message", "").Put("Sql", sql));
                        continue;
                    }
                    if (transaction && trans != null)
                    {
                        trans.Rollback();
                    }
                    list.Add(new Record("Success", "false").Put("Message", "保存操作失败").Put("Sql", sql));
                    return(rex.setCode(400, "操作失败").setData(list));
                }
                if (transaction && trans != null)
                {
                    trans.Commit();
                }
                return(rex.setData(list));
            }
            catch (Exception ex)
            {
                if (transaction && trans != null)
                {
                    trans.Rollback();
                }
                return(rex.setCode(500, ex.Message).setData(list));
            }
            finally
            {
                Close();
            }
        }
示例#7
0
 public LoginDao()
 {
     re = new ReResult();
 }