This commit is contained in:
王炜翔 2024-11-05 18:51:27 +08:00
parent 61e8f98af8
commit 7f85b4dac7
2 changed files with 119 additions and 63 deletions

View File

@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
using NPOI.OpenXmlFormats.Dml; using NPOI.OpenXmlFormats.Dml;
using System.Security.Claims; using System.Security.Claims;
using static allteam1; using static allteam1;
using static asg_form.Controllers.menuAssignController;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory; using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace asg_form.Controllers.Team namespace asg_form.Controllers.Team
@ -206,76 +207,110 @@ namespace asg_form.Controllers.Team
} }
} }
public class TeamFind public class tmpRole
{ {
public List<string> roleId { get; set; } public string? roleName { get; set; }
public long formId { get; set; } public int? roleRank { get; set; }
public string teamName { get; set; } public string? commonRoles { get; set; }
public List<string> roles { get; set; } public string? roleLin { get; set; }
public int voteOfLikes { get; set; }
public List<string>? roleLin { get; set; }
public List<string>? commonRoles { get; set; }
} }
public class TeamFind
{
public long formId { get; set; }
public string teamName { get; set; }
public int voteOfLikes { get; set; }
public List<tmpRole> roles { get; set; } = new List<tmpRole>();
}
public class AdminTeamFind
{
public form? form { get; set; }
public List<tmpRole>? roles { get; set; }= new List<tmpRole>();
}
private static List<tmpRole> findRolesByFormId(long formId)
{
var roles = new List<tmpRole>();
var db = new TestDbContext();
try
{
var query = db.Roles.Where(x => x.form.Id == formId);
roles = query
.Select(f => new tmpRole
{
roleName = f.role_name,
roleRank = f.Historical_Ranks,
commonRoles = f.Common_Roles,
roleLin = f.role_lin
})
.ToList();
}
catch (Exception ex)
{
Console.WriteLine($"Error in BuildTree: {ex.Message}");
throw;
}
return roles;
}
[Route("api/v3/form/all")] [Route("api/v3/form/all")]
[HttpGet] [HttpGet]
public async Task<ActionResult<object>> GetFormById(short page, short limit, int sort, int eventId) public async Task<ActionResult<object>> GetFormById(short page, short limit, int sort, int eventId)
{ {
using (var db = new TestDbContext()) using (var db = new TestDbContext())
{ {
try try
{ {
var query = from m in db.Forms var query = db.Forms.Where(f => f.events.Id == eventId);
where m.events.Id == eventId query = sort switch
join r in db.Roles on m.Id equals r.form.Id
select new
{
m.team_name,
r.role_id,
r.role_name,
r.role_lin,
r.Common_Roles,
m.piaoshu,
m.time,
m.Id
};
switch (sort)
{ {
case 0: 0 => query.OrderByDescending(m => m.piaoshu).ThenByDescending(m => m.time),
query = query.OrderByDescending(m => m.piaoshu).ThenByDescending(m => m.time); 1 => query.OrderBy(m => m.piaoshu),
break; 2 => query.OrderByDescending(m => m.piaoshu),
case 1: 3 => query.OrderBy(m => m.time),
query = query.OrderBy(m => m.piaoshu); 4 => query.OrderByDescending(m => m.time),
break; _ => query
case 2: };
query = query.OrderByDescending(m => m.piaoshu); if (this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "nbadmin"))
break; {
case 3: try
query = query.OrderBy(m => m.time);
break;
case 4:
query = query.OrderByDescending(m => m.time);
break;
default:
break;
}
var results = await query
.GroupBy(m => new { m.team_name, m.Id,m.piaoshu, m.time })
.Select(g => new TeamFind
{ {
formId = g.Key.Id, var results = await query
teamName = g.Key.team_name, .Skip((page - 1) * limit)
roles = g.Select(x => x.role_name).ToList(), .Take(limit)
voteOfLikes = g.Key.piaoshu, .Select(f => new AdminTeamFind
roleLin = g.Select(x => x.role_lin).ToList(), // 选择第一个roleLin {
commonRoles = g.Select(x => x.Common_Roles).ToList(), // 选择第一个commonRoles form = f,
roleId = g.Select(x => x.role_id).ToList(), // 选择第一个roleId roles = findRolesByFormId(f.Id)
}) })
.ToListAsync();
int total = await query.CountAsync();
return Ok(new { code = 200, message = "成功", data = results, total });
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "服务器错误", error = ex.Message });
}
}
else
{
var results = await query
.Skip((page - 1) * limit) .Skip((page - 1) * limit)
.Take(limit) .Take(limit)
.Select(f => new
{
formId = f.Id,
teamName = f.team_name,
voteOfLikes = f.piaoshu,
roles = findRolesByFormId(f.Id)
})
.ToListAsync(); .ToListAsync();
int total = results.Count(); int total = await query.CountAsync();
return Ok(new { code = 200, message = "成功", data = results,total }); return Ok(new { code = 200, message = "成功", data = results, total });
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -284,5 +319,7 @@ namespace asg_form.Controllers.Team
} }
} }
} }
} }

