namespace Gatedge.ScanCode.Common
{
///
/// 通用返回类
///
public class AjaxResult : Dictionary
{
///
/// 状态码
///
public static string CODE_TAG = "code";
///
/// 返回内容
///
public static string MSG_TAG = "msg";
///
/// 数据对象
///
public static string DATA_TAG = "data";
///
/// 单据类型Id
///
public static string BILLID_TAG = "billId";
///
/// 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
///
public AjaxResult()
{
}
///
/// 初始化一个新创建的 AjaxResult 对象
///
///
///
public AjaxResult(int code, string msg)
{
Add(CODE_TAG, code);
Add(MSG_TAG, msg);
}
///
/// 初始化一个新的带数据的 AjaxResult 对象
///
///
///
///
public AjaxResult(int code, string msg, object? data)
{
Add(CODE_TAG, code);
Add(MSG_TAG, msg);
Add(DATA_TAG, data);
}
///
/// 返回一个成功响应对象
///
///
public static AjaxResult Success()
{
return Success("操作成功");
}
///
/// 返回一个带消息成功响应对象
///
///
public static AjaxResult Success(object data)
{
return Success("操作成功", data);
}
///
/// 返回一个带消息和数据的响应对象
///
///
///
///
public static AjaxResult Success(string msg, object data)
{
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
///
/// 返回一个带消息的警告响应对象
///
///
///
public static AjaxResult Warn(string msg)
{
return Warn(msg, null);
}
///
/// 返回一个带消息和数据的警告响应对象
///
///
///
///
public static AjaxResult Warn(string msg, object? data)
{
return new AjaxResult(HttpStatus.WARN, msg, data);
}
///
/// 返回一个错误响应对象
///
///
public static AjaxResult Error()
{
return Error("操作失败");
}
///
/// 返回一个带消息的错误响应对象
///
///
///
public static AjaxResult Error(string msg)
{
return Error(msg, null);
}
///
/// 返回一个带消息和数据的响应对象
///
///
///
///
public static AjaxResult Error(string msg, object? data)
{
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
///
/// 返回一个带错误代码和消息的错误响应对象
///
///
///
///
public static AjaxResult Error(int code, string msg)
{
return new AjaxResult(code, msg, null);
}
///
/// 检查是否成功
///
///
public bool IsSuccess()
{
return HttpStatus.SUCCESS == Convert.ToInt32(this[CODE_TAG]);
}
///
/// 检查是否警告
///
///
public bool IsWarn()
{
return HttpStatus.WARN == Convert.ToInt32(this[CODE_TAG]);
}
///
/// 检查是否错误
///
///
public bool IsError()
{
return HttpStatus.ERROR == Convert.ToInt32(this[CODE_TAG]);
}
///
/// 添加BillId
///
public AjaxResult AddBillId(string billId)
{
Add(BILLID_TAG, billId);
return this;
}
}
}