AddressViewController.m 11 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9 10 11
//
//  AddressViewController.m
//  Lighting
//
//  Created by 曹云霄 on 16/5/10.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "AddressViewController.h"
#import "ModifyShippingAddressView.h"

勾芒's avatar
勾芒 committed
12
@interface AddressViewController ()<CityselectedDelegate>
曹云霄's avatar
曹云霄 committed
13 14 15 16 17


/**
 *  城市选择器
 */
勾芒's avatar
勾芒 committed
18
@property (nonatomic,strong) ModifyShippingAddressView *citySelecteview;
勾芒's avatar
勾芒 committed
19 20 21 22 23 24 25 26 27 28 29

/**
 *  选中省
 */
@property (nonatomic,copy) NSString *provincesString;

/**
 * 选中城市
 */
@property (nonatomic,copy) NSString *cityString;

曹云霄's avatar
曹云霄 committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
@end

@implementation AddressViewController


/**
 *  城市选择View
 *
 *  @return ModifyShippingAddressView
 */
- (ModifyShippingAddressView *)citySelecteview
{
    if (_citySelecteview == nil) {
        
        _citySelecteview = [[[NSBundle mainBundle] loadNibNamed:@"ModifyShippingAddressView"
勾芒's avatar
勾芒 committed
45
                                                          owner:self options:nil] lastObject];
曹云霄's avatar
曹云霄 committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    }
    return _citySelecteview;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self uiConfigAction];
}

#pragma mark -布局
- (void)uiConfigAction
{
    self.recipientUserBackview.layer.masksToBounds = YES;
    self.recipientUserBackview.layer.cornerRadius = kCornerRadius;
    self.phoneNumberBackview.layer.masksToBounds = YES;
    self.phoneNumberBackview.layer.cornerRadius = kCornerRadius;
    self.detailsAddressBackview.layer.masksToBounds = YES;
    self.detailsAddressBackview.layer.cornerRadius = kCornerRadius;
    self.citySelected.layer.masksToBounds = YES;
    self.citySelected.layer.cornerRadius = kCornerRadius;
勾芒's avatar
勾芒 committed
69 70 71 72
    self.delectAddressButton.layer.masksToBounds = YES;
    self.delectAddressButton.layer.cornerRadius = kCornerRadius;
    self.modifyButton.layer.masksToBounds = YES;
    self.modifyButton.layer.cornerRadius = kCornerRadius;
勾芒's avatar
勾芒 committed
73
    [self.citySelected addTarget:self action:@selector(SelectedCityButtonClick) forControlEvents:UIControlEventTouchUpInside];
勾芒's avatar
勾芒 committed
74 75
    //判断是修改还是新增
    if (_isChange) {
曹云霄's avatar
曹云霄 committed
76
        self.changAddressLabe.text = @"修改收货地址";
勾芒's avatar
勾芒 committed
77 78
        [self.delectAddressButton setTitle:@"删除" forState:UIControlStateNormal];
        [self.modifyButton setTitle:@"保存" forState:UIControlStateNormal];
曹云霄's avatar
曹云霄 committed
79 80 81
    }else
    {
        self.changAddressLabe.text = @"新增收货地址";
勾芒's avatar
勾芒 committed
82 83 84 85 86
    }
    self.recipientPerson.text = _model.name;
    self.PhoneNumber.text = _model.miblephone;
    [self.citySelected setTitle:_model.city forState:UIControlStateNormal];
    self.detailsAddress.text = _model.address;
曹云霄's avatar
曹云霄 committed
87 88 89 90 91
}

#pragma mark -取消弹出的圆角
- (void)viewWillAppear:(BOOL)animated
{
曹云霄's avatar
曹云霄 committed
92
    [super viewWillAppear:animated];
曹云霄's avatar
曹云霄 committed
93 94 95 96 97 98 99 100 101
    self.view.superview.layer.cornerRadius = 0;
}

#pragma mark -城市选择器
- (void)SelectedCityButtonClick
{
    
    self.citySelecteview.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 160);
    [self.view addSubview:self.citySelecteview];
勾芒's avatar
勾芒 committed
102
    self.citySelecteview.delegate = self;
曹云霄's avatar
曹云霄 committed
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 132 133
    [self.citySelecteview.selectedCityButton addTarget:self action:@selector(CompleteButton) forControlEvents:UIControlEventTouchUpInside];
    [UIView animateWithDuration:0.2 animations:^{
        self.citySelecteview.frame = CGRectMake(0, self.view.frame.size.height-160, self.view.frame.size.width, 160);
    }completion:^(BOOL finished) {
        self.citySelected.enabled = NO;
    }];
}


#pragma mark -完成按钮点击
- (void)CompleteButton
{
    [UIView animateWithDuration:0.2 animations:^{
        
        self.citySelecteview.frame = CGRectMake(0, ScreenHeight, self.view.frame.size.width, 160);
    }completion:^(BOOL finished) {
        
        [self.citySelecteview removeFromSuperview];
        self.citySelecteview = nil;
        self.citySelected.enabled = YES;
    }];
}


