AddressViewController.m 10.5 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 76 77 78 79
    //判断是修改还是新增
    if (_isChange) {
        
        [self.delectAddressButton setTitle:@"删除" forState:UIControlStateNormal];
        [self.modifyButton setTitle:@"保存" forState:UIControlStateNormal];
    }
曹云霄's avatar
曹云霄 committed
80
    
勾芒's avatar
勾芒 committed
81 82 83 84
    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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

#pragma mark -取消弹出的圆角
- (void)viewWillAppear:(BOOL)animated
{
    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
101
    self.citySelecteview.delegate = self;
曹云霄's avatar
曹云霄 committed
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 132 133 134 135
    [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
136
#pragma mark 完成城市选择后
勾芒's avatar
勾芒 committed
137
- (void)citySelected:(NSString *)cityString WithprovincesString:(NSString *)provincesString
勾芒's avatar
勾芒 committed
138
{
勾芒's avatar
勾芒 committed
139 140
    self.cityString = cityString;
    self.provincesString = provincesString;
勾芒's avatar
勾芒 committed
141 142 143 144
    [self.citySelected setTitle:cityString forState:UIControlStateNormal];
}


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


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


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


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

曹云霄's avatar
曹云霄 committed
295

勾芒's avatar
勾芒 committed
296 297 298 299
#pragma mark -删除收货地址
- (void)deleteAddressButtonClick
{
    [self CreateMBProgressHUDLoding];
300
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(REMOVEADDRESS),_model.fid] WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            [self SuccessMBProgressView:@"删除成功"];
            if ([self.delegate respondsToSelector:@selector(delecteCell:)]) {
                
                [self.delegate delecteCell:_model.fid];
            }
        }else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
    }];
}

曹云霄's avatar
曹云霄 committed
323 324 325 326 327 328 329 330 331 332 333 334

/*
#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