SPSBEditMailAddressViewController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // SPSBEditMailAddressViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/14.
  6. //
  7. #import "SPSBEditMailAddressViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "UIViewController+SPSBNetworkManager.h"
  10. #import "SPSBMailingAddressListModel.h"
  11. #import <UITextView+JXHPlaceHolder.h>
  12. #import "SPSBVerificationProfile.h"
  13. #import "SPSBTextField.h"
  14. #import "SPSBPickerViewManager.h"
  15. #import <JXHVerificationTools.h>
  16. #define spsb_editMailTextFieldTag 3000
  17. @interface SPSBEditMailAddressViewController ()<UITextFieldDelegate, UITextViewDelegate> {
  18. UIView *_bgView;
  19. UITextView *_detailsAddressTextView;
  20. SPSBPickerViewManager *_pickerManager;
  21. }
  22. @end
  23. @implementation SPSBEditMailAddressViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self initData];
  27. [self setupUI];
  28. }
  29. - (void)initData {
  30. _pickerManager = [[SPSBPickerViewManager alloc] initWithTypes:@[SPSBPickerViewTypeMailAddress]];
  31. _pickerManager.spsb_controller = self;
  32. }
  33. #pragma mark - Action
  34. - (void)selectDistrist {
  35. jxh_endEdit();
  36. @weakify(self)
  37. [_pickerManager showPickerViewWithType:SPSBPickerViewTypeMailAddress defaultValue:nil complete:^(SPSBPickerViewResult result) {
  38. @strongify(self)
  39. UITextField *districtTextField = [self->_bgView viewWithTag:spsb_editMailTextFieldTag + 2];
  40. districtTextField.text = result.spsb_showData;
  41. self->_spsb_data.spsb_area = result.spsb_uploadData;
  42. }];
  43. }
  44. - (void)saveAction {
  45. jxh_endEdit();
  46. if ([_spsb_data.spsb_u_name isEqualToString:@""]) {
  47. [self.view showToastWithTitle:@"请输入收货人姓名"];
  48. } else if (_spsb_data.spsb_phone.length != SPSBPhoneNumLength || !jxh_isNumber(_spsb_data.spsb_phone)) {
  49. [self.view showToastWithTitle:@"请输入正确的手机号"];
  50. } else if ([_spsb_data.spsb_area isEqualToString:@""]) {
  51. [self.view showToastWithTitle:@"请选择所在地区"];
  52. } else if ([_spsb_data.spsb_address isEqualToString:@""]) {
  53. [self.view showToastWithTitle:@"请填写详细地址"];
  54. } else {
  55. [self uploadMailAddress];
  56. }
  57. }
  58. #pragma mark - Network Action
  59. - (void)uploadMailAddress {
  60. @weakify(self)
  61. [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:_spsb_editType == SPSBEditMailAddressTypeAdd ? @"正在添加" : @"正在修改" isLogin:true url:spsb_appUrl(SPSBUrlAddAndUpdateMailingAddress) urlParameters:nil parameters:^NSDictionary * _Nonnull{
  62. return [self->_spsb_data getDictionary];
  63. } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  64. @strongify(self)
  65. if (self->_spsb_completion) {
  66. self->_spsb_completion(self->_spsb_data);
  67. }
  68. [self.navigationController popViewControllerAnimated:true];
  69. return @"";
  70. } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  71. @strongify(self)
  72. return self->_spsb_editType == SPSBEditMailAddressTypeAdd ? @"添加失败" : @"修改失败";
  73. }];
  74. }
  75. #pragma mark - UITextFieldDelegate
  76. - (void)textFieldDidEndEditing:(UITextField *)textField {
  77. if (textField.tag == spsb_editMailTextFieldTag) {
  78. _spsb_data.spsb_u_name = textField.text;
  79. } else if (textField.tag == spsb_editMailTextFieldTag + 1) {
  80. _spsb_data.spsb_phone = textField.text;
  81. }
  82. }
  83. #pragma mark - UITextViewDelegate
  84. - (void)textViewDidEndEditing:(UITextView *)textView {
  85. _spsb_data.spsb_address = textView.text;
  86. }
  87. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
  88. jxh_handleInputTextView(textView, textView.font, 4, 0, NSTextAlignmentLeft);
  89. return true;
  90. }
  91. - (void)textViewDidChange:(UITextView *)textView {
  92. UITextRange *selectedRange = [textView markedTextRange];
  93. if (!selectedRange) {
  94. if (textView.text.length > SPSBMaxMailAddressLength) {
  95. textView.text = [textView.text substringToIndex:SPSBMaxMailAddressLength];
  96. }
  97. }
  98. }
  99. #pragma mark - UI
  100. - (void)setupUI {
  101. self.title = _spsb_editType == SPSBEditMailAddressTypeAdd ? @"添加新地址" : @"修改信息";
  102. self.view.backgroundColor = spsb_F5F5F5_color();
  103. [self createBg];
  104. [self createTextFields];
  105. [self createTextView];
  106. [self createSaveButton];
  107. }
  108. - (void)createSaveButton {
  109. UIButton *saveButton = [UIButton convenienceWithFont:spsb_semiboldFont(16) target:self action:@selector(saveAction)];
  110. [saveButton setTitle:@"保存" titleColor:spsb_FFFFFF_color(1.f) image:nil backgroundImage:jxh_getImage(confirm_n) state:JXHButtonControlStateNormal];
  111. [saveButton setBackgroundImage:jxh_getImage(confirm_h) state:JXHButtonControlStateHighlighted];
  112. [saveButton setLayerCornerRadius:22.5 clipToBounds:true];
  113. [self.view addSubview:saveButton];
  114. [saveButton makeConstraints:^(JXHConstraintMaker *make) {
  115. make.top.equalTo(self -> _detailsAddressTextView.bottom).offset(24);
  116. make.leading.equalTo(16);
  117. make.trailing.equalTo(-16);
  118. make.height.equalTo(45);
  119. }];
  120. }
  121. - (void)createBg {
  122. _bgView = UIView.new;
  123. _bgView.backgroundColor = spsb_FFFFFF_color(1.f);
  124. [self.view addSubview:_bgView];
  125. [_bgView makeConstraints:^(JXHConstraintMaker *make) {
  126. make.top.equalTo(self.view.safetop).offset(12);
  127. make.leading.and.trailing.equalTo(0);
  128. make.height.equalTo(50 * 3 + 100);
  129. }];
  130. }
  131. - (void)createTextView {
  132. _detailsAddressTextView = UITextView.new;
  133. _detailsAddressTextView.showsVerticalScrollIndicator = false;
  134. _detailsAddressTextView.font = spsb_font(16);
  135. _detailsAddressTextView.textColor = spsb_666666_color();
  136. _detailsAddressTextView.delegate = self;
  137. _detailsAddressTextView.text = _spsb_data.spsb_address;
  138. [_detailsAddressTextView setPlaceholder:@"请填写详细地址" placeholdColor:spsb_CCCCCC_color()];
  139. [_bgView addSubview:_detailsAddressTextView];
  140. [_detailsAddressTextView makeConstraints:^(JXHConstraintMaker *make) {
  141. make.leading.equalTo(10);
  142. make.trailing.equalTo(-15);
  143. make.top.equalTo(50 * 3 + 5);
  144. make.bottom.equalTo(0);
  145. }];
  146. }
  147. - (void)createTextFields {
  148. NSArray *contents = @[_spsb_data.spsb_u_name, _spsb_data.spsb_phone, _spsb_data.spsb_area];
  149. NSArray *maxCount = @[@(SPSBMaxNameLength), @(SPSBPhoneNumLength), @50];
  150. NSArray *titleArray = @[@"收货人", @"联系电话", @"所在地区"];
  151. NSArray *placeholderArray = @[@"收货人姓名", @"收货人联系电话", @"省份 城市 区县"];
  152. for (NSInteger i = 0; i < 3; i ++) {
  153. UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(16) text:titleArray[i] textColor:spsb_333333_color()];
  154. [_bgView addSubview:titleLabel];
  155. [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
  156. make.leading.equalTo(15);
  157. make.centerY.equalTo(titleLabel.superview.top).offset(25 + i * 50);
  158. }];
  159. [_bgView createLineWithLocation:JXHLineLocationTop locationOffset:50 + i * 50 headOffset:15 footOffset:0];
  160. SPSBTextField *textField = [SPSBTextField convenienceWithFont:spsb_font(16) placeholder:placeholderArray[i] placeholderColor:spsb_CCCCCC_color() textColor:spsb_666666_color() clearButtonMode:UITextFieldViewModeWhileEditing returnType:UIReturnKeyDone keyboardType:i == 1 ? 4 : 0 delegate:self];
  161. textField.spsb_maxCount = [maxCount[i] integerValue];
  162. textField.tag = spsb_editMailTextFieldTag + i;
  163. textField.text = contents[i];
  164. [_bgView addSubview:textField];
  165. [textField makeConstraints:^(JXHConstraintMaker *make) {
  166. make.leading.equalTo(119);
  167. make.centerY.equalTo(titleLabel);
  168. make.trailing.equalTo(-15);
  169. }];
  170. if (i == 2) {
  171. textField.userInteractionEnabled = false;
  172. UIView *touchView = UIView.new;
  173. [_bgView addSubview:touchView];
  174. [touchView makeConstraints:^(JXHConstraintMaker *make) {
  175. make.edges.equalTo(textField);
  176. }];
  177. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectDistrist)];
  178. [touchView addGestureRecognizer:tap];
  179. }
  180. }
  181. }
  182. @end