ASNetTools.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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) NSMutableDictionary *defuatParam;
  13. @end
  14. @implementation ASNetTools
  15. + (instancetype)shared {
  16. static id sharedInstance = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. sharedInstance = [[self alloc] init];
  20. });
  21. return sharedInstance;
  22. }
  23. - (instancetype)init
  24. {
  25. self = [super init];
  26. if (self) {
  27. [self updateEngine];
  28. self.defuatParam = [NSMutableDictionary dictionary];
  29. }
  30. return self;
  31. }
  32. - (void)setDefualtParam:(NSDictionary *)defualt {
  33. self.defuatParam = [[NSMutableDictionary alloc] initWithDictionary:defualt];
  34. }
  35. - (void)updateDefualtParam:(NSDictionary *)newData {
  36. for (NSString *key in newData.allKeys) {
  37. self.defuatParam[key] = newData[key];
  38. }
  39. }
  40. - (void)updateEngine {
  41. NSString *token = [DataUtil loginToken];
  42. if (!token) {
  43. token = @"";
  44. }
  45. self.currentToken = token;
  46. self.engine = [[MKNetworkEngine alloc] initWithHostName:HostPath customHeaderFields:@{
  47. @"Authorization":token,
  48. @"powerby": @"longyitec",
  49. @"Content-Type": @"application/json;charset=utf-8"
  50. }];
  51. }
  52. - (void)checkToken {
  53. NSString *token = [DataUtil loginToken];
  54. if (!token) {
  55. token = @"";
  56. }
  57. if (![token isEqualToString:self.currentToken]) {
  58. [self updateEngine];
  59. }
  60. }
  61. // post请求
  62. - (void)postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  63. BOOL isSSL = true;
  64. [self checkToken];
  65. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  66. [tempDic addEntriesFromDictionary:param];
  67. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  68. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  69. __weak typeof(self) weakSelf = self;
  70. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  71. dispatch_async(dispatch_get_main_queue(), ^{
  72. NSString *json = [op responseJSON];
  73. id temp = [json mj_JSONObject];
  74. if ([temp isKindOfClass:[NSDictionary class]]) {
  75. NSDictionary *result = (NSDictionary *)temp;
  76. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  77. id data = result[@"data"];
  78. if ([status isEqualToString:@"1"]) {
  79. success(data);
  80. } else {
  81. NSString *msg = result[@"msg"];
  82. faild(status, msg);
  83. }
  84. }
  85. });
  86. } onError:^(NSError *error) {
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. NSString *json = [op responseJSON];
  89. id temp = [json mj_JSONObject];
  90. if ([temp isKindOfClass:[NSDictionary class]]) {
  91. NSDictionary *result = (NSDictionary *)temp;
  92. NSString *status = result[@"status"];
  93. NSString *msg = result[@"msg"];
  94. if (error.code == 401) {
  95. [DataUtil setLoginToken:@""];
  96. [weakSelf updateEngine];
  97. faild(status, msg);
  98. return;
  99. }
  100. faild(status, msg);
  101. return;
  102. }
  103. if (error.code == 401) {
  104. [DataUtil setLoginToken:@""];
  105. [weakSelf updateEngine];
  106. faild(@"-1", @"Plase Login");
  107. return;
  108. }
  109. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  110. });
  111. }];
  112. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  113. [self.engine enqueueOperation:op];
  114. });
  115. }
  116. // formData的post请求
  117. - (void)formData_postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  118. BOOL isSSL = true;
  119. [self checkToken];
  120. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  121. [tempDic addEntriesFromDictionary:param];
  122. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  123. for (NSString *key in param.allKeys) {
  124. // 循环拿到所有参数进行拼接
  125. NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  126. [op addData:[searchStr dataUsingEncoding:NSUTF8StringEncoding] forKey:key];
  127. }
  128. __weak typeof(self) weakSelf = self;
  129. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  130. dispatch_async(dispatch_get_main_queue(), ^{
  131. NSString *json = [op responseJSON];
  132. id temp = [json mj_JSONObject];
  133. if ([temp isKindOfClass:[NSDictionary class]]) {
  134. NSDictionary *result = (NSDictionary *)temp;
  135. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  136. id data = result[@"data"];
  137. if ([status isEqualToString:@"1"]) {
  138. success(data);
  139. } else {
  140. NSString *msg = result[@"msg"];
  141. faild(status, msg);
  142. }
  143. }
  144. });
  145. } onError:^(NSError *error) {
  146. dispatch_async(dispatch_get_main_queue(), ^{
  147. NSString *json = [op responseJSON];
  148. id temp = [json mj_JSONObject];
  149. if ([temp isKindOfClass:[NSDictionary class]]) {
  150. NSDictionary *result = (NSDictionary *)temp;
  151. NSString *status = result[@"status"];
  152. NSString *msg = result[@"msg"];
  153. if (error.code == 401) {
  154. [DataUtil setLoginToken:@""];
  155. [weakSelf updateEngine];
  156. faild(status, msg);
  157. return;
  158. }
  159. faild(status, msg);
  160. return;
  161. }
  162. if (error.code == 401) {
  163. [DataUtil setLoginToken:@""];
  164. [weakSelf updateEngine];
  165. faild(@"-1", @"Plase Login");
  166. return;
  167. }
  168. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  169. });
  170. }];
  171. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  172. [self.engine enqueueOperation:op];
  173. });
  174. }
  175. // get 请求
  176. - (void)getWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  177. BOOL isSSL = true;
  178. [self checkToken];
  179. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  180. [tempDic addEntriesFromDictionary:param];
  181. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL];
  182. __weak typeof(self) weakSelf = self;
  183. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  184. dispatch_async(dispatch_get_main_queue(), ^{
  185. NSString *json = [op responseJSON];
  186. id temp = [json mj_JSONObject];
  187. if ([temp isKindOfClass:[NSDictionary class]]) {
  188. NSDictionary *result = (NSDictionary *)temp;
  189. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  190. id data = result[@"data"];
  191. if ([status isEqualToString:@"1"]) {
  192. success(data);
  193. } else {
  194. NSString *msg = result[@"msg"];
  195. faild(status, msg);
  196. }
  197. }
  198. });
  199. } onError:^(NSError *error) {
  200. dispatch_async(dispatch_get_main_queue(), ^{
  201. NSString *json = [op responseJSON];
  202. id temp = [json mj_JSONObject];
  203. if ([temp isKindOfClass:[NSDictionary class]]) {
  204. NSDictionary *result = (NSDictionary *)temp;
  205. NSString *status = result[@"status"];
  206. NSString *msg = result[@"msg"];
  207. if (error.code == 401) {
  208. [DataUtil setLoginToken:@""];
  209. [weakSelf updateEngine];
  210. faild(status, msg);
  211. return;
  212. }
  213. faild(status, msg);
  214. return;
  215. }
  216. if (error.code == 401) {
  217. [DataUtil setLoginToken:@""];
  218. [weakSelf updateEngine];
  219. faild(@"-1", @"Plase Login");
  220. return;
  221. }
  222. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  223. });
  224. }];
  225. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  226. [self.engine enqueueOperation:op];
  227. });
  228. }
  229. // put 请求
  230. - (void)putWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  231. BOOL isSSL = true;
  232. //#if (DEBUG)
  233. // isSSL = false;
  234. //#endif
  235. [self checkToken];
  236. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  237. [tempDic addEntriesFromDictionary:param];
  238. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  239. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  240. __weak typeof(self) weakSelf = self;
  241. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  242. dispatch_async(dispatch_get_main_queue(), ^{
  243. NSString *json = [op responseJSON];
  244. id temp = [json mj_JSONObject];
  245. if ([temp isKindOfClass:[NSDictionary class]]) {
  246. NSDictionary *result = (NSDictionary *)temp;
  247. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  248. id data = result[@"data"];
  249. if ([status isEqualToString:@"1"]) {
  250. success(data);
  251. } else {
  252. NSString *msg = result[@"msg"];
  253. faild(status, msg);
  254. }
  255. }
  256. });
  257. } onError:^(NSError *error) {
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. NSString *json = [op responseJSON];
  260. id temp = [json mj_JSONObject];
  261. if ([temp isKindOfClass:[NSDictionary class]]) {
  262. NSDictionary *result = (NSDictionary *)temp;
  263. NSString *status = result[@"status"];
  264. NSString *msg = result[@"msg"];
  265. if (error.code == 401) {
  266. [DataUtil setLoginToken:@""];
  267. [weakSelf updateEngine];
  268. faild(status, msg);
  269. return;
  270. }
  271. faild(status, msg);
  272. return;
  273. }
  274. if (error.code == 401) {
  275. [DataUtil setLoginToken:@""];
  276. [weakSelf updateEngine];
  277. faild(@"-1", @"Plase Login");
  278. return;
  279. }
  280. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  281. });
  282. }];
  283. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  284. [self.engine enqueueOperation:op];
  285. });
  286. }
  287. // del 请求
  288. - (void)delWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  289. BOOL isSSL = true;
  290. //#if (DEBUG)
  291. // isSSL = false;
  292. //#endif
  293. [self checkToken];
  294. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  295. [tempDic addEntriesFromDictionary:param];
  296. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  297. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  298. __weak typeof(self) weakSelf = self;
  299. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  300. dispatch_async(dispatch_get_main_queue(), ^{
  301. NSString *json = [op responseJSON];
  302. id temp = [json mj_JSONObject];
  303. if ([temp isKindOfClass:[NSDictionary class]]) {
  304. NSDictionary *result = (NSDictionary *)temp;
  305. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  306. id data = result[@"data"];
  307. if ([status isEqualToString:@"1"]) {
  308. success(data);
  309. } else {
  310. NSString *msg = result[@"msg"];
  311. faild(status, msg);
  312. }
  313. }
  314. });
  315. } onError:^(NSError *error) {
  316. dispatch_async(dispatch_get_main_queue(), ^{
  317. NSString *json = [op responseJSON];
  318. id temp = [json mj_JSONObject];
  319. if ([temp isKindOfClass:[NSDictionary class]]) {
  320. NSDictionary *result = (NSDictionary *)temp;
  321. NSString *status = result[@"status"];
  322. NSString *msg = result[@"msg"];
  323. if (error.code == 401) {
  324. [DataUtil setLoginToken:@""];
  325. [weakSelf updateEngine];
  326. faild(status, msg);
  327. return;
  328. }
  329. faild(status, msg);
  330. return;
  331. }
  332. if (error.code == 401) {
  333. [DataUtil setLoginToken:@""];
  334. [weakSelf updateEngine];
  335. faild(@"-1", @"Plase Login");
  336. return;
  337. }
  338. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  339. });
  340. }];
  341. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  342. [self.engine enqueueOperation:op];
  343. });
  344. }
  345. + (void)debugRegist {
  346. NSDictionary *param = @{
  347. @"customer": @{
  348. @"email": @"abc123456789@qq.com",
  349. @"firstname": @"ujrbcf",
  350. @"lastname": @"serwt",
  351. @"dob": @"2023/10/20"
  352. },
  353. @"password": @"A123456a",
  354. @"extension_attributes" : @{
  355. @"is_subscribed": @(true)
  356. }
  357. };
  358. [ASNetTools.shared postWithPath:registerUrl param:param success:^(id _Nonnull result) {
  359. NSLog(@"----url:%@-----result:%@------", registerUrl, result);
  360. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  361. NSLog(@"----url:%@-----code:%@----msg:%@--", registerUrl, code, msg);
  362. }];
  363. }
  364. + (void)login {
  365. NSDictionary *param = @{
  366. @"username" :@"abc123456789@qq.com",
  367. @"password":@"A123456a"
  368. };
  369. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  370. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  371. [DataUtil setLoginToken:token];
  372. [ASNetTools.shared updateEngine];
  373. [ASUserInfoManager.shared getInfo];
  374. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  375. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  376. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  377. }];
  378. }
  379. +(void)xxx_loginWithParam:(NSDictionary *)param
  380. success:(void(^)(id))success
  381. faild:(void(^)(NSString *code, NSString *msg))faild {
  382. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  383. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  384. [DataUtil setLoginToken:token];
  385. [ASNetTools.shared updateEngine];
  386. [ASUserInfoManager.shared getInfo];
  387. success(result);
  388. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  389. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  390. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  391. faild(code,msg);
  392. }];
  393. }
  394. @end