#pragma mark -关闭地址输入框
- (IBAction)turnoffAddressView:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


勾芒's avatar
勾芒 committed
134
#pragma mark 完成城市选择后
勾芒's avatar
勾芒 committed
135
- (void)citySelected:(NSString *)cityString WithprovincesString:(NSString *)provincesString
勾芒's avatar
勾芒 committed
136
{
勾芒's avatar
勾芒 committed
137 138
    self.cityString = cityString;
    self.provincesString = provincesString;
勾芒's avatar
勾芒 committed
139 140 141 142
    [self.citySelected setTitle:cityString forState:UIControlStateNormal];
}


勾芒's avatar
勾芒 committed
143
#pragma mark 新增按钮点击,或者保存
勾芒's avatar
勾芒 committed
144 145
- (IBAction)addAddressButtonClick:(UIButton *)sender {
    
勾芒's avatar
勾芒 committed
146
    NSString *phoneNumber = self.PhoneNumber.text;
147
    if ([BaseViewController isBlankString:self.recipientPerson.text]) {
曹云霄's avatar
曹云霄 committed
148
        [XBLoadingView showHUDViewWithText:@"请输入收货人姓名"];
曹云霄's avatar
曹云霄 committed
149 150
        return;
    }
151
    if ([BaseViewController isBlankString:phoneNumber]) {
曹云霄's avatar
曹云霄 committed
152
        [XBLoadingView showHUDViewWithText:@"手机号码不能为空"];
153 154
        return;
    }
勾芒's avatar
勾芒 committed
155 156
    if (![HENLENSONG isValidateMobile:phoneNumber]) {
        
曹云霄's avatar
曹云霄 committed
157
        [XBLoadingView showHUDViewWithText:@"手机号码格式不正确"];
勾芒's avatar
勾芒 committed
158 159
        return;
    }
160
    if ([BaseViewController isBlankString:self.detailsAddress.text]) {
曹云霄's avatar
曹云霄 committed
161
        [XBLoadingView showHUDViewWithText:@"请输入详细地址"];
162 163
        return;
    }
勾芒's avatar
勾芒 committed
164 165 166 167 168 169
    if ([sender.currentTitle isEqualToString:@"新增"]) {
       
        [self addAddressInformationRequest];
    }
    else if ([sender.currentTitle isEqualToString:@"保存"])
    {
勾芒's avatar
勾芒 committed
170
        [self ChangeAddressInformationRequest];
勾芒's avatar
勾芒 committed
171
    }
勾芒's avatar
勾芒 committed
172 173 174 175 176 177 178
}


#pragma mark -新增地址信息
- (void)addAddressInformationRequest
{
    TOShippingAddrEntity *address = [[TOShippingAddrEntity alloc]init];
勾芒's avatar
勾芒 committed
179
    address.consumerId = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
180 181 182
    address.name = self.recipientPerson.text;
    address.miblephone = self.PhoneNumber.text;
    address.city = [self.citySelected currentTitle];
勾芒's avatar
勾芒 committed
183 184 185
    if (![self.provincesString isEqualToString:[self.citySelected currentTitle]]) {
      address.province = self.provincesString;
    }
勾芒's avatar
勾芒 committed
186
    address.address = self.detailsAddress.text;
曹云霄's avatar
曹云霄 committed
187
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
188
    WS(weakSelf);
189
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(ADDADDRESS)  WithRequestType:ZERO WithParameter:address WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
190
        
曹云霄's avatar
曹云霄 committed
191
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
192 193 194
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            AddressModel *model = [[AddressModel alloc]init];
曹云霄's avatar
曹云霄 committed
195 196 197 198
            model.name = weakSelf.recipientPerson.text;
            model.miblephone = weakSelf.PhoneNumber.text;
            if ([[weakSelf.citySelected currentTitle] isEqualToString:weakSelf.provincesString]) {
                model.city = weakSelf.citySelected.currentTitle;
曹云霄's avatar
曹云霄 committed
199
                model.province = nil;
200 201
            }else
            {
曹云霄's avatar
曹云霄 committed
202 203
                model.city = weakSelf.citySelected.currentTitle;
                model.province = weakSelf.provincesString;
204
            }
曹云霄's avatar
曹云霄 committed
205
            model.address = weakSelf.detailsAddress.text;
勾芒's avatar
勾芒 committed
206
            model.consumerId = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
207 208 209
            model.fid = returnValue[@"data"];
            model.isSelected = NO;
            //增加地址
曹云霄's avatar
曹云霄 committed
210
            if ([weakSelf.delegate respondsToSelector:@selector(addAddressCell:)]) {
勾芒's avatar
勾芒 committed
211
                
曹云霄's avatar
曹云霄 committed
212
                [weakSelf.delegate addAddressCell:model];
勾芒's avatar
勾芒 committed
213 214 215 216
            }
        }
        else
        {
曹云霄's avatar
曹云霄 committed
217
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
218
        }
