Fork Me On Github

我的json格式

普通json响应

这是普通链接响应json格式,不论是否出错,响应状态码都是 200

{
    "code": 200, // 200 表示程序处理成功,其他数字自定义
    "status": "success", // success 表示成功 failure 表示失败
    "message": "",      // 成功的消息提示
    "errors": "",     // 失败的信息
    "data": {},       // 成功响应的信息
    "url": "",         // 搭配 code: 302 实现页面跳转
}

具体实例

成功返回用户信息

{
    "code": 200,
    "status": "success",
    "data": {
        "id": 1,
        "name": "zodream"
    },
}

失败返回提示无权限需要登录

{
    "code": 302,
    "status": "failure",
    "errors": "当前账户无权限,请先登录",
    "url": "/login?redirect_uri=/"
}

RESTful 响应

状态码判断是否处理成功

具体实例

成功返回用户信息

HTTP/1.1 200 OK

{
    "id": 1,
    "name": "zodream"
}

成功返回用户列表

HTTP/1.1 200 OK

{
    "data": [
        {
            "id": 1,
            "name": "zodream"
        }
    ]
}

失败返回提示无权限需要登录

HTTP/1.1 302 Found

{
    "code": 302,
    "message": "当前账户无权限,请先登录",
}

具体代码实现,

netcore 版本为例

    public interface IJsonResponse
    {
        public object Render(object data);

        public object RenderData(object data);

        public object RenderData(object data, string message);

        public object RenderPage<T>(Page<T> page);

        public object RenderFailure(string message, int code);

        public object RenderFailure(string message);
    }

普通版本的实现

    public class JsonResponse : IJsonResponse
    {
        public object Render(object data)
        {
            return data;
        }

        public object RenderData(object data)
        {
            return Render(new
            {
                code = 200,
                status = "success",
                data
            });
        }

        public object RenderData(object data, string message)
        {
            return Render(new
            {
                code = 200,
                status = "success",
                data,
                message
            });
        }

        public object RenderFailure(string message, int code)
        {
            return new
            {
                code,
                status = "failure",
                message
            };
        }

        public object RenderFailure(string message)
        {
            return RenderFailure(message, 404);
        }

        public object RenderPage<T>(Page<T> page)
        {

            return Render(new
            {
                code = 200,
                status = "success",
                data = page.Items,
                paging = new
                {
                    limit = page.ItemsPerPage,
                    offset = page.CurrentPage,
                    total = page.TotalItems,
                    more = page.CurrentPage < page.TotalPages
                }
            });
        }
    }

RESTful 的实现

    public class PlatformResponse : IJsonResponse
    {

        public PlatformModel Platform { get; set; }

        public object Render(object data)
        {
            return data;
        }

        public object RenderData(object data)
        {
            return Render(new
            {
                data,
                appid = Platform.Appid
            });
        }

        public object RenderData(object data, string message)
        {
            return Render(new
            {
                data,
                message
            });
        }

        public object RenderFailure(string message, int code)
        {
            return new
            {
                code,
                message
            };
        }

        public object RenderFailure(string message)
        {
            return RenderFailure(message, 404);
        }

        public object RenderPage<T>(Page<T> page)
        {

            return Render(new
            {
                data = page.Items,
                paging = new
                {
                    limit = page.ItemsPerPage,
                    offset = page.CurrentPage,
                    total = page.TotalItems,
                    more = page.CurrentPage < page.TotalPages
                }
            });
        }
    }

通过中间件注入当前请求

public Task InvokeAsync(HttpContext context)
{
    context.Items.Add("json", new JsonResponse());
    return _next.Invoke(context);
}

再控制器中使用

HttpContext.Items["json"] as IJsonResponse

在这里有一个问题,即状态码并没有实现,可以考虑拓展方法,把HttpContext当作参数传入,内部做状态码输入

点击查看全文
0 6 0