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

414 lines
15 KiB

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;
using Znyc.Admin.AspNetCore.Controllers;
using Znyc.Admin.AspNetCore.Entitys;
using Znyc.Admin.AspNetCore.Mvc;
using Znyc.Admin.AspNetCore.Mvc.Filter;
using Znyc.Admin.AspNetCore.ViewModel;
using Znyc.Admin.Commons.Encrypt;
using Znyc.Admin.Commons.Entitys;
using Znyc.Admin.Commons.Enums;
using Znyc.Admin.Commons.Extensions;
using Znyc.Admin.Commons.Helpers;
using Znyc.Admin.Commons.Log;
using Znyc.Admin.Commons.Mapping;
using Znyc.Admin.Commons.Pages;
using Znyc.Admin.Security.Dtos;
using Znyc.Admin.Security.Entitys;
using Znyc.Admin.Security.IServices;
namespace Znyc.Admin.WebApi.Controllers
{
/// <summary>
/// 系统用户接口
/// </summary>
[ApiController]
[Route("api/Security/[controller]")]
[AllowAnonymous]
[NoPermissionRequired]
public class AdminUserController : AreaApiController<AdminUser, AdminUserOutputDto, AdminUserInputDto,
IAdminUserService, long>
{
private readonly IOrganizeService _organizeService;
private readonly IRoleService _roleService;
private readonly IAdminUserLogOnService _adminUserLogOnService;
/// <summary>
///
/// </summary>
/// <param name="service"></param>
/// <param name="organizeService"></param>
/// <param name="roleService"></param>
/// <param name="adminUserLogOnService"></param>
public AdminUserController(IAdminUserService service, IOrganizeService organizeService,
IRoleService roleService,
IAdminUserLogOnService adminUserLogOnService
) : base(service)
{
_service = service;
_organizeService = organizeService;
_roleService = roleService;
_adminUserLogOnService = adminUserLogOnService;
}
/// <summary>
/// 新增前处理数据
/// </summary>
/// <param name="info"></param>
protected override void OnBeforeInsert(AdminUser info)
{
info.Id = 0;
info.CreatedTime = DateTime.Now;
info.CreatedUserId = CurrentUser.UserId;
info.OrganizeId = _organizeService.GetRootOrganize(info.DepartmentId).Id;
info.IsDeleted = false;
}
/// <summary>
/// 在更新数据前对数据的修改操作
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
protected override void OnBeforeUpdate(AdminUser info)
{
info.ModifiedUserId = CurrentUser.UserId;
info.ModifiedTime = DateTime.Now;
info.OrganizeId = _organizeService.GetRootOrganize(info.DepartmentId).Id;
}
/// <summary>
/// 在软删除数据前对数据的修改操作
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
protected override void OnBeforeSoftDelete(AdminUser info)
{
info.IsDeleted = true;
}
/// <summary>
/// 异步新增数据
/// </summary>
/// <param name="tinfo"></param>
/// <returns></returns>
[HttpPost("Insert")]
[FunctionAuthorize("Add")]
public override async Task<IActionResult> InsertAsync(AdminUserInputDto tinfo)
{
CommonResult result = new CommonResult();
if (!string.IsNullOrEmpty(tinfo.Account))
{
string where = string.Format("Account='{0}' or MobilePhone='{0}'", tinfo.Account);
AdminUser Admin = _service.GetWhere(where);
if (Admin != null)
{
result.ErrMsg = "登录账号不能重复";
return ToJsonContent(result);
}
}
else
{
result.ErrMsg = "登录账号不能为空";
return ToJsonContent(result);
}
AdminUser info = tinfo.MapTo<AdminUser>();
OnBeforeInsert(info);
info.Id = YitIdHelper.NextId();
info.Status =(int) CommonStatus.ENABLE;
await _service.InsertAsync(info);
AdminUserLogOn adminLogOn = new AdminUserLogOn
{
AdminUserId = info.Id,
AdminUserSecretkey = MD5Util.GetMD5_16(GuidUtils.NewGuidFormatN()).ToLower()
};
adminLogOn.AdminUserPassword = MD5Util
.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32("12345678").ToLower(), adminLogOn.AdminUserSecretkey)
.ToLower()).ToLower();
adminLogOn.LogOnCount = 0;
adminLogOn.Language = "";
adminLogOn.Theme = "";
adminLogOn.Id = YitIdHelper.NextId();
await _adminUserLogOnService.InsertAsync(adminLogOn);
result.Success = adminLogOn.AdminUserId > 0 ? true : false;
if (result.Success)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43001;
result.ErrCode = "43001";
}
return ToJsonContent(result);
}
/// <summary>
/// 异步更新数据
/// </summary>
/// <param name="tinfo"></param>
/// <returns></returns>
[HttpPost("Update")]
[FunctionAuthorize("Edit")]
public override async Task<IActionResult> UpdateAsync(AdminUserInputDto tinfo)
{
CommonResult result = new CommonResult();
if (string.IsNullOrEmpty(tinfo.Account))
{
result.ErrMsg = "登录账号不能为空";
return ToJsonContent(result);
}
AdminUser info = _service.Get(tinfo.Id);
info.Account = tinfo.Account;
info.HeadIcon = tinfo.HeadIcon;
info.UserName = tinfo.UserName;
info.Gender = tinfo.Gender;
info.MobilePhone = tinfo.MobilePhone;
info.DepartmentId = tinfo.DepartmentId;
info.RoleId = tinfo.RoleId;
info.IsAdministrator = tinfo.IsAdministrator;
OnBeforeUpdate(info);
bool bl = await _service.UpdateAsync(info, tinfo.Id).ConfigureAwait(false);
if (bl)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
return ToJsonContent(result);
}
/// <summary>
/// 根据用户登录账号获取详细信息
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
[HttpGet("GetByUserName")]
[FunctionAuthorize("")]
public async Task<IActionResult> GetByUserName(string userName)
{
CommonResult result = new CommonResult();
try
{
AdminUser Admin = await _service.GetByUserName(userName);
result.ResData = Admin.MapTo<AdminUserOutputDto>();
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
catch (Exception ex)
{
Log4NetHelper.Error("获取用户异常", ex); //错误记录
result.ErrMsg = ex.Message;
}
return ToJsonContent(result);
}
/// <summary>
/// 异步分页查询
/// </summary>
/// <param name="search"></param>
/// <returns></returns>
[HttpPost("FindWithPagerSearchAsync")]
[FunctionAuthorize("List")]
public async Task<IActionResult> FindWithPagerSearchAsync(SearchUserModel search)
{
CommonResult<PageResult<AdminUserOutputDto>> result = new CommonResult<PageResult<AdminUserOutputDto>>
{
ResData = await _service.FindWithPagerSearchAsync(search),
ErrCode = ErrCode.successCode
};
return ToJsonContent(result);
}
// <summary>
// 重置密码
// </summary>
// <returns></returns>
//[HttpPost("ResetPassword")]
//[FunctionAuthorize("ResetPassword")]
//public async Task<IActionResult> ResetPassword(int UserId)
//{
// CommonResult result = new CommonResult();
// try
// {
// string where = string.Format("UserId='{0}'", UserId);
// AdminLogOn AdminLogOn = AdminLogOnService.GetWhere(where);
// Random random = new Random();
// string strRandom = random.Next(100000, 999999).ToString(); //生成编号
// AdminLogOn.AdminSecretkey = MD5Util.GetMD5_16(GuidUtils.NewGuidFormatN()).ToLower();
// AdminLogOn.AdminPassword = MD5Util.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(strRandom).ToLower(), AdminLogOn.AdminSecretkey).ToLower()).ToLower();
// AdminLogOn.ChangePasswordDate = DateTime.Now;
// bool bl = await AdminLogOnService.UpdateAsync(AdminLogOn, AdminLogOn.Id);
// if (bl)
// {
// result.ErrCode = ErrCode.successCode;
// result.ErrMsg = strRandom;
// result.Success = true;
// }
// else
// {
// result.ErrMsg = ErrCode.err43002;
// result.ErrCode = "43002";
// }
// }
// catch (Exception ex)
// {
// Log4NetHelper.Error("重置密码异常", ex); //错误记录
// result.ErrMsg = ex.Message;
// }
// return ToJsonContent(result);
//}
/// <summary>
/// 修改密码
/// </summary>
/// <param name="oldpassword">原密码</param>
/// <param name="password">新密码</param>
/// <param name="password2">重复新密码</param>
/// <returns></returns>
[HttpPost("ModifyPassword")]
[FunctionAuthorize("ModifyPassword")]
public async Task<IActionResult> ModifyPassword(string oldpassword, string password, string password2)
{
CommonResult result = new CommonResult();
try
{
if (string.IsNullOrEmpty(oldpassword))
{
result.ErrMsg = "原密码不能为空!";
}
else if (string.IsNullOrEmpty(password))
{
result.ErrMsg = "密码不能为空!";
}
else if (string.IsNullOrEmpty(password2))
{
result.ErrMsg = "重复输入密码不能为空!";
}
else if (password == password2)
{
AdminUserLogOn AdminSinginEntity = _adminUserLogOnService.GetByUserId(CurrentUser.UserId);
string inputPassword = MD5Util.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(oldpassword).ToLower(),
AdminSinginEntity.AdminUserSecretkey).ToLower()).ToLower();
if (inputPassword != AdminSinginEntity.AdminUserPassword)
{
result.ErrMsg = "原密码错误!";
}
else
{
string where = string.Format("AdminUserId='{0}'", CurrentUser.UserId);
AdminUserLogOn AdminLogOn = _adminUserLogOnService.GetWhere(where);
AdminLogOn.AdminUserSecretkey = MD5Util.GetMD5_16(GuidUtils.NewGuidFormatN()).ToLower();
AdminLogOn.AdminUserPassword = MD5Util
.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(password).ToLower(),
AdminLogOn.AdminUserSecretkey).ToLower()).ToLower();
AdminLogOn.ChangePasswordDate = DateTime.Now;
bool bl = await _adminUserLogOnService.UpdateAsync(AdminLogOn, AdminLogOn.Id);
if (bl)
{
result.ErrCode = ErrCode.successCode;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
}
}
else
{
result.ErrMsg = "两次输入的密码不一样";
}
}
catch (Exception ex)
{
Log4NetHelper.Error("重置密码异常", ex); //错误记录
result.ErrMsg = ex.Message;
}
return ToJsonContent(result);
}
/// <summary>
/// 异步批量禁用数据
/// </summary>
/// <param name="info"></param>
[HttpPost("SetEnabledMarktBatchAsync")]
[FunctionAuthorize("")]
public async Task<IActionResult> SetEnabledMarktBatchAsync(UpdateEnableViewModel info)
{
CommonResult result = new CommonResult();
string where = string.Empty;
if (typeof(int) == typeof(string))
{
@where = "id in ('" + info.Ids.Join(",").Trim(',').Replace(",", "','") + "')";
}
else if (typeof(int) == typeof(int))
{
@where = "id in (" + info.Ids.Join(",") + ")";
}
if (!string.IsNullOrEmpty(where))
{
bool bl = false;
if (info.Flag == "1")
{
bl = true;
}
bool blResult = await _service.SetEnabledMarkByWhereAsync(bl, where, CurrentUser.UserId);
if (blResult)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
}
return ToJsonContent(result);
}
/// <summary>
/// 保存用户自定义的软件主题
/// </summary>
/// <param name="info">主题配置信息</param>
/// <returns></returns>
[HttpPost("SaveUserTheme")]
[FunctionAuthorize("SaveUserTheme")]
public async Task<IActionResult> SaveUserTheme(UserThemeInputDto info)
{
CommonResult result = new CommonResult();
try
{
result.Success = await _adminUserLogOnService.SaveUserTheme(info, CurrentUser.UserId);
result.ErrCode = ErrCode.successCode;
}
catch (Exception ex)
{
Log4NetHelper.Error("保存用户自定义的软件主题异常", ex);//错误记录
result.ErrMsg = ex.Message;
}
return ToJsonContent(result);
}
}
}