优化分页统一返回

This commit is contained in:
不做码农
2021-11-28 11:11:34 +08:00
parent 79b3c306f9
commit 8dc832cdbd
37 changed files with 145 additions and 357 deletions

View File

@@ -1,103 +0,0 @@
using Newtonsoft.Json;
using System.ComponentModel;
namespace ZRModel
{
/// <summary>
/// 通用json格式实体返回类
/// Author by zhaorui
/// </summary>
public class ApiResult
{
public int Code { get; set; }
public string Msg { get; set; }
/// <summary>
/// 如果data值为null则忽略序列化将不会返回data字段
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public object Data { get; set; }
public ApiResult()
{
Code = 0;
Msg = "fail";
}
public ApiResult OnSuccess()
{
Code = 100;
Msg = "success";
return this;
}
public ApiResult(int code, string msg, object data = null)
{
Code = code;
Msg = msg;
Data = data;
}
public ApiResult OnSuccess(object data = null)
{
Code = (int)ResultCode.SUCCESS;
Msg = "SUCCESS";
Data = data;
return this;
}
/// <summary>
/// 返回成功消息
/// </summary>
/// <param name="data">数据对象</param>
/// <returns>成功消息</returns>
public static ApiResult Success(object data) { return new ApiResult(100, "success", data); }
/// <summary>
/// 返回成功消息
/// </summary>
/// <param name="msg">返回内容</param>
/// <returns>成功消息</returns>
public static ApiResult Success(string msg) { return new ApiResult(100, msg, null); }
/// <summary>
/// 返回成功消息
/// </summary>
/// <param name="msg">返回内容</param>
/// <param name="data">数据对象</param>
/// <returns>成功消息</returns>
public static ApiResult Success(string msg, object data) { return new ApiResult(100, msg, data); }
/// <summary>
/// 返回失败消息
/// </summary>
/// <param name="code"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static ApiResult Error(int code, string msg) { return new ApiResult(code, msg); }
/// <summary>
/// 返回失败消息
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static ApiResult Error(string msg) { return new ApiResult(200, msg); }
}
public enum ResultCode
{
[Description("success")]
SUCCESS = 100,
[Description("参数错误")]
PARAM_ERROR = 101,
[Description("自定义错误")]
CUSTOM_ERROR = 200,
FAIL = 0,
[Description("程序出错啦")]
GLOBAL_ERROR = 104,
[Description("请先绑定手机号")]
NOBIND_PHONENUM = 102,
[Description("授权失败")]
OAUTH_FAIL = 201,
[Description("未授权")]
DENY = 401
}
}

View File

@@ -0,0 +1,30 @@
using System.Collections.Generic;
namespace ZR.Model
{
/// <summary>
/// 分页扩展
/// </summary>
public static class PageExtension
{
/// <summary>
/// 读取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="parm"></param>
/// <returns></returns>
public static PagedInfo<T> ToPage<T>(this List<T> source, PagerInfo parm)
{
var page = new PagedInfo<T>
{
TotalPage = parm.TotalPage,
TotalNum = parm.TotalNum,
PageSize = parm.PageSize,
PageIndex = parm.PageNum,
Result = source
};
return page;
}
}
}

View File

@@ -1,50 +0,0 @@

namespace ZRModel
{
/// <summary>
/// 获取配置文件POCO实体类
/// </summary>
public class OptionsSetting
{
/// <summary>
/// 1、喵播 2、fireStar
/// </summary>
public int Platform { get; set; }
public string AppName { get; set; }
public string Redis { get; set; }
public string Conn_Live { get; set; }
public string Conn_Admin { get; set; }
/// <summary>
/// mongodb连接字符串
/// </summary>
public string Mongo_Conn { get; set; }
public string Database { get; set; }
public LoggingOptions Logging { get; set; }
public MailOptions MailOptions { get; set; }
}
/// <summary>
/// 发送邮件数据配置
/// </summary>
public class MailOptions
{
public string From { get; set; }
public string Password { get; set; }
public string Host { get; set; }
public int Port { get; set; }
}
/// <summary>
/// 日志
/// </summary>
public class LoggingOptions
{
public LogLevelOptions LogLevel { get; set; }
}
public class LogLevelOptions
{
public string Default { get; set; }
public string Ssytem { get; set; }
public string Microsoft { get; set; }
}
}

