Files
Web_SupplierManager_Prod/SupplierManager/Controllers/WyUsersController.cs
2025-11-20 09:14:00 +08:00

144 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SupplierManager.Models;
using ViewModels;
namespace SupplierManager.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class WyUsersController : ControllerBase
{
private readonly AgentApprovalSystemContext _context;
public WyUsersController(AgentApprovalSystemContext context)
{
_context = context;
}
// GET: api/WyUsers
[HttpPost()]
public async Task<ReturnInfo> GetUserInfo([FromBody] QueryAll_Or_Single S)
{
ReturnInfo r = new ReturnInfo();
try
{
r.isok = true;
r.response= await _context.WyUsers.ToListAsync();
}
catch (Exception ex)
{
r.isok = false;
r.message=ex.Message;
}
return r;
}
[HttpPost()]
[Authorize()]
public async Task<ReturnInfo> EditWyUser(WyUser wyUser)
{
ReturnInfo r = new ReturnInfo();
try
{
if (!WyUserExists(wyUser.Id))
{
r.isok = false;
r.message = "此用户不存在";
}
else
{
_context.Entry(wyUser).State = EntityState.Modified;
await _context.SaveChangesAsync();
r.isok = true;
}
}
catch (Exception ex)
{
r.isok = false;
r.message = ex.Message;
}
string Introduced = JsonSerializer.Serialize(wyUser);
string ReturnValue = JsonSerializer.Serialize(r);
await LogRecord(Introduced, ReturnValue);
return r;
}
private string GetIP()
{
string? ip = string.Empty;
string? NNN = HttpContext?.Request?.HttpContext?.Connection?.RemoteIpAddress?.ToString();
if (string.IsNullOrEmpty(NNN))
{
ip = HttpContext?.Request.Headers["HTTP_X_FORWARDED_FOR"];
}
else
{
ip = NNN;
}
return ip;
}
[HttpPost]
[Authorize()]
public async Task<ReturnInfo> AddWyUser(List<WyUser> wyUser)
{
ReturnInfo r = new ReturnInfo();
try
{
_context.WyUsers.AddRange(wyUser);
await _context.SaveChangesAsync();
r.isok = true;
}
catch (Exception ex)
{
r.isok = false;
r.message = ex.Message;
}
string Introduced = JsonSerializer.Serialize(wyUser);
string ReturnValue = JsonSerializer.Serialize(r);
await LogRecord(Introduced, ReturnValue);
return r;
}
private async Task LogRecord(string Introduced, string ReturnValue)
{
try
{
WyLog wx = new WyLog();
wx.Ip = GetIP();
wx.Operation = "Edit";
wx.Introduced = Introduced;
wx.ReturnValue = ReturnValue;
wx.CreationTime = Tools.ToUnixTimestampBySeconds(DateTime.Now);
_context.WyLogs.Add(wx);
await _context.SaveChangesAsync();
}
catch (Exception)
{
}
}
private bool WyUserExists(long id)
{
return _context.WyUsers.Any(e => e.Id == id);
}
}
}