All checks were successful
continuous-integration/drone/push Build is passing
95 lines
3.9 KiB
C#
95 lines
3.9 KiB
C#
using CampusErrand.Data;
|
|
using CampusErrand.Models;
|
|
using CampusErrand.Models.Dtos;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CampusErrand.Endpoints;
|
|
|
|
public static class RunnerEndpoints
|
|
{
|
|
public static void MapRunnerEndpoints(this WebApplication app)
|
|
{
|
|
// 提交跑腿认证申请
|
|
app.MapPost("/api/runner/certification", async (CertificationRequest request, HttpContext httpContext, AppDbContext db) =>
|
|
{
|
|
var userIdClaim = httpContext.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
|
|
if (userIdClaim == null) return Results.Unauthorized();
|
|
var userId = int.Parse(userIdClaim.Value);
|
|
|
|
// 校验
|
|
var errors = new List<object>();
|
|
if (string.IsNullOrWhiteSpace(request.RealName))
|
|
errors.Add(new { field = "realName", message = "姓名不能为空" });
|
|
if (string.IsNullOrWhiteSpace(request.Phone))
|
|
errors.Add(new { field = "phone", message = "手机号不能为空" });
|
|
if (errors.Count > 0)
|
|
return Results.BadRequest(new { code = 400, message = "校验失败", errors });
|
|
|
|
// 检查是否已有待审核或已通过的认证
|
|
var existing = await db.RunnerCertifications
|
|
.Where(c => c.UserId == userId && (c.Status == CertificationStatus.Pending || c.Status == CertificationStatus.Approved))
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (existing != null)
|
|
{
|
|
if (existing.Status == CertificationStatus.Approved)
|
|
return Results.BadRequest(new { code = 400, message = "您已通过跑腿认证" });
|
|
return Results.BadRequest(new { code = 400, message = "平台审核中" });
|
|
}
|
|
|
|
var certification = new RunnerCertification
|
|
{
|
|
UserId = userId,
|
|
RealName = request.RealName,
|
|
Phone = request.Phone,
|
|
Status = CertificationStatus.Pending,
|
|
CreatedAt = DateTime.UtcNow
|
|
};
|
|
|
|
db.RunnerCertifications.Add(certification);
|
|
await db.SaveChangesAsync();
|
|
|
|
return Results.Created($"/api/runner/certification", new CertificationResponse
|
|
{
|
|
Id = certification.Id,
|
|
UserId = certification.UserId,
|
|
RealName = certification.RealName,
|
|
Phone = certification.Phone,
|
|
Status = certification.Status.ToString(),
|
|
CreatedAt = certification.CreatedAt,
|
|
ReviewedAt = certification.ReviewedAt
|
|
});
|
|
}).RequireAuthorization();
|
|
|
|
// 查询跑腿认证状态
|
|
app.MapGet("/api/runner/certification", async (HttpContext httpContext, AppDbContext db) =>
|
|
{
|
|
var userIdClaim = httpContext.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
|
|
if (userIdClaim == null) return Results.Unauthorized();
|
|
var userId = int.Parse(userIdClaim.Value);
|
|
|
|
// 返回最新的认证记录
|
|
var certification = await db.RunnerCertifications
|
|
.Where(c => c.UserId == userId)
|
|
.OrderByDescending(c => c.CreatedAt)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (certification == null)
|
|
{
|
|
return Results.Ok(new { status = "None", message = "未提交认证" });
|
|
}
|
|
|
|
return Results.Ok(new CertificationResponse
|
|
{
|
|
Id = certification.Id,
|
|
UserId = certification.UserId,
|
|
RealName = certification.RealName,
|
|
Phone = certification.Phone,
|
|
Status = certification.Status.ToString(),
|
|
CreatedAt = certification.CreatedAt,
|
|
ReviewedAt = certification.ReviewedAt
|
|
});
|
|
}).RequireAuthorization();
|
|
}
|
|
}
|