1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// KWMDiscountVC.m
// iCemarose
//
// Created by HouWeiBin on 16/9/2.
// Copyright © 2016年 kollway. All rights reserved.
//
#import "KWMDiscountVC.h"
#import "KWMStringUtil.h"
@interface KWMDiscountVC ()
@end
@implementation KWMDiscountVC
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"输入优惠码";
[self initDiscountTF];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackgroundView:)];
[self.view addGestureRecognizer:tapGesture];
}
- (void)onClickBackgroundView:(UITapGestureRecognizer *)tapGesture{
[self.tfDiscount resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
+(NSString *)kwmTag{
return @"KWMDiscountVC";
}
//初始化優惠碼textfiled
- (void)initDiscountTF{
UILabel *leftPaddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 13, 25)];
UILabel *rightPaddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];
self.tfDiscount.leftView = leftPaddingView;
self.tfDiscount.rightView = rightPaddingView;
self.tfDiscount.leftViewMode = UITextFieldViewModeAlways;
self.tfDiscount.rightViewMode = UITextFieldViewModeAlways;
[self.tfDiscount addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
self.tfDiscount.delegate = self;
}
-(void)textFieldDidChange :(UITextField *)theTextField{
if([KWMStringUtil isEmpty:theTextField.text]){
self.btnClear.hidden = YES;
}else{
self.btnClear.hidden = NO;
}
NSLog( @"text changed: %@", theTextField.text);
}
-(void)onClickClear:(id)sender{
self.tfDiscount.text = @"";
self.btnClear.hidden = YES;
}
-(void)onClickAdd:(id)sender{
NSString *code = self.tfDiscount.text;
if([KWMStringUtil isEmpty:code]){
[self showToast:@"请输入优惠码~"];
}else{
[self addDiscountVC:code];
}
}
-(void)addDiscountVC:(NSString *)code{
if([KWMStringUtil isEmpty:code]){
return;
}
[self showLoading];
BUYDiscount *discount = [self.client.modelManager discountWithCode:code];
self.checkout.discount = discount;
self.checkout.attributes = nil;
[self.client updateCheckout:self.checkout completion:^(NSDictionary *dictionary, BUYCheckout *checkout, NSError *error) {
[self hideLoading];
if (error == nil && checkout) {
[self showToast:@"添加优惠码成功"];
NSLog(@"Successfully added discount");
self.checkout = checkout;
if(self.delegate != nil){
[self.delegate kwm_addedDiscount:checkout];
}
[self.navigationController popViewControllerAnimated:YES];
}
else {
self.checkout.discount = nil;
[self clearDiscount];
if(self.delegate != nil){
[self.delegate kwm_addedDiscount:self.checkout];
}
// 八成是因为checkout是web生成的所以如果discount如果传的有问题返回的是500如果没问题的话是正常的
if ([@"Internal Server Error" isEqualToString:error.userInfo[@"errors"]]) {
[self showToast:@"您输入的优惠码有误"];
}else {
[self showError:error];
}
// [self showToast:@"添加优惠码失败"];
NSLog(@"Error applying checkout: %@", error);
}
}];
}
-(void)clearDiscount{
[self showLoading];
[self.client updateCheckout:self.checkout completion:^(NSDictionary *dictionary, BUYCheckout *checkout, NSError *error) {
[self hideLoading];
}];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
@end