123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778 |
- //
- // ASNetTools.m
- // Asteria
- //
- // Created by iOS on 2023/11/25.
- //
- #import "ASNetTools.h"
- #import <MKNetworkKit/MKNetworkKit.h>
- @interface ASNetTools ()
- @property (nonatomic, copy) NSString *currentToken;
- @property (nonatomic, strong) MKNetworkEngine *engine;
- @property (nonatomic, strong) MKNetworkEngine *msgEngine;
- @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",
- @"Content-Currency": ASCurrencyManager.shared.currentCur
- }];
- self.msgEngine = [[MKNetworkEngine alloc] initWithHostName:MsgHostPath customHeaderFields:@{
- @"Authorization":token,
- @"powerby": @"longyitec",
- @"Content-Type": @"application/json;charset=utf-8",
- @"Content-Currency": ASCurrencyManager.shared.currentCur
- }];
-
- }
- - (void)checkToken {
-
- NSString *token = [DataUtil loginToken];
- if (!token) {
- token = @"";
- }
- if (![token isEqualToString:self.currentToken]) {
- [self updateEngine];
- }
- }
- // MARK: - message Api
- // post请求
- - (void)postMsgWithPath:(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.msgEngine 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];
- NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
- if ([temp isKindOfClass:[NSDictionary class]]) {
- NSDictionary *result = (NSDictionary *)temp;
- NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
- id data = result[@"data"];
- if ([code isEqualToString:@"200"]) {
- success(data);
- } else {
- NSString *msg = result[@"msg"];
- faild(code, 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[@"code"];
- 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.msgEngine enqueueOperation:op];
- // });
- }
- // formData的post请求
- - (void)formDataMsg_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.msgEngine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
- ///form-data 的请求格式
- NSData *data = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
-
- for (NSString *key in param.allKeys) {
- // 循环拿到所有参数进行拼接
- // NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
-
- NSString * searchStr = param[key];
- NSData *data =[searchStr dataUsingEncoding:NSUTF8StringEncoding];
- [op addData:data 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];
- NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
- if ([temp isKindOfClass:[NSDictionary class]]) {
- NSDictionary *result = (NSDictionary *)temp;
- NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
- id data = result[@"data"];
- if ([code isEqualToString:@"200"]) {
- success(data);
- } else {
- NSString *msg = result[@"msg"];
- faild(code, 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[@"code"];
- 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.msgEngine enqueueOperation:op];
- // });
- }
- // get 请求
- - (void)getMsgWithPath:(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.msgEngine 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];
- NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
- if ([temp isKindOfClass:[NSDictionary class]]) {
- NSDictionary *result = (NSDictionary *)temp;
- NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
- id data = result[@"data"];
- if ([code isEqualToString:@"200"]) {
- success(data);
- } else {
- NSString *msg = result[@"msg"];
- faild(code, 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[@"code"];
- 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);
- });
- }];
- [self.msgEngine enqueueOperation:op];
- // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
- // });
- }
- // put 请求
- - (void)putMsgWithPath:(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.msgEngine 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];
- NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
- if ([temp isKindOfClass:[NSDictionary class]]) {
- NSDictionary *result = (NSDictionary *)temp;
- NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
- id data = result[@"data"];
- if ([code isEqualToString:@"200"]) {
- success(data);
- } else {
- NSString *msg = result[@"msg"];
- faild(code, 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[@"code"];
- 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.msgEngine enqueueOperation:op];
- // });
- }
- // del 请求
- - (void)delMsgWithPath:(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.msgEngine 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];
- NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
- if ([temp isKindOfClass:[NSDictionary class]]) {
- NSDictionary *result = (NSDictionary *)temp;
- NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
- id data = result[@"data"];
- if ([code isEqualToString:@"200"]) {
- success(data);
- } else {
- NSString *msg = result[@"msg"];
- faild(code, 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[@"code"];
- 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.msgEngine enqueueOperation:op];
- // });
- }
- // MARK: - api V1
- // 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];
- NSLog(@"---Post-----url:\n%@\n-----param:\n%@\n-----data:\n%@\n--",path, param, temp);
- 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];
- // });d
- }
- // 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];
- ///form-data 的请求格式
- NSData *data = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
-
- for (NSString *key in param.allKeys) {
- // 循环拿到所有参数进行拼接
- // NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
-
- NSString * searchStr = param[key];
- NSData *data =[searchStr dataUsingEncoding:NSUTF8StringEncoding];
- [op addData:data 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];
- NSLog(@"---post-----url:\n%@\n-----data:\n%@\n--",path, temp);
- 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];
- NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, json);
- 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);
- }
- } else {
- NSString *msg = @"failed";
- faild(0, @"");
- }
-
- });
- } 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);
- });
- }];
- [self.engine enqueueOperation:op];
- // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
- // });
- }
- // 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];
- NSLog(@"---Put-----url:\n%@\n-----data:\n%@\n--",path, temp);
- 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 = [NSString stringWithFormat:@"%@", 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];
- // });
- }
- // MARK: - debug
- + (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" :@"abc123456789@qq.com",//@"Britneyngwafa@gmail.com",//
- @"password":@"A123456a",//@"1qazXSW@123",//
- };
- [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:^{
- [ASOneSignalManager setPushApnsTagWithUserInfo];
- }];
- 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:^{
- [ASOneSignalManager setPushApnsTagWithUserInfo];
- }];
- success(result);
- NSLog(@"----url:%@-----result:%@------", loginUrl, result);
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
- faild(code,msg);
- }];
- }
- ///获取广告位优惠卷列表
- +(void)reqNet_getAdvCoupons{
- [ASNetTools.shared getWithPath:BaseRequestrUrl(@"advCoupons") param:@{} success:^(id _Nonnull result) {
- [ASNetTools shared].xxx_couponAry = result;
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
-
- }];
-
- }
- -(NSMutableArray *)xxx_couponAry{
- if(!_xxx_couponAry){
- _xxx_couponAry = [[NSMutableArray alloc]init];
- }
- return _xxx_couponAry;
- }
- @end
|