Commit fdfa3996 by lee

购物车修改完成

parent 6c9ff8a5
...@@ -103,7 +103,10 @@ ...@@ -103,7 +103,10 @@
* *
* @return <#return value description#> * @return <#return value description#>
*/ */
- (NSURLSessionDataTask *) updateProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity success:(void(^)(NSURLSessionDataTask *task,KWMCartResult *result)) success failure:(void(^)(NSURLSessionDataTask *task,NSError *error)) failure; - (NSURLSessionDataTask *) changeProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity success:(void(^)(NSURLSessionDataTask *task,KWMCartResult *result)) success failure:(void(^)(NSURLSessionDataTask *task,NSError *error)) failure;
-(NSURLSessionDataTask *) updateProductWithVariantIds:(NSArray<NSNumber *> *) variantIds quantities:(NSArray<NSNumber *> *) quantities success:(void(^)(NSURLSessionDataTask *task,KWMCartResult *result)) success failure:(void(^)(NSURLSessionDataTask *task,NSError *error)) failure;
/** /**
* 获取系统购物车 * 获取系统购物车
* *
......
...@@ -419,12 +419,21 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651"; ...@@ -419,12 +419,21 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
failure:failure]; failure:failure];
} }
- (NSURLSessionDataTask *)updateProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger) quantity success:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { - (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]; NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/change/%@.js",Shopify_SHOP_DOMAIN,variantId];
id params = @{ @"quantity": @(quantity) }; id params = @{ @"quantity": @(quantity) };
return [self startSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:params result:[KWMCartResult class] model:nil success:success failure:failure]; return [self startSessionTask: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 {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/update.js",Shopify_SHOP_DOMAIN];
NSMutableDictionary *params = [NSMutableDictionary new];
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];
}
- (NSURLSessionDataTask *)addProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity success:(void (^)(NSURLSessionDataTask *, KWMRequestResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { - (NSURLSessionDataTask *)addProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity success:(void (^)(NSURLSessionDataTask *, KWMRequestResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/add.js",Shopify_SHOP_DOMAIN]; NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/add.js",Shopify_SHOP_DOMAIN];
id params = @{ id params = @{
......
...@@ -24,7 +24,9 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart); ...@@ -24,7 +24,9 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart);
- (void)increaseProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity callback:(ShoppingCartCallBack) callback; - (void)increaseProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity callback:(ShoppingCartCallBack) callback;
- (void)updateProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity callback:(ShoppingCartCallBack) callback; - (void)changeProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity callback:(ShoppingCartCallBack) callback;
- (void)updateProductWithVariantIds:(NSArray<NSNumber *> *) variantIds quantitties:(NSArray<NSNumber *> *) quantites callback:(ShoppingCartCallBack) callback;
- (void)deleteProductWithVariantId:(NSNumber *) variantId callback:(ShoppingCartCallBack) callback; - (void)deleteProductWithVariantId:(NSNumber *) variantId callback:(ShoppingCartCallBack) callback;
......
...@@ -63,40 +63,55 @@ ...@@ -63,40 +63,55 @@
} }
- (void)increaseProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback { - (void)increaseProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
[[KWMAPIManager sharedManager] addProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMRequestResult *result) { [[KWMAPIManager sharedManager] addProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMRequestResult *result) {
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) { [[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
// callback(error,cart); callback(error,cart);
}]; }];
} failure:^(NSURLSessionDataTask *task, NSError *error) { } failure:^(NSURLSessionDataTask *task, NSError *error) {
// callback(error,nil); // callback(error,nil);
}]; }];
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
} }
- (void)updateProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback { - (void)changeProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
[[KWMAPIManager sharedManager] changeProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[[KWMShoppingCart sharedInstance] setResult:result];
callback(nil,result);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
// callback(error,nil);
}];
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:quantity ? 2 : 0]); callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:quantity ? 2 : 0]);
[[KWMAPIManager sharedManager] updateProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) { }
-(void)updateProductWithVariantIds:(NSArray<NSNumber *> *)variantIds quantitties:(NSArray<NSNumber *> *)quantites callback:(ShoppingCartCallBack)callback {
[[KWMAPIManager sharedManager] updateProductWithVariantIds:variantIds quantities:quantites success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[[KWMShoppingCart sharedInstance] setResult:result]; [[KWMShoppingCart sharedInstance] setResult:result];
// callback(nil,result); callback(nil,result);
} failure:^(NSURLSessionDataTask *task, NSError *error) { } failure:^(NSURLSessionDataTask *task, NSError *error) {
// callback(error,nil); // callback(error,nil);
}]; }];
for (int i=0; i<quantites.count; i++) {
[self changeLocalCartWithVariantId:variantIds[i] quantity:quantites[i].integerValue flag:quantites[i].integerValue ? 2 : 0];
}
callback(nil,self.result);
} }
- (void)deleteProductWithVariantId:(NSNumber *)variantId callback:(ShoppingCartCallBack)callback { - (void)deleteProductWithVariantId:(NSNumber *)variantId callback:(ShoppingCartCallBack)callback {
[self updateProductWithVariantId:variantId quantity:0 callback:callback]; [self changeProductWithVariantId:variantId quantity:0 callback:callback];
} }
- (KWMCartResult *) changeLocalCartWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity flag:(NSInteger) flag { - (KWMCartResult *) changeLocalCartWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity flag:(NSInteger) flag {
NSMutableArray<KWMShopCartModel *> *items= self.items; NSMutableArray<KWMShopCartModel *> *items= self.items;
NSInteger count = self.result.item_count.integerValue; NSInteger count = self.result.item_count.integerValue;
BOOL processed = NO; BOOL processed = NO;
for (KWMShopCartModel *item in items) { NSUInteger size = items.count;
for (int i=0;i < size; i++) {
KWMShopCartModel *item = items[i];
if (item.identifier.integerValue == variantId.integerValue) { if (item.identifier.integerValue == variantId.integerValue) {
switch (flag) { switch (flag) {
case 0: case 0:
count -= item.quantity; count -= item.quantity;
[items removeObject:item]; [self.items removeObjectAtIndex:i];
break; break;
case 1: case 1:
count += quantity; count += quantity;
...@@ -115,7 +130,8 @@ ...@@ -115,7 +130,8 @@
model.identifier = variantId.stringValue; model.identifier = variantId.stringValue;
model.quantity = quantity; model.quantity = quantity;
count += quantity; count += quantity;
[items addObject:model]; // [items addObject:model];
[items insertObject:model atIndex:0];
} }
self.result.item_count = @(count); self.result.item_count = @(count);
return self.result; return self.result;
......
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
[super viewDidLoad]; [super viewDidLoad];
[self initScrollView]; [self initScrollView];
[self initRecommendProduct]; [self initRecommendProduct];
[self initShopCartCount]; // [self initShopCartCount];
//刷新product,如果是缓存的product,因为没有字典,所以会发生错误,需要重新请求一个有字典的product //刷新product,如果是缓存的product,因为没有字典,所以会发生错误,需要重新请求一个有字典的product
if(self.product && self.product.identifier && !self.product.JSONDictionary){ if(self.product && self.product.identifier && !self.product.JSONDictionary){
[self getProductBy:nil product:self.product]; [self getProductBy:nil product:self.product];
...@@ -71,6 +71,7 @@ ...@@ -71,6 +71,7 @@
- (void)viewWillAppear:(BOOL)animated{ - (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated]; [super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES]; [self.navigationController setNavigationBarHidden:YES animated:YES];
[self initShopCartCount];
} }
- (void)viewWillDisappear:(BOOL)animated{ - (void)viewWillDisappear:(BOOL)animated{
...@@ -213,18 +214,25 @@ ...@@ -213,18 +214,25 @@
#pragma mark - KWMSelectSizeVCDelegate #pragma mark - KWMSelectSizeVCDelegate
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback { - (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity variant:(BUYProductVariant *) variant buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
__weak KWMProductDetailVC *this = self; __weak KWMProductDetailVC *this = self;
if (!buyNow) { if (!buyNow) {
[[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:^(NSError *error, KWMCartResult *cart) { [[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:^(NSError *error, KWMCartResult *cart) {
if (!error) { if (cart.items.firstObject && !cart.items.firstObject.product_id) {
[this showToast:@"添加成功"]; KWMShopCartModel *model = cart.items.firstObject;
[this.btnShopCart setTitle:cart.item_count.stringValue forState:UIControlStateNormal]; model.imageStr = [KWMImageUtil getProductImageUrl:this.product.identifier ImageSize:NormalImage];
callback(YES); model.name = this.product.title;
}else{ model.size = variant.title;
[this showError:error]; model.price = variant.price.floatValue;
callback(NO); }
} if (!error) {
[this showToast:@"添加成功"];
[this.btnShopCart setTitle:cart.item_count.stringValue forState:UIControlStateNormal];
callback(YES);
}else{
[this showError:error];
callback(NO);
}
}]; }];
return; return;
} }
...@@ -249,27 +257,27 @@ ...@@ -249,27 +257,27 @@
} }
//KWMSelectSizeVCDelegate ////KWMSelectSizeVCDelegate
-(void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{ //-(void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{
[self initShopCartCount]; // [self initShopCartCount];
} //}
//
-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{ //-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{
if(fastBuyCart == nil){ // if(fastBuyCart == nil){
return; // return;
} // }
KWMUserModel *useModel = [KWMUserModel shareUser]; // KWMUserModel *useModel = [KWMUserModel shareUser];
if (useModel.status) { // if (useModel.status) {
KWMBeforePayVC *beforePayVC = (KWMBeforePayVC *)[KWMBaseVC findControllerBy:[KWMBeforePayVC kwmTag] fromStoryboard:@"ShopCart"]; // KWMBeforePayVC *beforePayVC = (KWMBeforePayVC *)[KWMBaseVC findControllerBy:[KWMBeforePayVC kwmTag] fromStoryboard:@"ShopCart"];
beforePayVC.fastPayCart = fastBuyCart; // beforePayVC.fastPayCart = fastBuyCart;
[self.navigationController pushViewController:beforePayVC animated:YES];} // [self.navigationController pushViewController:beforePayVC animated:YES];}
//用户未登录 // //用户未登录
else{ // else{
KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"]; // KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc]; // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
[self presentViewController:nav animated:YES completion:nil]; // [self presentViewController:nav animated:YES completion:nil];
} // }
} //}
#pragma mark - KWMSelectSizeVCDelegate End #pragma mark - KWMSelectSizeVCDelegate End
......
...@@ -15,11 +15,11 @@ ...@@ -15,11 +15,11 @@
@protocol KWMSelectSizeVCDelegate <NSObject> @protocol KWMSelectSizeVCDelegate <NSObject>
- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel; //- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel;
//
- (void)kwm_fastBuy:(BUYCart *)fastBuyCart; //- (void)kwm_fastBuy:(BUYCart *)fastBuyCart;
- (void)kwm_addShopWithVariantId:(NSNumber *) identifier quantity:(NSInteger) quantity buyNow:(BOOL) buyNow callback:(void(^)(BOOL)) callback; - (void)kwm_addShopWithVariantId:(NSNumber *) identifier quantity:(NSInteger) quantity variant:(BUYProductVariant *) variant buyNow:(BOOL) buyNow callback:(void(^)(BOOL)) callback;
@end @end
@interface KWMSelectSizeVC : KWMBaseVC<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,KWMSizeDelegate> @interface KWMSelectSizeVC : KWMBaseVC<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,KWMSizeDelegate>
......
...@@ -200,7 +200,7 @@ ...@@ -200,7 +200,7 @@
__weak id this = self; __weak id this = self;
[self showLoading]; [self showLoading];
if (self.delegate) { if (self.delegate) {
[self.delegate kwm_addShopWithVariantId:_selectSize.identifier quantity:_count buyNow:buyNow callback:^(BOOL success) { [self.delegate kwm_addShopWithVariantId:_selectSize.identifier quantity:_count variant:_selectSize buyNow:buyNow callback:^(BOOL success) {
[self hideLoading]; [self hideLoading];
if(success) { if(success) {
[this dismissViewControllerAnimated:YES completion:nil]; [this dismissViewControllerAnimated:YES completion:nil];
......
...@@ -156,7 +156,7 @@ ...@@ -156,7 +156,7 @@
#pragma mark -- KWMSelectSizeVCDelegate #pragma mark -- KWMSelectSizeVCDelegate
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback { - (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity variant:(BUYProductVariant *) variant buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
__weak KWMShopCartVC *this = self; __weak KWMShopCartVC *this = self;
void(^success)(NSError*,KWMCartResult*) = ^(NSError *error,KWMCartResult *cart){ void(^success)(NSError*,KWMCartResult*) = ^(NSError *error,KWMCartResult *cart){
if (!error) { if (!error) {
...@@ -168,27 +168,53 @@ ...@@ -168,27 +168,53 @@
} }
}; };
if (_changeModel.identifier.integerValue == identifier.integerValue) { if (_changeModel.identifier.integerValue == identifier.integerValue) {
[[KWMShoppingCart sharedInstance] updateProductWithVariantId:identifier quantity:quantity callback:success]; [[KWMShoppingCart sharedInstance] changeProductWithVariantId:identifier quantity:quantity callback:success];
}else{ }else{
NSNumber *oldId = @(_changeModel.identifier.integerValue); NSNumber *oldId = @(_changeModel.identifier.integerValue);
KWMShopCartModel *model = _changeModel;
_changeModel = nil; _changeModel = nil;
[[KWMShoppingCart sharedInstance] deleteProductWithVariantId:oldId callback:^(NSError *error, KWMCartResult *cart) { NSArray<NSNumber *> *variantIds = @[oldId,identifier];
[[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:success]; NSArray<NSNumber *> *quantities = @[@0,@(quantity)];
[[KWMShoppingCart sharedInstance] updateProductWithVariantIds:variantIds quantitties:quantities callback:^(NSError *error, KWMCartResult *cart) {
if (!cart.items.firstObject.product_id) {
KWMShopCartModel *tmp = cart.items.firstObject;
tmp.imageStr = model.imageStr;
tmp.size = variant.title;
tmp.brand = model.brand;
tmp.name = model.name;
tmp.price = variant.price.floatValue;
tmp.product_id = model.product_id;
}
success(error,cart);
}]; }];
// [[KWMShoppingCart sharedInstance] deleteProductWithVariantId:oldId callback:^(NSError *error, KWMCartResult *cart) {
// [[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:^(NSError *error, KWMCartResult *cart) {
// if (!cart.items.lastObject.product_id) {
// KWMShopCartModel *tmp = cart.items.lastObject;
// tmp.imageStr = model.imageStr;
// tmp.size = variant.title;
// tmp.brand = model.brand;
// tmp.name = model.name;
// tmp.price = variant.price.floatValue;
// tmp.product_id = model.product_id;
// }
// success(error,cart);
// }];
// }];
} }
} }
- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{ //- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{
//
[_shopCartList removeAllObjects]; // [_shopCartList removeAllObjects];
[_shopCartList addObjectsFromArray:[[KWMShopCartData alloc] getALLItems]]; // [_shopCartList addObjectsFromArray:[[KWMShopCartData alloc] getALLItems]];
[self.tbvCart reloadData]; // [self.tbvCart reloadData];
[self setTotalPrice]; // [self setTotalPrice];
} //}
//
-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{ //-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{
//
} //}
-(void)kwm_deleteProduct{ -(void)kwm_deleteProduct{
// BOOL isHas = NO; // BOOL isHas = NO;
......
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