using learun.iapplication;
using learun.operat;
using learun.util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
namespace learun.webapi.Controllers
{
///
/// 版 本 Learun-ADMS-Core 力软管理后台
/// Copyright (c) 2019-present 力软信息技术(苏州)有限公司
/// 创建人:tobin
/// 日 期:2020.04.17
/// 描 述:用户api
///
public class UserController : MvcControllerBase
{
private readonly UserIBLL _userIBLL;
private readonly PostIBLL _postIBLL;
private readonly RoleIBLL _roleIBLL;
private readonly UserRelationIBLL _userRelationIBLL;
private readonly IOperator _operator;
private readonly LogIBLL _logIBLL;
private readonly ImgIBLL _imgIBLL;
private readonly DepartmentIBLL _departmentIBLL;
private readonly IHttpContextAccessor _iHttpContextAccessor;
///
/// 构造方法,注入依赖项
///
/// 用户操作接口
/// 岗位操作接口
/// 角色操作接口
/// 用户关系操作接口
/// 日志接口
/// 会话操作接口
/// 图片操作接口
/// 请求上下文
/// 请求上下文
public UserController(
UserIBLL userIBLL,
PostIBLL postIBLL,
RoleIBLL roleIBLL,
UserRelationIBLL userRelationIBLL,
LogIBLL logIBLL,
IOperator ioperator,
ImgIBLL imgIBLL,
IHttpContextAccessor iHttpContextAccessor,
DepartmentIBLL departmentIBLL)
{
_userIBLL = userIBLL;
_postIBLL = postIBLL;
_roleIBLL = roleIBLL;
_userRelationIBLL = userRelationIBLL;
_logIBLL = logIBLL;
_operator = ioperator;
_imgIBLL = imgIBLL;
_iHttpContextAccessor = iHttpContextAccessor;
_departmentIBLL = departmentIBLL;
}
///
/// 登录接口
///
/// 账号
/// 密码
/// 登录令牌信息
[HttpPost]
[AllowAnonymous]
public async Task Login([FromForm]string username, [FromForm]string password)
{
#region 写入日志
LogEntity logEntity = new LogEntity();
logEntity.F_CategoryId = 1;
logEntity.F_OperateTypeId = ((int)OperationType.Login).ToString();
logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.ApiLogin);
logEntity.F_OperateAccount = username;
logEntity.F_OperateUserId = username;
logEntity.F_IPAddress = _iHttpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
logEntity.F_Module = ConfigHelper.GetConfig().SoftName;
#endregion
#region 内部账户验证
UserEntity userEntity = await _userIBLL.GetEntityByAccount(username);
if (userEntity == null)
{
logEntity.F_ExecuteResult = 0;
logEntity.F_ExecuteResultJson = "没有此账号!";
await _logIBLL.Write(logEntity);
return Fail("账号密码不匹配");
}
if (userEntity.F_EnabledMark != 1)
{
logEntity.F_ExecuteResult = 0;
logEntity.F_ExecuteResultJson = "账户被系统锁定,请联系管理员!";
await _logIBLL.Write(logEntity);
return Fail("账户被系统锁定,请联系管理员!");
}
bool isOk = _userIBLL.IsPasswordOk(userEntity.F_Password, password, userEntity.F_Secretkey);
if (!isOk)//登录失败
{
logEntity.F_ExecuteResult = 0;
logEntity.F_ExecuteResultJson = "账号密码不匹配";
await _logIBLL.Write(logEntity);
return Fail("账号密码不匹配");
}
else
{
logEntity.F_ExecuteResult = 1;
logEntity.F_ExecuteResultJson = "登录成功";
await _logIBLL.Write(logEntity);
string token = _operator.EncodeToken(userEntity.F_UserId, userEntity.F_RealName, username);
return Success(token);
}
#endregion
}
///
/// 修改密码
///
/// 新密码
/// 旧密码
///
[HttpPost]
public async Task Modifypw([FromForm]string newpassword, [FromForm]string oldpassword)
{
var userInfo = await CurrentUser();
if (userInfo.F_SecurityLevel == 1)
{
return Fail("当前账户不能修改密码");
}
bool res = await _userIBLL.RevisePassword(newpassword, oldpassword);
if (!res)
{
return Fail("原密码错误,请重新输入");
}
return SuccessInfo("密码修改成功,请牢记新密码。\r 将会自动安全退出。");
}
///
/// 获取登录者用户信息
///
/// 登录者用户信息
[HttpGet]
public async Task Current()
{
var data = await CurrentUser();
data.F_Password = null;
data.F_Secretkey = null;
var roleIds = await _userRelationIBLL.GetObjectIds(data.F_UserId, 1);
var postIds = await _userRelationIBLL.GetObjectIds(data.F_UserId, 2);
var jsonData = new
{
baseinfo = data,
post = await _postIBLL.GetListByPostIds(postIds),
role = await _roleIBLL.GetListByRoleIds(roleIds)
};
return Success(jsonData);
}
///
/// 根据用户 id 获取用户头像
///
///
[HttpGet]
[AllowAnonymous]
public async Task HeadImg(string id)
{
ImgEntity imgEntity = await _imgIBLL.GetEntity(id);
if (imgEntity != null && !string.IsNullOrEmpty(imgEntity.F_Content))
{
string imgContent = imgEntity.F_Content.Replace("data:image/" + imgEntity.F_ExName.Replace(".", "") + ";base64,", "");
byte[] arr = Convert.FromBase64String(imgContent);
return File(arr, "application/octet-stream");
}
else
{
byte[] arr2 = FileHelper.ReadRoot("/img/header.png");
return File(arr2, "application/octet-stream");
}
}
///
/// 按 id 获取用户信息
///
/// 单个用户信息
[HttpGet]
public async Task Info(string id)
{
var user = await _userIBLL.GetEntity(id);
user.F_Password = null;
user.F_Secretkey = null;
return Success(user);
}
///
/// 根据传入的 id 获取多个用户信息
///
/// 多个用户信息的数组
[HttpPost]
public async Task Infos([FromForm]string ids)
{
var users = await _userIBLL.GetListByKeyValues(ids);
return Success(users);
}
///
/// 根据公司/部门/关键字搜索获取用户
///
/// 公司 id
/// 部门 id
/// 搜索关键字
///
[HttpGet]
public async Task ByOrganize(string companyId, string departmentId, string keywords = "")
{
var userList = await _userIBLL.GetList(companyId, departmentId, keywords);
return Success(userList);
}
}
}