// // ASNetTools.m // Asteria // // Created by iOS on 2023/11/25. // #import "ASNetTools.h" #import @interface ASNetTools () @property (nonatomic, copy) NSString *currentToken; @property (nonatomic, strong) MKNetworkEngine *engine; @property (nonatomic, strong) NSMutableDictionary *defuatParam; @end @implementation ASNetTools + (instancetype)shared { static id sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; } - (instancetype)init { self = [super init]; if (self) { [self updateEngine]; self.defuatParam = [NSMutableDictionary dictionary]; } return self; } - (void)setDefualtParam:(NSDictionary *)defualt { self.defuatParam = [[NSMutableDictionary alloc] initWithDictionary:defualt]; } - (void)updateDefualtParam:(NSDictionary *)newData { for (NSString *key in newData.allKeys) { self.defuatParam[key] = newData[key]; } } - (void)updateEngine { NSString *token = [DataUtil loginToken]; if (!token) { token = @""; } self.currentToken = token; self.engine = [[MKNetworkEngine alloc] initWithHostName:HostPath customHeaderFields:@{ @"Authorization":token, @"powerby": @"longyitec", @"Content-Type": @"application/json;charset=utf-8" }]; } - (void)checkToken { NSString *token = [DataUtil loginToken]; if (!token) { token = @""; } if (![token isEqualToString:self.currentToken]) { [self updateEngine]; } } // post请求 - (void)postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild { BOOL isSSL = true; [self checkToken]; NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam]; [tempDic addEntriesFromDictionary:param]; MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL]; [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON]; __weak typeof(self) weakSelf = self; [op onCompletion:^(MKNetworkOperation *completedOperation) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]]; id data = result[@"data"]; if ([status isEqualToString:@"1"]) { success(data); } else { NSString *msg = result[@"msg"]; faild(status, msg); } } }); } onError:^(NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = result[@"status"]; NSString *msg = result[@"msg"]; if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(status, msg); return; } faild(status, msg); return; } if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(@"-1", @"Plase Login"); return; } faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription); }); }]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{ [self.engine enqueueOperation:op]; }); } // formData的post请求 - (void)formData_postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild { BOOL isSSL = true; [self checkToken]; NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam]; [tempDic addEntriesFromDictionary:param]; MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL]; for (NSString *key in param.allKeys) { // 循环拿到所有参数进行拼接 NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; [op addData:[searchStr dataUsingEncoding:NSUTF8StringEncoding] forKey:key]; } __weak typeof(self) weakSelf = self; [op onCompletion:^(MKNetworkOperation *completedOperation) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]]; id data = result[@"data"]; if ([status isEqualToString:@"1"]) { success(data); } else { NSString *msg = result[@"msg"]; faild(status, msg); } } }); } onError:^(NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = result[@"status"]; NSString *msg = result[@"msg"]; if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(status, msg); return; } faild(status, msg); return; } if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(@"-1", @"Plase Login"); return; } faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription); }); }]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{ [self.engine enqueueOperation:op]; }); } // get 请求 - (void)getWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild { BOOL isSSL = true; [self checkToken]; NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam]; [tempDic addEntriesFromDictionary:param]; MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL]; __weak typeof(self) weakSelf = self; [op onCompletion:^(MKNetworkOperation *completedOperation) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]]; id data = result[@"data"]; if ([status isEqualToString:@"1"]) { success(data); } else { NSString *msg = result[@"msg"]; faild(status, msg); } } }); } onError:^(NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = result[@"status"]; NSString *msg = result[@"msg"]; if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(status, msg); return; } faild(status, msg); return; } if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(@"-1", @"Plase Login"); return; } faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription); }); }]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{ [self.engine enqueueOperation:op]; }); } // put 请求 - (void)putWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild { BOOL isSSL = true; //#if (DEBUG) // isSSL = false; //#endif [self checkToken]; NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam]; [tempDic addEntriesFromDictionary:param]; MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL]; [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON]; __weak typeof(self) weakSelf = self; [op onCompletion:^(MKNetworkOperation *completedOperation) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]]; id data = result[@"data"]; if ([status isEqualToString:@"1"]) { success(data); } else { NSString *msg = result[@"msg"]; faild(status, msg); } } }); } onError:^(NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = result[@"status"]; NSString *msg = result[@"msg"]; if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(status, msg); return; } faild(status, msg); return; } if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(@"-1", @"Plase Login"); return; } faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription); }); }]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{ [self.engine enqueueOperation:op]; }); } // del 请求 - (void)delWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild { BOOL isSSL = true; //#if (DEBUG) // isSSL = false; //#endif [self checkToken]; NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam]; [tempDic addEntriesFromDictionary:param]; MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL]; [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON]; __weak typeof(self) weakSelf = self; [op onCompletion:^(MKNetworkOperation *completedOperation) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]]; id data = result[@"data"]; if ([status isEqualToString:@"1"]) { success(data); } else { NSString *msg = result[@"msg"]; faild(status, msg); } } }); } onError:^(NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ NSString *json = [op responseJSON]; id temp = [json mj_JSONObject]; if ([temp isKindOfClass:[NSDictionary class]]) { NSDictionary *result = (NSDictionary *)temp; NSString *status = result[@"status"]; NSString *msg = result[@"msg"]; if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(status, msg); return; } faild(status, msg); return; } if (error.code == 401) { [DataUtil setLoginToken:@""]; [weakSelf updateEngine]; faild(@"-1", @"Plase Login"); return; } faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription); }); }]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{ [self.engine enqueueOperation:op]; }); } + (void)debugRegist { NSDictionary *param = @{ @"customer": @{ @"email": @"abc123456789@qq.com", @"firstname": @"ujrbcf", @"lastname": @"serwt", @"dob": @"2023/10/20" }, @"password": @"A123456a", @"extension_attributes" : @{ @"is_subscribed": @(true) } }; [ASNetTools.shared postWithPath:registerUrl param:param success:^(id _Nonnull result) { NSLog(@"----url:%@-----result:%@------", registerUrl, result); } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { NSLog(@"----url:%@-----code:%@----msg:%@--", registerUrl, code, msg); }]; } + (void)login { NSDictionary *param = @{ @"username" :@"Britneyngwafa@gmail.com",//@"abc123456789@qq.com", @"password":@"1qazXSW@123"//@"A123456a" }; [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) { NSString *token = [NSString stringWithFormat:@"Bearer %@", result]; [DataUtil setLoginToken:token]; [ASNetTools.shared updateEngine]; [ASUserInfoManager.shared getInfo]; NSLog(@"----url:%@-----result:%@------", loginUrl, result); } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg); }]; } +(void)xxx_loginWithParam:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild { [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) { NSString *token = [NSString stringWithFormat:@"Bearer %@", result]; [DataUtil setLoginToken:token]; [ASNetTools.shared updateEngine]; [ASUserInfoManager.shared getInfo]; success(result); NSLog(@"----url:%@-----result:%@------", loginUrl, result); } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg); faild(code,msg); }]; } @end