public enum ApiResponseStatus { Success, Error } public enum ApiErrorCode { InvalidRequest = 1000, Forbidden = 2000, NotFound = 3000, InternalServerError = 4000 } public class ApiResponse<T> { public ApiResponseStatus Status { get; set; } public string Message { get; set; } public T Data { get; set; } public ApiErrorCode ErrorCode { get; set; } public ApiResponse() { Status = ApiResponseStatus.Success; Message = "Success"; } public ApiResponse(T data) { Status = ApiResponseStatus.Success; Message = "Success"; Data = data; } public ApiResponse(string message, ApiErrorCode errorCode = ApiErrorCode.InternalServerError) { Status = ApiResponseStatus.Error; Message = message; ErrorCode = errorCode; } public void SetError(string message, ApiErrorCode errorCode = ApiErrorCode.InternalServerError) { Status = ApiResponseStatus.Error; Message = message; ErrorCode = errorCode; } public void SetData(T data) { Status = ApiResponseStatus.Success; Message = "Success"; Data = data; } public static ApiResponse<T> CreateError(string message, ApiErrorCode errorCode = ApiErrorCode.InternalServerError) { return new ApiResponse<T> { Status = ApiResponseStatus.Error, Message = message, ErrorCode = errorCode }; } public static ApiResponse<T> CreateSuccess(T data) { return new ApiResponse<T> { Status = ApiResponseStatus.Success, Message = "Success", Data = data }; } }
标签:返回,Status,通用,ApiErrorCode,Success,ApiResponse,ApiResponseStatus,API,public From: https://www.cnblogs.com/ZkbFighting/p/17881850.html