This commit is contained in:
王炜翔 2024-11-03 15:27:27 +08:00
parent 1368e68b1b
commit 3ad96bcce9
4 changed files with 96 additions and 27 deletions

View File

@ -13,6 +13,7 @@ using System.Security.Cryptography;
using System.Text; using System.Text;
using static asg_form.Controllers.Budget.BgCountController; using static asg_form.Controllers.Budget.BgCountController;
using static asg_form.Controllers.InviteReferee; using static asg_form.Controllers.InviteReferee;
using static asg_form.Controllers.login;
using static asg_form.Controllers.menuAssignController; using static asg_form.Controllers.menuAssignController;
namespace asg_form.Controllers namespace asg_form.Controllers
@ -287,6 +288,7 @@ namespace asg_form.Controllers
public DbSet<InviteBg> T_Invitation { get; set; } public DbSet<InviteBg> T_Invitation { get; set; }
public DbSet<menuDB> mainMenu { get; set; } public DbSet<menuDB> mainMenu { get; set; }
public DbSet<capData> T_captcha_check { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
string connStr = @"Host=localhost;Port=2345;Database=asg;Username=postgres;Password=luolan12323;"; string connStr = @"Host=localhost;Port=2345;Database=asg;Username=postgres;Password=luolan12323;";

View File

@ -261,8 +261,8 @@ namespace asg_form.Controllers
else else
{ {
a.cout = userManager.Users.Where(a => a.chinaname == keyword || a.UserName == keyword || a.Email == keyword).Count(); 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 == 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.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); return Ok(a);
} }

View File

@ -18,6 +18,8 @@ using System.Security.Authentication;
using Flurl.Http; using Flurl.Http;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Manganese.Array;
namespace asg_form.Controllers namespace asg_form.Controllers
{ {
@ -220,25 +222,71 @@ namespace asg_form.Controllers
public string captchaNow = "7777"; 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; }
}
/// <summary> /// <summary>
/// 验证码生成 /// 验证码生成
/// </summary> /// </summary>
[Route("api/v2/makeCaptcha")] [Route("api/v2/makeCaptcha")]
[HttpGet] [HttpGet]
public ActionResult<(string captchaImage, string captchaCode)> GetCaptcha() public async Task<ActionResult<(string captchaImage, string captchaCode)> >GetCaptcha()
{ {
var captchaService = new CaptchaService(); try
var (captchaImage, captchaCode) = captchaService.GenerateCaptcha(); {
var captchaService = new CaptchaService();
captchaNow=captchaCode; var (captchaImage, captchaCode) = captchaService.GenerateCaptcha();
captchaNow=captchaCode;
return Ok(new { captchaImage, 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) 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;
}
}
} }
/// <summary> /// <summary>
@ -253,7 +301,7 @@ namespace asg_form.Controllers
try try
{ {
bool isCaptchaValid = ValidateCaptcha(newuser.captcha); 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) if (!isCaptchaValid)
{ {
captchaNow = "7777"; captchaNow = "7777";

View File

@ -21,6 +21,7 @@ namespace asg_form.Controllers
public string component { get; set; } public string component { get; set; }
public string allow_operate { get; set; } public string allow_operate { get; set; }
public string show { get; set; } public string show { get; set; }
public int sort { get; set; }
} }
public class menuInput public class menuInput
{ {
@ -33,6 +34,7 @@ namespace asg_form.Controllers
public string component { get; set; } public string component { get; set; }
public string allowOperate { get; set; } public string allowOperate { get; set; }
public string show { get; set; } public string show { get; set; }
public int sort { get; set; }
} }
private readonly UserManager<User> userManager; private readonly UserManager<User> userManager;
@ -67,6 +69,8 @@ namespace asg_form.Controllers
component = msg.component, component = msg.component,
allow_operate = msg.allowOperate, allow_operate = msg.allowOperate,
show = msg.show, show = msg.show,
sort = msg.sort,
}; };
db.mainMenu.Add(menu); db.mainMenu.Add(menu);
db.SaveChanges(); db.SaveChanges();
@ -82,6 +86,7 @@ namespace asg_form.Controllers
mA.component = msg.component; mA.component = msg.component;
mA.allow_operate = msg.allowOperate; mA.allow_operate = msg.allowOperate;
mA.show = msg.show; mA.show = msg.show;
mA.sort = msg.sort;
db.SaveChanges(); db.SaveChanges();
} }
return Ok(new error_mb { code = 200, message = "成功存入" }); return Ok(new error_mb { code = 200, message = "成功存入" });
@ -109,8 +114,10 @@ namespace asg_form.Controllers
{ {
try try
{ {
db.Remove(db.mainMenu.FirstOrDefaultAsync(n => n.id==uid)); var menumessage = db.mainMenu.FirstOrDefault(n => n.id == uid);
await db.SaveChangesAsync(); 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 = "成功删除" }); return Ok(new error_mb { code = 200, message = "成功删除" });
} }
catch (Exception ex) catch (Exception ex)
@ -130,27 +137,39 @@ namespace asg_form.Controllers
public string component { get; set; } public string component { get; set; }
public string allowOperate { get; set; } public string allowOperate { get; set; }
public string show { get; set; } public string show { get; set; }
public int sort { get; set; } = 1;
public List<menuFront> children { get; set; } = new List<menuFront>(); public List<menuFront> children { get; set; } = new List<menuFront>();
} }
private List<menuFront> BuildTree(IEnumerable<menuDB> nodes, string? parentId) private List<menuFront> BuildTree(IEnumerable<menuDB> nodes, string? parentId)
{ {
return nodes.Where(x => x.parent_id == parentId) try
.Select(x => new menuFront {
{ return nodes.Where(x => x.parent_id == parentId)
id = x.id, .OrderBy(x => x.sort)
path = x.path, .Select(x => new menuFront
iconClass = x.icon_class, {
title = x.title, id = x.id,
parentId = x.parent_id, path = x.path,
auth = x.auth, iconClass = x.icon_class,
component = x.component, title = x.title,
allowOperate = x.allow_operate, parentId = x.parent_id,
show = x.show, auth = x.auth,
children = BuildTree(nodes, x.id) component = x.component,
}).ToList(); 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;
}
} }
/// <summary> /// <summary>
/// 查询菜单 /// 查询菜单
/// </summary> /// </summary>