StaffGradeService.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Microsoft.AspNetCore.Components.Authorization;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Net.Http.Json;
  7. using System.Threading.Tasks;
  8. using wispro.sp.entity;
  9. using wispro.sp.share;
  10. using wispro.sp.web.Auth;
  11. using static System.Net.WebRequestMethods;
  12. namespace wispro.sp.web.Services
  13. {
  14. public class StaffGradeService
  15. {
  16. private readonly HttpClient _httpClient;
  17. private readonly JwtAuthenticationStateProvider _jwt;
  18. public StaffGradeService(HttpClient httpClient, AuthenticationStateProvider jwt)
  19. {
  20. _httpClient = httpClient;
  21. _jwt = (JwtAuthenticationStateProvider)jwt;
  22. }
  23. public async Task<List<StaffGrade>> GetAll()
  24. {
  25. try
  26. {
  27. var _StaffGrade = await _httpClient.GetFromJsonAsync<List<StaffGrade>>($"http://localhost:39476/api/StaffGrade/GetAll");
  28. return _StaffGrade;
  29. }
  30. catch(Exception ex)
  31. {
  32. if (ex.Message.Contains("Unauthorized"))
  33. {
  34. _jwt.NotifyUserLogOut();
  35. }
  36. return null;
  37. }
  38. }
  39. public async Task<ApiSaveResponse> Save(StaffGrade staffGrade)
  40. {
  41. var data = await _httpClient.PostAsJsonAsync<StaffGrade>($"http://localhost:39476/api/StaffGrade/Save", staffGrade);
  42. if (data.IsSuccessStatusCode)
  43. {
  44. ApiSaveResponse result = await data.Content.ReadFromJsonAsync<ApiSaveResponse>();
  45. //await Task.Delay(1000);
  46. return result;
  47. }
  48. else
  49. {
  50. if(data.StatusCode == System.Net.HttpStatusCode.Unauthorized)
  51. {
  52. _jwt.NotifyUserLogOut();
  53. }
  54. return new ApiSaveResponse() { Success = false , ErrorMessage = $"请求发生错误 {data.StatusCode}" };
  55. }
  56. }
  57. }
  58. }