UserService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Blazored.LocalStorage;
  2. using Microsoft.AspNetCore.Components.Authorization;
  3. using System.Net.Http;
  4. using System.Net.Http.Json;
  5. using System.Threading.Tasks;
  6. using wispro.sp.web.Auth;
  7. using wispro.sp.web.Models;
  8. using wispro.sp.web.Utils;
  9. using System.Security.Claims;
  10. using System.Collections.Generic;
  11. using wispro.sp.entity;
  12. using System;
  13. using wispro.sp.share.webViewObject;
  14. using System.Text.Json;
  15. namespace wispro.sp.web.Services
  16. {
  17. public interface IUserService
  18. {
  19. //CurrentUser CurrentUser { get; set; }
  20. Task<List<Staff>> GetAll();
  21. Task<CurrentUser> GetUser();
  22. Task<List<Staff>> GetReviewers(int? itemId, int appealTypeId);
  23. Task<Staff> GetUser(int staffId);
  24. Task<Staff> GetUserByName(string Name);
  25. }
  26. public class UserService : IUserService
  27. {
  28. private readonly IHttpService _httpClient;
  29. private readonly JwtAuthenticationStateProvider _jwt;
  30. private readonly ILocalStorageService _localStorageService;
  31. //private CurrentUser _CurrentUser= new CurrentUser() { Name = "" };
  32. public UserService(IHttpService httpClient, ILocalStorageService localStorageService, AuthenticationStateProvider jwt)
  33. {
  34. _httpClient = httpClient;
  35. _localStorageService = localStorageService;
  36. _jwt = (JwtAuthenticationStateProvider)jwt;
  37. }
  38. public async Task<CurrentUser> GetUser()
  39. {
  40. CurrentUser _user = new CurrentUser();
  41. var tokenInLocalStorage = await _localStorageService.GetItemAsync<userToken>("authToken");
  42. if (tokenInLocalStorage == null)
  43. {
  44. _jwt.NotifyUserLogOut();
  45. return null;
  46. }
  47. var claims = JwtParser.ParseClaimsFromJwt(tokenInLocalStorage.Token);
  48. foreach (Claim claim in claims)
  49. {
  50. if (claim.Type == ClaimTypes.Name )
  51. {
  52. _user.Name = claim.Value;
  53. break;
  54. }
  55. if(claim.Type == ClaimTypes.Role)
  56. {
  57. //Console.WriteLine($"user Roles:{claim.Value}");
  58. _user.Roles.Add(claim.Value);
  59. }
  60. }
  61. _user.Userid = tokenInLocalStorage.UserId;
  62. //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(_user));
  63. return _user;
  64. }
  65. public async Task<List<Staff>> GetAll()
  66. {
  67. try
  68. {
  69. var _StaffGrade = await _httpClient.Get<List<Staff>>($"Staff/GetAll");
  70. return _StaffGrade;
  71. }
  72. catch (Exception ex)
  73. {
  74. if (ex.Message.Contains("Unauthorized"))
  75. {
  76. _jwt.NotifyUserLogOut();
  77. }
  78. return null;
  79. }
  80. }
  81. public async Task<List<Staff>> GetReviewers(int? itemId, int appealTypeId)
  82. {
  83. try
  84. {
  85. var _StaffGrade = await _httpClient.Get<List<Staff>>($"Staff/GetReviewers?ItemId={itemId}&appealTypeId={appealTypeId}");
  86. return _StaffGrade;
  87. }
  88. catch (Exception ex)
  89. {
  90. if (ex.Message.Contains("Unauthorized"))
  91. {
  92. _jwt.NotifyUserLogOut();
  93. }
  94. return null;
  95. }
  96. }
  97. public async Task<Staff> GetUser(int staffId)
  98. {
  99. try
  100. {
  101. var staff = await _httpClient.Get<Staff>($"Staff/GetUser?Id={staffId}");
  102. return staff;
  103. }
  104. catch (Exception ex)
  105. {
  106. if (ex.Message.Contains("Unauthorized"))
  107. {
  108. _jwt.NotifyUserLogOut();
  109. }
  110. return null;
  111. }
  112. }
  113. public async Task<Staff> GetUserByName(string Name)
  114. {
  115. try
  116. {
  117. var staff = await _httpClient.Get<Staff>($"Staff/GetByName?Name={Name}");
  118. return staff;
  119. }
  120. catch (Exception ex)
  121. {
  122. if (ex.Message.Contains("Unauthorized"))
  123. {
  124. _jwt.NotifyUserLogOut();
  125. }
  126. return null;
  127. }
  128. }
  129. }
  130. }