// // UserSettingViewController.m // KulexiuForTeacher // // Created by 王智 on 2022/5/27. // #import "UserSettingViewController.h" #import "UseBodyView.h" #import "LoginViewController.h" #import "AppDelegate.h" #import "CustomNavViewController.h" #import "JPUSHService.h" #import "KSBaseWKWebViewController.h" #import "ModifyNameViewController.h" #import "PasswordCheckViewController.h" #import "ModifyPhoneCheckController.h" #import #import "KSMediaManager.h" #import "UserAuthViewController.h" #import "UserInfoManager.h" #import #import #import @interface UserSettingViewController () @property (nonatomic, strong) UseBodyView *bodyView; @property (nonatomic, strong) NSString *userSex; @property (nonatomic, strong) KSMediaManager *mediaManager; @property (nonatomic, strong) UserInfo *mineInfo; @property (nonatomic, assign) NSInteger lastChooseIndex; @property (nonatomic, strong) UIImage *preDisplayImage; @end @implementation UserSettingViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self allocTitle:@"设置"]; [self configUI]; [self requestUserMessage]; } - (void)requestUserMessage { [USER_MANAGER queryUserInfoCallback:^(UserInfo * _Nonnull userInfo) { self.mineInfo = userInfo; [self configMessage]; }]; } - (void)configMessage { if (![NSString isEmptyString:self.mineInfo.heardUrl]) { [self.bodyView.userAvatal displayImageWithUrl:[NSURL URLWithString:[self.mineInfo.heardUrl getUrlEndcodeString]] placeholder:self.preDisplayImage defaultImage:[UIImage imageNamed:TEACHER_AVATAR] callback:^(UIImage * _Nonnull image) { self.preDisplayImage = image; }]; } else { [self.bodyView.userAvatal setImage:[UIImage imageNamed:TEACHER_AVATAR]]; self.preDisplayImage = [UIImage imageNamed:TEACHER_AVATAR]; } self.bodyView.nickNameLabel.text = [NSString returnNoNullStringWithString:self.mineInfo.username]; NSString *userSex = @""; if (self.mineInfo.gender == 1) { userSex = @"男"; self.userSex = @"1"; self.lastChooseIndex = 1; } else { userSex = @"女"; self.userSex = @"0"; self.lastChooseIndex = 2; } self.bodyView.genderLabel.text = userSex; self.bodyView.phoneLabel.text = [NSString returnNoNullStringWithString:self.mineInfo.phone]; NSString *authStatus = @""; if (USER_MANAGER.hasAuth) { authStatus = @"已认证"; } else { authStatus = @"未认证"; } self.bodyView.authStatusLabel.text = authStatus; } - (void)configUI { [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.top.mas_equalTo(self.view.mas_top); make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin); }]; self.bodyView = [UseBodyView shareInstance]; CGFloat viewHeight = KPortraitHeight - kNaviBarHeight - iPhoneXSafeBottomMargin; [self.scrollView addSubview:self.bodyView]; [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.scrollView.mas_top); make.right.left.mas_equalTo(self.view); make.height.mas_equalTo(viewHeight); }]; [self.scrollView setContentSize:CGSizeMake(kScreenWidth, viewHeight)]; KSWeakSelf(weakSelf); [self.bodyView userSettingAction:^(USERSETTING type) { [weakSelf operationAction:type]; }]; } - (void)operationAction:(USERSETTING)type { switch (type) { case USERSETTING_AVATAL: // 头像 { [self chooseImage]; } break; case USERSETTING_NAME: { ModifyNameViewController *nameCtrl = [[ModifyNameViewController alloc] init]; nameCtrl.preNickName = self.mineInfo.username; MJWeakSelf; [nameCtrl successCallback:^{ [weakSelf requestUserMessage]; }]; [self.navigationController pushViewController:nameCtrl animated:YES]; } break; case USERSETTING_SEX: { [self showModifySexAlert]; } break; case USERSETTING_PHONE: { ModifyPhoneCheckController *checkCtrl = [[ModifyPhoneCheckController alloc] init]; [self.navigationController pushViewController:checkCtrl animated:YES]; } break; case USERSETTING_PWD: { PasswordCheckViewController *modifyVC = [[PasswordCheckViewController alloc] init]; [self.navigationController pushViewController:modifyVC animated:YES]; } break; case USERSETTING_VEFI: { if (USER_MANAGER.hasAuth == NO) { UserAuthViewController *ctrl = [[UserAuthViewController alloc] init]; MJWeakSelf; [ctrl sureCallback:^{ [weakSelf requestUserMessage]; }]; [self.navigationController pushViewController:ctrl animated:YES]; } } break; case USERSETTING_LOGOUT: { [self logoutAction]; } break; default: break; } } - (void)chooseImage { // 调用相册 self.mediaManager = [[KSMediaManager alloc] init]; self.mediaManager.mediaType = MEDIATYPE_PHOTO; self.mediaManager.maxPhotoNumber = 1; self.mediaManager.baseCtrl = self; self.mediaManager.needCropImage = NO; MJWeakSelf; [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) { UIImage *sendImg = [imageArray lastObject]; // 跳转到裁剪功能 RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:sendImg cropMode:RSKImageCropModeSquare]; imageCropVC.avoidEmptySpaceAroundImage = YES; imageCropVC.rotationEnabled = YES; imageCropVC.delegate = weakSelf; CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:imageCropVC]; navCtrl.modalPresentationStyle = UIModalPresentationFullScreen; [weakSelf.navigationController presentViewController:navCtrl animated:YES completion:nil]; }]; [self.mediaManager pushImagePickerController]; } - (void)updateWithUserLogo:(UIImage *)image { NSData *imgData = [UIImage turnsImaegDataByImage:image]; NSString *fileName = @"image"; [UPLOAD_MANAGER configWithfilePath:@"/user/"]; [[KSUploadManager shareInstance] uploadImage:imgData fileName:fileName successCallback:^(NSMutableArray * _Nonnull fileUrlArray) { NSString *avatarUrl = [fileUrlArray lastObject]; [self modifyUserMessage:avatarUrl gender:nil]; } faliure:^(NSError * _Nullable error, NSString *descMessaeg) { if (![NSString isEmptyString:descMessaeg]) { [LOADING_MANAGER MBShowAUTOHidingInWindow:descMessaeg]; } }]; } - (void)modifyUserMessage:(NSString *)imgUrl gender:(NSString *)gender { NSString *userName = nil; [LOADING_MANAGER showHUD]; [KSNetworkingManager modifyTeacherMessage:KS_POST avatal:imgUrl gender:gender username:userName success:^(NSDictionary * _Nonnull dic) { [LOADING_MANAGER removeHUD]; if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"修改成功"]; if (![NSString isEmptyString:imgUrl]) { UserDefaultSetObjectForKey(imgUrl, AvatarUrlKey); } [self requestUserMessage]; } else { [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { [LOADING_MANAGER removeHUD]; }]; } - (void)showModifySexAlert { KSSmallChoosePicker *picker = [[KSSmallChoosePicker alloc] initWithTitle:@"修改性别" sourceData:@[@"男",@"女"] lastChoose:self.lastChooseIndex chooseColor:THEMECOLOR chooseReturnWithBlock:^(NSString * _Nonnull returnValue, NSInteger chooseIndex) { self.bodyView.genderLabel.text = returnValue; self.lastChooseIndex = chooseIndex; if (chooseIndex == 1) { self.userSex = @"1"; } else { self.userSex = @"0"; } [self modifyUserSex]; } cancel:^{ }]; [picker showPicker]; } - (void)modifyUserSex { // 更新性别 [self modifyUserMessage:nil gender:self.userSex]; } - (void)logoutAction { [KSNetworkingManager logoutRequest:KS_GET success:^(NSDictionary * _Nonnull dic) { [self clearSource]; } faliure:^(NSError * _Nonnull error) { [self clearSource]; }]; } - (void)clearSource { [APPLOGIN_MANAGER logoutActionWithMsg:@""]; } #pragma mark --- RSKImageCropViewControllerDelegate /** 通知 delegate 取消裁剪图像。 */ - (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller { self.zh_statusBarHidden = NO; [controller.navigationController dismissViewControllerAnimated:YES completion:nil]; } /** 通知 delegate 原始图像已经被裁剪。此外,还提供了一个裁剪矩形和用于生成图像的旋转角度。 */ - (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect rotationAngle:(CGFloat)rotationAngle { self.zh_statusBarHidden = NO; // croppedImage [self updateWithUserLogo:croppedImage]; [controller.navigationController dismissViewControllerAnimated:YES completion:nil]; } /* #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