Skip to content

Commit 60a49d8

Browse files
committed
Uniform return format
统一数据返回格式
1 parent cb7c68a commit 60a49d8

16 files changed

Lines changed: 157 additions & 294 deletions

Blog.Core.Model/MessageModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ namespace Blog.Core.Model
99
/// </summary>
1010
public class MessageModel<T>
1111
{
12+
/// <summary>
13+
/// 状态码
14+
/// </summary>
15+
public int status { get; set; } = 200;
1216
/// <summary>
1317
/// 操作是否成功
1418
/// </summary>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Blog.Core.Model.ViewModels
6+
{
7+
public class TokenInfoViewModel
8+
{
9+
public bool success { get; set; }
10+
public string token { get; set; }
11+
public double expires_in { get; set; }
12+
public string token_type { get; set; }
13+
}
14+
}

Blog.Core.Tests/Controller_Test/ClaimsController_Should.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

Blog.Core.Tests/Controller_Test/DepartmentController_Should.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

Blog.Core/AuthHelper/Policys/ApiResponse.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Blog.Core.Model;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -8,9 +9,10 @@ namespace Blog.Core.AuthHelper.Policys
89
public class ApiResponse
910
{
1011
public int Status { get; set; } = 404;
11-
public object Value { get; set; } = "No Found";
12+
public string Value { get; set; } = "No Found";
13+
public MessageModel<string> MessageModel = new MessageModel<string>() { };
1214

13-
public ApiResponse(StatusCode apiCode, object msg = null)
15+
public ApiResponse(StatusCode apiCode, string msg = "")
1416
{
1517
switch (apiCode)
1618
{
@@ -33,6 +35,13 @@ public ApiResponse(StatusCode apiCode, object msg = null)
3335
}
3436
break;
3537
}
38+
39+
MessageModel = new MessageModel<string>()
40+
{
41+
status = Status,
42+
msg = Value,
43+
success = false
44+
};
3645
}
3746
}
3847

Blog.Core/AuthHelper/Policys/ApiResponseHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop
2424
{
2525
Response.ContentType = "application/json";
2626
Response.StatusCode = StatusCodes.Status401Unauthorized;
27-
await Response.WriteAsync(JsonConvert.SerializeObject(new ApiResponse(StatusCode.CODE401)));
27+
await Response.WriteAsync(JsonConvert.SerializeObject((new ApiResponse(StatusCode.CODE401)).MessageModel));
2828
}
2929

3030
protected override async Task HandleForbiddenAsync(AuthenticationProperties properties)
3131
{
3232
Response.ContentType = "application/json";
3333
Response.StatusCode = StatusCodes.Status403Forbidden;
34-
await Response.WriteAsync(JsonConvert.SerializeObject(new ApiResponse(StatusCode.CODE403)));
34+
await Response.WriteAsync(JsonConvert.SerializeObject((new ApiResponse(StatusCode.CODE403)).MessageModel));
3535
}
3636

3737
}

Blog.Core/AuthHelper/Policys/JwtToken.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Blog.Core.Model.ViewModels;
2+
using System;
23
using System.IdentityModel.Tokens.Jwt;
34
using System.Security.Claims;
45

