ASNetTools.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. //
  2. // ASNetTools.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/11/25.
  6. //
  7. #import "ASNetTools.h"
  8. #import <MKNetworkKit/MKNetworkKit.h>
  9. @interface ASNetTools ()
  10. @property (nonatomic, copy) NSString *currentToken;
  11. @property (nonatomic, strong) MKNetworkEngine *engine;
  12. @property (nonatomic, strong) MKNetworkEngine *msgEngine;
  13. @property (nonatomic, strong) NSMutableDictionary *defuatParam;
  14. @end
  15. @implementation ASNetTools
  16. + (instancetype)shared {
  17. static id sharedInstance = nil;
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. sharedInstance = [[self alloc] init];
  21. });
  22. return sharedInstance;
  23. }
  24. - (instancetype)init
  25. {
  26. self = [super init];
  27. if (self) {
  28. [self updateEngine];
  29. self.defuatParam = [NSMutableDictionary dictionary];
  30. }
  31. return self;
  32. }
  33. - (void)setDefualtParam:(NSDictionary *)defualt {
  34. self.defuatParam = [[NSMutableDictionary alloc] initWithDictionary:defualt];
  35. }
  36. - (void)updateDefualtParam:(NSDictionary *)newData {
  37. for (NSString *key in newData.allKeys) {
  38. self.defuatParam[key] = newData[key];
  39. }
  40. }
  41. - (void)updateEngine {
  42. NSString *token = [DataUtil loginToken];
  43. if (!token) {
  44. token = @"";
  45. }
  46. self.currentToken = token;
  47. self.engine = [[MKNetworkEngine alloc] initWithHostName:HostPath customHeaderFields:@{
  48. @"Authorization":token,
  49. @"powerby": @"longyitec",
  50. @"Content-Type": @"application/json;charset=utf-8",
  51. @"Content-Currency": ASCurrencyManager.shared.currentCur
  52. }];
  53. self.msgEngine = [[MKNetworkEngine alloc] initWithHostName:MsgHostPath customHeaderFields:@{
  54. @"Authorization":token,
  55. @"powerby": @"longyitec",
  56. @"Content-Type": @"application/json;charset=utf-8",
  57. @"Content-Currency": ASCurrencyManager.shared.currentCur
  58. }];
  59. }
  60. - (void)checkToken {
  61. NSString *token = [DataUtil loginToken];
  62. if (!token) {
  63. token = @"";
  64. }
  65. if (![token isEqualToString:self.currentToken]) {
  66. [self updateEngine];
  67. }
  68. }
  69. // MARK: - message Api
  70. // post请求
  71. - (void)postMsgWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  72. BOOL isSSL = true;
  73. [self checkToken];
  74. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  75. [tempDic addEntriesFromDictionary:param];
  76. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  77. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  78. __weak typeof(self) weakSelf = self;
  79. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  80. dispatch_async(dispatch_get_main_queue(), ^{
  81. NSString *json = [op responseJSON];
  82. id temp = [json mj_JSONObject];
  83. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  84. if ([temp isKindOfClass:[NSDictionary class]]) {
  85. NSDictionary *result = (NSDictionary *)temp;
  86. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  87. id data = result[@"data"];
  88. if ([code isEqualToString:@"200"]) {
  89. success(data);
  90. } else {
  91. NSString *msg = result[@"msg"];
  92. faild(code, msg);
  93. }
  94. }
  95. });
  96. } onError:^(NSError *error) {
  97. dispatch_async(dispatch_get_main_queue(), ^{
  98. NSString *json = [op responseJSON];
  99. id temp = [json mj_JSONObject];
  100. if ([temp isKindOfClass:[NSDictionary class]]) {
  101. NSDictionary *result = (NSDictionary *)temp;
  102. NSString *status = result[@"code"];
  103. NSString *msg = result[@"msg"];
  104. if (error.code == 401) {
  105. [DataUtil setLoginToken:@""];
  106. [weakSelf updateEngine];
  107. faild(status, msg);
  108. return;
  109. }
  110. faild(status, msg);
  111. return;
  112. }
  113. if (error.code == 401) {
  114. [DataUtil setLoginToken:@""];
  115. [weakSelf updateEngine];
  116. faild(@"-1", @"Plase Login");
  117. return;
  118. }
  119. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  120. });
  121. }];
  122. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  123. [self.msgEngine enqueueOperation:op];
  124. // });
  125. }
  126. // formData的post请求
  127. - (void)formDataMsg_postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  128. BOOL isSSL = true;
  129. [self checkToken];
  130. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  131. [tempDic addEntriesFromDictionary:param];
  132. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  133. ///form-data 的请求格式
  134. NSData *data = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
  135. for (NSString *key in param.allKeys) {
  136. // 循环拿到所有参数进行拼接
  137. // NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  138. NSString * searchStr = param[key];
  139. NSData *data =[searchStr dataUsingEncoding:NSUTF8StringEncoding];
  140. [op addData:data forKey:key];
  141. }
  142. __weak typeof(self) weakSelf = self;
  143. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  144. dispatch_async(dispatch_get_main_queue(), ^{
  145. NSString *json = [op responseJSON];
  146. id temp = [json mj_JSONObject];
  147. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  148. if ([temp isKindOfClass:[NSDictionary class]]) {
  149. NSDictionary *result = (NSDictionary *)temp;
  150. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  151. id data = result[@"data"];
  152. if ([code isEqualToString:@"200"]) {
  153. success(data);
  154. } else {
  155. NSString *msg = result[@"msg"];
  156. faild(code, msg);
  157. }
  158. }
  159. });
  160. } onError:^(NSError *error) {
  161. dispatch_async(dispatch_get_main_queue(), ^{
  162. NSString *json = [op responseJSON];
  163. id temp = [json mj_JSONObject];
  164. if ([temp isKindOfClass:[NSDictionary class]]) {
  165. NSDictionary *result = (NSDictionary *)temp;
  166. NSString *status = result[@"code"];
  167. NSString *msg = result[@"msg"];
  168. if (error.code == 401) {
  169. [DataUtil setLoginToken:@""];
  170. [weakSelf updateEngine];
  171. faild(status, msg);
  172. return;
  173. }
  174. faild(status, msg);
  175. return;
  176. }
  177. if (error.code == 401) {
  178. [DataUtil setLoginToken:@""];
  179. [weakSelf updateEngine];
  180. faild(@"-1", @"Plase Login");
  181. return;
  182. }
  183. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  184. });
  185. }];
  186. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  187. [self.msgEngine enqueueOperation:op];
  188. // });
  189. }
  190. // get 请求
  191. - (void)getMsgWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  192. BOOL isSSL = true;
  193. [self checkToken];
  194. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  195. [tempDic addEntriesFromDictionary:param];
  196. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL];
  197. __weak typeof(self) weakSelf = self;
  198. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  199. dispatch_async(dispatch_get_main_queue(), ^{
  200. NSString *json = [op responseJSON];
  201. id temp = [json mj_JSONObject];
  202. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  203. if ([temp isKindOfClass:[NSDictionary class]]) {
  204. NSDictionary *result = (NSDictionary *)temp;
  205. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  206. id data = result[@"data"];
  207. if ([code isEqualToString:@"200"]) {
  208. success(data);
  209. } else {
  210. NSString *msg = result[@"msg"];
  211. faild(code, msg);
  212. }
  213. }
  214. });
  215. } onError:^(NSError *error) {
  216. dispatch_async(dispatch_get_main_queue(), ^{
  217. NSString *json = [op responseJSON];
  218. id temp = [json mj_JSONObject];
  219. if ([temp isKindOfClass:[NSDictionary class]]) {
  220. NSDictionary *result = (NSDictionary *)temp;
  221. NSString *status = result[@"code"];
  222. NSString *msg = result[@"msg"];
  223. if (error.code == 401) {
  224. [DataUtil setLoginToken:@""];
  225. [weakSelf updateEngine];
  226. faild(status, msg);
  227. return;
  228. }
  229. faild(status, msg);
  230. return;
  231. }
  232. if (error.code == 401) {
  233. [DataUtil setLoginToken:@""];
  234. [weakSelf updateEngine];
  235. faild(@"-1", @"Plase Login");
  236. return;
  237. }
  238. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  239. });
  240. }];
  241. [self.msgEngine enqueueOperation:op];
  242. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  243. // });
  244. }
  245. // put 请求
  246. - (void)putMsgWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  247. BOOL isSSL = true;
  248. //#if (DEBUG)
  249. // isSSL = false;
  250. //#endif
  251. [self checkToken];
  252. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  253. [tempDic addEntriesFromDictionary:param];
  254. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  255. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  256. __weak typeof(self) weakSelf = self;
  257. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. NSString *json = [op responseJSON];
  260. id temp = [json mj_JSONObject];
  261. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  262. if ([temp isKindOfClass:[NSDictionary class]]) {
  263. NSDictionary *result = (NSDictionary *)temp;
  264. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  265. id data = result[@"data"];
  266. if ([code isEqualToString:@"200"]) {
  267. success(data);
  268. } else {
  269. NSString *msg = result[@"msg"];
  270. faild(code, msg);
  271. }
  272. }
  273. });
  274. } onError:^(NSError *error) {
  275. dispatch_async(dispatch_get_main_queue(), ^{
  276. NSString *json = [op responseJSON];
  277. id temp = [json mj_JSONObject];
  278. if ([temp isKindOfClass:[NSDictionary class]]) {
  279. NSDictionary *result = (NSDictionary *)temp;
  280. NSString *status = result[@"code"];
  281. NSString *msg = result[@"msg"];
  282. if (error.code == 401) {
  283. [DataUtil setLoginToken:@""];
  284. [weakSelf updateEngine];
  285. faild(status, msg);
  286. return;
  287. }
  288. faild(status, msg);
  289. return;
  290. }
  291. if (error.code == 401) {
  292. [DataUtil setLoginToken:@""];
  293. [weakSelf updateEngine];
  294. faild(@"-1", @"Plase Login");
  295. return;
  296. }
  297. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  298. });
  299. }];
  300. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  301. [self.msgEngine enqueueOperation:op];
  302. // });
  303. }
  304. // del 请求
  305. - (void)delMsgWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  306. BOOL isSSL = true;
  307. //#if (DEBUG)
  308. // isSSL = false;
  309. //#endif
  310. [self checkToken];
  311. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  312. [tempDic addEntriesFromDictionary:param];
  313. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  314. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  315. __weak typeof(self) weakSelf = self;
  316. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  317. dispatch_async(dispatch_get_main_queue(), ^{
  318. NSString *json = [op responseJSON];
  319. id temp = [json mj_JSONObject];
  320. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  321. if ([temp isKindOfClass:[NSDictionary class]]) {
  322. NSDictionary *result = (NSDictionary *)temp;
  323. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  324. id data = result[@"data"];
  325. if ([code isEqualToString:@"200"]) {
  326. success(data);
  327. } else {
  328. NSString *msg = result[@"msg"];
  329. faild(code, msg);
  330. }
  331. }
  332. });
  333. } onError:^(NSError *error) {
  334. dispatch_async(dispatch_get_main_queue(), ^{
  335. NSString *json = [op responseJSON];
  336. id temp = [json mj_JSONObject];
  337. if ([temp isKindOfClass:[NSDictionary class]]) {
  338. NSDictionary *result = (NSDictionary *)temp;
  339. NSString *status = result[@"code"];
  340. NSString *msg = result[@"msg"];
  341. if (error.code == 401) {
  342. [DataUtil setLoginToken:@""];
  343. [weakSelf updateEngine];
  344. faild(status, msg);
  345. return;
  346. }
  347. faild(status, msg);
  348. return;
  349. }
  350. if (error.code == 401) {
  351. [DataUtil setLoginToken:@""];
  352. [weakSelf updateEngine];
  353. faild(@"-1", @"Plase Login");
  354. return;
  355. }
  356. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  357. });
  358. }];
  359. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  360. [self.msgEngine enqueueOperation:op];
  361. // });
  362. }
  363. // MARK: - api V1
  364. // post请求
  365. - (void)postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  366. BOOL isSSL = true;
  367. [self checkToken];
  368. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  369. [tempDic addEntriesFromDictionary:param];
  370. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  371. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  372. __weak typeof(self) weakSelf = self;
  373. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  374. dispatch_async(dispatch_get_main_queue(), ^{
  375. NSString *json = [op responseJSON];
  376. id temp = [json mj_JSONObject];
  377. NSLog(@"---Post-----url:\n%@\n-----param:\n%@\n-----data:\n%@\n--",path, param, temp);
  378. if ([temp isKindOfClass:[NSDictionary class]]) {
  379. NSDictionary *result = (NSDictionary *)temp;
  380. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  381. id data = result[@"data"];
  382. if ([status isEqualToString:@"1"]) {
  383. success(data);
  384. } else {
  385. NSString *msg = result[@"msg"];
  386. faild(status, msg);
  387. }
  388. }
  389. });
  390. } onError:^(NSError *error) {
  391. dispatch_async(dispatch_get_main_queue(), ^{
  392. NSString *json = [op responseJSON];
  393. id temp = [json mj_JSONObject];
  394. if ([temp isKindOfClass:[NSDictionary class]]) {
  395. NSDictionary *result = (NSDictionary *)temp;
  396. NSString *status = result[@"status"];
  397. NSString *msg = result[@"msg"];
  398. if (error.code == 401) {
  399. [DataUtil setLoginToken:@""];
  400. [weakSelf updateEngine];
  401. faild(status, msg);
  402. return;
  403. }
  404. faild(status, msg);
  405. return;
  406. }
  407. if (error.code == 401) {
  408. [DataUtil setLoginToken:@""];
  409. [weakSelf updateEngine];
  410. faild(@"-1", @"Plase Login");
  411. return;
  412. }
  413. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  414. });
  415. }];
  416. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  417. [self.engine enqueueOperation:op];
  418. // });d
  419. }
  420. // formData的post请求
  421. - (void)formData_postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  422. BOOL isSSL = true;
  423. [self checkToken];
  424. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  425. [tempDic addEntriesFromDictionary:param];
  426. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  427. ///form-data 的请求格式
  428. NSData *data = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
  429. for (NSString *key in param.allKeys) {
  430. // 循环拿到所有参数进行拼接
  431. // NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  432. NSString * searchStr = param[key];
  433. NSData *data =[searchStr dataUsingEncoding:NSUTF8StringEncoding];
  434. [op addData:data forKey:key];
  435. }
  436. __weak typeof(self) weakSelf = self;
  437. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  438. dispatch_async(dispatch_get_main_queue(), ^{
  439. NSString *json = [op responseJSON];
  440. id temp = [json mj_JSONObject];
  441. NSLog(@"---post-----url:\n%@\n-----data:\n%@\n--",path, temp);
  442. if ([temp isKindOfClass:[NSDictionary class]]) {
  443. NSDictionary *result = (NSDictionary *)temp;
  444. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  445. id data = result[@"data"];
  446. if ([status isEqualToString:@"1"]) {
  447. success(data);
  448. } else {
  449. NSString *msg = result[@"msg"];
  450. faild(status, msg);
  451. }
  452. }
  453. });
  454. } onError:^(NSError *error) {
  455. dispatch_async(dispatch_get_main_queue(), ^{
  456. NSString *json = [op responseJSON];
  457. id temp = [json mj_JSONObject];
  458. if ([temp isKindOfClass:[NSDictionary class]]) {
  459. NSDictionary *result = (NSDictionary *)temp;
  460. NSString *status = result[@"status"];
  461. NSString *msg = result[@"msg"];
  462. if (error.code == 401) {
  463. [DataUtil setLoginToken:@""];
  464. [weakSelf updateEngine];
  465. faild(status, msg);
  466. return;
  467. }
  468. faild(status, msg);
  469. return;
  470. }
  471. if (error.code == 401) {
  472. [DataUtil setLoginToken:@""];
  473. [weakSelf updateEngine];
  474. faild(@"-1", @"Plase Login");
  475. return;
  476. }
  477. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  478. });
  479. }];
  480. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  481. [self.engine enqueueOperation:op];
  482. });
  483. }
  484. // get 请求
  485. - (void)getWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  486. BOOL isSSL = true;
  487. [self checkToken];
  488. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  489. [tempDic addEntriesFromDictionary:param];
  490. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL];
  491. __weak typeof(self) weakSelf = self;
  492. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  493. dispatch_async(dispatch_get_main_queue(), ^{
  494. NSString *json = [op responseJSON];
  495. id temp = [json mj_JSONObject];
  496. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, json);
  497. if ([temp isKindOfClass:[NSDictionary class]]) {
  498. NSDictionary *result = (NSDictionary *)temp;
  499. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  500. id data = result[@"data"];
  501. if ([status isEqualToString:@"1"]) {
  502. success(data);
  503. } else {
  504. NSString *msg = result[@"msg"];
  505. faild(status, msg);
  506. }
  507. } else {
  508. NSString *msg = @"failed";
  509. faild(0, @"");
  510. }
  511. });
  512. } onError:^(NSError *error) {
  513. dispatch_async(dispatch_get_main_queue(), ^{
  514. NSString *json = [op responseJSON];
  515. id temp = [json mj_JSONObject];
  516. if ([temp isKindOfClass:[NSDictionary class]]) {
  517. NSDictionary *result = (NSDictionary *)temp;
  518. NSString *status = result[@"status"];
  519. NSString *msg = result[@"msg"];
  520. if (error.code == 401) {
  521. [DataUtil setLoginToken:@""];
  522. [weakSelf updateEngine];
  523. faild(status, msg);
  524. return;
  525. }
  526. faild(status, msg);
  527. return;
  528. }
  529. if (error.code == 401) {
  530. [DataUtil setLoginToken:@""];
  531. [weakSelf updateEngine];
  532. faild(@"-1", @"Plase Login");
  533. return;
  534. }
  535. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  536. });
  537. }];
  538. [self.engine enqueueOperation:op];
  539. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  540. // });
  541. }
  542. // put 请求
  543. - (void)putWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  544. BOOL isSSL = true;
  545. //#if (DEBUG)
  546. // isSSL = false;
  547. //#endif
  548. [self checkToken];
  549. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  550. [tempDic addEntriesFromDictionary:param];
  551. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  552. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  553. __weak typeof(self) weakSelf = self;
  554. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  555. dispatch_async(dispatch_get_main_queue(), ^{
  556. NSString *json = [op responseJSON];
  557. id temp = [json mj_JSONObject];
  558. NSLog(@"---Put-----url:\n%@\n-----data:\n%@\n--",path, temp);
  559. if ([temp isKindOfClass:[NSDictionary class]]) {
  560. NSDictionary *result = (NSDictionary *)temp;
  561. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  562. id data = result[@"data"];
  563. if ([status isEqualToString:@"1"]) {
  564. success(data);
  565. } else {
  566. NSString *msg = result[@"msg"];
  567. faild(status, msg);
  568. }
  569. }
  570. });
  571. } onError:^(NSError *error) {
  572. dispatch_async(dispatch_get_main_queue(), ^{
  573. NSString *json = [op responseJSON];
  574. id temp = [json mj_JSONObject];
  575. if ([temp isKindOfClass:[NSDictionary class]]) {
  576. NSDictionary *result = (NSDictionary *)temp;
  577. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  578. NSString *msg = result[@"msg"];
  579. if (error.code == 401) {
  580. [DataUtil setLoginToken:@""];
  581. [weakSelf updateEngine];
  582. faild(status, msg);
  583. return;
  584. }
  585. faild(status, msg);
  586. return;
  587. }
  588. if (error.code == 401) {
  589. [DataUtil setLoginToken:@""];
  590. [weakSelf updateEngine];
  591. faild(@"-1", @"Plase Login");
  592. return;
  593. }
  594. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  595. });
  596. }];
  597. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  598. [self.engine enqueueOperation:op];
  599. // });
  600. }
  601. // del 请求
  602. - (void)delWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  603. BOOL isSSL = true;
  604. //#if (DEBUG)
  605. // isSSL = false;
  606. //#endif
  607. [self checkToken];
  608. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  609. [tempDic addEntriesFromDictionary:param];
  610. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  611. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  612. __weak typeof(self) weakSelf = self;
  613. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  614. dispatch_async(dispatch_get_main_queue(), ^{
  615. NSString *json = [op responseJSON];
  616. id temp = [json mj_JSONObject];
  617. if ([temp isKindOfClass:[NSDictionary class]]) {
  618. NSDictionary *result = (NSDictionary *)temp;
  619. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  620. id data = result[@"data"];
  621. if ([status isEqualToString:@"1"]) {
  622. success(data);
  623. } else {
  624. NSString *msg = result[@"msg"];
  625. faild(status, msg);
  626. }
  627. }
  628. });
  629. } onError:^(NSError *error) {
  630. dispatch_async(dispatch_get_main_queue(), ^{
  631. NSString *json = [op responseJSON];
  632. id temp = [json mj_JSONObject];
  633. if ([temp isKindOfClass:[NSDictionary class]]) {
  634. NSDictionary *result = (NSDictionary *)temp;
  635. NSString *status = result[@"status"];
  636. NSString *msg = result[@"msg"];
  637. if (error.code == 401) {
  638. [DataUtil setLoginToken:@""];
  639. [weakSelf updateEngine];
  640. faild(status, msg);
  641. return;
  642. }
  643. faild(status, msg);
  644. return;
  645. }
  646. if (error.code == 401) {
  647. [DataUtil setLoginToken:@""];
  648. [weakSelf updateEngine];
  649. faild(@"-1", @"Plase Login");
  650. return;
  651. }
  652. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  653. });
  654. }];
  655. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  656. [self.engine enqueueOperation:op];
  657. // });
  658. }
  659. // MARK: - debug
  660. + (void)debugRegist {
  661. NSDictionary *param = @{
  662. @"customer": @{
  663. @"email": @"abc123456789@qq.com",
  664. @"firstname": @"ujrbcf",
  665. @"lastname": @"serwt",
  666. @"dob": @"2023/10/20"
  667. },
  668. @"password": @"A123456a",
  669. @"extension_attributes" : @{
  670. @"is_subscribed": @(true)
  671. }
  672. };
  673. [ASNetTools.shared postWithPath:registerUrl param:param success:^(id _Nonnull result) {
  674. NSLog(@"----url:%@-----result:%@------", registerUrl, result);
  675. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  676. NSLog(@"----url:%@-----code:%@----msg:%@--", registerUrl, code, msg);
  677. }];
  678. }
  679. + (void)login {
  680. NSDictionary *param = @{
  681. @"username" :@"abc123456789@qq.com",//@"Britneyngwafa@gmail.com",//
  682. @"password":@"A123456a",//@"1qazXSW@123",//
  683. };
  684. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  685. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  686. [DataUtil setLoginToken:token];
  687. [ASNetTools.shared updateEngine];
  688. [ASUserInfoManager.shared getInfo:^{
  689. [ASOneSignalManager setPushApnsTagWithUserInfo];
  690. }];
  691. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  692. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  693. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  694. }];
  695. }
  696. +(void)xxx_loginWithParam:(NSDictionary *)param
  697. success:(void(^)(id))success
  698. faild:(void(^)(NSString *code, NSString *msg))faild {
  699. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  700. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  701. [DataUtil setLoginToken:token];
  702. [ASNetTools.shared updateEngine];
  703. [ASUserInfoManager.shared getInfo:^{
  704. [ASOneSignalManager setPushApnsTagWithUserInfo];
  705. }];
  706. success(result);
  707. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  708. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  709. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  710. faild(code,msg);
  711. }];
  712. }
  713. ///获取广告位优惠卷列表
  714. +(void)reqNet_getAdvCoupons{
  715. [ASNetTools.shared getWithPath:BaseRequestrUrl(@"advCoupons") param:@{} success:^(id _Nonnull result) {
  716. [ASNetTools shared].xxx_couponAry = result;
  717. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  718. }];
  719. }
  720. -(NSMutableArray *)xxx_couponAry{
  721. if(!_xxx_couponAry){
  722. _xxx_couponAry = [[NSMutableArray alloc]init];
  723. }
  724. return _xxx_couponAry;
  725. }
  726. @end