using System;
using learun.iapplication;
using learun.operat;
using learun.util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace EC.Web.Controllers
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.09.11
/// 描 述:登录模块控制器
///
public class LoginController : MvcControllerBase
{
private readonly IOperator _operator;
private readonly UserIBLL _userIBLL;
private readonly LogIBLL _logIBLL;
private readonly IHttpContextAccessor _iHttpContextAccessor;
public LoginController(UserIBLL userIBLL, LogIBLL logIBLL, IOperator ioperator, IHttpContextAccessor iHttpContextAccessor)
{
_userIBLL = userIBLL;
_logIBLL = logIBLL;
_operator = ioperator;
_iHttpContextAccessor = iHttpContextAccessor;
}
#region 视图功能
///
/// 页面
///
///
public IActionResult Index()
{
return View();
}
#endregion 视图功能
#region 获取数据
///
/// 获取用户登录信息
///
///
[HttpGet]
[AjaxOnly]
public async Task GetUserInfo()
{
var entity = await _userIBLL.GetEntity();
entity.F_Password = null;
entity.F_Secretkey = null;
return Success(entity);
}
#endregion 获取数据
#region 提交数据
///
/// 登录验证
///
/// 账号
/// 密码
///
[HttpPost]
[AjaxOnly]
[AllowAnonymous]
public async Task CheckLogin(string account, string password)
{
#region 写入日志
LogEntity logEntity = new LogEntity();
logEntity.F_CategoryId = 1;
logEntity.F_OperateTypeId = ((int)OperationType.Login).ToString();
logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Login);
logEntity.F_OperateAccount = account;
logEntity.F_OperateUserId = account;
logEntity.F_IPAddress = _iHttpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
logEntity.F_Module = ConfigHelper.GetConfig().SoftName;
#endregion 写入日志
#region 内部账户验证
UserEntity userEntity = await _userIBLL.GetEntityByAccount(account);
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, account);
return Success(token);
}
#endregion 内部账户验证
}
#endregion 提交数据
}
}