@@ -15,7 +16,7 @@ public class JwtToken
1516
/// <param name="claims">需要在登陆的时候配置</param>
1617
/// <param name="permissionRequirement">在startup中定义的参数</param>
1718
/// <returns></returns>
18-
public static dynamic BuildJwtToken(Claim[] claims, PermissionRequirement permissionRequirement)
19+
public static TokenInfoViewModel BuildJwtToken(Claim[] claims, PermissionRequirement permissionRequirement)
1920
{
2021
var now = DateTime.Now;
2122
// 实例化JwtSecurityToken
@@ -31,7 +32,7 @@ public static dynamic BuildJwtToken(Claim[] claims, PermissionRequirement permis
3132
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
3233

3334
//打包返回前台
34-
var responseJson = new
35+
var responseJson = new TokenInfoViewModel
3536
{
3637
success = true,
3738
token = encodedJwt,

Blog.Core/Blog.Core.Model.xml

Lines changed: 20 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Blog.Core/Blog.Core.xml

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Blog.Core/Controllers/BlogController.cs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Blog.Core.IServices;
88
using Blog.Core.Model;
99
using Blog.Core.Model.Models;
10+
using Blog.Core.Model.ViewModels;
1011
using Blog.Core.SwaggerHelper;
1112
using Microsoft.AspNetCore.Authorization;
1213
using Microsoft.AspNetCore.Mvc;
@@ -54,7 +55,7 @@ public BlogController(IBlogArticleServices blogArticleServices, IRedisCacheManag
5455
[AllowAnonymous]
5556
//[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
5657
//[ResponseCache(Duration = 600)]
57-
public async Task<object> Get(int id, int page = 1, string bcategory = "技术博文", string key = "")
58+
public async Task<MessageModel<PageModel<BlogArticle>>> Get(int id, int page = 1, string bcategory = "技术博文", string key = "")
5859
{
5960
int intTotalCount = 6;
6061
int total;
@@ -90,14 +91,18 @@ public async Task<object> Get(int id, int page = 1, string bcategory = "技术
9091
}
9192
}
9293

93-
return Ok(new
94+
return new MessageModel<PageModel<BlogArticle>>()
9495
{
9596
success = true,
96-
page,
97-
total,
98-
pageCount = totalCount,
99-
data = blogArticleList
100-
});
97+
msg = "获取成功",
98+
response = new PageModel<BlogArticle>()
99+
{
100+
page = page,
101+
dataCount = total,
102+
data = blogArticleList,
103+
pageCount = totalCount,
104+
}
105+
};
101106
}
102107

103108

@@ -108,14 +113,14 @@ public async Task<object> Get(int id, int page = 1, string bcategory = "技术
108113
/// <returns></returns>
109114
[HttpGet("{id}")]
110115
[Authorize(Roles = "Admin")]
111-
public async Task<object> Get(int id)
116+
public async Task<MessageModel<BlogViewModels>> Get(int id)
112117
{
113-
var model = await _blogArticleServices.GetBlogDetails(id);
114-
return Ok(new
118+
return new MessageModel<BlogViewModels>()
115119
{
120+
msg = "获取成功",
116121
success = true,
117-
data = model
118-
});
122+
response = await _blogArticleServices.GetBlogDetails(id)
123+
};
119124
}
120125

121126

@@ -127,15 +132,15 @@ public async Task<object> Get(int id)
127132
[HttpGet]
128133
[Route("DetailNuxtNoPer")]
129134
[AllowAnonymous]
130-
public async Task<object> DetailNuxtNoPer(int id)
135+
public async Task<MessageModel<BlogViewModels>> DetailNuxtNoPer(int id)
131136
{
132137
_logger.LogInformation("xxxxxxxxxxxxxxxxxxx");
133-
var model = await _blogArticleServices.GetBlogDetails(id);
134-
return Ok(new
138+
return new MessageModel<BlogViewModels>()
135139
{
140+
msg = "获取成功",
136141
success = true,
137-
data = model
138-
});
142+
response = await _blogArticleServices.GetBlogDetails(id)
143+
};
139144
}
140145

141146

@@ -152,9 +157,14 @@ public async Task<object> DetailNuxtNoPer(int id)
152157

153158
[CustomRoute(ApiVersions.V2, "Blogtest")]
154159
[AllowAnonymous]
155-
public object V2_Blogtest()
160+
public MessageModel<string> V2_Blogtest()
156161
{
157-
return Ok(new { status = 220, data = "我是第二版的博客信息" });
162+
return new MessageModel<string>()
163+
{
164+
msg = "获取成功",
165+
success = true,
166+
response = "我是第二版的博客信息"
167+
};
158168
}
159169

160170
/// <summary>
@@ -252,13 +262,14 @@ public async Task<MessageModel<string>> Put([FromBody] BlogArticle BlogArticle)
252262
[HttpGet]
253263
[Route("ApacheTestUpdate")]
254264
[AllowAnonymous]
255-
public async Task<object> ApacheTestUpdate()
265+
public async Task<MessageModel<bool>> ApacheTestUpdate()
256266
{
257-
return Ok(new
267+
return new MessageModel<bool>()
258268
{
259269
success = true,
260-
data = await _blogArticleServices.Update(new { bsubmitter = $"laozhang{DateTime.Now.Millisecond}", bID = 1 })
261-
});
270+
msg = "更新成功",
271+
response = await _blogArticleServices.Update(new { bsubmitter = $"laozhang{DateTime.Now.Millisecond}", bID = 1 })
272+
};
262273
}
263274
}
264275
}

0 commit comments

Comments
 (0)