7
This commit is contained in:
		
							parent
							
								
									61e8f98af8
								
							
						
					
					
						commit
						7f85b4dac7
					
				@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using NPOI.OpenXmlFormats.Dml;
 | 
			
		||||
using System.Security.Claims;
 | 
			
		||||
using static allteam1;
 | 
			
		||||
using static asg_form.Controllers.menuAssignController;
 | 
			
		||||
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
 | 
			
		||||
 | 
			
		||||
namespace asg_form.Controllers.Team
 | 
			
		||||
@ -206,17 +207,53 @@ namespace asg_form.Controllers.Team
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public class TeamFind
 | 
			
		||||
        public class tmpRole 
 | 
			
		||||
        {
 | 
			
		||||
            public List<string> roleId { get; set; }
 | 
			
		||||
            public long formId {  get; set; }
 | 
			
		||||
            public string teamName { get; set; }
 | 
			
		||||
            public List<string> roles { get; set; }
 | 
			
		||||
            public int voteOfLikes { get; set; }
 | 
			
		||||
            public List<string>? roleLin {  get; set; }
 | 
			
		||||
            public List<string>? commonRoles { get; set; }
 | 
			
		||||
            public string? roleName { get; set; }
 | 
			
		||||
            public int? roleRank { get; set; }
 | 
			
		||||
            public string? commonRoles { get; set; }
 | 
			
		||||
            public string? roleLin { 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")]
 | 
			
		||||
        [HttpGet]
 | 
			
		||||
        public async Task<ActionResult<object>> GetFormById(short page, short limit, int sort, int eventId)
 | 
			
		||||
@ -225,57 +262,55 @@ namespace asg_form.Controllers.Team
 | 
			
		||||
            {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    var query = from m in db.Forms
 | 
			
		||||
                                where m.events.Id == eventId
 | 
			
		||||
                                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)
 | 
			
		||||
                    var query = db.Forms.Where(f => f.events.Id == eventId);
 | 
			
		||||
                    query = sort switch
 | 
			
		||||
                    {
 | 
			
		||||
                        case 0:
 | 
			
		||||
                            query = query.OrderByDescending(m => m.piaoshu).ThenByDescending(m => m.time);
 | 
			
		||||
                            break;
 | 
			
		||||
                        case 1: 
 | 
			
		||||
                            query = query.OrderBy(m => m.piaoshu);
 | 
			
		||||
                            break;
 | 
			
		||||
                        case 2: 
 | 
			
		||||
                            query = query.OrderByDescending(m => m.piaoshu);
 | 
			
		||||
                            break;
 | 
			
		||||
                        case 3:
 | 
			
		||||
                            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
 | 
			
		||||
                        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"))
 | 
			
		||||
                    {
 | 
			
		||||
                        try
 | 
			
		||||
                        {
 | 
			
		||||
                            formId = g.Key.Id,
 | 
			
		||||
                            teamName = g.Key.team_name,
 | 
			
		||||
                            roles = g.Select(x => x.role_name).ToList(),
 | 
			
		||||
                            voteOfLikes = g.Key.piaoshu,
 | 
			
		||||
                            roleLin = g.Select(x => x.role_lin).ToList(), // 选择第一个roleLin
 | 
			
		||||
                            commonRoles = g.Select(x => x.Common_Roles).ToList(), // 选择第一个commonRoles
 | 
			
		||||
                            roleId = g.Select(x => x.role_id).ToList(), // 选择第一个roleId
 | 
			
		||||
                        })
 | 
			
		||||
                            var results = await query
 | 
			
		||||
                                .Skip((page - 1) * limit)
 | 
			
		||||
                                .Take(limit)
 | 
			
		||||
                                .Select(f => new AdminTeamFind
 | 
			
		||||
                                {
 | 
			
		||||
                                    form = f,
 | 
			
		||||
                                    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)
 | 
			
		||||
                        .Take(limit)
 | 
			
		||||
                        .Select(f => new
 | 
			
		||||
                        {
 | 
			
		||||
                            formId = f.Id,
 | 
			
		||||
                            teamName = f.team_name,
 | 
			
		||||
                            voteOfLikes = f.piaoshu,
 | 
			
		||||
                            roles = findRolesByFormId(f.Id)
 | 
			
		||||
                        })
 | 
			
		||||
                        .ToListAsync();
 | 
			
		||||
                    int total = results.Count();
 | 
			
		||||
                    return Ok(new { code = 200, message = "成功", data = results,total });
 | 
			
		||||
                        int total = await query.CountAsync();
 | 
			
		||||
                        return Ok(new { code = 200, message = "成功", data = results, total });
 | 
			
		||||
                    }                        
 | 
			
		||||
                    
 | 
			
		||||
                }
 | 
			
		||||
                catch (Exception ex)
 | 
			
		||||
                {
 | 
			
		||||
@ -284,5 +319,7 @@ namespace asg_form.Controllers.Team
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -228,13 +228,14 @@ namespace asg_form.Controllers
 | 
			
		||||
            public string? captcha_alphabet { get; set; }
 | 
			
		||||
            public int? captcha_number { get; set; }
 | 
			
		||||
            public string off_time { get; set; }
 | 
			
		||||
            public string uid { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 验证码生成
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        [Route("api/v2/makeCaptcha")]
 | 
			
		||||
        [HttpGet]
 | 
			
		||||
        public async Task<ActionResult<object> >GetCaptcha()
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
        public async Task<ActionResult<object>> GetCaptcha([FromBody]string uid)
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
@ -253,6 +254,7 @@ namespace asg_form.Controllers
 | 
			
		||||
                        id = maxId + 1,
 | 
			
		||||
                        captcha_alphabet = captchaCode,
 | 
			
		||||
                        off_time = DateTime.Now.ToString(),
 | 
			
		||||
                        uid = uid,
 | 
			
		||||
                    };
 | 
			
		||||
                    db.Add(msg);
 | 
			
		||||
                    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())
 | 
			
		||||
            {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    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);
 | 
			
		||||
                    if(isVali) db.T_captcha_check.Remove(msg);
 | 
			
		||||
                    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>
 | 
			
		||||
@ -296,14 +316,13 @@ namespace asg_form.Controllers
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                bool isCaptchaValid = ValidateCaptcha(newuser.captcha);
 | 
			
		||||
                bool isCaptchaValid = ValidateCaptcha(newuser.uid,newuser.captcha);
 | 
			
		||||
                if (captchaNow == "7777") return BadRequest(new { code = 503, message = "服务器正忙" });
 | 
			
		||||
                if (!isCaptchaValid)
 | 
			
		||||
                {
 | 
			
		||||
                    captchaNow = "7777";
 | 
			
		||||
                    moveOutTimeData(newuser.uid);
 | 
			
		||||
                    return BadRequest(new { code = 400, message = "验证码无效" });
 | 
			
		||||
                }
 | 
			
		||||
                captchaNow = "7777";
 | 
			
		||||
                var maxId = await userManager.Users.MaxAsync(u => u.Id);
 | 
			
		||||
                User? user = await userManager.FindByNameAsync(newuser.userName);
 | 
			
		||||
                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 captcha);
 | 
			
		||||
        public record AddUserReq(string userName, string password, string chinaname, string captcha,string uid);
 | 
			
		||||
 | 
			
		||||
        [Route("api/v1/setimg")]
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user