ASNetTools.m 31 KB

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