using AntDesign; using DFS.Infrastructure.Extension.SM; using AI.Platform.Service.Common; using AI.Platform.Core; using AI.Platform.Core.Dto.CardManagement; using AI.Platform.Core.Entity; using AI.Platform.Core.Entity.System.VehicleTerminal.CardInfo; using AI.Platform.Core.Entity.System.VehicleTerminal.Company; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Reflection.Emit; using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.Company; using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.ElectronicAccount; using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.UserInfo; using static AI.Platform.Core.Entity.PublicEnum; namespace AI.Platform.Service; [AllowAnonymous] [ApiGroup(ApiGroupNames.Auth)] public class AuthService : BaseService { /// /// 注意,非blazor环境,不能使用[Inject]方式注入 /// private readonly SqlSugarRepository _user; private SqlSugarRepository _CardInforepository { get; set; } private SqlSugarRepository _Companyrepository { get; set; } private SqlSugarRepository _Accountrepository { get; set; } private SqlSugarRepository _UserInforepository { get; set; } private SqlSugarRepository _UserCardRelationrepository { get; set; } private SqlSugarRepository _CompanyCardRuleRepository { get; set; } /// /// /// private readonly IHttpContextAccessor _contextAccessor; public AuthService(IHttpContextAccessor contextAccessor, SqlSugarRepository user, SqlSugarRepository CardInforepository, SqlSugarRepository Companyrepository, SqlSugarRepository Accountrepository, SqlSugarRepository UserInforepository, SqlSugarRepository CompanyCardRuleRepository) { _contextAccessor = contextAccessor; _user = user; _CardInforepository = CardInforepository; _Companyrepository = Companyrepository; _Accountrepository = Accountrepository; _UserInforepository = UserInforepository; _CompanyCardRuleRepository = CompanyCardRuleRepository; } /// /// 登录 /// {"username":"admin","password":"123456"} /// /// /// /// {"username":"admin","password":"123456"} [HttpPost] public async Task Login(LoginInput input) { try { var Password = Crypto.MD5Encrypt(input.Password); var user = await _user.AsQueryable() .Where(x => x.Account.Equals(input.Account) && x.Password.Equals(Password)) .FirstAsync(); _ = user ?? throw Oops.Oh(ErrorCode.E1000); //生成Token令牌 var token = Jwt.Serialize(new TokenModelJwt { UserId = user.Id, Name = user.Account, UserType = PublicEnum.UserType.Admin, }); string Buid = "c75b2e74-d51e-42ae-bc89-2d39312c9c30"; _contextAccessor.HttpContext.Response.Headers["access-token"] = token; return new { token , Buid }; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } /// /// 获取企业密钥 /// /// /// /// {"username":"admin","password":"123456"} [HttpPost] public async Task GetEnterpriseSecretKey() { try { var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"]; var EnterpriseSecretKey = "1234234532345234".SM4Encrypt_ECB("54CD806F28AF7FAF61A48DF82DF17C96"); return EnterpriseSecretKey; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } /// /// 开卡or销卡 /// /// /// /// 开卡:“newcard”;销卡“cancelcard” /// [HttpPost] public async Task IssueCard(string CardNo, string operatetype) { try { var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"]; var CardInfo = await _CardInforepository.AsQueryable() .LeftJoin((a, b) => b.Id == a.AccountId) .LeftJoin((a, b, c) => c.Id == b.UserId) .Where((a, b, c) => a.CardNo == CardNo) .Select((a, b, c) => new CardInfoDto { Type = a.CardType, UserName = c.UserName }).FirstAsync(); if (CardInfo == null) { return new { result = false, message = "卡不存在", cardtype = 0, accountname = "" }; ; } if (operatetype == "newcard") { return new { result = true, message = "开卡成功", cardtype = CardInfo.CardType, accountname = CardInfo.UserName }; ; } else if(operatetype == "cancelcard") { return new { result = true, message = "销卡成功", cardtype = CardInfo.CardType, accountname = CardInfo.UserName }; ; } return null; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } /// /// 获取卡信息 /// /// /// /// {"username":"admin","password":"123456"} [HttpPost] public async Task GetCardInfo(string CardNo) { try { var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"]; var CardInfo = await _CardInforepository.AsQueryable() .LeftJoin((a, b) => b.Id == a.AccountId) .LeftJoin((a, b,c) => c.Id == b.UserId) .Where((a, b, c) => a.CardNo == CardNo) .Select((a, b, c) => new CardInfoDto { Type = a.CardType, UserName = c.UserName }).FirstAsync(); var result = new { code = CardInfo != null ? 200 : 201, message = "", cardtype = CardInfo.Type, accountname = CardInfo.UserName }; return result; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } /// /// 查询是否可以发卡 /// /// /// /// {"username":"admin","password":"123456"} [HttpPost] public async Task CanIssueCard(string CardNo, string PhyNo) { try { var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"]; var CardInfo = await _CardInforepository.AsQueryable() .LeftJoin((a, b) => b.Id == a.AccountId) .LeftJoin((a, b, c) => c.Id == b.UserId) .Where((a, b, c) => a.CardNo == CardNo) .Select((a, b, c) => new CardInfoDto { Type = a.CardType, UserName = c.UserName }).FirstAsync(); if (CardInfo == null) { return new { result = false, code = 201, message = "账户没有该卡", cardtype = 0, accountname = "" }; } var resultJson = new { result = true, code = 200, message = "", cardtype = CardInfo.Type, accountname = CardInfo.UserName }; return resultJson; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } /// /// 申请发卡 /// /// /// /// {"username":"admin","password":"123456"} [HttpPost] public async Task ApplyForCard(string CardNo,string PhyNo) { try { var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"]; var CardInfo = await _CardInforepository.AsQueryable() .LeftJoin((a, b) => b.Id == a.AccountId) .LeftJoin((a, b, c) => c.Id == b.UserId) .Where((a, b, c) => a.CardNo == CardNo) .Select((a, b, c) => new CardInfoDto { Type = a.CardType, UserName = c.UserName }).FirstAsync(); if (CardInfo == null) { return new { result = false, code = 201, message = "卡不存在", cardtype = 0, accountname = "" }; ; } return new { result = true, code = 200, message = "开卡成功", cardtype = CardInfo.Type, accountname = CardInfo.UserName }; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } /// /// 查询是否可以销卡 /// /// /// /// {"username":"admin","password":"123456"} [HttpPost] public async Task CanCancelCard(string CardNo, string PhyNo) { try { var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"]; var CardInfo = await _CardInforepository.AsQueryable() .LeftJoin((a, b) => b.Id == a.AccountId) .LeftJoin((a, b, c) => c.Id == b.UserId) .Where((a, b, c) => a.CardNo == CardNo) .Select((a, b, c) => new CardInfoDto { Type = a.CardType, UserName = c.UserName, AccountBalance = b.Balance }).FirstAsync(); if (CardInfo == null) { return new { result = false, code = 201, message = "卡不存在", cardtype = 0, accountname = "" }; } if (CardInfo.AccountBalance > 0) { return new { result = false, code = 203, message = "账户余额大于0", cardtype = 0, accountname = "" }; } var result = new { result = true, code = 200, message = "", cardtype = CardInfo.Type, accountname = CardInfo.UserName }; return result; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } /// /// 申请销卡 /// /// /// /// {"username":"admin","password":"123456"} [HttpPost] public async Task ApplyForCardCancellation(string CardNo, string PhyNo) { try { var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"]; var CardInfo = await _CardInforepository.AsQueryable() .LeftJoin((a, b) => b.Id == a.AccountId) .LeftJoin((a, b, c) => c.Id == b.UserId) .Where((a, b, c) => a.CardNo == CardNo) .Select((a, b, c) => new CardInfoDto { Type = a.CardType, UserName = c.UserName }).FirstAsync(); return new { result = true, coo = "200", message = "销卡成功", cardtype = CardInfo.Type, accountname = CardInfo.UserName }; ; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } /// /// 刷新Token /// /// /// [HttpGet] public async Task Refresh(string token) { try { return new { token }; } catch (Exception ex) { throw Oops.Oh(ex.Message); } } }