12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //
- // UIViewController+SPSBNetworkManager.m
- // 我的社保
- //
- // Created by shanp on 2021/4/21.
- //
- #import "UIViewController+SPSBNetworkManager.h"
- #import <objc/runtime.h>
- @implementation UIViewController (SPSBNetworkManager)
- - (void)networkUseMethod:(SPSBNetworkMethod)method
- loadingTips:(nullable NSString *)loadingTips
- isLogin:(bool)isLogin
- url:(NSString *)url
- filtrationKey:(NSString *)filtrationKey
- urlParameters:(NSDictionary *(^_Nullable)(void))getUrlParameters
- parameters:(NSDictionary * (^_Nullable)(void))getParameters
- success:(SPSBNetworkCallbackBlock)success
- failure:(SPSBNetworkCallbackBlock)failure {
- if (loadingTips) {
- [self.view showLoadingToastWithTitle:loadingTips userClick:false];
- }
- NSDictionary *urlParameters = nil;
- if (getUrlParameters) {
- urlParameters = getUrlParameters();
- }
- NSDictionary *parameters = nil;
- if (getParameters) {
- parameters = getParameters();
- }
- @weakify(self)
- [self.spsb_networkManager networkUseMethod:method isLogin:isLogin url:url filtrationKey:filtrationKey urlParameters:urlParameters parameters:parameters completion:^(NSError *error, id _Nullable data) {
- dispatch_main_async_safe(^{
- @strongify(self)
- if (error.code == SPSBNetworkErrorCodeSuccess) {
- if (success) {
- NSString *tips = success(error, data);
- if (tips) {
- if ([tips isEqualToString:@""]) {
- [self.view dismissToast];
- } else {
- [self.view showToastWithTitle:tips];
- }
- }
- }
-
- } else {
- if (failure) {
- NSString *tips = failure(error, data);
- if (tips) {
- if ([tips isEqualToString:@""]) {
- [self.view dismissToast];
- } else {
- [self handleFailureTipsWithError:error tips:failure(error, data)];
- }
- }
- }
- }
- });
- }];
- }
- - (void)networkUseMethod:(SPSBNetworkMethod)method
- loadingTips:(nullable NSString *)loadingTips
- isLogin:(bool)isLogin
- url:(NSString *)url
- urlParameters:(NSDictionary *(^_Nullable)(void))getUrlParameters
- parameters:(NSDictionary * (^_Nullable)(void))getParameters
- success:(SPSBNetworkCallbackBlock)success
- failure:(SPSBNetworkCallbackBlock)failure {
- [self networkUseMethod:method loadingTips:loadingTips isLogin:isLogin url:url filtrationKey:@"" urlParameters:getUrlParameters parameters:getParameters success:success failure:failure];
- }
- - (void)handleFailureTipsWithError:(nullable NSError *)error tips:(NSString *)tips {
- if (error.code == SPSBNetworkErrorCodeNetwrokMiss | error.code == SPSBNetworkErrorCodeConnectErrorMessage) {
- [self.view showToastWithTitle:error.domain];
- } else {
- [self.view showToastWithTitle:tips];
- }
- }
- - (SPSBNetworkManager *)spsb_networkManager {
- SPSBNetworkManager *manager = objc_getAssociatedObject(self, _cmd);
- if (!manager) {
- manager = SPSBNetworkManager.new;
- objc_setAssociatedObject(self, _cmd, manager, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- return manager;
- }
- @end
|