Commit 0dc28325 by lee

u

parent ecf2e870
......@@ -2162,7 +2162,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
F84386F9A53E1205C21D5570 /* [CP] Copy Pods Resources */ = {
......
......@@ -329,6 +329,30 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
failure:failure];
}
- (NSURLSessionDataTask *)makeSessionTask:(KWMHTTPMethod)httpMethod
apiPath:(NSString *)apiPath
parameters:(NSDictionary *)parameters
result:(Class)result
model:(Class)model
success:(void (^)(NSURLSessionDataTask *task, id result))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
void(^successCallback)(NSURLSessionDataTask *, id ) = [self buildSuccessCallbackWithResultClass:result
modelClass:model
success:success
failure:failure];
AFHTTPSessionManager *sessionManager = [self buildSessionManager:NO];
NSString *method = httpMethod == KWMHTTPMethodPOST ? @"POST" : @"GET";
NSURLRequest *request = [sessionManager.requestSerializer requestWithMethod:method URLString:apiPath parameters:parameters error:nil];
__block id task = [sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
failure(task,error);
}else{
successCallback(task,responseObject);
}
}];
return task;
}
- (NSURLSessionDataTask *)startSessionTask:(KWMHTTPMethod)httpMethod
apiPath:(NSString *)apiPath
......@@ -371,6 +395,7 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
if (!password) {
return;
}
NSLog(@"loginTask username:%@,password:%@",account,password);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
......@@ -420,10 +445,12 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
failure:failure];
}
#pragma mark - 购物车接口
- (NSURLSessionDataTask *)changeProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger) quantity success:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/change/%@.js",Shopify_SHOP_DOMAIN,variantId];
id params = @{ @"quantity": @(quantity) };
return [self startSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:params result:[KWMCartResult class] model:nil success:success failure:failure];
return [self makeSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:params result:[KWMCartResult class] model:nil success:success failure:failure];
}
- (NSURLSessionDataTask *)updateProductWithVariantIds:(NSArray<NSNumber *> *)variantIds quantities:(NSArray<NSNumber *> *)quantities success:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
......@@ -432,7 +459,7 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
for (int i=0; i<quantities.count; i++) {
[params setObject:quantities[i] forKey:[NSString stringWithFormat:@"updates[%@]",variantIds[i]]];
}
return [self startSessionTask:KWMHTTPMethodPOST apiPath:apiPath parameters:params result:[KWMCartResult class] model:nil success:success failure:failure];
return [self makeSessionTask:KWMHTTPMethodPOST apiPath:apiPath parameters:params result:[KWMCartResult class] model:nil success:success failure:failure];
}
- (NSURLSessionDataTask *)addProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity success:(void (^)(NSURLSessionDataTask *, KWMRequestResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
......@@ -441,7 +468,12 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
@"id":variantId,
@"quantity":@(quantity),
};
return [self startSessionTask:KWMHTTPMethodPOST apiPath:apiPath parameters:params result:[KWMDictionaryResult class] model:nil success:success failure:failure];
return [self makeSessionTask:KWMHTTPMethodPOST apiPath:apiPath parameters:params result:[KWMDictionaryResult class] model:nil success:success failure:failure];
}
-(NSURLSessionDataTask *)getCartSuccess:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart.js",Shopify_SHOP_DOMAIN];
return [self makeSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:nil result:[KWMCartResult class] model:nil success:success failure:failure];
}
- (NSURLSessionDataTask *)tmpCartWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity success:(void (^)(NSURLSessionDataTask *, NSString *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
......@@ -461,11 +493,6 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
} failure:failure];
}
-(NSURLSessionDataTask *)getCartSuccess:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart.js",Shopify_SHOP_DOMAIN];
return [self startSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:nil result:[KWMCartResult class] model:nil success:success failure:failure];
}
/**
* 手机登录-获取短信API
* @param parameters {mobile=?手机号}
......
......@@ -9,11 +9,13 @@
#import "KWMShoppingCart.h"
#import "KWMCartResult.h"
#import "KWMShopCartData.h"
#import <RegexKitLite/RegexKitLite.h>
@interface KWMShoppingCart ()
@property (nonatomic, strong) KWMCartResult *result;
@property (nonatomic, assign) CGFloat version;
//@property (nonatomic, assign) CGFloat version;
@property (nonatomic, strong) NSMutableArray<NSURLSessionDataTask *> *tasks;
@end
......@@ -41,6 +43,7 @@
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
instance = [[super allocWithZone:NULL] init];
instance.tasks = [[NSMutableArray alloc] init];
});
if (!instance.result) {
[instance setResultNoSync:[KWMCartResult new]];
......@@ -54,63 +57,91 @@
return instance;
}
- (void) missionCompleted {
[self.tasks removeObjectAtIndex:0];
[self.tasks.firstObject resume];
}
- (void) addTasksObject:(NSURLSessionDataTask *)object {
if (self.tasks.lastObject && [self.tasks.lastObject.currentRequest.URL.absoluteString isEqualToString:object.currentRequest.URL.absoluteString] && [object.currentRequest.URL.absoluteString isMatchedByRegex:@"cart.js$"]) {
return;
}
[self.tasks addObject:object];
if (self.tasks.count == 1) {
[object resume];
}
}
- (void)allItemsWithCallback:(ShoppingCartCallBack)callback {
CGFloat version = [[NSDate date] timeIntervalSince1970];
_version = version;
// CGFloat version = [[NSDate date] timeIntervalSince1970];
// _version = version;
__weak typeof(self) this = self;
[[KWMAPIManager sharedManager] getCartSuccess:^(NSURLSessionDataTask *task, KWMCartResult *result) {
if (version == this.version) {
id task = [[KWMAPIManager sharedManager] getCartSuccess:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[this missionCompleted];
// if (version == this.version) {
[[KWMShoppingCart sharedInstance] setResult:result];
callback(nil,result);
}
// }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[this missionCompleted];
callback(error,nil);
}];
[self addTasksObject:task];
}
- (void)increaseProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
CGFloat version = [[NSDate date] timeIntervalSince1970];
_version = version;
// CGFloat version = [[NSDate date] timeIntervalSince1970];
// _version = version;
__weak typeof(self) this = self;
[[KWMAPIManager sharedManager] addProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMRequestResult *result) {
if (version == this.version) {
id task = [[KWMAPIManager sharedManager] addProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMRequestResult *result) {
[this missionCompleted];
// if (version == this.version) {
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
// callback(error,cart);
}];
}
// }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[this missionCompleted];
// callback(error,nil);
}];
[self addTasksObject:task];
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
}
- (void)changeProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
CGFloat version = [[NSDate date] timeIntervalSince1970];
_version = version;
// CGFloat version = [[NSDate date] timeIntervalSince1970];
// _version = version;
__weak typeof(self) this = self;
[[KWMAPIManager sharedManager] changeProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
if (version == this.version) {
id task = [[KWMAPIManager sharedManager] changeProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[this missionCompleted];
// if (version == this.version) {
[[KWMShoppingCart sharedInstance] setResult:result];
// callback(nil,result);
}
// }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[this missionCompleted];
// callback(error,nil);
}];
[self addTasksObject:task];
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:quantity ? 2 : 0]);
}
-(void)updateProductWithVariantIds:(NSArray<NSNumber *> *)variantIds quantitties:(NSArray<NSNumber *> *)quantites callback:(ShoppingCartCallBack)callback {
CGFloat version = [[NSDate date] timeIntervalSince1970];
_version = version;
// CGFloat version = [[NSDate date] timeIntervalSince1970];
// _version = version;
__weak typeof(self) this = self;
[[KWMAPIManager sharedManager] updateProductWithVariantIds:variantIds quantities:quantites success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
if (version == this.version) {
id task = [[KWMAPIManager sharedManager] updateProductWithVariantIds:variantIds quantities:quantites success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[this missionCompleted];
// if (version == this.version) {
[[KWMShoppingCart sharedInstance] setResult:result];
// callback(nil,result);
}
// }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[this missionCompleted];
// callback(error,nil);
}];
[self addTasksObject:task];
for (int i=0; i<quantites.count; i++) {
[self changeLocalCartWithVariantId:variantIds[i] quantity:quantites[i].integerValue flag:quantites[i].integerValue ? 2 : 0];
}
......
......@@ -357,6 +357,7 @@
- (void)saveData{
[_userModel login];
_userModel.name = _phoneNum;
_userModel.email = _customer.JSONDictionary[@"email"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_customer.JSONDictionary forKey:@"customer"];
[defaults setObject:_token.JSONDictionary forKey:@"token"];
......@@ -441,8 +442,8 @@
NSLog(@"登录成功");
//异步保存数据
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.api loginWebSuccess:nil failure:nil];
[self saveData];
[self.api loginWebSuccess:nil failure:nil];
});
[self dismissViewControllerAnimated:YES completion:nil];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment