Compare commits

...
5 Commits
Author SHA1 Message Date
xrain 8f41ba0150 move common controller to api group 2021-05-30 03:35:27 +08:00
xrain 9bce77a08e fix 301 302 2021-05-29 14:55:14 +08:00
xrain 8c7f4ac12f add 3.1 support 2021-05-29 11:41:03 +08:00
xrain 856c70d4ac exceptionMiddleware now capture statusCode != 200 response 2021-05-29 11:33:30 +08:00
xrain dfa9fbf909 fix controller Error return wrong message 2020-12-29 17:39:06 +08:00
6 changed files with 22 additions and 8 deletions
+4 -1
View File
@@ -95,7 +95,7 @@ namespace SimApi.Communications
{ {
IgnoreReadOnlyProperties = true, IgnoreReadOnlyProperties = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new JsonStringEnumConverter() } Converters = {new JsonStringEnumConverter()}
}); });
} }
} }
@@ -110,10 +110,13 @@ namespace SimApi.Communications
/// 动态内容列表 /// 动态内容列表
/// </summary> /// </summary>
public T List { get; set; } public T List { get; set; }
//当前页码 //当前页码
public int Page { get; set; } public int Page { get; set; }
//每页条数 //每页条数
public int Count { get; set; } public int Count { get; set; }
//总计条数 //总计条数
public int Total { get; set; } public int Total { get; set; }
} }
+3 -3
View File
@@ -47,7 +47,7 @@ namespace SimApi.Controllers
/// <param name="code">错误代码</param> /// <param name="code">错误代码</param>
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param> /// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
/// <returns></returns> /// <returns></returns>
protected static void Error(int code = 500, string message = "服务器错误") protected static void Error(int code = 500, string message = "")
{ {
throw new SimApiException(code, message); throw new SimApiException(code, message);
} }
@@ -58,7 +58,7 @@ namespace SimApi.Controllers
/// <param name="condition">检测条件</param> /// <param name="condition">检测条件</param>
/// <param name="code">错误代码</param> /// <param name="code">错误代码</param>
/// <param name="message">错误描述</param> /// <param name="message">错误描述</param>
protected static void ErrorWhen(bool condition, int code = 500, string message = "服务器错误") protected static void ErrorWhen(bool condition, int code = 500, string message = "")
{ {
if (condition) if (condition)
{ {
@@ -72,7 +72,7 @@ namespace SimApi.Controllers
/// <param name="condition">检测条件</param> /// <param name="condition">检测条件</param>
/// <param name="code">错误代码</param> /// <param name="code">错误代码</param>
/// <param name="message">错误描述</param> /// <param name="message">错误描述</param>
protected static void ErrorWhenNull(object condition, int code = 404, string message = "资源不存在") protected static void ErrorWhenNull(object condition, int code = 404, string message = "")
{ {
ErrorWhen(condition == null, code, message); ErrorWhen(condition == null, code, message);
} }
+3 -1
View File
@@ -6,6 +6,7 @@ using SimApi.Helpers;
namespace SimApi.Controllers namespace SimApi.Controllers
{ {
[ApiExplorerSettings(GroupName = "api")]
public class YYCommonController : SimApiBaseController public class YYCommonController : SimApiBaseController
{ {
private SimApiAuth Auth { get; } private SimApiAuth Auth { get; }
@@ -37,7 +38,7 @@ namespace SimApi.Controllers
public SimApiBaseResponse<int> CheckLogin() public SimApiBaseResponse<int> CheckLogin()
{ {
ErrorWhenNull(LoginInfo, 401); ErrorWhenNull(LoginInfo, 401);
return new SimApiBaseResponse<int> { Data = LoginInfo.Id }; return new SimApiBaseResponse<int> {Data = LoginInfo.Id};
} }
/// <summary> /// <summary>
@@ -53,6 +54,7 @@ namespace SimApi.Controllers
{ {
token = Request.Headers["Token"]; token = Request.Headers["Token"];
} }
Auth.Logout(token); Auth.Logout(token);
return new SimApiBaseResponse(); return new SimApiBaseResponse();
} }
+9
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using SimApi.Communications; using SimApi.Communications;
@@ -32,6 +33,13 @@ namespace SimApi.Middlewares
try try
{ {
await Next(context); await Next(context);
if (context.Response.StatusCode != 200)
{
if (!new[] {301, 302}.Contains(context.Response.StatusCode))
{
throw new SimApiException(context.Response.StatusCode);
}
}
} }
catch (SimApiException ex) catch (SimApiException ex)
{ {
@@ -43,6 +51,7 @@ namespace SimApi.Middlewares
{ {
response.SetCodeMsg(ex.Code, ex.Message); response.SetCodeMsg(ex.Code, ex.Message);
} }
ErrorResponse(context, response); ErrorResponse(context, response);
} }
catch (Exception ex) catch (Exception ex)
+1 -1
View File
@@ -1,4 +1,4 @@
# YYApi 基于Asp.net Core 3的一个基础辅助包 # YYApi 基于Asp.net Core 3/5的一个基础辅助包
![CI](https://github.com/YY-Tech/YYApi/workflows/CI/badge.svg) ![CI](https://github.com/YY-Tech/YYApi/workflows/CI/badge.svg)
+1 -1
View File
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<PackOnBuild>true</PackOnBuild> <PackOnBuild>true</PackOnBuild>
<Version>0.2.3</Version> <Version>0.2.3</Version>
@@ -15,6 +14,7 @@
<SynchReleaseVersion>false</SynchReleaseVersion> <SynchReleaseVersion>false</SynchReleaseVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>5.0.2</PackageVersion> <PackageVersion>5.0.2</PackageVersion>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'YYApi' " /> <PropertyGroup Condition=" '$(RunConfiguration)' == 'YYApi' " />