691 lines
21 KiB
C#
Raw Permalink Normal View History

2024-08-03 20:40:34 +08:00
2024-08-11 17:29:12 +08:00
using System.Security.Cryptography;
using System.Runtime.InteropServices.ComTypes;
2024-08-03 20:40:34 +08:00
using asg_form.Controllers.Hubs;
using Manganese.Text;
using Masuit.Tools;
using Masuit.Tools.Win32.AntiVirus;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using NLog;
using RestSharp;
using SixLabors.ImageSharp;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using static asg_form.Controllers.excel;
2024-11-17 13:37:37 +08:00
using static <EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD>;
2024-08-11 17:29:12 +08:00
using Manganese.Array;
2024-11-17 13:37:37 +08:00
using Microsoft.AspNetCore.Identity;
using Flurl.Http;
2024-11-24 00:18:51 +08:00
using System.Security.Claims;
2024-08-03 20:40:34 +08:00
namespace asg_form.Controllers
{
2025-01-27 20:32:23 +08:00
2024-11-24 00:18:51 +08:00
public class form_cs : ControllerBase
{
[Route("api/v3/form/all")]
[HttpGet]
public async Task<ActionResult<object>> GetFormById(short page, short limit, int sort, int eventId)
{
using (var db = new TestDbContext())
{
try
{
var query = db.Forms.Where(f => f.events.Id == eventId);
query = sort switch
{
0 => query.OrderByDescending(m => m.piaoshu).ThenByDescending(m => m.time),
1 => query.OrderBy(m => m.piaoshu),
2 => query.OrderByDescending(m => m.piaoshu),
3 => query.OrderBy(m => m.time),
4 => query.OrderByDescending(m => m.time),
_ => query
};
if (this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "nbadmin") || this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
try
{
var results = await query
.Skip((page - 1) * limit)
.Take(limit)
.Select(f => new AdminTeamFind
{
form = f,
roles = findRolesByFormId(f.Id, 0)
})
.ToListAsync();
int total = await query.CountAsync();
return Ok(new { code = 200, message = "<22>ɹ<EFBFBD>", data = results, total });
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", error = ex.Message });
}
}
else
{
var results = await query
.Skip((page - 1) * limit)
.Take(limit)
.Select(f => new AdminTeamFind
{
form = new form
{
Id = f.Id,
team_name = f.team_name,
piaoshu = f.piaoshu,
},
roles = findRolesByFormId(f.Id, 1)
})
.ToListAsync();
int total = await query.CountAsync();
return Ok(new { code = 200, message = "<22>ɹ<EFBFBD>", data = results, total });
}
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", ex = ex.Message });
}
}
}
public class tmpRole
{
public string? roleName { get; set; }
public int? roleRank { get; set; }
public string? commonRoles { get; set; }
public string? roleLin { get; set; }
public string? roleId { get; set; }
public string? gameName { get; set; }
public string? idCard { get; set; }
public string? phoneNumber { get; set; }
public string? idCardName { 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>();
2024-08-03 20:40:34 +08:00
2024-11-24 00:18:51 +08:00
}
public class AdminTeamFind
{
public form? form { get; set; }
public List<tmpRole>? roles { get; set; } = new List<tmpRole>();
}
private static List<tmpRole> findRolesByFormId(long formId, int y)
{
var roles = new List<tmpRole>();
var db = new TestDbContext();
try
{
var query = db.Roles.Where(x => x.form.Id == formId);
if (y == 1)
{
roles = query
.Select(f => new tmpRole
{
roleName = f.role_name,
roleRank = f.Historical_Ranks,
commonRoles = f.Common_Roles,
roleLin = f.role_lin
})
.ToList();
}
else
{
roles = query
.Select(f => new tmpRole
{
roleName = f.role_name,
roleRank = f.Historical_Ranks,
commonRoles = f.Common_Roles,
roleLin = f.role_lin,
roleId = f.role_id,
gameName = f.Game_Name,
idCard = f.Id_Card,
phoneNumber = f.Phone_Number,
idCardName = f.Id_Card_Name
})
.ToList();
}
2024-08-03 20:40:34 +08:00
2024-11-24 00:18:51 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"Error in BuildTree: {ex.Message}");
throw;
}
return roles;
}
}
2024-08-03 20:40:34 +08:00
// [Route("api/updateform/")]
2024-11-17 13:37:37 +08:00
public class <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : ControllerBase
2024-08-03 20:40:34 +08:00
{
2025-01-27 20:32:23 +08:00
2024-08-03 20:40:34 +08:00
public static void WriteFile(String str)
{
StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "/allteam.txt", true, System.Text.Encoding.Default);
sw.WriteLine(str);
sw.Close();
}
/// <summary>
/// <20><><EFBFBD>ޱ<EFBFBD><DEB1><EFBFBD>
/// </summary>
/// <param name="name"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="captoken"><3E>ȸ<EFBFBD><C8B8><EFBFBD>֤<EFBFBD><D6A4>token</param>
/// <returns></returns>
[Route("api/v1/form/like/")]
[HttpPost]
public async Task<ActionResult<like>> Post(long formid, string captoken)
{
var client = new RestClient($"https://www.recaptcha.net/recaptcha/api/siteverify?secret=6LcdXUEmAAAAAJLICuxBgtMsDiMSCm5XpB0z-fzK&response={captoken}");
var request = new RestRequest(Method.POST);
IRestResponse response = client.Execute(request);
string a = response.Content;
JObject d = a.ToJObject();
string ok = d["success"].ToString();
if (ok == "True")
{
try
{
using TestDbContext ctx = new TestDbContext();
var b = ctx.Forms.Single(b => b.Id == formid);
b.piaoshu = b.piaoshu + 1;
await ctx.SaveChangesAsync();
return new like { Number = b.piaoshu };
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
else
{
return BadRequest(new error_mb { code = 400, message = "<22>˻<EFBFBD><CBBB><EFBFBD>֤δͨ<CEB4><CDA8>" });
}
}
public class like
{
public int Number { get; set; }
}
/// <summary>
/// <20>޸<EFBFBD><DEB8>ʾ<EFBFBD>
/// </summary>
/// <param name="password"><3E><><EFBFBD><EFBFBD></param>
/// <param name="formid"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="for1"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ</param>
/// <returns></returns>
[Route("api/v1/form/")]
[HttpPut]
public async Task<ActionResult<string>> updateform(string password,string formname,[FromBody] form_get for1)
{
TestDbContext ctx = new TestDbContext();
var form = ctx.Forms.Include(a => a.role).FirstOrDefault(a => a.team_name==formname);
if (form.team_password == password)
{
List<role> role = new List<role>();
foreach (role_get a in for1.role_get)
{
role.Add(new role { role_id = a.role_id, role_lin = a.role_lin, role_name = a.role_name });
}
form.role = role;
await ctx.SaveChangesAsync();
return Ok("<22>ɹ<EFBFBD><C9B9><EFBFBD>");
}
else
{
return BadRequest(new error_mb { code=400,message="<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"});
}
}
private readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly IHubContext<room> hubContext;
2024-11-17 13:37:37 +08:00
public <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(IHubContext<room> hubContext)
{
this.hubContext= hubContext;
}
2024-08-03 20:40:34 +08:00
[Route("api/v1/websocket/")]
[HttpGet]
public async Task GetAsync()
{
await hubContext.Clients.All.SendAsync("formok", $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1/32");
}
2024-08-11 17:29:12 +08:00
/// <summary>
/// <20><EFBFBD><E1BDBB><EFBFBD><EFBFBD>
/// </summary>
/// <param name="imageFile"></param>
/// <param name="for1"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ</param>
/// <param name="captoken"><3E>ȸ<EFBFBD><C8B8>˻<EFBFBD><CBBB><EFBFBD>֤<EFBFBD><D6A4>֤<EFBFBD><D6A4></param>
/// <returns></returns>
[Route("api/v2/form/")]
[HttpPost]
2024-11-17 13:37:37 +08:00
public async Task<ActionResult<string>> PostAsync(IFormFile imageFile,[FromForm] form_get_new for1, string server, string token, string ip)
2024-08-11 17:29:12 +08:00
{
2024-11-17 13:37:37 +08:00
object data = new { id = "67134feddc0ff12924d9aaf4", secretkey = "c3e08b07b8034e6b961f010abed5586a", scene = 3, token = token, ip = ip };
var a1 = await server.PostJsonAsync(data);
var ok = await a1.GetJsonAsync();
Console.WriteLine(ok);
if (ok.success == 1)
{
2024-08-11 17:29:12 +08:00
2024-11-17 13:37:37 +08:00
using (var ctx = new TestDbContext())
{
if (ctx.Forms.Include(a => a.events).Where(a => a.events.name == for1.events_name).Any(e => e.team_name == for1.team_name))
2024-08-11 17:29:12 +08:00
{
2024-11-17 13:37:37 +08:00
return BadRequest(new error_mb { code = 400, message = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" });
}
else
2024-08-11 17:29:12 +08:00
{
2024-11-17 13:37:37 +08:00
if (imageFile == null || imageFile.Length == 0)
return BadRequest("Invalid image file.");
// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><E6B5BD><EFBFBD><EFBFBD>
2024-12-01 13:13:03 +08:00
var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"loge/{for1.events_name}/", $"{for1.team_name}.png");
2024-11-17 13:37:37 +08:00
using (var stream = new FileStream(filePath, FileMode.Create))
{
await imageFile.CopyToAsync(stream);
} // <20><><EFBFBD>سɹ<D8B3><C9B9><EFBFBD>Ӧ
// base64toimg(for1.logo_base64, $@"{AppDomain.CurrentDomain.BaseDirectory}loge\{for1.events_name}\{for1.team_name}.png");
var events = await ctx.events.FirstAsync(ctx => ctx.name == for1.events_name);
2024-08-11 17:29:12 +08:00
form form1 = new form();
2024-11-17 13:37:37 +08:00
form1.logo_uri = $"/loge/{for1.events_name}/{for1.team_name}.png";
2024-08-11 17:29:12 +08:00
form1.team_name = for1.team_name;
form1.team_password = for1.team_password;
form1.team_tel = for1.team_tel;
form1.events = events;
2024-11-17 13:37:37 +08:00
2024-08-11 17:29:12 +08:00
List<role> role = new List<role>();
foreach (role_get a in for1.role_get)
{
2024-11-17 13:37:37 +08:00
role.Add(new role { role_id = a.role_id, role_lin = a.role_lin, role_name = a.role_name, Common_Roles = a.Common_Roles, Historical_Ranks = a.Historical_Ranks, Id_Card = a.Id_Card, Game_Name = a.Game_Name, Phone_Number = a.Phone_Number, Id_Card_Name = a.Id_Card_Name });
2024-08-11 17:29:12 +08:00
}
form1.role = role;
ctx.Forms.Add(form1);
await ctx.SaveChangesAsync();
2024-11-17 13:37:37 +08:00
int nownumber = ctx.Forms.Count();
//ChatRoomHub chat = new ChatRoomHub();
// await chat.formok(nownumber, for1.team_name);
try
{
await hubContext.Clients.All.SendAsync("formok", $"<22><><EFBFBD><EFBFBD>{for1.team_name}<7D>Ѿ<EFBFBD><D1BE>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{ctx.Forms.Count()}/32");
2024-08-11 17:29:12 +08:00
2024-11-17 13:37:37 +08:00
}
catch
{
2024-08-11 17:29:12 +08:00
2024-11-17 13:37:37 +08:00
}
logger.Info($"<22><><EFBFBD><EFBFBD><C2B6><EFBFBD><E9B1A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>{for1.team_name} ");
2024-08-11 17:29:12 +08:00
}
return "ok!";
2024-11-17 13:37:37 +08:00
}
2024-08-11 17:29:12 +08:00
2024-11-17 13:37:37 +08:00
}
else
{
return BadRequest(new error_mb { code = 400, message = $"δͨ<CEB4><CDA8><EFBFBD>˻<EFBFBD><CBBB><EFBFBD>֤{await a1.GetStringAsync()}" });
}
2024-08-11 17:29:12 +08:00
}
2025-01-27 20:32:23 +08:00
2024-08-03 20:40:34 +08:00
/// <summary>
/// <20>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>logo
/// </summary>
/// <param name="imageFile"></param>
/// <returns></returns>
[Route("api/v1/updata_logo")]
[HttpPost]
public async Task<ActionResult<object>> update_logo(IFormFile imageFile,string eventname)
{
if (imageFile == null || imageFile.Length == 0)
return BadRequest("Invalid image file.");
// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><E6B5BD><EFBFBD><EFBFBD>
var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"loge/{eventname}/", $"{imageFile.FileName}");
using (var stream = new FileStream(filePath, FileMode.Create))
{
await imageFile.CopyToAsync(stream);
} // <20><><EFBFBD>سɹ<D8B3><C9B9><EFBFBD>Ӧ
return Ok("Image file uploaded successfully.");
}
2024-11-17 13:37:37 +08:00
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>Ϣ
/// </summary>
/// <param name="page">ҳ<><D2B3></param>
/// <param name="page_long">ÿҳ<C3BF><D2B3><EFBFBD><EFBFBD></param>
/// <returns></returns>
[Route("api/v1/form/all")]
[HttpGet]
[Authorize]
public List<team> Getform(short page,short page_long,string sort,string eventsname)
{
TestDbContext ctx = new TestDbContext();
int c = ctx.Forms.Count();
int b = page_long * page;
if (page_long * page > c)
{
b = c;
}
var events= ctx.events.First(ctx => ctx.name == eventsname);
List<form> forms;
if(sort=="vote")
{
forms = ctx.Forms.Include(a => a.role).Include(a=>a.events).OrderByDescending(a => a.piaoshu).Where(a=>a.events==events).Skip(page_long * page - page_long).Take(page_long).ToList();
}
else
{
//<2F><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>id<69><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//forms = ctx.Forms.Include(a => a.role).Skip(page_long * page - page_long).Take(page_long).ToList();
forms = ctx.Forms.Include(a => a.role).Include(a => a.events).OrderByDescending(a => a.Id).Where(a => a.events == events).Skip(page_long * page - page_long).Take(page_long).ToList();
}
List<team> teams = new List<team>();
foreach (form for1 in forms)
{
var team = new team { id=for1.Id,name = for1.team_name, timer = for1.time, piaoshu = for1.piaoshu ,logo_uri=for1.logo_uri};
foreach (var role in for1.role)
{
team.rolename.Add(new roletwo { name = role.role_name, lin = role.role_lin });
}
teams.Add(team);
// a++;
}
return teams;
}
2024-08-03 20:40:34 +08:00
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="team_name"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <returns></returns>
[Route("api/v1/form/{team_name}")]
[HttpGet]
public async Task<ActionResult<List<allteam>>> formsearch(string team_name)
{
TestDbContext ctx = new TestDbContext();
List<allteam> data = new List<allteam>();
List<form> teams = ctx.Forms.Include(a => a.role).Where(a => a.team_name.IndexOf(team_name) >= 0).ToList();
foreach (var team in teams)
{
var roles = team.role;
allteam allteam = new allteam();
allteam.Id = team.Id;
allteam.Name = team.team_name;
foreach (var role in roles)
{
role.form = null;
allteam.role.Add(role);
}
data.Add(allteam);
}
return data;
}
/// <summary>
/// ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="team_name"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <returns></returns>
[Route("api/v1/form/name/{team_name}")]
[HttpGet]
public async Task<ActionResult<List<string>>> search_name(string team_name,string events_name)
{
var ctx = new TestDbContext();
2024-08-11 17:29:12 +08:00
2024-08-03 20:40:34 +08:00
var data = ctx.Forms.Where(a => a.team_name.IndexOf(team_name) >= 0&&a.events.name==events_name).Select(a => a.team_name).ToList();
return data;
}
}
2024-11-24 00:18:51 +08:00
2024-08-03 20:40:34 +08:00
public class role
{
public long Id { get; set; }
public form form { get; set; }//<2F><><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
public string role_id { get; set; } = "<22><>";
public string role_name { get; set; } = "<22><>";//<2F><><EFBFBD><EFBFBD>
public string? Game_Name { get; set; } = "δ֪";
public string role_lin { get; set; }
public string? Id_Card { get; set; } = "δ֪";
public string? Common_Roles { get; set; } = "δ֪";
public string? Phone_Number { get; set; } = "δ֪";
public string? Id_Card_Name { get; set; } = "δ֪";
public int? Historical_Ranks { get; set; } = 0;
}
public class role_get
{
public string role_id { get; set; } = "<22><>";
public string role_name { get; set; } = "<22><>";//<2F><><EFBFBD><EFBFBD>
public string role_lin { get; set; } = "<22><>";
public string? Game_Name { get; set; }
public string? Id_Card { get; set; }
public string? Common_Roles { get; set; }
public string? Phone_Number { get; set; }
public string? Id_Card_Name { get; set; }
public int? Historical_Ranks { get; set; }
}
public class form
{
public long Id { get; set; }
public int piaoshu { get; set; }
2024-11-24 00:07:52 +08:00
public DateTime time { get; set; } = DateTime.UtcNow;
2024-08-03 20:40:34 +08:00
public string team_name { get; set; }
public string team_password { get; set; }
public string team_tel { get; set; }
public string logo_uri { get; set; }
2024-08-25 00:21:41 +08:00
public T_events events { get; set; }
2024-08-11 17:29:12 +08:00
2024-08-03 20:40:34 +08:00
// public string? belong { get; set; }
public List<role> role { get; set; } = new List<role>();
}
public class form_get
{
2024-08-11 17:29:12 +08:00
// public DateTime time { get; set; } = DateTime.Now;
public string team_name { get; set; }
public string team_password { get; set; }
public string team_tel { get; set; }
public string logo_base64 { get; set; }
public string events_name { get; set; }
// public string? belong { get; set; }
public List<role_get> role_get { get; set; }
}
public class form_get_new
{
2024-08-03 20:40:34 +08:00
public DateTime time { get; set; } = DateTime.Now;
public string team_name { get; set; }
2024-11-17 13:37:37 +08:00
2024-08-03 20:40:34 +08:00
public string team_password { get; set; }
public string team_tel { get; set; }
2024-08-11 17:29:12 +08:00
// public string logo_base64 { get; set; }
2024-11-17 13:37:37 +08:00
public string events_name { get; set; }
2024-08-03 20:40:34 +08:00
// public string? belong { get; set; }
public List<role_get> role_get { get; set; }
}
public class form1
{
public long Id { get; set; }
2024-08-11 17:29:12 +08:00
public string Team_name { get; set; }
2024-08-03 20:40:34 +08:00
public string team_password { get; set; }
public string team_tel { get; set; }
public role[] role1 { get; set; }
public string loge_base64 { get; set; } = "null";
}
}
2024-11-17 13:37:37 +08:00
public class <EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD> : ControllerBase
2024-08-03 20:40:34 +08:00
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public class team
{
public long id { get; set; }
public string name { get; set; }
2024-11-17 13:37:37 +08:00
public DateTime timer { get; set; }
2024-08-03 20:40:34 +08:00
public int piaoshu { get; set; }
public string logo_uri { get; set; }
2024-11-17 13:37:37 +08:00
public List<roletwo> rolename { get; set; }=new List<roletwo>();
2024-08-03 20:40:34 +08:00
}
2024-11-17 13:37:37 +08:00
public class roletwo
{
public string name { get; set; }
public string lin { get; set; }
}
2024-08-03 20:40:34 +08:00
public class form_uri
{
public string uri { get; set; }
}
}