UserSettingViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // UserSettingViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/5/27.
  6. //
  7. #import "UserSettingViewController.h"
  8. #import "UseBodyView.h"
  9. #import "AppDelegate.h"
  10. #import "CustomNavViewController.h"
  11. #import "JPUSHService.h"
  12. #import "KSBaseWKWebViewController.h"
  13. #import "ModifyNameViewController.h"
  14. #import "PasswordCheckViewController.h"
  15. #import "ModifyPhoneCheckController.h"
  16. #import "KSMediaManager.h"
  17. #import "UserAuthViewController.h"
  18. #import "UserInfoManager.h"
  19. #import "InstrumentChooseViewController.h"
  20. #import "UserInfo.h"
  21. #import <KSFullDatePicker.h>
  22. #import "UserInfoManager.h"
  23. #import <RSKImageCropper/RSKImageCropper.h>
  24. #import <KSSmallChoosePicker.h>
  25. #import <UIImageView+DisplayImage.h>
  26. @interface UserSettingViewController ()<RSKImageCropViewControllerDelegate>
  27. @property (nonatomic, strong) UseBodyView *bodyView;
  28. @property (nonatomic, strong) NSString *userSex;
  29. @property (nonatomic, strong) KSMediaManager *mediaManager;
  30. @property (nonatomic, strong) UserInfo *mineInfo;
  31. @property (nonatomic, strong) NSString *birthday;
  32. @property (nonatomic, assign) NSInteger lastChooseIndex;
  33. @property (nonatomic, strong) UIImage *preDisplayImage;
  34. @end
  35. @implementation UserSettingViewController
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. // Do any additional setup after loading the view.
  39. [self allocTitle:@"设置"];
  40. [self configUI];
  41. [self requestUserMessage];
  42. [self requestSubjectInfo];
  43. }
  44. - (void)requestSubjectInfo {
  45. [KSNetworkingManager queryStudentInfoRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
  46. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  47. UserInfo *studentInfo = [[UserInfo alloc] initWithDictionary:[dic ks_dictionaryValueForKey:@"data"]];
  48. [self refreshStudentSubject:studentInfo.subjectName];
  49. }
  50. else {
  51. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  52. }
  53. } faliure:^(NSError * _Nonnull error) {
  54. }];
  55. }
  56. - (void)refreshStudentSubject:(NSString *)subjectName {
  57. if ([NSString isEmptyString:subjectName]) {
  58. subjectName = @"请选择乐器";
  59. }
  60. self.bodyView.subjectLabel.text = subjectName;
  61. }
  62. - (void)requestUserMessage {
  63. [USER_MANAGER queryUserInfoCallback:^(UserInfo * _Nonnull userInfo) {
  64. self.mineInfo = userInfo;
  65. [self configMessage];
  66. }];
  67. }
  68. - (void)configMessage {
  69. if (![NSString isEmptyString:self.mineInfo.heardUrl]) {
  70. [self.bodyView.userAvatal displayImageWithUrl:[NSURL URLWithString:[self.mineInfo.heardUrl getUrlEndcodeString]] placeholder:self.preDisplayImage defaultImage:[UIImage imageNamed:STUDENT_AVATAR] callback:^(UIImage * _Nonnull image) {
  71. self.preDisplayImage = image;
  72. }];
  73. }
  74. else {
  75. [self.bodyView.userAvatal setImage:[UIImage imageNamed:STUDENT_AVATAR]];
  76. self.preDisplayImage = [UIImage imageNamed:STUDENT_AVATAR];
  77. }
  78. self.bodyView.nickNameLabel.text = [NSString returnNoNullStringWithString:self.mineInfo.username];
  79. NSString *userSex = @"";
  80. if (self.mineInfo.gender == 1) {
  81. userSex = @"男";
  82. self.userSex = @"1";
  83. self.lastChooseIndex = 1;
  84. }
  85. else {
  86. userSex = @"女";
  87. self.userSex = @"0";
  88. self.lastChooseIndex = 2;
  89. }
  90. self.bodyView.genderLabel.text = userSex;
  91. self.bodyView.phoneLabel.text = [NSString returnNoNullStringWithString:self.mineInfo.phone];
  92. NSString *authStatus = @"";
  93. if (USER_MANAGER.hasAuth) {
  94. authStatus = @"已认证";
  95. }
  96. else {
  97. authStatus = @"未认证";
  98. }
  99. self.bodyView.authStatusLabel.text = authStatus;
  100. if ([NSString isEmptyString:self.mineInfo.birthdate]) {
  101. self.bodyView.birthdayLabel.text = @"未设置";
  102. }
  103. else {
  104. self.bodyView.birthdayLabel.text = [[self.mineInfo.birthdate componentsSeparatedByString:@" "] firstObject];
  105. }
  106. }
  107. - (void)configUI {
  108. [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
  109. make.left.right.mas_equalTo(self.view);
  110. make.top.mas_equalTo(self.view.mas_top);
  111. make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin);
  112. }];
  113. self.bodyView = [UseBodyView shareInstance];
  114. CGFloat height = [self.bodyView getViewHeight];
  115. CGFloat viewHeight = height > KPortraitHeight - kNaviBarHeight - iPhoneXSafeBottomMargin ? height : KPortraitHeight - kNaviBarHeight - iPhoneXSafeBottomMargin;
  116. [self.scrollView addSubview:self.bodyView];
  117. [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
  118. make.top.mas_equalTo(self.scrollView.mas_top);
  119. make.right.left.mas_equalTo(self.view);
  120. make.height.mas_equalTo(viewHeight);
  121. }];
  122. [self.scrollView setContentSize:CGSizeMake(kScreenWidth, viewHeight)];
  123. KSWeakSelf(weakSelf);
  124. [self.bodyView userSettingAction:^(USERSETTING type) {
  125. [weakSelf operationAction:type];
  126. }];
  127. }
  128. - (void)operationAction:(USERSETTING)type {
  129. switch (type) {
  130. case USERSETTING_AVATAL: // 头像
  131. {
  132. [self chooseImage];
  133. }
  134. break;
  135. case USERSETTING_NAME:
  136. {
  137. ModifyNameViewController *nameCtrl = [[ModifyNameViewController alloc] init];
  138. nameCtrl.preNickName = self.mineInfo.username;
  139. MJWeakSelf;
  140. [nameCtrl successCallback:^{
  141. [weakSelf requestUserMessage];
  142. }];
  143. [self.navigationController pushViewController:nameCtrl animated:YES];
  144. }
  145. break;
  146. case USERSETTING_SEX:
  147. {
  148. [self showModifySexAlert];
  149. }
  150. break;
  151. case USERSETTING_PHONE:
  152. {
  153. ModifyPhoneCheckController *checkCtrl = [[ModifyPhoneCheckController alloc] init];
  154. [self.navigationController pushViewController:checkCtrl animated:YES];
  155. }
  156. break;
  157. case USERSETTING_PWD:
  158. {
  159. PasswordCheckViewController *modifyVC = [[PasswordCheckViewController alloc] init];
  160. [self.navigationController pushViewController:modifyVC animated:YES];
  161. }
  162. break;
  163. case USERSETTING_BIRTHDAY:
  164. {
  165. [self chooseBirthDay];
  166. }
  167. break;
  168. case USERSETTING_SUBJECT:
  169. {
  170. [self showInstrumentView];
  171. }
  172. break;
  173. case USERSETTING_VEFI:
  174. {
  175. if (USER_MANAGER.hasAuth == NO) {
  176. UserAuthViewController *ctrl = [[UserAuthViewController alloc] init];
  177. MJWeakSelf;
  178. [ctrl sureCallback:^{
  179. [weakSelf requestUserMessage];
  180. }];
  181. [self.navigationController pushViewController:ctrl animated:YES];
  182. }
  183. }
  184. break;
  185. case USERSETTING_LOGOUT:
  186. {
  187. [self logoutAction];
  188. }
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. - (void)showInstrumentView {
  195. InstrumentChooseViewController *ctrl = [[InstrumentChooseViewController alloc] init];
  196. MJWeakSelf;
  197. [ctrl chooseCallback:^{
  198. [weakSelf requestSubjectInfo];
  199. }];
  200. [self.navigationController pushViewController:ctrl animated:YES];
  201. }
  202. - (void)chooseBirthDay {
  203. NSDate *data = [NSDate date];
  204. if (![NSString isEmptyString:self.mineInfo.birthdate]) {
  205. NSString *birthday = self.mineInfo.birthdate;
  206. NSDateFormatter *formatter = [NSObject getDateformatter];
  207. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  208. NSDate *birthdate = [formatter dateFromString:birthday];
  209. if (birthdate) {
  210. data = birthdate;
  211. }
  212. }
  213. MJWeakSelf;
  214. KSFullDatePicker *datePicker = [[KSFullDatePicker alloc] initWithTitle:@"" date:data pickMode:KSDATEPICKER_MODE_DAY sureButtonColor:CLIENT_THEMECOLOR selectDateBlock:^(NSString *date) {
  215. [weakSelf.bodyView.birthdayLabel setText:date];
  216. weakSelf.birthday = date;
  217. [weakSelf modifyUserMessage:nil gender:self.userSex birthday:date];
  218. } cancleBlock:^{
  219. }];
  220. [datePicker show];
  221. }
  222. - (void)chooseImage {
  223. // 调用相册
  224. self.mediaManager = [[KSMediaManager alloc] init];
  225. self.mediaManager.mediaType = MEDIATYPE_PHOTO;
  226. self.mediaManager.maxPhotoNumber = 1;
  227. self.mediaManager.baseCtrl = self;
  228. self.mediaManager.needCropImage = NO;
  229. MJWeakSelf;
  230. [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
  231. UIImage *sendImg = [imageArray lastObject];
  232. // 跳转到裁剪功能
  233. RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:sendImg cropMode:RSKImageCropModeSquare];
  234. imageCropVC.avoidEmptySpaceAroundImage = YES;
  235. imageCropVC.rotationEnabled = YES;
  236. imageCropVC.delegate = weakSelf;
  237. CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:imageCropVC];
  238. navCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
  239. [weakSelf.navigationController presentViewController:navCtrl animated:YES completion:nil];
  240. }];
  241. [self.mediaManager pushImagePickerController];
  242. }
  243. - (void)updateWithUserLogo:(UIImage *)image {
  244. NSData *imgData = [UIImage turnsImaegDataByImage:image];
  245. NSString *fileName = @"image";
  246. [UPLOAD_MANAGER configWithfilePath:@"/user/"];
  247. [UPLOAD_MANAGER uploadImage:imgData fileName:fileName successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
  248. NSString *avatarUrl = [fileUrlArray lastObject];
  249. [self modifyUserMessage:avatarUrl gender:nil birthday:nil];
  250. } faliure:^(NSError * _Nullable error, NSString *descMessaeg) {
  251. if (![NSString isEmptyString:descMessaeg]) {
  252. [LOADING_MANAGER MBShowAUTOHidingInWindow:descMessaeg];
  253. }
  254. }];
  255. }
  256. - (void)modifyUserMessage:(NSString *)imgUrl gender:(NSString *)gender birthday:(NSString *)birthday {
  257. NSString *userName = nil;
  258. [LOADING_MANAGER showCustomLoading:@"加载中..."];
  259. [KSNetworkingManager modifyUserMessage:KS_POST avatal:imgUrl gender:gender username:userName birthdate:birthday success:^(NSDictionary * _Nonnull dic) {
  260. [LOADING_MANAGER removeCustomLoading];
  261. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  262. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"修改成功"];
  263. [self requestUserMessage];
  264. }
  265. else {
  266. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  267. }
  268. } faliure:^(NSError * _Nonnull error) {
  269. [LOADING_MANAGER removeCustomLoading];
  270. }];
  271. }
  272. - (void)showModifySexAlert {
  273. KSSmallChoosePicker *picker = [[KSSmallChoosePicker alloc] initWithTitle:@"修改性别" sourceData:@[@"男",@"女"] lastChoose:self.lastChooseIndex chooseColor:CLIENT_THEMECOLOR chooseReturnWithBlock:^(NSString * _Nonnull returnValue, NSInteger chooseIndex) {
  274. self.bodyView.genderLabel.text = returnValue;
  275. self.lastChooseIndex = chooseIndex;
  276. if (chooseIndex == 1) {
  277. self.userSex = @"1";
  278. }
  279. else {
  280. self.userSex = @"0";
  281. }
  282. [self modifyUserSex];
  283. } cancel:^{
  284. }];
  285. [picker showPicker];
  286. }
  287. - (void)modifyUserSex {
  288. // 更新性别
  289. [self modifyUserMessage:nil gender:self.userSex birthday:nil];
  290. }
  291. - (void)logoutAction {
  292. [KSNetworkingManager logoutRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
  293. [self clearSource];
  294. } faliure:^(NSError * _Nonnull error) {
  295. [self clearSource];
  296. }];
  297. }
  298. - (void)clearUMCount {
  299. [USER_MANAGER sendUMEvent:@"klx_logout"];
  300. [USER_MANAGER stopCountUMEvent];
  301. }
  302. - (void)clearSource {
  303. [APPLOGIN_MANAGER logoutActionWithMsg:@""];
  304. }
  305. #pragma mark --- RSKImageCropViewControllerDelegate
  306. /**
  307. 通知 delegate 取消裁剪图像。
  308. */
  309. - (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller {
  310. self.zh_statusBarHidden = NO;
  311. [controller.navigationController dismissViewControllerAnimated:YES completion:nil];
  312. }
  313. /**
  314. 通知 delegate 原始图像已经被裁剪。此外,还提供了一个裁剪矩形和用于生成图像的旋转角度。
  315. */
  316. - (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect rotationAngle:(CGFloat)rotationAngle {
  317. self.zh_statusBarHidden = NO;
  318. // croppedImage
  319. [self updateWithUserLogo:croppedImage];
  320. [controller.navigationController dismissViewControllerAnimated:YES completion:nil];
  321. }
  322. /*
  323. #pragma mark - Navigation
  324. // In a storyboard-based application, you will often want to do a little preparation before navigation
  325. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  326. // Get the new view controller using [segue destinationViewController].
  327. // Pass the selected object to the new view controller.
  328. }
  329. */
  330. @end