You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

420 lines
13 KiB
C#

using Admin.Core.Model;
using Admin.Core.Common.Resource;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Admin.Core.Common;
using System.Linq;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using System.IO;
using Admin.Core.IService.ISys;
using Admin.Core.Model.Sys;
namespace Admin.Core.Api
{
/// <summary>
/// SysUserController
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysUserController : BaseApiUserController
{
/// <summary>
/// _sysUserService
/// </summary>
private readonly ISysUserRoleService _sysUserRoleService;
private readonly ISysRoleService _sysRoleService;
private readonly ISysPostService _sysPostService;
private readonly ISysUserPostService _sysUserPostService;
private readonly ISysDeptService _sysDeptService;
private readonly IMapper _mapper;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="sysUserService"></param>
/// <param name="sysUserRoleService"></param>
/// <param name="sysRoleService"></param>
/// <param name="sysPostService"></param>
/// <param name="sysUserPostService"></param>
/// <param name="sysDeptService"></param>
/// <param name="mapper"></param>
public SysUserController(ISysUserService sysUserService, ISysUserRoleService sysUserRoleService, ISysRoleService sysRoleService, ISysPostService sysPostService,
ISysUserPostService sysUserPostService, ISysDeptService sysDeptService, IMapper mapper) : base(sysUserService)
{
_sysUserService = sysUserService;
_sysUserRoleService = sysUserRoleService;
_sysRoleService = sysRoleService;
_sysPostService = sysPostService;
_sysUserPostService = sysUserPostService;
_sysDeptService = sysDeptService;
_mapper = mapper;
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="pageQuery"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<PageModel<UserModel>>> GetByPage([FromBody] PageQuery<UserQuery> pageQuery)
{
if (!pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery.Query = new UserQuery();
}
var model = await _sysUserService.SelectUserList(pageQuery);
return SuccessPage(model);
}
/// <summary>
/// 获取所有用户
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<List<SysUser>>> GetAllUser(string roleKey = "")
{
return Success(await _sysUserRoleService.GetAllUser(roleKey));
}
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<UserView>> GetByID(int id)
{
return Success(await _sysUserService.GetByID(id));
}
/// <summary>
/// 新增一条数据
/// </summary>
/// <param name="request">SysUser</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<int>> Add([FromBody] UserView request)
{
MessageModel<int> data = new MessageModel<int>();
var exist = await _sysUserService.CheckLoginNameUnique(request.SysUser.LoginName);
if (exist)
{
data.success = false;
data.msg = "账号已存在!";
return data;
}
request.SysUser.CreateBy = CurrentUser.LoginName;
request.SysUser.UpdateBy = CurrentUser.LoginName;
data.data = await _sysUserService.InsertUser(request);
if (data.data > 0)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 更新一条数据
/// </summary>
/// <param name="request">SysUser</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Update([FromBody] UserView request)
{
var data = new MessageModel<bool>();
if (UserView.IsAdmin(request.SysUser.UserID))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
request.SysUser.UpdateBy = CurrentUser.LoginName;
data.data = await _sysUserService.UpdateUser(request);
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="ids">主键</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Del(List<int> ids)
{
var data = new MessageModel<bool>();
foreach (var userId in ids)
{
if (UserView.IsAdmin(userId))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
}
data.data = await _sysUserService.DeleteUserByIds(ids.ToArray(), CurrentUser.LoginName);
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 修改用户状态
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> ChangeStatus(SysUser user)
{
var data = new MessageModel<bool>();
if (UserView.IsAdmin(user.UserID))
{
data.success = false;
data.msg = Resource_SysBase.OprateAdmin;
return data;
}
user.UpdateBy = CurrentUser.LoginName;
data.data = await _sysUserService.UpdateUserStatus(user);
if (data.data)
{
data.success = true;
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.success = false;
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 用户数据导出
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<string>> Export(SysUser user)
{
Expression<Func<SysUser, bool>> whereExpression = a => true;
var list = await _sysUserService.QueryAsync(whereExpression);
return Success<string>(ExcelHelper.ExportToList(list, "用户数据"));
}
/// <summary>
/// 用户数据导入
/// </summary>
/// <param name="file"></param>
/// <param name="updateSupport"></param>
/// <returns></returns>
[HttpPost]
public MessageModel<bool> ImportData(string file, bool updateSupport)
{
return Success(true);
}
/// <summary>
/// 用户数据导入模板
/// </summary>
/// <returns></returns>
[HttpGet]
public MessageModel<string> ImportTemplate()
{
return Success<string>(string.Empty);
}
/// <summary>
/// 根据用户编号获取详细信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<UserView>> GetInfo(int userId)
{
return Success(await _sysUserService.GetInfo(userId));
}
/// <summary>
/// 重置密码
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<string>> ResetPwd(SysUser user)
{
var data = new MessageModel<string>();
user.UpdateBy = CurrentUser.LoginName;
data.success = await _sysUserService.ResetPwd(user);
if (data.success)
{
data.msg = Resource_SysBase.OprateSuccess;
}
else
{
data.msg = Resource_SysBase.OprateFail;
}
return data;
}
/// <summary>
/// 根据用户编号获取授权角色
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<UserView>> AuthRole(int userId)
{
return Success(await _sysUserService.AuthRole(userId));
}
/// <summary>
/// 查询用户已分配角色列表
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<UserView>> AllocatedList(int userId)
{
return Success(await _sysUserService.AllocatedList(userId));
}
/// <summary>
/// 查询用户未分配角色列表
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<UserView>> UnallocatedList(int userId)
{
return Success(await _sysUserService.UnallocatedList(userId));
}
/// <summary>
/// 用户授权角色
/// </summary>
/// <param name="view"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> InsertAuthRole(UserRoleView view)
{
return Success(await _sysUserService.InsertUserAuth(view.UserID, view.RoleIds.ToArray()));
}
/// <summary>
/// 个人信息
/// </summary>
[HttpGet]
public async Task<MessageModel<UserView>> Profile()
{
return Success(await _sysUserService.GetInfo(CurrentUser.UserID));
}
/// <summary>
/// 修改用户基础信息
/// </summary>
[HttpPost]
public async Task<MessageModel<bool>> UpdateProfile(SysUser user)
{
user.UpdateBy = CurrentUser.LoginName;
return Success(await _sysUserService.UpdateUserProfile(user));
}
/// <summary>
/// 重置密码
/// </summary>
[HttpPost]
public async Task<MessageModel<bool>> UpdatePwd(string oldPassword, string newPassword)
{
MessageModel<bool> model = new MessageModel<bool>();
var user = await _sysUserService.QueryByIdAsync(CurrentUser.UserID);
if (MD5Helper.MD5Encrypt32(oldPassword) == user.Password)
{
user.Password = MD5Helper.MD5Encrypt32(newPassword);
user.UpdateBy = CurrentUser.LoginName;
user.UpdateTime = DateTime.Now;
model.success = await _sysUserService.UpdateAsync(user);
}
else
{
model.success = false;
model.msg = Resource_SysBase.OprateFail;
}
return model;
}
/// <summary>
/// 头像上传
/// </summary>
[HttpPost]
public async Task<MessageModel<string>> Avatar()
{
MessageModel<string> model = new MessageModel<string>();
//获取Form提交的文件
var files = Request.Form.Files;
if (files.Count == 0)
{
model.success = false;
model.msg = "文件不存在!";
return model;
}
model = await FileHelper.FileSaveByBlob(files);
if (model.success)
{
var user = await _sysUserService.QueryByIdAsync(CurrentUser.UserID);
user.Avatar = model.data;
user.UpdateBy = CurrentUser.LoginName;
user.UpdateTime = DateTime.Now;
await _sysUserService.UpdateAsync(user);
}
return model;
}
}
}