ASNetTools.m 30 KB

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