// // SPSBMailAddressListViewController.m // 我的社保 // // Created by jiaxian_he on 2021/5/13. // #import "SPSBMailAddressListViewController.h" #import "SPSBUIGeneralHeader.h" #import "UIViewController+SPSBNetworkManager.h" #import "SPSBMailingAddressListModel.h" #import "UIViewController+SPSBDefault.h" #import #import "SPSBConfirmAlertViewController.h" #import "SPSBEditMailAddressViewController.h" @interface SPSBMailAddressListViewController () { UIView *_addressView; UIView *_noDataView; SPSBMailingAddressListModel *_data; } @end @implementation SPSBMailAddressListViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; [self getData]; } #pragma mark - Action - (void)addAddressAction { SPSBEditMailAddressViewController *vc = SPSBEditMailAddressViewController.new; vc.spsb_editType = SPSBEditMailAddressTypeAdd; vc.spsb_data = [SPSBMailingAddressListModel getModelWithDictionary:@{}]; @weakify(self) vc.spsb_completion = ^(SPSBMailingAddressListModel * _Nonnull data) { @strongify(self) self->_data = data; [self reloadView]; }; [self.navigationController pushViewController:vc animated:true]; } - (void)deleteAddress { SPSBConfirmAlertViewController *vc = [SPSBConfirmAlertViewController alertWithTitle:@"删除后将无法恢复" content:@"是否删除该信息"]; [vc setCancelButtonTitle:nil titleColor:nil action:^{ }]; @weakify(self) [vc setConfirmButtonTitle:@"删除" titleColor:nil action:^{ @strongify(self) [self deleteAction]; }]; [self presentViewController:vc animated:false completion:nil]; } - (void)editAddress { SPSBEditMailAddressViewController *vc = SPSBEditMailAddressViewController.new; vc.spsb_editType = SPSBEditMailAddressTypeChange; vc.spsb_data = [_data copy]; @weakify(self) vc.spsb_completion = ^(SPSBMailingAddressListModel * _Nonnull data) { @strongify(self) self->_data = data; [self reloadView]; }; [self.navigationController pushViewController:vc animated:true]; } #pragma mark - NetworkAction - (void)getData { @weakify(self) [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:@"正在加载" isLogin:true url:spsb_appUrl(SPSBUrlGetMailingAddress) urlParameters:nil parameters:nil success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) [self getDataSuccess:data]; return @""; } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) [self getDataFailure:error]; return @"加载失败"; }]; } - (void)getDataSuccess:(id)data { _data = [SPSBMailingAddressListModel getModelWithDictionary:data[@"data"]]; if (_data.spsb_u_name.length > 0) { [self addAddressView]; } else { [self addNoDataView]; } } - (void)getDataFailure:(NSError *)error { self.view.backgroundColor = spsb_FFFFFF_color(1.f); @weakify(self) [self setDefaultViewWithError:error image:nil action:^{ @strongify(self) self.view.backgroundColor = spsb_F5F5F5_color(); [self removeDefaultView]; [self getData]; }]; } - (void)deleteAction { @weakify(self) [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:@"正在删除" isLogin:true url:spsb_appUrl(SPSBUrlDeleteMailingAddress) urlParameters:nil parameters:nil success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) [self -> _addressView removeFromSuperview]; [self addNoDataView]; return @""; } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) [self getDataFailure:error]; return @"删除失败"; }]; } #pragma mark - UI - (void)setupUI { self.title = @"邮寄信息"; self.view.backgroundColor = spsb_F5F5F5_color(); } - (void)reloadView { [_addressView removeFromSuperview]; [_noDataView removeFromSuperview]; [self addAddressView]; } - (void)addAddressView { _addressView = [self createBgView]; [self.view addSubview: _addressView]; [_addressView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.view.safetop).offset(12); make.leading.equalTo(10); make.trailing.equalTo(-10); }]; UILabel *firstLabel = [UILabel convenienceWithFont:spsb_semiboldFont(16) text:[NSString stringWithFormat:@"%@ %@", _data.spsb_u_name, _data.spsb_phone] textColor:spsb_333333_color()]; [_addressView addSubview:firstLabel]; [firstLabel makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(21); make.leading.equalTo(20); }]; NSString *address = [NSString stringWithFormat:@"%@%@", _data.spsb_area, _data.spsb_address]; UILabel *secondLabel = [UILabel convenienceWithFont:spsb_font(14) textColor:spsb_666666_color() limitWidth:jxh_screenWidth() - 60]; [secondLabel setAttributedStringWithText:address lineSpacing:6]; [_addressView addSubview:secondLabel]; [secondLabel makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(firstLabel.bottom).offset(15); make.leading.equalTo(20); make.trailing.equalTo(-20); }]; UIView *line = [_addressView createLine]; [line makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(secondLabel.bottom).offset(18); make.leading.equalTo(20); make.trailing.equalTo(0); make.height.equalTo(jxh_onePixe()); }]; UIView *buttonView = [[UIView alloc] init]; buttonView.backgroundColor = [UIColor whiteColor]; [_addressView addSubview:buttonView]; [buttonView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(line.bottom); make.leading.and.trailing.and.bottom.equalTo(0); make.height.equalTo(44); }]; UIButton *deleteButton = [UIButton convenienceWithTarget:self action:@selector(deleteAddress)]; [deleteButton setImage:jxh_getImage(purchasers_delete) state:JXHButtonControlStateNormal]; [buttonView addSubview:deleteButton]; [deleteButton makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(-6); make.top.and.bottom.equalTo(0); make.width.equalTo(44); }]; UIButton *editButton = [UIButton convenienceWithTarget:self action:@selector(editAddress)]; [editButton setImage:jxh_getImage(purchasers_edit) state:JXHButtonControlStateNormal]; [buttonView addSubview:editButton]; [editButton makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(deleteButton.leading).offset(-10); make.top.and.bottom.equalTo(0); make.width.equalTo(44); }]; } - (void)addNoDataView { _noDataView = [self createBgView]; [self.view addSubview:_noDataView]; [_noDataView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(self.view.safetop).offset(12); make.leading.equalTo(10); make.trailing.equalTo(-10); make.height.equalTo(130); }]; UIButton *button = [UIButton convenienceWithTarget:self action:@selector(addAddressAction)]; [_noDataView addSubview:button]; [button makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(self->_noDataView); }]; UIView *contentView = UIView.new; contentView.userInteractionEnabled = false; contentView.backgroundColor = spsb_FFFFFF_color(1.f); [button addSubview:contentView]; [contentView makeConstraints:^(JXHConstraintMaker *make) { make.center.equalTo(button); }]; UIImageView *icon = [[UIImageView alloc] initWithImage:jxh_getImage(purchasers_add)]; [contentView addSubview:icon]; [icon makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(0); make.top.greaterThanOrEqualTo(0); make.bottom.lessThanOrEqualTo(0); make.centerY.equalTo(contentView); }]; UILabel *label = [UILabel convenienceWithFont:spsb_font(15) text:@"添加邮寄地址" textColor:spsb_3296FB_color()]; [contentView addSubview:label]; [label makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(icon.trailing).offset(7); make.trailing.equalTo(0); make.top.greaterThanOrEqualTo(0); make.bottom.lessThanOrEqualTo(0); make.centerY.equalTo(contentView); }]; } - (UIView *)createBgView { UIView *view = UIView.new; view.backgroundColor = spsb_FFFFFF_color(1.f); [view.layer setShadowWithOffset:(CGSize){0, 2} alpha:0.03 blur:4 color:spsb_000000_color(1.f) cornerRadius:0]; return view; } @end