AuthService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using AntDesign;
  2. using DFS.Infrastructure.Extension.SM;
  3. using AI.Platform.Service.Common;
  4. using AI.Platform.Core;
  5. using AI.Platform.Core.Dto.CardManagement;
  6. using AI.Platform.Core.Entity;
  7. using AI.Platform.Core.Entity.System.VehicleTerminal.CardInfo;
  8. using AI.Platform.Core.Entity.System.VehicleTerminal.Company;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Components;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. using System.Reflection.Emit;
  14. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.Company;
  15. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.ElectronicAccount;
  16. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.UserInfo;
  17. using static AI.Platform.Core.Entity.PublicEnum;
  18. namespace AI.Platform.Service;
  19. [AllowAnonymous]
  20. [ApiGroup(ApiGroupNames.Auth)]
  21. public class AuthService : BaseService
  22. {
  23. /// <summary>
  24. /// 注意,非blazor环境,不能使用[Inject]方式注入
  25. /// </summary>
  26. private readonly SqlSugarRepository<SystemUser> _user;
  27. private SqlSugarRepository<CardInfoEntity> _CardInforepository { get; set; }
  28. private SqlSugarRepository<CompanyEntity> _Companyrepository { get; set; }
  29. private SqlSugarRepository<ElectronicAccountEntity> _Accountrepository { get; set; }
  30. private SqlSugarRepository<UserInfoEntity> _UserInforepository { get; set; }
  31. private SqlSugarRepository<UserCardRelationEntity> _UserCardRelationrepository { get; set; }
  32. private SqlSugarRepository<CompanyCardRuleEntity> _CompanyCardRuleRepository { get; set; }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. private readonly IHttpContextAccessor _contextAccessor;
  37. public AuthService(IHttpContextAccessor contextAccessor,
  38. SqlSugarRepository<SystemUser> user,
  39. SqlSugarRepository<CardInfoEntity> CardInforepository,
  40. SqlSugarRepository<CompanyEntity> Companyrepository,
  41. SqlSugarRepository<ElectronicAccountEntity> Accountrepository,
  42. SqlSugarRepository<UserInfoEntity> UserInforepository,
  43. SqlSugarRepository<CompanyCardRuleEntity> CompanyCardRuleRepository)
  44. {
  45. _contextAccessor = contextAccessor;
  46. _user = user;
  47. _CardInforepository = CardInforepository;
  48. _Companyrepository = Companyrepository;
  49. _Accountrepository = Accountrepository;
  50. _UserInforepository = UserInforepository;
  51. _CompanyCardRuleRepository = CompanyCardRuleRepository;
  52. }
  53. /// <summary>
  54. /// 登录
  55. /// {"username":"admin","password":"123456"}
  56. /// </summary>
  57. /// <param name="input"></param>
  58. /// <returns></returns>
  59. /// <remarks><code>{"username":"admin","password":"123456"}</code></remarks>
  60. [HttpPost]
  61. public async Task<object> Login(LoginInput input)
  62. {
  63. try
  64. {
  65. var Password = Crypto.MD5Encrypt(input.Password);
  66. var user = await _user.AsQueryable()
  67. .Where(x => x.Account.Equals(input.Account) && x.Password.Equals(Password))
  68. .FirstAsync();
  69. _ = user ?? throw Oops.Oh(ErrorCode.E1000);
  70. //生成Token令牌
  71. var token = Jwt.Serialize(new TokenModelJwt
  72. {
  73. UserId = user.Id,
  74. Name = user.Account,
  75. UserType = PublicEnum.UserType.Admin,
  76. });
  77. string Buid = "c75b2e74-d51e-42ae-bc89-2d39312c9c30";
  78. _contextAccessor.HttpContext.Response.Headers["access-token"] = token;
  79. return new { token , Buid };
  80. }
  81. catch (Exception ex)
  82. {
  83. throw Oops.Oh(ex.Message);
  84. }
  85. }
  86. /// <summary>
  87. /// 获取企业密钥
  88. /// </summary>
  89. /// <param name="input"></param>
  90. /// <returns></returns>
  91. /// <remarks><code>{"username":"admin","password":"123456"}</code></remarks>
  92. [HttpPost]
  93. public async Task<object> GetEnterpriseSecretKey()
  94. {
  95. try
  96. {
  97. var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"];
  98. var EnterpriseSecretKey = "1234234532345234".SM4Encrypt_ECB("54CD806F28AF7FAF61A48DF82DF17C96");
  99. return EnterpriseSecretKey;
  100. }
  101. catch (Exception ex)
  102. {
  103. throw Oops.Oh(ex.Message);
  104. }
  105. }
  106. /// <summary>
  107. /// 开卡or销卡
  108. /// </summary>
  109. /// <param name="CardNo"></param>
  110. /// <param name="AccountID"></param>
  111. /// <param name="operatetype">开卡:“newcard”;销卡“cancelcard”</param>
  112. /// <returns></returns>
  113. [HttpPost]
  114. public async Task<object> IssueCard(string CardNo, string operatetype)
  115. {
  116. try
  117. {
  118. var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"];
  119. var CardInfo = await _CardInforepository.AsQueryable()
  120. .LeftJoin<ElectronicAccountEntity>((a, b) => b.Id == a.AccountId)
  121. .LeftJoin<UserInfoEntity>((a, b, c) => c.Id == b.UserId)
  122. .Where((a, b, c) => a.CardNo == CardNo)
  123. .Select((a, b, c) => new CardInfoDto
  124. {
  125. Type = a.CardType,
  126. UserName = c.UserName
  127. }).FirstAsync();
  128. if (CardInfo == null)
  129. {
  130. return new
  131. {
  132. result = false,
  133. message = "卡不存在",
  134. cardtype = 0,
  135. accountname = ""
  136. }; ;
  137. }
  138. if (operatetype == "newcard")
  139. {
  140. return new
  141. {
  142. result = true,
  143. message = "开卡成功",
  144. cardtype = CardInfo.CardType,
  145. accountname = CardInfo.UserName
  146. }; ;
  147. }
  148. else if(operatetype == "cancelcard")
  149. {
  150. return new
  151. {
  152. result = true,
  153. message = "销卡成功",
  154. cardtype = CardInfo.CardType,
  155. accountname = CardInfo.UserName
  156. }; ;
  157. }
  158. return null;
  159. }
  160. catch (Exception ex)
  161. {
  162. throw Oops.Oh(ex.Message);
  163. }
  164. }
  165. /// <summary>
  166. /// 获取卡信息
  167. /// </summary>
  168. /// <param name="input"></param>
  169. /// <returns></returns>
  170. /// <remarks><code>{"username":"admin","password":"123456"}</code></remarks>
  171. [HttpPost]
  172. public async Task<object> GetCardInfo(string CardNo)
  173. {
  174. try
  175. {
  176. var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"];
  177. var CardInfo = await _CardInforepository.AsQueryable()
  178. .LeftJoin<ElectronicAccountEntity>((a, b) => b.Id == a.AccountId)
  179. .LeftJoin<UserInfoEntity>((a, b,c) => c.Id == b.UserId)
  180. .Where((a, b, c) => a.CardNo == CardNo)
  181. .Select((a, b, c) => new CardInfoDto
  182. {
  183. Type = a.CardType,
  184. UserName = c.UserName
  185. }).FirstAsync();
  186. var result = new {
  187. code = CardInfo != null ? 200 : 201,
  188. message = "",
  189. cardtype = CardInfo.Type,
  190. accountname = CardInfo.UserName
  191. };
  192. return result;
  193. }
  194. catch (Exception ex)
  195. {
  196. throw Oops.Oh(ex.Message);
  197. }
  198. }
  199. /// <summary>
  200. /// 查询是否可以发卡
  201. /// </summary>
  202. /// <param name="input"></param>
  203. /// <returns></returns>
  204. /// <remarks><code>{"username":"admin","password":"123456"}</code></remarks>
  205. [HttpPost]
  206. public async Task<object> CanIssueCard(string CardNo, string PhyNo)
  207. {
  208. try
  209. {
  210. var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"];
  211. var CardInfo = await _CardInforepository.AsQueryable()
  212. .LeftJoin<ElectronicAccountEntity>((a, b) => b.Id == a.AccountId)
  213. .LeftJoin<UserInfoEntity>((a, b, c) => c.Id == b.UserId)
  214. .Where((a, b, c) => a.CardNo == CardNo)
  215. .Select((a, b, c) => new CardInfoDto
  216. {
  217. Type = a.CardType,
  218. UserName = c.UserName
  219. }).FirstAsync();
  220. if (CardInfo == null)
  221. {
  222. return new
  223. {
  224. result = false,
  225. code = 201,
  226. message = "账户没有该卡",
  227. cardtype = 0,
  228. accountname = ""
  229. };
  230. }
  231. var resultJson = new
  232. {
  233. result = true,
  234. code = 200,
  235. message = "",
  236. cardtype = CardInfo.Type,
  237. accountname = CardInfo.UserName
  238. };
  239. return resultJson;
  240. }
  241. catch (Exception ex)
  242. {
  243. throw Oops.Oh(ex.Message);
  244. }
  245. }
  246. /// <summary>
  247. /// 申请发卡
  248. /// </summary>
  249. /// <param name="input"></param>
  250. /// <returns></returns>
  251. /// <remarks><code>{"username":"admin","password":"123456"}</code></remarks>
  252. [HttpPost]
  253. public async Task<object> ApplyForCard(string CardNo,string PhyNo)
  254. {
  255. try
  256. {
  257. var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"];
  258. var CardInfo = await _CardInforepository.AsQueryable()
  259. .LeftJoin<ElectronicAccountEntity>((a, b) => b.Id == a.AccountId)
  260. .LeftJoin<UserInfoEntity>((a, b, c) => c.Id == b.UserId)
  261. .Where((a, b, c) => a.CardNo == CardNo)
  262. .Select((a, b, c) => new CardInfoDto
  263. {
  264. Type = a.CardType,
  265. UserName = c.UserName
  266. }).FirstAsync();
  267. if (CardInfo == null)
  268. {
  269. return new
  270. {
  271. result = false,
  272. code = 201,
  273. message = "卡不存在",
  274. cardtype = 0,
  275. accountname = ""
  276. }; ;
  277. }
  278. return new
  279. {
  280. result = true,
  281. code = 200,
  282. message = "开卡成功",
  283. cardtype = CardInfo.Type,
  284. accountname = CardInfo.UserName
  285. };
  286. }
  287. catch (Exception ex)
  288. {
  289. throw Oops.Oh(ex.Message);
  290. }
  291. }
  292. /// <summary>
  293. /// 查询是否可以销卡
  294. /// </summary>
  295. /// <param name="input"></param>
  296. /// <returns></returns>
  297. /// <remarks><code>{"username":"admin","password":"123456"}</code></remarks>
  298. [HttpPost]
  299. public async Task<object> CanCancelCard(string CardNo, string PhyNo)
  300. {
  301. try
  302. {
  303. var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"];
  304. var CardInfo = await _CardInforepository.AsQueryable()
  305. .LeftJoin<ElectronicAccountEntity>((a, b) => b.Id == a.AccountId)
  306. .LeftJoin<UserInfoEntity>((a, b, c) => c.Id == b.UserId)
  307. .Where((a, b, c) => a.CardNo == CardNo)
  308. .Select((a, b, c) => new CardInfoDto
  309. {
  310. Type = a.CardType,
  311. UserName = c.UserName,
  312. AccountBalance = b.Balance
  313. }).FirstAsync();
  314. if (CardInfo == null)
  315. {
  316. return new
  317. {
  318. result = false,
  319. code = 201,
  320. message = "卡不存在",
  321. cardtype = 0,
  322. accountname = ""
  323. };
  324. }
  325. if (CardInfo.AccountBalance > 0)
  326. {
  327. return new
  328. {
  329. result = false,
  330. code = 203,
  331. message = "账户余额大于0",
  332. cardtype = 0,
  333. accountname = ""
  334. };
  335. }
  336. var result = new
  337. {
  338. result = true,
  339. code = 200,
  340. message = "",
  341. cardtype = CardInfo.Type,
  342. accountname = CardInfo.UserName
  343. };
  344. return result;
  345. }
  346. catch (Exception ex)
  347. {
  348. throw Oops.Oh(ex.Message);
  349. }
  350. }
  351. /// <summary>
  352. /// 申请销卡
  353. /// </summary>
  354. /// <param name="input"></param>
  355. /// <returns></returns>
  356. /// <remarks><code>{"username":"admin","password":"123456"}</code></remarks>
  357. [HttpPost]
  358. public async Task<object> ApplyForCardCancellation(string CardNo, string PhyNo)
  359. {
  360. try
  361. {
  362. var CurrentBuId = _contextAccessor.HttpContext.Response.Headers["CurrentBuId"];
  363. var CardInfo = await _CardInforepository.AsQueryable()
  364. .LeftJoin<ElectronicAccountEntity>((a, b) => b.Id == a.AccountId)
  365. .LeftJoin<UserInfoEntity>((a, b, c) => c.Id == b.UserId)
  366. .Where((a, b, c) => a.CardNo == CardNo)
  367. .Select((a, b, c) => new CardInfoDto
  368. {
  369. Type = a.CardType,
  370. UserName = c.UserName
  371. }).FirstAsync();
  372. return new
  373. {
  374. result = true,
  375. coo = "200",
  376. message = "销卡成功",
  377. cardtype = CardInfo.Type,
  378. accountname = CardInfo.UserName
  379. }; ;
  380. }
  381. catch (Exception ex)
  382. {
  383. throw Oops.Oh(ex.Message);
  384. }
  385. }
  386. /// <summary>
  387. /// 刷新Token
  388. /// </summary>
  389. /// <param name="token"></param>
  390. /// <returns></returns>
  391. [HttpGet]
  392. public async Task<object> Refresh(string token)
  393. {
  394. try
  395. {
  396. return new { token };
  397. }
  398. catch (Exception ex)
  399. {
  400. throw Oops.Oh(ex.Message);
  401. }
  402. }
  403. }