UserSettingViewController.m 13 KB

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