2024-08-03 20:40:34 +08:00
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Net.Mail;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Security.Claims;
|
2024-08-19 16:37:40 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
2024-08-03 20:40:34 +08:00
|
|
|
|
|
|
|
|
|
namespace asg_form.Controllers
|
|
|
|
|
{
|
2025-01-27 20:32:23 +08:00
|
|
|
|
|
|
|
|
|
|
2025-02-09 19:17:34 +08:00
|
|
|
|
|
2024-08-03 20:40:34 +08:00
|
|
|
|
public class news : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private readonly RoleManager<Role> roleManager;
|
|
|
|
|
private readonly UserManager<User> userManager;
|
|
|
|
|
public news(
|
|
|
|
|
RoleManager<Role> roleManager, UserManager<User> userManager)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
this.roleManager = roleManager;
|
|
|
|
|
this.userManager = userManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过密码获得管理员
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="password">密码</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[Route("api/v1/getadmin/")]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<ActionResult<string>> Post(string password)
|
|
|
|
|
{
|
|
|
|
|
if (password == "luolanzuishuai")
|
|
|
|
|
{
|
|
|
|
|
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
|
|
|
|
var user = await userManager.FindByIdAsync(id);
|
|
|
|
|
|
|
|
|
|
await userManager.AddToRoleAsync(user, "admin");
|
|
|
|
|
await userManager.AddToRoleAsync(user, "nbadmin");
|
|
|
|
|
return "ok";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("无权访问!");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获得所有新闻
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[Route("api/v1/news/")]
|
|
|
|
|
[HttpGet]
|
2024-08-19 16:37:40 +08:00
|
|
|
|
public async Task<ActionResult<List<T_news>>> getnews([FromQuery] string type = null)
|
2024-08-03 20:40:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TestDbContext test =new TestDbContext();
|
2024-08-19 16:37:40 +08:00
|
|
|
|
|
|
|
|
|
var query = test.news.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(type))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(n => n.Type == type);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 15:55:25 +08:00
|
|
|
|
return query.OrderBy(a => a.Id).ToList();
|
2024-08-03 20:40:34 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[Route("api/v1/admin/news/")]
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
public async Task<ActionResult<string>> delnews(long newid)
|
|
|
|
|
{
|
|
|
|
|
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
|
|
|
|
var user = await userManager.FindByIdAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool a = await userManager.IsInRoleAsync(user, "admin");
|
|
|
|
|
if (a)
|
|
|
|
|
{
|
|
|
|
|
TestDbContext ctx = new TestDbContext();
|
|
|
|
|
T_news delnew= ctx.news.FirstOrDefault(a => a.Id == newid);
|
|
|
|
|
ctx.news.Remove(delnew);
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
return "ok";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "无权访问";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发布新闻
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="req_News">新闻内容</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[Route("api/v1/admin/news/")]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<ActionResult<string>> Post([FromBody]req_news req_News)
|
|
|
|
|
{
|
|
|
|
|
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
|
|
|
|
var user = await userManager.FindByIdAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool a = await userManager.IsInRoleAsync(user, "admin");
|
|
|
|
|
if (a)
|
|
|
|
|
{
|
2024-11-11 08:58:59 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-12-07 23:57:41 +08:00
|
|
|
|
using (TestDbContext ctx = new TestDbContext())
|
|
|
|
|
{
|
|
|
|
|
ctx.news.Add(new T_news { Title = req_News.Title, msg = req_News.msg, Type = req_News.Type, FormName = user.chinaname, time = DateTime.Now.ToString() });
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
return Ok(new TReturn() { code=200,msg="添加成功!"});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 08:58:59 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return Ok(new { code = 500, message = "服务器错误", ex });
|
|
|
|
|
}
|
2024-08-03 20:40:34 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "无权访问";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 17:29:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 修改新闻
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="req_News">新闻内容</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[Route("api/v1/admin/news/")]
|
|
|
|
|
[HttpPut]
|
|
|
|
|
public async Task<ActionResult<string>> Put([FromBody] req_news req_News,long newsid)
|
|
|
|
|
{
|
|
|
|
|
string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
|
|
|
|
var user = await userManager.FindByIdAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool a = await userManager.IsInRoleAsync(user, "admin");
|
|
|
|
|
if (a)
|
|
|
|
|
{
|
|
|
|
|
using (TestDbContext ctx = new TestDbContext())
|
|
|
|
|
{
|
2024-11-11 08:58:59 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-19 16:37:40 +08:00
|
|
|
|
var qwq= await ctx.news.FindAsync(newsid);
|
2024-08-19 16:19:38 +08:00
|
|
|
|
if (qwq == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound("News item not found or type mismatch.");
|
|
|
|
|
}
|
2024-08-11 17:29:12 +08:00
|
|
|
|
qwq.msg=req_News.msg;
|
|
|
|
|
qwq.Title=req_News.Title;
|
|
|
|
|
qwq.FormName = user.UserName;
|
2024-08-18 11:19:30 +08:00
|
|
|
|
qwq.Type = req_News.Type;
|
2024-11-11 08:58:59 +08:00
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return Ok(new { code = 500, message = "服务器错误", ex });
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 17:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "ok!";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "无权访问";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-03 20:40:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 17:29:12 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class T_news
|
2024-08-03 20:40:34 +08:00
|
|
|
|
{
|
|
|
|
|
public long Id { get; set; }
|
|
|
|
|
public string Title { get; set; }
|
|
|
|
|
public string FormName { get; set; }
|
2024-11-11 08:58:59 +08:00
|
|
|
|
public string? time { get; set; }
|
2024-08-03 20:40:34 +08:00
|
|
|
|
|
|
|
|
|
public string msg { get; set; }
|
2024-08-18 11:19:30 +08:00
|
|
|
|
public string Type { get; set; }
|
|
|
|
|
}
|
2024-08-03 20:40:34 +08:00
|
|
|
|
|
|
|
|
|
public class req_news {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 标题
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Title { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 内容,推荐使用markdown格式
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string msg { get; set; }
|
2024-08-18 11:19:30 +08:00
|
|
|
|
public string Type { get; set; }
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-03 20:40:34 +08:00
|
|
|
|
|