From 3ad96bcce94b8d3e48591309df52fb4b90a01c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=82=9C=E7=BF=94?= <2307953404@qq.com> Date: Sun, 3 Nov 2024 15:27:27 +0800 Subject: [PATCH] 1 --- asg_form/Controllers/Dbset.cs | 2 + asg_form/Controllers/admin.cs | 4 +- asg_form/Controllers/login.cs | 66 +++++++++++++++++--- asg_form/Controllers/menuAssignController.cs | 51 ++++++++++----- 4 files changed, 96 insertions(+), 27 deletions(-) diff --git a/asg_form/Controllers/Dbset.cs b/asg_form/Controllers/Dbset.cs index f9f1287..d9763c0 100644 --- a/asg_form/Controllers/Dbset.cs +++ b/asg_form/Controllers/Dbset.cs @@ -13,6 +13,7 @@ using System.Security.Cryptography; using System.Text; using static asg_form.Controllers.Budget.BgCountController; using static asg_form.Controllers.InviteReferee; +using static asg_form.Controllers.login; using static asg_form.Controllers.menuAssignController; namespace asg_form.Controllers @@ -287,6 +288,7 @@ namespace asg_form.Controllers public DbSet T_Invitation { get; set; } public DbSet mainMenu { get; set; } + public DbSet T_captcha_check { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string connStr = @"Host=localhost;Port=2345;Database=asg;Username=postgres;Password=luolan12323;"; diff --git a/asg_form/Controllers/admin.cs b/asg_form/Controllers/admin.cs index 01310f5..3e98be8 100644 --- a/asg_form/Controllers/admin.cs +++ b/asg_form/Controllers/admin.cs @@ -261,8 +261,8 @@ namespace asg_form.Controllers else { - a.cout = userManager.Users.Where(a => a.chinaname == keyword || a.UserName == keyword || a.Email == keyword).Count(); - a.msg = await userManager.Users.Where(a => a.chinaname == keyword || a.UserName == keyword || a.Email == keyword).Paginate(pageindex, pagesize).Select(a => new { a.Id, a.Email, a.chinaname, a.UserName, a.Integral, a.officium,a.qqnumber, a.roleListName, a.roleListCode }).ToListAsync(); + a.cout = userManager.Users.Where(a => a.chinaname.Contains(keyword) || a.UserName == keyword || a.Email == keyword).Count(); + a.msg = await userManager.Users.Where(a => a.chinaname.Contains(keyword) || a.UserName == keyword || a.Email == keyword).Paginate(pageindex, pagesize).Select(a => new { a.Id, a.Email, a.chinaname, a.UserName, a.Integral, a.officium,a.qqnumber, a.roleListName, a.roleListCode }).ToListAsync(); } return Ok(a); } diff --git a/asg_form/Controllers/login.cs b/asg_form/Controllers/login.cs index b983f91..9cd5121 100644 --- a/asg_form/Controllers/login.cs +++ b/asg_form/Controllers/login.cs @@ -18,6 +18,8 @@ using System.Security.Authentication; using Flurl.Http; using Microsoft.EntityFrameworkCore; using System.CodeDom.Compiler; +using Microsoft.EntityFrameworkCore.Metadata.Conventions; +using Manganese.Array; namespace asg_form.Controllers { @@ -220,25 +222,71 @@ namespace asg_form.Controllers public string captchaNow = "7777"; + public class capData + { + public int id { get; set; } + public string? captcha_alphabet { get; set; } + public int? captcha_number { get; set; } + public string off_time { get; set; } + } /// /// 验证码生成 /// [Route("api/v2/makeCaptcha")] [HttpGet] - public ActionResult<(string captchaImage, string captchaCode)> GetCaptcha() + public async Task >GetCaptcha() { - var captchaService = new CaptchaService(); - var (captchaImage, captchaCode) = captchaService.GenerateCaptcha(); - - captchaNow=captchaCode; - - return Ok(new { captchaImage, captchaCode }); + try + { + var captchaService = new CaptchaService(); + var (captchaImage, captchaCode) = captchaService.GenerateCaptcha(); + captchaNow=captchaCode; + var offTime = new DateTime(); + using (var db = new TestDbContext()) + { + var query = db.T_captcha_check.AsQueryable(); + var currentDateTime = DateTime.Now; + var fiveMinutesAgo = currentDateTime.AddMinutes(-5); + var recordsToDelete = db.T_captcha_check + .Where(x => DateTime.TryParse(x.off_time,out offTime) && offTime < fiveMinutesAgo) + .ToList(); + db.T_captcha_check.RemoveRange(recordsToDelete); + int maxId = query.Max(n => n.id); + var msg = new capData + { + id = maxId + 1, + captcha_alphabet = captchaCode, + off_time = DateTime.Now.ToString(), + }; + db.Add(msg); + db.SaveChanges(); + } + return Ok(new { captchaImage }); + } + catch (Exception ex) { + return Ok(new { code = 500, message = "服务器错误", ex }); + } + } // 验证用户输入的验证码 public bool ValidateCaptcha(string userInput) { - return userInput.Equals(captchaNow, StringComparison.OrdinalIgnoreCase); + using (var db = new TestDbContext()) + { + try + { + var query = db.T_captcha_check.AsQueryable(); + var msg = query.FirstOrDefault(n => n.captcha_alphabet == userInput); + bool isVali = (msg != null); + if(isVali) db.T_captcha_check.Remove(msg); + return isVali; + } + catch (Exception ex) + { + throw; + } + } } /// @@ -253,7 +301,7 @@ namespace asg_form.Controllers try { bool isCaptchaValid = ValidateCaptcha(newuser.captcha); - if (captchaNow == "7777") return BadRequest(new { code = 400, message = "服务器正忙" }); + if (captchaNow == "7777") return BadRequest(new { code = 503, message = "服务器正忙" }); if (!isCaptchaValid) { captchaNow = "7777"; diff --git a/asg_form/Controllers/menuAssignController.cs b/asg_form/Controllers/menuAssignController.cs index 5127761..5302e12 100644 --- a/asg_form/Controllers/menuAssignController.cs +++ b/asg_form/Controllers/menuAssignController.cs @@ -21,6 +21,7 @@ namespace asg_form.Controllers public string component { get; set; } public string allow_operate { get; set; } public string show { get; set; } + public int sort { get; set; } } public class menuInput { @@ -33,6 +34,7 @@ namespace asg_form.Controllers public string component { get; set; } public string allowOperate { get; set; } public string show { get; set; } + public int sort { get; set; } } private readonly UserManager userManager; @@ -67,6 +69,8 @@ namespace asg_form.Controllers component = msg.component, allow_operate = msg.allowOperate, show = msg.show, + sort = msg.sort, + }; db.mainMenu.Add(menu); db.SaveChanges(); @@ -82,6 +86,7 @@ namespace asg_form.Controllers mA.component = msg.component; mA.allow_operate = msg.allowOperate; mA.show = msg.show; + mA.sort = msg.sort; db.SaveChanges(); } return Ok(new error_mb { code = 200, message = "成功存入" }); @@ -109,8 +114,10 @@ namespace asg_form.Controllers { try { - db.Remove(db.mainMenu.FirstOrDefaultAsync(n => n.id==uid)); - await db.SaveChangesAsync(); + var menumessage = db.mainMenu.FirstOrDefault(n => n.id == uid); + if (menumessage != null) db.Remove(menumessage); + else return Ok(new error_mb { code = 400, message = "数据不存在" }); + db.SaveChanges(); return Ok(new error_mb { code = 200, message = "成功删除" }); } catch (Exception ex) @@ -130,27 +137,39 @@ namespace asg_form.Controllers public string component { get; set; } public string allowOperate { get; set; } public string show { get; set; } + public int sort { get; set; } = 1; public List children { get; set; } = new List(); } private List BuildTree(IEnumerable nodes, string? parentId) { - return nodes.Where(x => x.parent_id == parentId) - .Select(x => new menuFront - { - id = x.id, - path = x.path, - iconClass = x.icon_class, - title = x.title, - parentId = x.parent_id, - auth = x.auth, - component = x.component, - allowOperate = x.allow_operate, - show = x.show, - children = BuildTree(nodes, x.id) - }).ToList(); + try + { + return nodes.Where(x => x.parent_id == parentId) + .OrderBy(x => x.sort) + .Select(x => new menuFront + { + id = x.id, + path = x.path, + iconClass = x.icon_class, + title = x.title, + parentId = x.parent_id, + auth = x.auth, + component = x.component, + allowOperate = x.allow_operate, + show = x.show, + sort = x.sort, + children = BuildTree(nodes, x.id) + }).ToList(); + } + catch (Exception ex) + { + Console.WriteLine($"Error in BuildTree: {ex.Message}"); + throw; + } } + /// /// 查询菜单 ///