AuthService.cs 15 KB

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