UserService.cs 3.1 KB

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