45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
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;
|
|
}
|
|
}
|