View File

@ -228,13 +228,14 @@ namespace asg_form.Controllers
public string? captcha_alphabet { get; set; } public string? captcha_alphabet { get; set; }
public int? captcha_number { get; set; } public int? captcha_number { get; set; }
public string off_time { get; set; } public string off_time { get; set; }
public string uid { get; set; }
} }
/// <summary> /// <summary>
/// 验证码生成 /// 验证码生成
/// </summary> /// </summary>
[Route("api/v2/makeCaptcha")] [Route("api/v2/makeCaptcha")]
[HttpGet] [HttpPost]
public async Task<ActionResult<object> >GetCaptcha() public async Task<ActionResult<object>> GetCaptcha([FromBody]string uid)
{ {
try try
{ {
@ -253,6 +254,7 @@ namespace asg_form.Controllers
id = maxId + 1, id = maxId + 1,
captcha_alphabet = captchaCode, captcha_alphabet = captchaCode,
off_time = DateTime.Now.ToString(), off_time = DateTime.Now.ToString(),
uid = uid,
}; };
db.Add(msg); db.Add(msg);
db.SaveChanges(); db.SaveChanges();
@ -266,14 +268,14 @@ namespace asg_form.Controllers
} }
// 验证用户输入的验证码 // 验证用户输入的验证码
public bool ValidateCaptcha(string userInput) public bool ValidateCaptcha(string uid,string userInput)
{ {
using (var db = new TestDbContext()) using (var db = new TestDbContext())
{ {
try try
{ {
var query = db.T_captcha_check.AsQueryable(); var query = db.T_captcha_check.AsQueryable();
var msg = query.FirstOrDefault(n => n.captcha_alphabet == userInput); var msg = query.FirstOrDefault(n => n.captcha_alphabet == userInput && n.uid == uid);
bool isVali = (msg != null); bool isVali = (msg != null);
if(isVali) db.T_captcha_check.Remove(msg); if(isVali) db.T_captcha_check.Remove(msg);
return isVali; return isVali;
@ -285,6 +287,24 @@ namespace asg_form.Controllers
} }
} }
public void moveOutTimeData(string uid)
{
using (var db = new TestDbContext())
{
try
{
var query = db.T_captcha_check.AsQueryable();
var msg = query.FirstOrDefault(n => n.uid == uid);
bool isVali = (msg != null);
if (isVali) db.T_captcha_check.Remove(msg);
}
catch (Exception ex)
{
throw;
}
}
}
/// <summary> /// <summary>
/// 验证码注册 /// 验证码注册
/// </summary> /// </summary>
@ -296,14 +316,13 @@ namespace asg_form.Controllers
try try
{ {
bool isCaptchaValid = ValidateCaptcha(newuser.captcha); bool isCaptchaValid = ValidateCaptcha(newuser.uid,newuser.captcha);
if (captchaNow == "7777") return BadRequest(new { code = 503, message = "服务器正忙" }); if (captchaNow == "7777") return BadRequest(new { code = 503, message = "服务器正忙" });
if (!isCaptchaValid) if (!isCaptchaValid)
{ {
captchaNow = "7777"; moveOutTimeData(newuser.uid);
return BadRequest(new { code = 400, message = "验证码无效" }); return BadRequest(new { code = 400, message = "验证码无效" });
} }
captchaNow = "7777";
var maxId = await userManager.Users.MaxAsync(u => u.Id); var maxId = await userManager.Users.MaxAsync(u => u.Id);
User? user = await userManager.FindByNameAsync(newuser.userName); User? user = await userManager.FindByNameAsync(newuser.userName);
if (user == null) if (user == null)
@ -345,7 +364,7 @@ namespace asg_form.Controllers
public record Adduserreq(string userName, string password, string chinaname, string token); public record Adduserreq(string userName, string password, string chinaname, string token);
public record AddUserReq(string userName, string password, string chinaname, string captcha); public record AddUserReq(string userName, string password, string chinaname, string captcha,string uid);
[Route("api/v1/setimg")] [Route("api/v1/setimg")]
[HttpPost] [HttpPost]