勾芒's avatar
勾芒 committed
219
        
220
    }WithFailureBlock:^(id error) {
曹云霄's avatar
曹云霄 committed
221
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
222 223
    }];
}
勾芒's avatar
勾芒 committed
224 225 226 227 228 229 230


#pragma mark -更改地址
- (void)ChangeAddressInformationRequest
{
    
    TOShippingAddrEntity *address = [[TOShippingAddrEntity alloc]init];
勾芒's avatar
勾芒 committed
231
    address.consumerId = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
232 233 234 235
    address.name = self.recipientPerson.text;
    address.miblephone = self.PhoneNumber.text;
    address.city = [self.citySelected currentTitle];
    address.address = self.detailsAddress.text;
勾芒's avatar
勾芒 committed
236 237 238
    if (![self.provincesString isEqualToString:[self.citySelected currentTitle]]) {
        address.province = self.provincesString;
    }
239
    address.fid = self.model.fid;
曹云霄's avatar
曹云霄 committed
240
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
241
    WS(weakSelf);
242
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(CHANGEADDRESS)  WithRequestType:ZERO WithParameter:address WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
243
        
曹云霄's avatar
曹云霄 committed
244
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
245 246 247
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            AddressModel *model = [[AddressModel alloc]init];
曹云霄's avatar
曹云霄 committed
248 249 250
            model.name = weakSelf.recipientPerson.text;
            model.miblephone = weakSelf.PhoneNumber.text;
            model.city = weakSelf.citySelected.currentTitle;
勾芒's avatar
勾芒 committed
251
            model.consumerId = [Customermanager manager].model.fid;
曹云霄's avatar
曹云霄 committed
252 253
            model.province = weakSelf.provincesString?weakSelf.provincesString:@"";
            model.fid = weakSelf.model.fid;
勾芒's avatar
勾芒 committed
254
            model.isSelected = NO;
曹云霄's avatar
曹云霄 committed
255
            //地址拼接
曹云霄's avatar
曹云霄 committed
256 257
            if (![weakSelf.provincesString isEqualToString:[weakSelf.citySelected currentTitle]]) {
                model.address = [NSString stringWithFormat:@"%@",weakSelf.detailsAddress.text];
曹云霄's avatar
曹云霄 committed
258 259
            }else
            {
曹云霄's avatar
曹云霄 committed
260
                model.address = [NSString stringWithFormat:@"%@",weakSelf.detailsAddress.text];
曹云霄's avatar
曹云霄 committed
261
            }
勾芒's avatar
勾芒 committed
262
            //修改地址
曹云霄's avatar
曹云霄 committed
263
            if ([weakSelf.delegate respondsToSelector:@selector(ChangeAddresscell:Withcellindex:)]) {
勾芒's avatar
勾芒 committed
264
                
曹云霄's avatar
曹云霄 committed
265
                [weakSelf.delegate ChangeAddresscell:model Withcellindex:_cellindex];
勾芒's avatar
勾芒 committed
266 267 268 269
            }
        }
        else
        {
曹云霄's avatar
曹云霄 committed
270
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
271 272 273
        }
        
    } WithFailureBlock:^(id error) {
曹云霄's avatar
曹云霄 committed
274
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
275 276 277 278
    }];
}


勾芒's avatar
勾芒 committed
279 280 281
#pragma mark -取消新增地址,或者删除
- (IBAction)cancelButtonClick:(UIButton *)sender {
    
勾芒's avatar
勾芒 committed
282 283 284 285 286 287 288 289
    if ([sender.currentTitle isEqualToString:@"取消"]) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if ([sender.currentTitle isEqualToString:@"删除"])
    {
        [self deleteAddressButtonClick];
    }
勾芒's avatar
勾芒 committed
290 291
}

曹云霄's avatar
曹云霄 committed
292

勾芒's avatar
勾芒 committed
293 294 295
#pragma mark -删除收货地址
- (void)deleteAddressButtonClick
{
曹云霄's avatar
曹云霄 committed
296
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
297
    WS(weakSelf);
298
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(REMOVEADDRESS),_model.fid]  WithRequestType:ONE WithParameter:nil WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
299
        
曹云霄's avatar
曹云霄 committed
300
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
301
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
302 303 304 305 306 307
            [XBLoadingView showHUDViewWithSuccessText:@"删除成功" completeBlock:^{
                if ([weakSelf.delegate respondsToSelector:@selector(delecteCell:)]) {
                    [weakSelf.delegate delecteCell:_model.fid];
                }
            }];

勾芒's avatar
勾芒 committed
308 309
        }else
        {
曹云霄's avatar
曹云霄 committed
310
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
311 312
        }
        
313
    }WithFailureBlock:^(id error) {
曹云霄's avatar
曹云霄 committed
314
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
315 316 317
    }];
}

曹云霄's avatar
曹云霄 committed
318 319 320 321 322 323 324 325 326 327 328 329

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end