码迷,mamicode.com
首页 > 其他好文 > 详细

ResultHandler(自定义返回)

时间:2017-05-24 10:06:10      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:space   父窗口   erb   using   ring   tin   style   erro   val   

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Wdj
{

    /// <summary>
    /// 自定义处理结果抽象类
    /// </summary>
    /// <typeparam name="TEntity">返回数据类型</typeparam>
    public abstract class ResultHandler<TEntity>
    {
        #region 构造函数
        /// <summary>
        /// 操作结果,成功为true,否则为false
        /// </summary>
        [JsonProperty("success")]
        public bool Success { get; set; }
        /// <summary>
        /// 操作状态,True表示状态正常
        /// </summary>
        [JsonProperty("state")]
        public bool State { get; set; }
        /// <summary>
        /// 来源页URL地址
        /// </summary>
        [JsonProperty("url")]
        public string Url { get; set; }
        /// <summary>
        /// 操作提示信息
        /// </summary>
        [JsonProperty("msg")]
        public string Message { get; set; }
        /// <summary>
        /// 操作返回数据集合
        /// </summary>
        [JsonProperty("data")]
        public TEntity Data { get; set; }

        #endregion

        #region 初始化

        /// <summary>
        /// 初始化错误处理结果抽象类,直接返回提示信息
        /// </summary>
        /// <param name="msg">错误提示信息</param>
        public WdjResultHandler(string msg)
        {
            State = false;
            Message = msg;
            Success = false;
            var req = HttpContext.Current.Request;
            if (req.UrlReferrer != null)
            {
                Url = req.UrlReferrer.ToString();
            }
        }


        /// <summary>
        /// 初始化处理结果抽象类
        /// </summary>
        public WdjResultHandler() : this(true) { }

        /// <summary>
        /// 初始化处理结果抽象类
        /// </summary>
        /// <param name="checkRef">是否检测操作来源</param>
        public WdjResultHandler(bool checkRef)
        {
            Message = WdjKey.ErrorOperation;
            Success = false;
            State = false;

            var req = HttpContext.Current.Request;
            if (checkRef)
            {
                if (req.UrlReferrer != null)
                {
                    string myHost = req.Url.Host;
                    string urlHost = req.UrlReferrer.Host;
                    if (myHost.Equals(urlHost, StringComparison.InvariantCultureIgnoreCase))
                    {
                        Url = req.UrlReferrer.ToString();
                        State = true;
                    }
                    else
                    {
                        Message = WdjKey.ErrorNoParameters;
                    }
                }
                else
                {
                    Message = WdjKey.ErrorNoParameters;
                }
            }
            else
            {
                if (req.UrlReferrer != null)
                {
                    Url = req.UrlReferrer.ToString();
                }
                State = true;
            }


        }

        #endregion

        #region 输出操作结果
        /// <summary>
        /// 输出操作结果Json字符串
        /// </summary>
        public abstract void WriteJson();


        /// <summary>
        /// 弹出提示信息并返回来源页面
        /// </summary>
        public string ShowMsgReferrer()
        {
            Message = HttpUtility.UrlEncode(Message);
            string ResultMsgUrl = "<script>parent.layerurl(\"{0}\", {1}, {2}, \"{3}\");</script>";
            if (Success)
            {
                return string.Format(ResultMsgUrl, Message, 1, 2000, Url);
            }
            else
            {
                return string.Format(ResultMsgUrl, Message, 9, 3000, Url);
            }
        }

        /// <summary>
        /// 弹出提示信息并返回指定页面
        /// </summary>
        /// <param name="url">URL</param>
        public string ShowMsgUrl(string url)
        {
            Message = HttpUtility.UrlEncode(Message);
            string ResultMsgUrl = "<script>parent.layerurl(\"{0}\", {1}, {2}, \"{3}\");</script>";

            if (Success)
            {
                return string.Format(ResultMsgUrl, Message, 1, 2000, url);
            }
            else
            {
                return string.Format(ResultMsgUrl, Message, 9, 3000, url);
            }
        }
        /// <summary>
        /// 弹出提示信息并在父窗口返回指定页面
        /// </summary>
        /// <param name="url">URL</param>
        public string ShowMsgTopUrl(string url)
        {
            Message = HttpUtility.UrlEncode(Message);
            string ResultMsgTopUrl = "<script>parent.layertopurl(\"{0}\", {1}, {2},\"{3}\");</script>";
            if (Success)
            {
                return string.Format(ResultMsgTopUrl, Message, 1, 2000, url);
            }
            else
            {
                return string.Format(ResultMsgTopUrl, Message, 9, 3000, url);
            }
        }

        /// <summary>
        /// 弹出提示信息并返回上一页
        /// </summary>
        public string ShowMsgBack()
        {
            Message = HttpUtility.UrlEncode(Message);
            string ResultMsgBack = "<script>parent.layerback(\"{0}\", {1}, {2});</script>";
            if (Success)
            {
                return string.Format(ResultMsgBack, Message, 1, 2000);
            }
            else
            {
                return string.Format(ResultMsgBack, Message, 9, 3000);
            }
        }

        /// <summary>
        /// 转到指定的页面
        /// </summary>
        public string ShowLoadUrl(string url)
        {
            return string.Format("<script>parent.loadurl(\"{0}\");</script>", url);
        }



        /// <summary>
        /// 返回操作结果Json字符串
        /// </summary>
        /// <returns></returns>
        public string ShowJson()
        {
            return JsonConvert.SerializeObject(this, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });
        }

        /// <summary>
        /// 输出操作结果Json字符串
        /// </summary>
        protected void WriteResult()
        {
            var rsp = HttpContext.Current.Response;
            rsp.Clear();
            rsp.ContentType = "text/json";
            rsp.Write(ShowJson());
        }
        #endregion
    }
}

 

ResultHandler(自定义返回)

标签:space   父窗口   erb   using   ring   tin   style   erro   val   

原文地址:http://www.cnblogs.com/wc918/p/6897152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!