66
ZR.Model/PagedInfo.cs Normal file
View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ZR.Model
{
/// <summary>
/// 分页参数
/// </summary>
public class PagedInfo<T>
{
/// <summary>
/// 每页行数
/// </summary>
public int PageSize { get; set; } = 10;
/// <summary>
/// 当前页
/// </summary>
public int PageIndex { get; set; } = 1;
/// <summary>
/// 排序列
/// </summary>
public string Sort { get; set; }
/// <summary>
/// 排序类型
/// </summary>
public string SortType { get; set; }
/// <summary>
/// 总记录数
/// </summary>
public int TotalNum { get; set; }
/// <summary>
/// 总页数
/// </summary>
public int TotalPage
{
get
{
if (TotalNum > 0)
{
return TotalNum % this.PageSize == 0 ? TotalNum / this.PageSize : TotalNum / this.PageSize + 1;
}
else
{
return 0;
}
}
set { }
}
public List<T> Result { get; set; }
public PagedInfo()
{
}
public PagedInfo(List<T> source, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
PageSize = pageSize;
TotalNum = source.Count;
TotalPage = (int)Math.Ceiling(TotalNum / (double)PageSize);//计算总页数
}
}
}

View File

@@ -18,9 +18,20 @@ namespace ZR.Model
/// <summary>
/// 总页码
/// </summary>
public int TotalPageNum { get; set; }
/// <summary>
/// 总页数
/// </summary>
public int TotalPage
{
get
{
return TotalNum > 0 ? TotalNum % PageSize == 0 ? TotalNum / PageSize : TotalNum / PageSize + 1 : 0;
}
set { }
}
public int PageSize { get => pageSize; set => pageSize = value; }
public string Sort { get; set; }
public string OrderBy { get; set; }
public PagerInfo()
{
PageNum = 1;

View File

@@ -1,53 +0,0 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Data;
namespace ZR.Model.Vo
{
public class VMPageResult<T> where T : new()
{
public int Page { get; set; }
public int PageSize { get; set; }
public long TotalNum { get; set; }
//public int TotalPages { get; set; }
public IList<T> Result { get; set; }
public long EndPage { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DataTable Data { get; set; }
public VMPageResult()
{
}
/// <summary>
/// 集合使用
/// </summary>
/// <param name="result"></param>
/// <param name="pagerInfo"></param>
public VMPageResult(IList<T> result, PagerInfo pagerInfo)
{
this.Result = result;
this.TotalNum = pagerInfo.TotalNum;
this.PageSize = pagerInfo.PageSize;
this.Page = pagerInfo.PageNum;
//计算
this.EndPage = pagerInfo.TotalNum % PageSize == 0 ? pagerInfo.TotalNum / PageSize : pagerInfo.TotalNum / PageSize + 1;
}
/// <summary>
/// datable使用
/// </summary>
/// <param name="result"></param>
/// <param name="pagerInfo"></param>
public VMPageResult(DataTable result, PagerInfo pagerInfo)
{
this.Data = result;
this.TotalNum = pagerInfo.TotalNum;
this.PageSize = pagerInfo.PageSize;
this.Page = pagerInfo.PageNum;
//计算
this.EndPage = pagerInfo.TotalNum % PageSize == 0 ? pagerInfo.TotalNum / PageSize : pagerInfo.TotalNum / PageSize + 1;
}
}
}

View File

@@ -4,11 +4,6 @@
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="ApiResult.cs" />
<Compile Remove="OptionsSetting.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.0.4.3" />
@@ -18,6 +13,7 @@
<ItemGroup>
<Folder Include="Dto\" />
<Folder Include="Enum\" />
<Folder Include="Vo\" />
</ItemGroup>
</Project>