382 lines
12 KiB
C#
382 lines
12 KiB
C#
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.Http;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using SupplierManager.Models;
|
|||
|
|
using ViewModels;
|
|||
|
|
using ViewModels.Common;
|
|||
|
|
|
|||
|
|
namespace SupplierManager.Controllers
|
|||
|
|
{
|
|||
|
|
[Route("api/[controller]/[action]")]
|
|||
|
|
[ApiController]
|
|||
|
|
public class UsersController : ControllerBase
|
|||
|
|
{
|
|||
|
|
public class duser
|
|||
|
|
{
|
|||
|
|
public int Id { get; set; }
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 删除用户
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="LLL"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpPost()]
|
|||
|
|
[Authorize()]
|
|||
|
|
public ReturnInfo DelUser([FromBody] duser LLL)
|
|||
|
|
{
|
|||
|
|
ReturnInfo returnInfo = new();
|
|||
|
|
if(LLL.Id == 2 || LLL.Id == 1)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = "超级管理员不可删除!";
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var q = new AgentApprovalSystemContext())
|
|||
|
|
{
|
|||
|
|
var FFF = q.AasUsers.FirstOrDefault(A => A.Id == LLL.Id);
|
|||
|
|
if (FFF != null)
|
|||
|
|
{
|
|||
|
|
FFF.Isdelete = true;
|
|||
|
|
q.AasUsers.Update(FFF);
|
|||
|
|
q.SaveChanges();
|
|||
|
|
returnInfo.isok = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 修改用户
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="LLL"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpPost()]
|
|||
|
|
[Authorize()]
|
|||
|
|
public ReturnInfo EditUser([FromBody] ReturnUser LLL)
|
|||
|
|
{
|
|||
|
|
ReturnInfo returnInfo = new ReturnInfo();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var q = new AgentApprovalSystemContext())
|
|||
|
|
{
|
|||
|
|
var lll = q.AasUsers.SingleOrDefault(A => A.Id == LLL.Id);
|
|||
|
|
if (lll != null)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
string username = LLL.Username;
|
|||
|
|
int? companyId = LLL.ComId;
|
|||
|
|
string mobile = LLL.Mobile;
|
|||
|
|
string weiXin = LLL.Weixin;
|
|||
|
|
string email = LLL.Email;
|
|||
|
|
|
|||
|
|
lll.Realname = LLL.Realname;
|
|||
|
|
lll.RoleId = LLL.RoleId;
|
|||
|
|
lll.Position = LLL.Position;
|
|||
|
|
if (!string.IsNullOrWhiteSpace(LLL.Avatar))
|
|||
|
|
{
|
|||
|
|
lll.Avatar = "Uploads/face/" + LLL.Avatar;
|
|||
|
|
}
|
|||
|
|
lll.Username = username;
|
|||
|
|
lll.ComId = companyId;
|
|||
|
|
lll.Mobile = mobile;
|
|||
|
|
lll.Weixin = weiXin;
|
|||
|
|
lll.Email = email;
|
|||
|
|
lll.UpdateTime = DateTime.Now;
|
|||
|
|
|
|||
|
|
string PPP = LLL.Password.Trim();
|
|||
|
|
if (!string.IsNullOrWhiteSpace(PPP))
|
|||
|
|
{
|
|||
|
|
lll.Password = Tools.HashPassword(PPP);
|
|||
|
|
lll.PswEncryption = Tools.EncryptString(PPP);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
q.AasUsers.Update(lll);
|
|||
|
|
q.SaveChanges();
|
|||
|
|
returnInfo.isok = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 重置密码
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="LLL"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpPost()]
|
|||
|
|
[Authorize()]
|
|||
|
|
public ReturnInfo ModifyPassWord([FromBody] PWD_Reset LLL)
|
|||
|
|
{
|
|||
|
|
ReturnInfo returnInfo = new ReturnInfo();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var q = new AgentApprovalSystemContext())
|
|||
|
|
{
|
|||
|
|
var QQQ = q.AasUsers.SingleOrDefault(A => A.Id == LLL.Id);
|
|||
|
|
if (QQQ != null)
|
|||
|
|
{
|
|||
|
|
QQQ.Password = Tools.HashPassword(LLL.PlaintextPwd);
|
|||
|
|
QQQ.PswEncryption = Tools.EncryptString(LLL.PlaintextPwd);
|
|||
|
|
q.AasUsers.Update(QQQ);
|
|||
|
|
q.SaveChanges();
|
|||
|
|
returnInfo.isok = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 新增密码为123456
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="LLL"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpPost()]
|
|||
|
|
[Authorize()]
|
|||
|
|
public ReturnInfo ResetPassWord([FromBody] PWD_Reset LLL)
|
|||
|
|
{
|
|||
|
|
ReturnInfo returnInfo = new ReturnInfo();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var q = new AgentApprovalSystemContext())
|
|||
|
|
{
|
|||
|
|
var Q = q.AasUsers.SingleOrDefault(A => A.Id == LLL.Id);
|
|||
|
|
if (Q != null)
|
|||
|
|
{
|
|||
|
|
Q.Password = Tools.HashPassword("123456");
|
|||
|
|
Q.PswEncryption = Tools.EncryptString("123456");
|
|||
|
|
q.AasUsers.Update(Q);
|
|||
|
|
q.SaveChanges();
|
|||
|
|
returnInfo.isok = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取用户信息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="S"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpPost()]
|
|||
|
|
[Authorize()]
|
|||
|
|
public ReturnInfo GetUserInfo([FromBody] QueryAll_Or_Single S)
|
|||
|
|
{
|
|||
|
|
ReturnInfo returnInfo = new ReturnInfo();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var q = new AgentApprovalSystemContext())
|
|||
|
|
{
|
|||
|
|
if (S.IsAll)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = true;
|
|||
|
|
returnInfo.response = q.AasUsers.Where(A=>!A.Isdelete).Select(F => new ReturnUser
|
|||
|
|
{
|
|||
|
|
Id = F.Id,
|
|||
|
|
Username = F.Username,
|
|||
|
|
Realname = F.Realname,
|
|||
|
|
RoleId = F.RoleId,
|
|||
|
|
Email = F.Email,
|
|||
|
|
Weixin = F.Weixin,
|
|||
|
|
Position = F.Position,
|
|||
|
|
Avatar = F.Avatar,
|
|||
|
|
ComId = F.ComId,
|
|||
|
|
Mobile = F.Mobile,
|
|||
|
|
}).ToList();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = true;
|
|||
|
|
var a = q.AasUsers.SingleOrDefault(A => A.Id == S.ID);
|
|||
|
|
if (a != null)
|
|||
|
|
{
|
|||
|
|
ReturnUser u = new ReturnUser();
|
|||
|
|
u.Id = a.Id;
|
|||
|
|
u.Username = a.Username;
|
|||
|
|
u.Realname = a.Realname;
|
|||
|
|
u.RoleId = a.RoleId;
|
|||
|
|
u.ComId = a.ComId;
|
|||
|
|
u.Mobile = a.Mobile;
|
|||
|
|
u.Weixin = a.Weixin;
|
|||
|
|
u.Position = a.Position;
|
|||
|
|
u.Avatar = a.Avatar;
|
|||
|
|
u.Email = a.Email;
|
|||
|
|
returnInfo.response = u;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 新增用户
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="LLL"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpPost()]
|
|||
|
|
//[Authorize()]
|
|||
|
|
public ReturnInfo AddUser([FromBody] ReturnUser LLL)
|
|||
|
|
{
|
|||
|
|
ReturnInfo returnInfo = new ReturnInfo();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
string username = LLL.Username;
|
|||
|
|
int? companyId = LLL.ComId;
|
|||
|
|
string mobile = LLL.Mobile;
|
|||
|
|
string weiXin = LLL.Weixin;
|
|||
|
|
string email = LLL.Email;
|
|||
|
|
|
|||
|
|
AasUser lll = new AasUser();
|
|||
|
|
lll.Username = username;
|
|||
|
|
lll.Realname = LLL.Realname;
|
|||
|
|
lll.ComId = LLL.ComId;
|
|||
|
|
lll.RoleId = LLL.RoleId;
|
|||
|
|
lll.Position = LLL.Position;
|
|||
|
|
lll.Weixin = LLL.Weixin;
|
|||
|
|
lll.Email = email;
|
|||
|
|
lll.Mobile = mobile;
|
|||
|
|
if (!string.IsNullOrWhiteSpace(LLL.Avatar))
|
|||
|
|
{
|
|||
|
|
lll.Avatar = "Uploads/face/" + LLL.Avatar;
|
|||
|
|
}
|
|||
|
|
lll.Isdelete = false;
|
|||
|
|
|
|||
|
|
lll.CreationTime = DateTime.Now;
|
|||
|
|
lll.UpdateTime = DateTime.Now;
|
|||
|
|
|
|||
|
|
using (var q = new AgentApprovalSystemContext())
|
|||
|
|
{
|
|||
|
|
var Q = q.AasUsers.Where(A => A.Username.Equals(username));
|
|||
|
|
if (Q.Count() > 0)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = "此用户名已经存在";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//lll.Password = Tools.HashPassword("123456");
|
|||
|
|
//lll.PswEncryption = Tools.EncryptString("123456");
|
|||
|
|
|
|||
|
|
lll.Password = Tools.HashPassword(LLL.Password.Trim());
|
|||
|
|
lll.PswEncryption = Tools.EncryptString(LLL.Password.Trim());
|
|||
|
|
q.AasUsers.Add(lll);
|
|||
|
|
returnInfo.isok = true;
|
|||
|
|
}
|
|||
|
|
q.SaveChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
returnInfo.isok = false;
|
|||
|
|
returnInfo.message = ex.Message;
|
|||
|
|
}
|
|||
|
|
return returnInfo;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 返回用户信息
|
|||
|
|
/// </summary>
|
|||
|
|
public class ReturnUser
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 主键
|
|||
|
|
/// </summary>
|
|||
|
|
public int Id { get; set; } = 0;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 用户名(登录名)
|
|||
|
|
/// </summary>
|
|||
|
|
public string? Username { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 真实姓名
|
|||
|
|
/// </summary>
|
|||
|
|
public string? Realname { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 所属公司ID
|
|||
|
|
/// </summary>
|
|||
|
|
public int? ComId { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 角色ID
|
|||
|
|
/// </summary>
|
|||
|
|
public int? RoleId { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 职位
|
|||
|
|
/// </summary>
|
|||
|
|
public string? Position { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 密码
|
|||
|
|
/// </summary>
|
|||
|
|
public string Password { get; set; } = "";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 密码加密处理
|
|||
|
|
/// </summary>
|
|||
|
|
public string PswEncryption { get; set; } = "";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 微信号
|
|||
|
|
/// </summary>
|
|||
|
|
public string? Weixin { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 邮箱
|
|||
|
|
/// </summary>
|
|||
|
|
public string? Email { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 电话号码
|
|||
|
|
/// </summary>
|
|||
|
|
public string? Mobile { get; set; }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 账户头像
|
|||
|
|
/// </summary>
|
|||
|
|
public string? Avatar { get; set; }
|
|||
|
|
}
|
|||
|
|
public class PWD_Reset
|
|||
|
|
{
|
|||
|
|
public int Id { get; set; }
|
|||
|
|
public string PlaintextPwd { get; set; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|