using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Yitter.IdGenerator;
using Znyc.Recruitment.Admin.AspNetCore.Controllers;
using Znyc.Recruitment.Admin.AspNetCore.Entitys;
using Znyc.Recruitment.Admin.AspNetCore.Mvc;
using Znyc.Recruitment.Admin.AspNetCore.Mvc.Filter;
using Znyc.Recruitment.Admin.AspNetCore.ViewModel;
using Znyc.Recruitment.Admin.Commons.Encrypt;
using Znyc.Recruitment.Admin.Commons.Entitys;
using Znyc.Recruitment.Admin.Commons.Extensions;
using Znyc.Recruitment.Admin.Commons.Helpers;
using Znyc.Recruitment.Admin.Commons.Log;
using Znyc.Recruitment.Admin.Commons.Mapping;
using Znyc.Recruitment.Admin.Commons.Pages;
using Znyc.Recruitment.Admin.Security.Dtos;
using Znyc.Recruitment.Admin.Security.Entitys;
using Znyc.Recruitment.Admin.Security.IServices;
namespace Znyc.Recruitment.Admin.WebApi.Controllers
{
///
/// 系统用户接口
///
[ApiController]
[Route("api/Security/[controller]")]
[AllowAnonymous]
[NoPermissionRequired]
public class AdminUserController : AreaApiController
{
private readonly IAdminUserLogOnService _adminUserLogOnService;
private readonly IOrganizeService _organizeService;
private readonly IRoleService _roleService;
///
///
///
///
///
///
public AdminUserController(IAdminUserService service, IOrganizeService organizeService,
IRoleService roleService,
IAdminUserLogOnService adminUserLogOnService
) : base(service)
{
_service = service;
_organizeService = organizeService;
_roleService = roleService;
_adminUserLogOnService = adminUserLogOnService;
}
///
/// 新增前处理数据
///
///
protected override void OnBeforeInsert(AdminUserEntity info)
{
info.Id = YitIdHelper.NextId();
info.CreatedTime = DateTime.Now;
info.CreatedUserId = CurrentUser.UserId;
info.OrganizeId = _organizeService.GetRootOrganize(info.DepartmentId).ParentId;
info.IsDeleted = false;
}
///
/// 在更新数据前对数据的修改操作
///
///
///
protected override void OnBeforeUpdate(AdminUserEntity info)
{
info.ModifiedUserId = CurrentUser.UserId;
info.ModifiedTime = DateTime.Now;
info.OrganizeId = _organizeService.GetRootOrganize(info.DepartmentId).ParentId;
}
///
/// 在软删除数据前对数据的修改操作
///
///
///
protected override void OnBeforeSoftDelete(AdminUserEntity info)
{
info.IsDeleted = true;
}
///
/// 异步新增数据
///
///
///
[HttpPost("Insert")]
[FunctionAuthorize("Add")]
public async Task InsertAsync(AdminUserInputDto tinfo)
{
CommonResult result = new CommonResult();
if (!string.IsNullOrEmpty(tinfo.Account))
{
string where = string.Format("Account='{0}' or MobilePhone='{0}'", tinfo.Account);
AdminUserEntity Admin = _service.GetWhere(where);
if (Admin != null)
{
result.ErrMsg = "登录账号不能重复";
return ToJsonContent(result);
}
}
else
{
result.ErrMsg = "登录账号不能为空";
return ToJsonContent(result);
}
AdminUserEntity info = tinfo.MapTo();
OnBeforeInsert(info);
info.Status = 1;
info.ModifiedTime = DateTime.Now;
await _service.InsertAsync(info);
AdminUserLogOnEntity adminLogOn = new AdminUserLogOnEntity
{
UserId = info.Id,
UserSecretkey = MD5Util.GetMD5_16(GuidUtils.NewGuidFormatN()).ToLower()
};
adminLogOn.UserPassword = MD5Util
.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32("12345678").ToLower(), adminLogOn.UserSecretkey)
.ToLower()).ToLower();
adminLogOn.LogOnCount = 0;
adminLogOn.Language = "";
adminLogOn.Theme = "";
adminLogOn.Id = YitIdHelper.NextId();
await _adminUserLogOnService.InsertAsync(adminLogOn);
result.Success = adminLogOn.UserId > 0;
if (result.Success)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43001;
result.ErrCode = "43001";
}
return ToJsonContent(result);
}
///
/// 异步更新数据
///
///
///
[HttpPost("Update")]
[FunctionAuthorize("Edit")]
public async Task UpdateAsync(AdminUserInputDto tinfo)
{
CommonResult result = new CommonResult();
if (string.IsNullOrEmpty(tinfo.Account))
{
result.ErrMsg = "登录账号不能为空";
return ToJsonContent(result);
}
AdminUserEntity 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;
info.Status = tinfo.Status;
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);
}
///
/// 根据用户登录账号获取详细信息
///
///
///
[HttpGet("GetByUserName")]
[FunctionAuthorize("")]
public async Task GetByUserName(string userName)
{
CommonResult result = new CommonResult();
try
{
AdminUserEntity Admin = await _service.GetByUserName(userName);
result.ResData = Admin.MapTo();
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
catch (Exception ex)
{
Log4NetHelper.Error("获取用户异常", ex); //错误记录
result.ErrMsg = ex.Message;
}
return ToJsonContent(result);
}
///
/// 异步分页查询
///
///
///
[HttpPost("FindWithPagerSearchAsync")]
[FunctionAuthorize("List")]
public async Task FindWithPagerSearchAsync(SearchUserModel search)
{
CommonResult> result = new CommonResult>
{
ResData = await _service.FindWithPagerSearchAsync(search),
ErrCode = ErrCode.successCode
};
return ToJsonContent(result);
}
///
/// 重置密码
///
///
[HttpPost("ResetPassword")]
[FunctionAuthorize("ResetPassword")]
public async Task ResetPassword(long userId)
{
CommonResult result = new CommonResult();
try
{
string where = string.Format("UserId={0}", userId);
AdminUserLogOnEntity adminLogOn = _adminUserLogOnService.GetWhere(where);
adminLogOn.UserSecretkey = MD5Util.GetMD5_16(GuidUtils.NewGuidFormatN()).ToLower();
adminLogOn.UserPassword = MD5Util
.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32("12345678").ToLower(), adminLogOn.UserSecretkey)
.ToLower()).ToLower();
adminLogOn.ChangePasswordDate = DateTime.Now;
bool bl = await _adminUserLogOnService.UpdateAsync(adminLogOn, adminLogOn.Id);
if (bl)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
result.Success = true;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
}
catch (Exception ex)
{
Log4NetHelper.Error("重置密码异常", ex); //错误记录
result.ErrMsg = ex.Message;
}
return ToJsonContent(result);
}
///
/// 修改密码
///
/// 原密码
/// 新密码
/// 重复新密码
///
[HttpPost("ModifyPassword")]
[FunctionAuthorize("ModifyPassword")]
public async Task 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)
{
AdminUserLogOnEntity AdminSinginEntity = _adminUserLogOnService.GetByUserId(CurrentUser.UserId);
string inputPassword = MD5Util.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(oldpassword).ToLower(),
AdminSinginEntity.UserSecretkey).ToLower()).ToLower();
if (inputPassword != AdminSinginEntity.UserPassword)
{
result.ErrMsg = "原密码错误!";
}
else
{
string where = string.Format("UserId='{0}'", CurrentUser.UserId);
AdminUserLogOnEntity AdminLogOn = _adminUserLogOnService.GetWhere(where);
AdminLogOn.UserSecretkey = MD5Util.GetMD5_16(GuidUtils.NewGuidFormatN()).ToLower();
AdminLogOn.UserPassword = MD5Util
.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(password).ToLower(),
AdminLogOn.UserSecretkey).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);
}
///
/// 异步批量禁用/启用数据
///
///
[HttpPost("SetStatusBatchAsync")]
[FunctionAuthorize("")]
public async Task SetStatusBatchAsync(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 blResult = await _service.SetStatusByWhereAsync(info.Flag.ToInt(), where, CurrentUser.UserId);
if (blResult)
{
result.ErrCode = ErrCode.successCode;
result.ErrMsg = ErrCode.err0;
}
else
{
result.ErrMsg = ErrCode.err43002;
result.ErrCode = "43002";
}
}
return ToJsonContent(result);
}
///
/// 保存用户自定义的软件主题
///
/// 主题配置信息
///
[HttpPost("SaveUserTheme")]
[FunctionAuthorize("SaveUserTheme")]
public async Task 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);
}
}
}