ASNetTools.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. // get 请求
  117. - (void)getWithPath:(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:@"GET" ssl:isSSL];
  123. __weak typeof(self) weakSelf = self;
  124. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  125. dispatch_async(dispatch_get_main_queue(), ^{
  126. NSString *json = [op responseJSON];
  127. id temp = [json mj_JSONObject];
  128. if ([temp isKindOfClass:[NSDictionary class]]) {
  129. NSDictionary *result = (NSDictionary *)temp;
  130. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  131. id data = result[@"data"];
  132. if ([status isEqualToString:@"1"]) {
  133. success(data);
  134. } else {
  135. NSString *msg = result[@"msg"];
  136. faild(status, msg);
  137. }
  138. }
  139. });
  140. } onError:^(NSError *error) {
  141. dispatch_async(dispatch_get_main_queue(), ^{
  142. NSString *json = [op responseJSON];
  143. id temp = [json mj_JSONObject];
  144. if ([temp isKindOfClass:[NSDictionary class]]) {
  145. NSDictionary *result = (NSDictionary *)temp;
  146. NSString *status = result[@"status"];
  147. NSString *msg = result[@"msg"];
  148. if (error.code == 401) {
  149. [DataUtil setLoginToken:@""];
  150. [weakSelf updateEngine];
  151. faild(status, msg);
  152. return;
  153. }
  154. faild(status, msg);
  155. return;
  156. }
  157. if (error.code == 401) {
  158. [DataUtil setLoginToken:@""];
  159. [weakSelf updateEngine];
  160. faild(@"-1", @"Plase Login");
  161. return;
  162. }
  163. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  164. });
  165. }];
  166. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  167. [self.engine enqueueOperation:op];
  168. });
  169. }
  170. // put 请求
  171. - (void)putWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  172. BOOL isSSL = true;
  173. //#if (DEBUG)
  174. // isSSL = false;
  175. //#endif
  176. [self checkToken];
  177. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  178. [tempDic addEntriesFromDictionary:param];
  179. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  180. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  181. __weak typeof(self) weakSelf = self;
  182. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  183. dispatch_async(dispatch_get_main_queue(), ^{
  184. NSString *json = [op responseJSON];
  185. id temp = [json mj_JSONObject];
  186. if ([temp isKindOfClass:[NSDictionary class]]) {
  187. NSDictionary *result = (NSDictionary *)temp;
  188. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  189. id data = result[@"data"];
  190. if ([status isEqualToString:@"1"]) {
  191. success(data);
  192. } else {
  193. NSString *msg = result[@"msg"];
  194. faild(status, msg);
  195. }
  196. }
  197. });
  198. } onError:^(NSError *error) {
  199. dispatch_async(dispatch_get_main_queue(), ^{
  200. NSString *json = [op responseJSON];
  201. id temp = [json mj_JSONObject];
  202. if ([temp isKindOfClass:[NSDictionary class]]) {
  203. NSDictionary *result = (NSDictionary *)temp;
  204. NSString *status = result[@"status"];
  205. NSString *msg = result[@"msg"];
  206. if (error.code == 401) {
  207. [DataUtil setLoginToken:@""];
  208. [weakSelf updateEngine];
  209. faild(status, msg);
  210. return;
  211. }
  212. faild(status, msg);
  213. return;
  214. }
  215. if (error.code == 401) {
  216. [DataUtil setLoginToken:@""];
  217. [weakSelf updateEngine];
  218. faild(@"-1", @"Plase Login");
  219. return;
  220. }
  221. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  222. });
  223. }];
  224. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  225. [self.engine enqueueOperation:op];
  226. });
  227. }
  228. // del 请求
  229. - (void)delWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  230. BOOL isSSL = true;
  231. //#if (DEBUG)
  232. // isSSL = false;
  233. //#endif
  234. [self checkToken];
  235. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  236. [tempDic addEntriesFromDictionary:param];
  237. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  238. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  239. __weak typeof(self) weakSelf = self;
  240. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  241. dispatch_async(dispatch_get_main_queue(), ^{
  242. NSString *json = [op responseJSON];
  243. id temp = [json mj_JSONObject];
  244. if ([temp isKindOfClass:[NSDictionary class]]) {
  245. NSDictionary *result = (NSDictionary *)temp;
  246. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  247. id data = result[@"data"];
  248. if ([status isEqualToString:@"1"]) {
  249. success(data);
  250. } else {
  251. NSString *msg = result[@"msg"];
  252. faild(status, msg);
  253. }
  254. }
  255. });
  256. } onError:^(NSError *error) {
  257. dispatch_async(dispatch_get_main_queue(), ^{
  258. NSString *json = [op responseJSON];
  259. id temp = [json mj_JSONObject];
  260. if ([temp isKindOfClass:[NSDictionary class]]) {
  261. NSDictionary *result = (NSDictionary *)temp;
  262. NSString *status = result[@"status"];
  263. NSString *msg = result[@"msg"];
  264. if (error.code == 401) {
  265. [DataUtil setLoginToken:@""];
  266. [weakSelf updateEngine];
  267. faild(status, msg);
  268. return;
  269. }
  270. faild(status, msg);
  271. return;
  272. }
  273. if (error.code == 401) {
  274. [DataUtil setLoginToken:@""];
  275. [weakSelf updateEngine];
  276. faild(@"-1", @"Plase Login");
  277. return;
  278. }
  279. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  280. });
  281. }];
  282. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  283. [self.engine enqueueOperation:op];
  284. });
  285. }
  286. + (void)debugRegist {
  287. NSDictionary *param = @{
  288. @"customer": @{
  289. @"email": @"abc123456@qq.com",
  290. @"firstname": @"ujrbcf",
  291. @"lastname": @"serwt",
  292. @"dob": @"2023/10/20"
  293. },
  294. @"password": @"a123456A",
  295. @"extension_attributes" : @{
  296. @"is_subscribed": @(true)
  297. }
  298. };
  299. [ASNetTools.shared postWithPath:registerUrl param:param success:^(id _Nonnull result) {
  300. NSLog(@"----url:%@-----result:%@------", registerUrl, result);
  301. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  302. NSLog(@"----url:%@-----code:%@----msg:%@--", registerUrl, code, msg);
  303. }];
  304. }
  305. + (void)login {
  306. NSDictionary *param = @{
  307. @"username" :@"abc123456@qq.com",
  308. @"password":@"a123456A"
  309. };
  310. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  311. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  312. [DataUtil setLoginToken:token];
  313. [ASNetTools.shared updateEngine];
  314. [ASUserInfoManager.shared getInfo];
  315. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  316. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  317. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  318. }];
  319. }
  320. @end