using DocumentServiceAPI.Application.UserAndLogin.Services;
|
using DocumentServiceAPI.Application.UserAndLogin.ViewMode;
|
using DocumentServiceAPI.Model.cyDocumentModel;
|
using DocumentServiceAPI.Model.UserInfoModel;
|
using DocumentServiceAPI.Utility;
|
using Furion.DynamicApiController;
|
using Furion.JsonSerialization;
|
using Microsoft.AspNetCore.Http;
|
using Microsoft.IdentityModel.Tokens;
|
using NetTaste;
|
using Newtonsoft.Json.Linq;
|
using Swashbuckle.AspNetCore.Annotations;
|
using System;
|
using System.Collections.Generic;
|
using System.IdentityModel.Tokens.Jwt;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace DocumentServiceAPI.Application.UserAndLogin
|
{
|
/// <summary>
|
/// 登录控制器
|
/// </summary>
|
[ApiDescriptionSettings("LogInAndUser")]
|
[DynamicApiController]
|
|
public class LogInController
|
{
|
private TokenService _tokenService;
|
private UserService _userService;
|
public LogInController(TokenService tokenService,UserService userService) {
|
_tokenService = tokenService;
|
_userService = userService;
|
}
|
/// <summary>
|
/// 登录 1 输入员工账户或者租户账户,2 选择租户账户,3 选择公司
|
/// 当密码为空时,带入用户中心token即可实现登录
|
/// </summary>
|
/// <returns>登录信息</returns>
|
[HttpPost("LogoIn")]
|
public async Task<RetLoginVM> LogoIn ( LoginPsWordIN Parma)
|
{
|
//zctok校验成功是否标志
|
bool IsTokenPass=false;
|
|
if(string.IsNullOrEmpty(Parma.PassWord))
|
{
|
|
var Request = App.HttpContext.Request;
|
string stoken = Request.Headers["Authorization"];
|
if (stoken == null)
|
throw Oops.Oh("没有输入密码");
|
stoken = stoken.Replace("Bearer ", "");
|
|
var isvale = _tokenService.CheckJwt(stoken, out SecurityToken securityToken);
|
JwtSecurityToken jwtToken = securityToken as JwtSecurityToken;
|
string itcode = jwtToken.Claims.FirstOrDefault(claim => claim.Type == "sub")?.Value;
|
if (isvale == false)
|
throw Oops.Oh("登录信息出错");
|
if(Parma.ITCode!= itcode)
|
throw Oops.Oh("用户名错误");
|
IsTokenPass=true;
|
|
}
|
|
bool needtoken = false;
|
|
if (Parma.TenantId == null)
|
{
|
//如果还没确定租户ID 需要先确定租户ID
|
List<TenderVM> Tender = await _userService.GetUserTenderList(Parma.ITCode);
|
|
switch (Tender.Count)
|
{
|
case 0:
|
throw Oops.Oh($"账户或者密码错误");
|
case 1:
|
if (!IsTokenPass)
|
{
|
needtoken = await _userService.CheckPsw(Tender[0].TenderId, Tender[0].EmployeeID, Parma.PassWord);
|
|
if (!needtoken)
|
throw Oops.Oh($"账户或者密码错误");
|
}
|
|
|
Parma.TenantId = Tender[0].TenderId;
|
Parma.employeeID = Tender[0].EmployeeID;
|
if (Parma.UnitID == null)
|
{
|
|
List<UnitVM> Units = await _userService.GetUserUnitList(Parma.TenantId ?? 0);
|
if (Units.Count == 0)
|
{
|
//没有公司
|
needtoken = true;
|
}
|
else if (Units.Count == 1)
|
{
|
Parma.UnitID = Units[0].UnitID;
|
needtoken = true;
|
}
|
else
|
{
|
//其他需要用户选择租户
|
return new RetLoginVM
|
{
|
NeedNext = true,
|
Units = Units,
|
|
};
|
}
|
}
|
|
break;
|
|
default:
|
//其他需要用户选择租户
|
return new RetLoginVM
|
{
|
NeedNext = true,
|
Tenants = Tender,
|
|
};
|
|
|
}
|
|
|
|
|
|
}
|
else if (Parma.UnitID == null)
|
{
|
|
var Units = await _userService.GetUserUnitList(Parma.TenantId ?? 0);
|
if (Units.Count == 0)
|
{
|
//没有公司
|
needtoken = true;
|
}
|
else if (Units.Count == 1) {
|
Parma.UnitID = Units[0].UnitID;
|
needtoken = true;
|
}
|
else
|
{
|
//其他需要用户选择租户
|
return new RetLoginVM
|
{
|
NeedNext = true,
|
Units = Units,
|
|
};
|
}
|
|
}
|
|
if (needtoken || Parma.UnitID != null)
|
{
|
|
TenderVM Tender = (await _userService.GetUserTenderList(Parma.ITCode)).Where(x => x.TenderId == Parma.TenantId && x.EmployeeID == Parma.employeeID).SingleOrDefault();
|
if (Tender == null)
|
{
|
throw Oops.Oh($"账户密码或者租户选择错误");
|
}
|
if (!IsTokenPass)
|
{
|
needtoken = await _userService.CheckPsw(Tender.TenderId, Tender.EmployeeID, Parma.PassWord);
|
|
if (!needtoken)
|
throw Oops.Oh($"账户或者密码错误");
|
}
|
|
var hasPermissions= await _userService.TenderHasPermissions(Tender.TenderId);
|
if(!hasPermissions)
|
{
|
throw Oops.Oh($"集团用账号:{Tender.ItCode}使用期限已经过期或者没有使用权限");
|
}
|
|
|
var token = _tokenService.CreateToken(new Model.JwtInfo
|
{
|
EID = Tender.EmployeeID,
|
LogInSource = Parma.Form,
|
TEID = Tender.TenderId,
|
UID = Parma.UnitID,
|
});
|
return new RetLoginVM
|
{
|
NeedNext = false,
|
Token = token,
|
|
};
|
}
|
throw Oops.Oh($"参数错误");
|
|
|
|
|
|
}
|
|
/// <summary>
|
/// 是否是用户租户,或者是员工
|
/// </summary>
|
/// <param name="ITCode">账号</param>
|
/// <returns> 返回true 代表是, false代表不是</returns>
|
[HttpGet("IDIsEmployee")]
|
public async Task<bool> IDIsEmployee(string ITCode, [FromServices] ISqlSugarClient db)
|
{
|
var ad= ITCode.TryValidate(ValidationTypes.PhoneNumber);
|
if(!ad.IsValid)
|
{
|
throw Oops.Oh("请输入正确的账号");
|
}
|
|
var count= await db.Queryable<TenantInfo>().Where(x=>x.ItCode==ITCode&&x.IsEn==true&&x.IsDel==false).WithCache(600).CountAsync();
|
if (count > 0)
|
return true;
|
count=await db.Queryable<Document_EmployeeInfo>().Where(x=>x.UserName==ITCode).WithCache(600).CountAsync();
|
if (count > 0)
|
return true;
|
return false;
|
|
}
|
|
|
|
|
|
}
|
}
|