UserService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. }
  25. public class UserService : IUserService
  26. {
  27. private readonly IHttpService _httpClient;
  28. private readonly JwtAuthenticationStateProvider _jwt;
  29. private readonly ILocalStorageService _localStorageService;
  30. //private CurrentUser _CurrentUser= new CurrentUser() { Name = "" };
  31. public UserService(IHttpService httpClient, ILocalStorageService localStorageService, AuthenticationStateProvider jwt)
  32. {
  33. _httpClient = httpClient;
  34. _localStorageService = localStorageService;
  35. _jwt = (JwtAuthenticationStateProvider)jwt;
  36. }
  37. public async Task<CurrentUser> GetUser()
  38. {
  39. CurrentUser _user = new CurrentUser();
  40. var tokenInLocalStorage = await _localStorageService.GetItemAsync<userToken>("authToken");
  41. if (tokenInLocalStorage == null)
  42. {
  43. _jwt.NotifyUserLogOut();
  44. return null;
  45. }
  46. var claims = JwtParser.ParseClaimsFromJwt(tokenInLocalStorage.Token);
  47. foreach (Claim claim in claims)
  48. {
  49. if (claim.Type == ClaimTypes.Name )
  50. {
  51. _user.Name = claim.Value;
  52. break;
  53. }
  54. if(claim.Type == ClaimTypes.Role)
  55. {
  56. //Console.WriteLine($"user Roles:{claim.Value}");
  57. _user.Roles.Add(claim.Value);
  58. }
  59. }
  60. _user.Userid = tokenInLocalStorage.UserId;
  61. //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(_user));
  62. return _user;
  63. }
  64. public async Task<List<Staff>> GetAll()
  65. {
  66. try
  67. {
  68. var _StaffGrade = await _httpClient.Get<List<Staff>>($"Staff/GetAll");
  69. return _StaffGrade;
  70. }
  71. catch (Exception ex)
  72. {
  73. if (ex.Message.Contains("Unauthorized"))
  74. {
  75. _jwt.NotifyUserLogOut();
  76. }
  77. return null;
  78. }
  79. }
  80. public async Task<List<Staff>> GetReviewers(int? itemId, int appealTypeId)
  81. {
  82. try
  83. {
  84. var _StaffGrade = await _httpClient.Get<List<Staff>>($"Staff/GetReviewers?ItemId={itemId}&appealTypeId={appealTypeId}");
  85. return _StaffGrade;
  86. }
  87. catch (Exception ex)
  88. {
  89. if (ex.Message.Contains("Unauthorized"))
  90. {
  91. _jwt.NotifyUserLogOut();
  92. }
  93. return null;
  94. }
  95. }
  96. public async Task<Staff> GetUser(int staffId)
  97. {
  98. try
  99. {
  100. var staff = await _httpClient.Get<Staff>($"Staff/GetUser?Id={staffId}");
  101. return staff;
  102. }
  103. catch (Exception ex)
  104. {
  105. if (ex.Message.Contains("Unauthorized"))
  106. {
  107. _jwt.NotifyUserLogOut();
  108. }
  109. return null;
  110. }
  111. }
  112. }
  113. }