This commit is contained in:
2026-03-25 14:55:34 +08:00
commit 2c44b3a4b2
131 changed files with 7453 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
using Microsoft.Extensions.Options;
using StopShopping.Services;
using StopShopping.Services.Extensions;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Admin
{
public static RouteGroupBuilder MapAdmin(this RouteGroupBuilder routes)
{
routes.MapPost("/admin/signin", SignInAsync)
.AllowAnonymous().WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse<SignInAdmin>> SignInAsync(
SignInParams model,
IUserService userService,
HttpContext httpContext,
IWebHostEnvironment env,
IOptions<AppOptions> options)
{
var result = await userService.SignInAdminAsync(model);
var resp = new ApiResponse<SignInAdmin>
{
IsSucced = result.IsSucced,
Data = result.User,
Message = result.Message
};
if (result.IsSucced)
{
httpContext.Response.Cookies.AppendRefreshToken(
env,
options.Value,
TimeSpan.FromSeconds(result.RefreshToken!.ExpiresIn),
result.RefreshToken.Token!
);
}
return resp;
}
}

View File

@@ -0,0 +1,60 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Category
{
public static RouteGroupBuilder MapCategoryCommon(this RouteGroupBuilder routes)
{
routes.MapGet("/category/list", GetTree)
.WithTags(OpenApiTags..ToString())
.AllowAnonymous();
return routes;
}
public static RouteGroupBuilder MapCategory(this RouteGroupBuilder routes)
{
routes.MapPost("/category/edit", EditCategoryAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/category/resort", ResortCategoryAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/category/delete", DeleteCategoryAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static ApiResponse<List<Services.Models.Resp.Category>> GetTree(
ICategoryService categoryService
)
{
return categoryService.GetCategoriesTree();
}
private static async Task<ApiResponse<Services.Models.Resp.Category>> EditCategoryAsync(
EditCategoryParams model,
ICategoryService categoryService)
{
return await categoryService.EditCategoryAsync(model);
}
private static async Task<ApiResponse> ResortCategoryAsync(
ResortCategoryParams model,
ICategoryService categoryService)
{
return await categoryService.ResortCategoryAsync(model);
}
private static async Task<ApiResponse> DeleteCategoryAsync(
CategoryIdParams model,
ICategoryService categoryService
)
{
return await categoryService.DeleteCategoryAsync(model);
}
}

View File

@@ -0,0 +1,87 @@
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Common
{
public static RouteGroupBuilder MapCommon(this RouteGroupBuilder routes)
{
routes.MapPost("/common/upload", UploadAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/common/refreshtoken", RefreshTokenAsync)
.AllowAnonymous()
.Produces<ApiResponse<AccessToken>>()
.WithTags(OpenApiTags..ToString());
routes.MapPost("/common/signout", SignOutAsync)
.AllowAnonymous().WithTags(OpenApiTags..ToString());
routes.MapPost("/common/antiforgery-token", AntiForgeryToken)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse<FileUpload>> UploadAsync(
[FromForm] UploadParams payload,
IFileService fileService,
HttpContext httpContext)
{
return await fileService.UploadFileAsync(payload);
}
private static ApiResponse<AntiForgeryToken> AntiForgeryToken(
HttpContext httpContext,
IAntiforgery antiforgery)
{
var antiforgeryToken = antiforgery.GetAndStoreTokens(httpContext);
return new ApiResponse<AntiForgeryToken>(new AntiForgeryToken
{
Token = antiforgeryToken.RequestToken,
HeaderName = antiforgeryToken.HeaderName
});
}
private static async Task<IResult> RefreshTokenAsync(
HttpContext httpContext,
IAccessTokenService accessTokenService)
{
var refreshToken = httpContext.Request.Cookies[HttpExtensions.REFRESH_TOKEN_COOKIE_KEY];
if (string.IsNullOrWhiteSpace(refreshToken))
return Results.Unauthorized();
var accessToken = await accessTokenService.GenerateAccessTokenAsync(refreshToken);
if (null == accessToken)
return Results.Unauthorized();
return Results.Ok(new ApiResponse<AccessToken>(accessToken));
}
public static async Task<ApiResponse> SignOutAsync(
HttpContext httpContext,
IAccessTokenService accessTokenService)
{
var accessTokenHeader = httpContext.Request.Headers[HeaderNames.Authorization];
if (accessTokenHeader.Count != 0)
{
var accessToken = accessTokenHeader.First()!.Split(" ").Last();
if (!string.IsNullOrWhiteSpace(accessToken))
await accessTokenService.AddAccessTokenBlacklistAsync(accessToken);
}
var refreshToken = httpContext.Request.Cookies[HttpExtensions.REFRESH_TOKEN_COOKIE_KEY];
if (!string.IsNullOrWhiteSpace(refreshToken))
{
await accessTokenService.RevokeRefreshTokenAsync(refreshToken);
httpContext.Response.Cookies.Delete(HttpExtensions.REFRESH_TOKEN_COOKIE_KEY);
}
return ApiResponse.Succed();
}
}

View File

@@ -0,0 +1,34 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class District
{
public static RouteGroupBuilder MapDistrict(this RouteGroupBuilder routes)
{
routes.MapGet("/district/top3level", GetTop3LevelDistrictsAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/district/children", GetChildrenDistricts)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static ApiResponse<List<Services.Models.Resp.District>> GetChildrenDistricts(
[AsParameters] DistrictParentIdParams model,
IDistrictService districtService
)
{
return districtService.GetChildren(model);
}
private static async Task<ApiResponse<List<Services.Models.Resp.District>>> GetTop3LevelDistrictsAsync(
IDistrictService districtService
)
{
return await districtService.GetTop3LevelDistrictsAsync();
}
}

View File

@@ -0,0 +1,61 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
/// <summary>
/// 商品相关路由
/// </summary>
public static class Product
{
public static RouteGroupBuilder MapProduct(this RouteGroupBuilder routes)
{
routes.MapGet("/product/list", SearchProductsAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/product/detail", Detail)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/product/edit", EditAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/product/delete", DeleteAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async
Task<ApiResponse<PagedResult<Services.Models.Resp.Product>>>
SearchProductsAsync(
[AsParameters] ProductSearchParms model,
IProductService productService
)
{
return await productService.SearchAsync(model);
}
private static ApiResponse<ProductInfo> Detail(
[AsParameters] ProductIdParams model,
IProductService productService)
{
return productService.Detail(model);
}
private static async Task<ApiResponse> EditAsync(
EditProductParams model,
IProductService productService
)
{
return await productService.EditAsync(model);
}
private static async Task<ApiResponse> DeleteAsync(
ProductIdParams model,
IProductService productService
)
{
return await productService.DeleteAsync(model);
}
}

View File

@@ -0,0 +1,45 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Reply
{
public static RouteGroupBuilder MapReply(this RouteGroupBuilder routes)
{
routes.MapPost("/reply/post", SubmitReplyAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/reply/list", ListAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/reply/orders", OrderSearchAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse> SubmitReplyAsync(
ReplyParams model,
IReplyService replyService)
{
return await replyService.ReplyAsync(model);
}
private static async Task<ApiResponse<List<Services.Models.Resp.Reply>>> ListAsync(
[AsParameters] RequestIdParams model,
IReplyService replyService
)
{
return await replyService.GetRepliesAsync(model);
}
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> OrderSearchAsync(
[AsParameters] RequestSearchWithStatusParams model,
IRequestService requestService
)
{
return await requestService.RequestOrderSearchAsync(model);
}
}

View File

@@ -0,0 +1,57 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Request
{
public static RouteGroupBuilder MapRequest(this RouteGroupBuilder routes)
{
routes.MapPost("/request/publish", PublishRequestAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/request/search", SearchAsync)
.WithTags(OpenApiTags..ToString())
.AllowAnonymous();
routes.MapGet("/request/orders", OrderSearchAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/request/delete", DeleteRequestAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse> PublishRequestAsync(
CreateRequestParams model,
IRequestService requestService)
{
return await requestService.PublishRequestAsync(model);
}
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> SearchAsync(
[AsParameters] RequestSearchParams model,
IRequestService requestService
)
{
return await requestService.SearchAsync(model);
}
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> OrderSearchAsync(
[AsParameters] RequestSearchWithStatusParams model,
IRequestService requestService
)
{
return await requestService.RequestOrderSearchAsync(model);
}
private static async Task<ApiResponse> DeleteRequestAsync(
RequestIdParams model,
IRequestService requestService
)
{
return await requestService.DeleteRequestAsync(model);
}
}

View File

@@ -0,0 +1,45 @@
using Scalar.AspNetCore;
using StopShopping.Services.Models;
namespace StopShopping.Api.Routes;
/// <summary>
/// 其他路由从RouteGroupBuilder扩展并添加到MapGroup之后
/// </summary>
public static class Root
{
public static void MapRoutes(WebApplication app)
{
app.MapGroup("")
.MapUser()
.MapProduct()
.MapRequest()
.MapReply()
.MapDistrict()
.WithDescription("用户端调用")
.RequireAuthorization(policy => policy.RequireRole(SystemRoles.User.ToString()));
app.MapGroup("")
.MapCommon()
.MapCategoryCommon()
.WithDescription("公共调用")
.RequireAuthorization();
app.MapGroup("")
.MapAdmin()
.MapCategory()
.WithDescription("管理端调用")
.RequireAuthorization(policy => policy.RequireRole(SystemRoles.Admin.ToString()));
}
}
public enum OpenApiTags
{
,
,
,
,
,
,
,
,
}

View File

@@ -0,0 +1,95 @@
using Microsoft.Extensions.Options;
using StopShopping.Services;
using StopShopping.Services.Extensions;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class User
{
public static RouteGroupBuilder MapUser(this RouteGroupBuilder routes)
{
routes.MapPost("/user/signup", SignUpAsync)
.AllowAnonymous().WithTags(OpenApiTags..ToString());
routes.MapPost("/user/signin", SignInAsync)
.AllowAnonymous().WithTags(OpenApiTags..ToString());
routes.MapPost("/user/changepassword", ChangePasswordAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/user/info", GetUserAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/user/edit", EditUserAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse> SignUpAsync(
SignUpParams model,
IUserService userService)
{
await userService.SignUpAsync(model);
return ApiResponse.Succed();
}
private static async Task<ApiResponse<SignInUser>> SignInAsync(
SignInParams model,
IUserService userService,
HttpContext httpContext,
IWebHostEnvironment env,
IOptions<AppOptions> options)
{
var result = await userService.SignInAsync(model);
var resp = new ApiResponse<SignInUser>
{
IsSucced = result.IsSucced,
Data = result.User,
Message = result.Message
};
if (result.IsSucced)
{
httpContext.Response.Cookies.AppendRefreshToken(
env,
options.Value,
TimeSpan.FromSeconds(result.RefreshToken!.ExpiresIn),
result.RefreshToken.Token!
);
}
return resp;
}
private static async Task<ApiResponse> ChangePasswordAsync(
ChangePasswordParams model,
IUserService userService,
HttpContext httpContext,
IAccessTokenService accessTokenService
)
{
var resp = await userService.ChangePasswordAsync(model);
if (resp.IsSucced)
await Common.SignOutAsync(httpContext, accessTokenService);
return resp;
}
private static async Task<ApiResponse<Services.Models.Resp.User>> GetUserAsync(
IUserService userService
)
{
return await userService.GetUserInfoAsync();
}
private static async Task<ApiResponse> EditUserAsync(
EditUserParams model,
IUserService userService
)
{
return await userService.EditAsync(model);
}
}