Response.cs 4.54 KB
namespace Hh.Mes.POJO.Response
{
    public class Response
    {
        /// <summary>
        /// 默认返回操作成功
        /// </summary>
        public Response(bool status = true, string msg = "业务数据操作成功!")
        {
            Code = status ? (int)RetCode.SUCCESS : (int)RetCode.INTERNAL_SERVER_ERROR;
            Status = status;
            Message = msg;
        }


        public int Code { get; set; }

        public string Message { get; set; }

        public bool Status { get; set; }

        public int Count { get; set; }

        /// <summary>
        /// 登陆标识
        /// </summary>
        public string Token { get; set; }


        public dynamic Result { get; set; }

        /// <summary>
        /// 统一返回前端失败
        /// </summary>
        /// <param name="msg">失败消息</param>
        public virtual void ResponseErr(string msg)
        {
            Code = (int)RetCode.FAIL;
            Message = msg;
            Status = false;
        }

        /// <summary>
        /// 统一返回前端失败
        /// </summary>
        /// <param name="msg">失败消息</param>
        public virtual Response ResponseError(string msg = "业务数据操作失败")
        {
            Code = (int)RetCode.INTERNAL_SERVER_ERROR;
            Message = msg;
            Status = false;
            return this;
        }
        /// <summary>
        /// 统一返回前端成功
        /// </summary>
        public virtual Response ResponseSuccess(string msg = "业务数据操作成功!")
        {
            Code = (int)RetCode.SUCCESS;
            Message = msg;
            Status = true;
            return this;
        }
    }

    public class Response<T> : Response
    {
        /// <summary>
        /// 回传的结果
        /// </summary>
        public new T Result { get; set; }


        public Response(bool status, string msg) : base(status, msg)
        {
        }

        public Response()
        {
        }
    }

    public class ResponseUpstream<T> : Response<T>
    {
        public string plmeid { get; set; }

        //public string flag { get { return Code == (int)RetCode.SUCCESS ? "1" : "0"; } }

        //public string msg { get { return Message; } }

        public ResponseUpstream(string plmeid, bool status = true, string msg = "业务数据操作成功!") : base(status, msg)
        {
            this.plmeid = plmeid;
        }
    }

    /// <summary>
    /// wms 
    /// </summary>
    public class ResponseWms
    {
        // <summary>
        /// 默认返回操作成功
        ///</summary>
        public ResponseWms(bool status = true, string msg = "业务数据操作成功")
        {
            code = status ? 200 : 500;
            this.status = status;
            this.msg = msg;
        }

        public int code { get; set; }

        public string msg { get; set; }

        public bool status { get; set; }

        public dynamic result { get; set; }

        /// <summary>
        /// 登陆标识
        /// </summary>
        public string token { get; set; }

        /// <summary>
        /// 统一返回失败
        /// </summary>
        public virtual ResponseWms ResponseError(string msg = "业务数据操作失败", int code = 500)
        {
            this.code = code;
            this.msg = msg;
            this.status = false;
            result = "[]";
            return this;
        }

        /// <summary>
        /// 统一返回成功
        /// </summary>
        public virtual ResponseWms ResponseSuccess(string msg = "业务数据操作成功!")
        {
            code = 200;
            this.msg = msg;
            status = true;
            return this;
        }
    }

    public enum RetCode
    {
        OK = 0,
        /// <summary>
        /// 成功
        /// </summary>
        SUCCESS = 200,

        /// <summary>
        /// 失败
        /// </summary>
        FAIL = 400,

        /// <summary>
        /// 未认证(签名错误)
        /// </summary>
        UNAUTHORIZED = 401,

        /// <summary>
        /// 找不到请求文件
        /// </summary>
        NoHandlerFoundException = 404,

        /// <summary>
        /// 未登录 
        /// </summary>
        UNAUTHEN = 401,

        /// <summary>
        /// 未授权,拒绝访问
        /// </summary>
        UNAUTHZ = 403,

        /// <summary>
        /// 服务器内部错误
        /// </summary>
        INTERNAL_SERVER_ERROR = 500,

        /// <summary>
        /// 业务处理错误
        /// </summary>
        Business_ERROR = 600,

    }
}