// // WXYZ_UserDataViewController.m // WXReader // // Created by Andrew on 2018/7/17. // Copyright © 2018年 Andrew. All rights reserved. // #import "WXYZ_UserDataViewController.h" #import "WXYZ_BindPhoneViewController.h" #import "WXYZ_CancelAccountViewController.h" #import "WXYZ_UserDataTableViewCell.h" #import "TFWeChatPermissionsManager.h" #import "TFTencentPermissionsManager.h" #import "TFURLProtocol.h" #import "WXYZ_ImagePicker.h" #import "TFTextFieldAlertView.h" #import "TFUserDataModel.h" typedef NS_ENUM(NSUInteger, WXYZ_UserInfoStyle) { WXYZ_UserInfoStyleName, WXYZ_UserInfoStyleGender, WXYZ_UserInfoStyleAvatar }; @interface WXYZ_UserDataViewController () @property (nonatomic, strong) TFUserDataModel *userDataModel; @end @implementation WXYZ_UserDataViewController - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self netRequest]; [self setStatusBarDefaultStyle]; } - (void)initialize { [TFURLProtocol startMonitor]; [self setNavigationBarTitle:TFLocalizedString(@"个人资料")]; #if TF_WeChat_Login_Mode [TFWeChatPermissionsManager sharedManager].delegate = self; #endif #if TF_QQ_Login_Mode [TFTencentPermissionsManager sharedManager].delegate = self; #endif } - (void)createSubviews { self.mainTableViewGroup.delegate = self; self.mainTableViewGroup.dataSource = self; [self.view addSubview:self.mainTableViewGroup]; [self.mainTableViewGroup mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(PUB_NAVBAR_HEIGHT); make.height.mas_equalTo(SCREEN_HEIGHT - PUB_NAVBAR_HEIGHT); make.width.mas_equalTo(SCREEN_WIDTH); }]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.userDataModel.panel_list.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.userDataModel.panel_list[section].count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { WXYZ_UserDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WXYZ_UserDataTableViewCell"]; if (!cell) { cell = [[WXYZ_UserDataTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WXYZ_UserDataTableViewCell"]; } cell.cellModel = self.userDataModel.panel_list[indexPath.section][indexPath.row]; cell.hiddenEndLine = (indexPath.row == self.userDataModel.panel_list[indexPath.section].count - 1); cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { WS(weakSelf) WXYZ_UserDataTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // 修改头像 if ([cell.cellModel.action isEqualToString:@"avatar"] && cell.cellModel.is_click) { WXYZ_ImagePicker *picker = [WXYZ_ImagePicker sharedManager]; picker.editPhoto = YES; picker.delegate = self; [picker showInController:self]; return; } // 修改昵称 if ([cell.cellModel.action isEqualToString:@"nickname"] && cell.cellModel.is_click) { TFTextFieldAlertView *alert = [[TFTextFieldAlertView alloc] init]; alert.alertTitle = TFLocalizedString(@"修改昵称"); alert.placeHoldTitle = cell.cellModel.desc ?: @""; alert.endEditedBlock = ^(NSString *inputText) { [weakSelf setUserInfoNetRequestWithStyle:WXYZ_UserInfoStyleName url:Set_NickName parametersKey:@"nickname" parametersValue:inputText editedImage:nil]; }; [alert showAlertView]; return; } if ([cell.cellModel.action isEqualToString:@"gender"] && cell.cellModel.is_click) { UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; if (is_iPad) { UIPopoverPresentationController *popover = actionSheet.popoverPresentationController; if (popover) { popover.sourceView = self.view; popover.sourceRect = self.view.bounds; popover.permittedArrowDirections = UIPopoverArrowDirectionDown; } } [actionSheet addAction:[UIAlertAction actionWithTitle:TFLocalizedString(@"男") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [weakSelf setUserInfoNetRequestWithStyle:WXYZ_UserInfoStyleGender url:Set_Gender parametersKey:@"gender" parametersValue:@"2" editedImage:nil]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:TFLocalizedString(@"女") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [weakSelf setUserInfoNetRequestWithStyle:WXYZ_UserInfoStyleGender url:Set_Gender parametersKey:@"gender" parametersValue:@"1" editedImage:nil]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:TFLocalizedString(@"取消") style:UIAlertActionStyleCancel handler:nil]]; [[TFViewHelper getWindowRootController] presentViewController:actionSheet animated:YES completion:nil]; return; } // 注销账号 if ([cell.cellModel.action isEqualToString:@"cancel"] && cell.cellModel.is_click) { [self.navigationController pushViewController:[[WXYZ_CancelAccountViewController alloc] init] animated:YES]; return; } // 绑定手机号 if ([cell.cellModel.action isEqualToString:@"mobile"] && cell.cellModel.is_click) { WXYZ_BindPhoneViewController *vc = [[WXYZ_BindPhoneViewController alloc] init]; WS(weakSelf) vc.completionBlock = ^{ [weakSelf netRequest]; }; [self.navigationController pushViewController:vc animated:YES]; return; } // 绑定微信 if ([cell.cellModel.action isEqualToString:@"wechat"] && cell.cellModel.is_click) { [self weChatBindingRequest]; } // 绑定QQ if ([cell.cellModel.action isEqualToString:@"QQ"] && cell.cellModel.is_click) { [self qqBindingRequest]; } } //section头部间距 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return kHalfMargin; } //section头部视图 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHalfMargin)]; view.backgroundColor = kGrayViewColor; return view; } //section底部间距 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } //section底部视图 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHalfMargin)]; view.backgroundColor = kGrayViewColor; return view; } #pragma mark - DPImagePickerDelegate - (void)imagePickerDidFinishPickingWithOriginalImage:(UIImage *)originalImage editedImage:(UIImage *)editedImage; { [TFPromptManager showPromptViewWithStatus:TFPromptStatusLoading promptTitle:TFLocalizedString(@"上传中")]; [self setUserInfoNetRequestWithStyle:WXYZ_UserInfoStyleAvatar url:Set_Avatar parametersKey:@"avatar" parametersValue:[TFViewHelper getBase64StringWithImageData:UIImagePNGRepresentation(editedImage)] editedImage:[TFViewHelper fixOrientation:editedImage]]; } - (void)setUserInfoNetRequestWithStyle:(WXYZ_UserInfoStyle)style url:(NSString *)url parametersKey:(NSString *)parametersKey parametersValue:(NSString *)parametersValue editedImage:(UIImage *)editedImage { WS(weakSelf) [TFNetworkTools POST:url parameters:@{parametersKey:parametersValue} model:nil success:^(BOOL isSuccess, NSDictionary *_Nullable t_model, TFNetworkRequestModel *requestModel) { if (isSuccess) { switch (style) { case WXYZ_UserInfoStyleName: { [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"修改成功")]; [[NSNotificationCenter defaultCenter] postNotificationName:Notification_NickName_Changed object:parametersValue]; } break; case WXYZ_UserInfoStyleGender: { [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"修改成功")]; } break; case WXYZ_UserInfoStyleAvatar: { [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"修改成功")]; [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Avatar_Changed object:editedImage]; } break; default: break; } } else { [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg]; } [weakSelf netRequest]; } failure:nil]; } - (void)weChatBindingRequest { if (![TFWeChatPermissionsManager isInstallWechat]) { [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"您的手机没有安装微信")]; return ; } [[TFWeChatPermissionsManager sharedManager] weChatPermissionsType:TFWeChatPermissionsTypeBinding]; } - (void)wechatResponseSuccess:(TFUserInfoManager *)userData { [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"微信绑定成功")]; [self netRequest]; } - (void)wechatResponseFail:(NSString *)error { [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"绑定失败")]; } - (void)qqBindingRequest { if (![TFTencentPermissionsManager isInstallTencent]) { [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"您的手机没有安装QQ或TIM")]; return ; } [[TFTencentPermissionsManager sharedManager] tencentPermissionsType:TFTencentPermissionsTypeBinding]; } - (void)tencentPermissionsSuccess:(TFUserInfoManager *)userData { [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"QQ绑定成功")]; [self netRequest]; } - (void)tencentPermissionsFail:(NSString *)error { [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"绑定失败")]; } - (void)netRequest { WS(weakSelf) [TFNetworkTools POST:User_Data parameters:nil model:nil success:^(BOOL isSuccess, NSDictionary *_Nullable t_model, TFNetworkRequestModel *requestModel) { if (isSuccess) { TFUserDataModel *model = [TFUserDataModel modelWithDictionary:t_model[@"data"]]; weakSelf.userDataModel = model; } [weakSelf.mainTableViewGroup reloadData]; } failure:nil]; } - (void)dealloc { [TFURLProtocol stopMonitor]; } @end