UIViewController+SPSBNetworkManager.m 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // UIViewController+SPSBNetworkManager.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/21.
  6. //
  7. #import "UIViewController+SPSBNetworkManager.h"
  8. #import <objc/runtime.h>
  9. @implementation UIViewController (SPSBNetworkManager)
  10. - (void)networkUseMethod:(SPSBNetworkMethod)method
  11. loadingTips:(nullable NSString *)loadingTips
  12. isLogin:(bool)isLogin
  13. url:(NSString *)url
  14. filtrationKey:(NSString *)filtrationKey
  15. urlParameters:(NSDictionary *(^_Nullable)(void))getUrlParameters
  16. parameters:(NSDictionary * (^_Nullable)(void))getParameters
  17. success:(SPSBNetworkCallbackBlock)success
  18. failure:(SPSBNetworkCallbackBlock)failure {
  19. if (loadingTips) {
  20. [self.view showLoadingToastWithTitle:loadingTips userClick:false];
  21. }
  22. NSDictionary *urlParameters = nil;
  23. if (getUrlParameters) {
  24. urlParameters = getUrlParameters();
  25. }
  26. NSDictionary *parameters = nil;
  27. if (getParameters) {
  28. parameters = getParameters();
  29. }
  30. @weakify(self)
  31. [self.spsb_networkManager networkUseMethod:method isLogin:isLogin url:url filtrationKey:filtrationKey urlParameters:urlParameters parameters:parameters completion:^(NSError *error, id _Nullable data) {
  32. dispatch_main_async_safe(^{
  33. @strongify(self)
  34. if (error.code == SPSBNetworkErrorCodeSuccess) {
  35. if (success) {
  36. NSString *tips = success(error, data);
  37. if (tips) {
  38. if ([tips isEqualToString:@""]) {
  39. [self.view dismissToast];
  40. } else {
  41. [self.view showToastWithTitle:tips];
  42. }
  43. }
  44. }
  45. } else {
  46. if (failure) {
  47. NSString *tips = failure(error, data);
  48. if (tips) {
  49. if ([tips isEqualToString:@""]) {
  50. [self.view dismissToast];
  51. } else {
  52. [self handleFailureTipsWithError:error tips:failure(error, data)];
  53. }
  54. }
  55. }
  56. }
  57. });
  58. }];
  59. }
  60. - (void)networkUseMethod:(SPSBNetworkMethod)method
  61. loadingTips:(nullable NSString *)loadingTips
  62. isLogin:(bool)isLogin
  63. url:(NSString *)url
  64. urlParameters:(NSDictionary *(^_Nullable)(void))getUrlParameters
  65. parameters:(NSDictionary * (^_Nullable)(void))getParameters
  66. success:(SPSBNetworkCallbackBlock)success
  67. failure:(SPSBNetworkCallbackBlock)failure {
  68. [self networkUseMethod:method loadingTips:loadingTips isLogin:isLogin url:url filtrationKey:@"" urlParameters:getUrlParameters parameters:getParameters success:success failure:failure];
  69. }
  70. - (void)handleFailureTipsWithError:(nullable NSError *)error tips:(NSString *)tips {
  71. if (error.code == SPSBNetworkErrorCodeNetwrokMiss | error.code == SPSBNetworkErrorCodeConnectErrorMessage) {
  72. [self.view showToastWithTitle:error.domain];
  73. } else {
  74. [self.view showToastWithTitle:tips];
  75. }
  76. }
  77. - (SPSBNetworkManager *)spsb_networkManager {
  78. SPSBNetworkManager *manager = objc_getAssociatedObject(self, _cmd);
  79. if (!manager) {
  80. manager = SPSBNetworkManager.new;
  81. objc_setAssociatedObject(self, _cmd, manager, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  82. }
  83. return manager;
  84. }
  85. @end