INSSettingViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // INSSettingViewController.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2023/9/11.
  6. //
  7. #import "INSSettingViewController.h"
  8. #import "SettingBottomView.h"
  9. #import "INSSettingBodyView.h"
  10. #import "ModifyNameViewController.h"
  11. #import <KSFullDatePicker.h>
  12. #import <RSKImageCropper/RSKImageCropper.h>
  13. #import "KSMediaManager.h"
  14. #import "AccountDeleteViewController.h"
  15. #import "ModifyPhoneCheckController.h"
  16. #import "PasswordCheckViewController.h"
  17. #import "InstrumentChooseViewController.h"
  18. #import "UserAuthViewController.h"
  19. #import "UserInfoManager.h"
  20. #import "AccountDeleteViewController.h"
  21. #import <KSSmallChoosePicker.h>
  22. #import "CustomNavViewController.h"
  23. #import "KSBaseWKWebViewController.h"
  24. #import "CoursewareDownloadManager.h"
  25. #import "KSBaseGuideManager.h"
  26. #import "NSDate+Extension.h"
  27. @interface INSSettingViewController ()<RSKImageCropViewControllerDelegate>
  28. @property (nonatomic, strong) INSSettingBodyView *bodyView;
  29. @property (nonatomic, strong) SettingBottomView *bottomView;
  30. @property (nonatomic, strong) NSString *userSex;
  31. @property (nonatomic, strong) NSString *birthday;
  32. @property (nonatomic, strong) KSMediaManager *mediaManager;
  33. @property (nonatomic, strong) UserInfo *mineInfo;
  34. @property (nonatomic, assign) NSInteger lastChooseIndex;
  35. @end
  36. @implementation INSSettingViewController
  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. }
  44. - (void)requestUserMessage {
  45. [USER_MANAGER queryUserInfoCallback:^(UserInfo * _Nonnull userInfo) {
  46. self.mineInfo = userInfo;
  47. [self refreshView:userInfo];
  48. }];
  49. }
  50. - (void)configUI {
  51. [self.scrollView addSubview:self.bodyView];
  52. CGFloat bodyHeight = [self.bodyView getViewHeight];
  53. [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.left.right.mas_equalTo(self.view);
  55. make.top.mas_equalTo(self.scrollView.mas_top);
  56. make.height.mas_equalTo(bodyHeight);
  57. }];
  58. [self.scrollView addSubview:self.bottomView];
  59. CGFloat bottomHeight = [SettingBottomView getViewHeight];
  60. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.left.right.mas_equalTo(self.view);
  62. make.top.mas_equalTo(self.bodyView.mas_bottom).offset(20);
  63. make.height.mas_equalTo(bottomHeight);
  64. make.bottom.mas_equalTo(self.scrollView.mas_bottom);
  65. }];
  66. }
  67. - (void)refreshView:(UserInfo *)info {
  68. if (info.gender == 1) {
  69. self.lastChooseIndex = 1;
  70. }
  71. else {
  72. self.lastChooseIndex = 2;
  73. }
  74. [self.bodyView configUserWithUserInfo:info];
  75. }
  76. - (void)settingAction:(INSETTINGTYPE)type {
  77. switch (type) {
  78. case INSETTINGTYPE_AVATAR:
  79. {
  80. [self chooseImage];
  81. }
  82. break;
  83. case INSETTINGTYPE_NAME:
  84. {
  85. ModifyNameViewController *nameCtrl = [[ModifyNameViewController alloc] init];
  86. nameCtrl.preNickName = self.mineInfo.username;
  87. MJWeakSelf;
  88. [nameCtrl successCallback:^{
  89. [weakSelf requestUserMessage];
  90. }];
  91. [self.navigationController pushViewController:nameCtrl animated:YES];
  92. }
  93. break;
  94. case INSETTINGTYPE_SEX:
  95. {
  96. [self showModifySexAlert];
  97. }
  98. break;
  99. case INSETTINGTYPE_SUBJECT: // 9月18同步机构学员不可修改声部
  100. {
  101. // [self showInstrumentView];
  102. }
  103. break;
  104. case INSETTINGTYPE_BIRTHDAY:
  105. {
  106. [self chooseBirthDay];
  107. }
  108. break;
  109. case INSETTINGTYPE_PHONE:
  110. {
  111. ModifyPhoneCheckController *checkCtrl = [[ModifyPhoneCheckController alloc] init];
  112. [self.navigationController pushViewController:checkCtrl animated:YES];
  113. }
  114. break;
  115. case INSETTINGTYPE_PWD:
  116. {
  117. PasswordCheckViewController *modifyVC = [[PasswordCheckViewController alloc] init];
  118. [self.navigationController pushViewController:modifyVC animated:YES];
  119. }
  120. break;
  121. case INSETTINGTYPE_CLEARCACHE:
  122. {
  123. [self clearCache];
  124. }
  125. break;
  126. case INSETTINGTYPE_LOGOFF:
  127. {
  128. AccountDeleteViewController *deleteVC = [[AccountDeleteViewController alloc] init];
  129. [self.navigationController pushViewController:deleteVC animated:YES];
  130. }
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. - (void)closeDownload {
  137. if (COURSEWARE_MANAGER.isDownloading) { // 如果在下载 取消
  138. [COURSEWARE_MANAGER cancleDownloadCourseware];
  139. }
  140. }
  141. - (void)deleteWebCache {
  142. //allWebsiteDataTypes清除所有缓存
  143. NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
  144. NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  145. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
  146. }];
  147. }
  148. - (void)clearCache {
  149. [self closeDownload];
  150. [self deleteWebCache];
  151. [VoNetworking clearDownloadData];
  152. UserDefaultSetBoolForKey(NO, MERGE_GUIDEKEY);
  153. NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  154. NSString *bundlePath = [documentsPath stringByAppendingPathComponent:@"CoursewarePath"];
  155. NSFileManager *fileManager = [NSFileManager defaultManager];
  156. BOOL isExist = [fileManager fileExistsAtPath:bundlePath];
  157. BOOL isSuccessClearCourseware = NO;
  158. if (isExist) {
  159. isSuccessClearCourseware = [fileManager removeItemAtPath:bundlePath error:nil];
  160. }
  161. else {
  162. isSuccessClearCourseware = YES;
  163. }
  164. NSString *accompanyCachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  165. NSString *accompanyFilePath= [accompanyCachePath stringByAppendingPathComponent:@"AccompanySong"];
  166. BOOL isAccompanySongExist = [fileManager fileExistsAtPath:accompanyFilePath];
  167. BOOL isSuccessClearAccompany = NO;
  168. if (isAccompanySongExist) {
  169. isSuccessClearAccompany = [fileManager removeItemAtPath:accompanyFilePath error:nil];
  170. }
  171. else {
  172. isSuccessClearAccompany = YES;
  173. }
  174. if (isSuccessClearCourseware && isSuccessClearAccompany) {
  175. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"清理完成"];
  176. }
  177. }
  178. - (void)showInstrumentView {
  179. InstrumentChooseViewController *ctrl = [[InstrumentChooseViewController alloc] init];
  180. MJWeakSelf;
  181. [ctrl chooseCallback:^{
  182. [weakSelf requestUserMessage];
  183. }];
  184. [self.navigationController pushViewController:ctrl animated:YES];
  185. }
  186. - (void)chooseBirthDay {
  187. NSDate *data = [NSDate date];
  188. if (![NSString isEmptyString:self.mineInfo.birthdate]) {
  189. NSString *birthday = self.mineInfo.birthdate;
  190. NSDateFormatter *formatter = [NSObject getDateformatter];
  191. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  192. NSDate *birthdate = [formatter dateFromString:birthday];
  193. if (birthdate) {
  194. data = birthdate;
  195. }
  196. }
  197. MJWeakSelf;
  198. KSFullDatePicker *datePicker = [[KSFullDatePicker alloc] initWithTitle:@"" date:data pickMode:KSDATEPICKER_MODE_DAY sureButtonColor:CLIENT_THEMECOLOR selectDateBlock:^(NSString *date) {
  199. [weakSelf.bodyView.birthdayLabel setText:date];
  200. weakSelf.birthday = date;
  201. [weakSelf modifyUserMessage:nil gender:self.userSex birthday:date];
  202. } cancleBlock:^{
  203. }];
  204. [datePicker show];
  205. }
  206. - (void)chooseImage {
  207. // 调用相册
  208. self.mediaManager = [[KSMediaManager alloc] init];
  209. self.mediaManager.mediaType = MEDIATYPE_PHOTO;
  210. self.mediaManager.maxPhotoNumber = 1;
  211. self.mediaManager.baseCtrl = self;
  212. self.mediaManager.needCropImage = NO;
  213. MJWeakSelf;
  214. [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
  215. UIImage *sendImg = [imageArray lastObject];
  216. // 跳转到裁剪功能
  217. RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:sendImg cropMode:RSKImageCropModeSquare];
  218. imageCropVC.avoidEmptySpaceAroundImage = YES;
  219. imageCropVC.rotationEnabled = YES;
  220. imageCropVC.delegate = weakSelf;
  221. CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:imageCropVC];
  222. navCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
  223. [weakSelf.navigationController presentViewController:navCtrl animated:YES completion:nil];
  224. }];
  225. [self.mediaManager pushImagePickerController];
  226. }
  227. - (void)updateWithUserLogo:(UIImage *)image {
  228. NSData *imgData = [UIImage turnsImaegDataByImage:image];
  229. [UPLOAD_MANAGER configWithfilePath:@"/user/"];
  230. NSString *avatarName = [NSString stringWithFormat:@"%@.png", UserDefault(UIDKey)];
  231. [UPLOAD_MANAGER uploadFixedNameImage:imgData fileName:avatarName successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
  232. NSString *avatarUrl = [fileUrlArray lastObject];
  233. avatarUrl = [NSString stringWithFormat:@"%@?t=%@", avatarUrl, [NSDate getCurrentTimestamp]];
  234. [self modifyUserMessage:avatarUrl gender:nil birthday:nil];
  235. } faliure:^(NSError * _Nullable error, NSString * _Nullable descMessaeg) {
  236. if (![NSString isEmptyString:descMessaeg]) {
  237. [LOADING_MANAGER MBShowAUTOHidingInWindow:descMessaeg];
  238. }
  239. }];
  240. }
  241. - (void)modifyUserMessage:(NSString *)imgUrl gender:(NSString *)gender birthday:(NSString *)birthday {
  242. NSString *userName = nil;
  243. [LOADING_MANAGER showCustomLoading:@"加载中..."];
  244. [KSNetworkingManager modifyUserMessage:KS_POST avatal:imgUrl gender:gender username:userName birthdate:birthday success:^(NSDictionary * _Nonnull dic) {
  245. [LOADING_MANAGER removeCustomLoading];
  246. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  247. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"修改成功"];
  248. [self requestUserMessage];
  249. }
  250. else {
  251. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  252. }
  253. } faliure:^(NSError * _Nonnull error) {
  254. [LOADING_MANAGER removeCustomLoading];
  255. }];
  256. }
  257. - (void)showModifySexAlert {
  258. KSSmallChoosePicker *picker = [[KSSmallChoosePicker alloc] initWithTitle:@"修改性别" sourceData:@[@"男",@"女"] lastChoose:self.lastChooseIndex chooseColor:CLIENT_THEMECOLOR chooseReturnWithBlock:^(NSString * _Nonnull returnValue, NSInteger chooseIndex) {
  259. self.bodyView.userSex.text = returnValue;
  260. self.lastChooseIndex = chooseIndex;
  261. if (chooseIndex == 1) {
  262. self.userSex = @"1";
  263. }
  264. else {
  265. self.userSex = @"0";
  266. }
  267. [self modifyUserSex];
  268. } cancel:^{
  269. }];
  270. [picker showPicker];
  271. }
  272. - (void)modifyUserSex {
  273. // 更新性别
  274. [self modifyUserMessage:nil gender:self.userSex birthday:nil];
  275. }
  276. - (void)logoutAction {
  277. [KSNetworkingManager logoutRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
  278. [self clearSource];
  279. } faliure:^(NSError * _Nonnull error) {
  280. [self clearSource];
  281. }];
  282. }
  283. - (void)clearSource {
  284. [APPLOGIN_MANAGER logoutActionWithMsg:@""];
  285. }
  286. #pragma mark ----- lazying
  287. - (INSSettingBodyView *)bodyView {
  288. if (!_bodyView) {
  289. _bodyView = [INSSettingBodyView shareInstance];
  290. MJWeakSelf;
  291. [_bodyView settingCallback:^(INSETTINGTYPE type) {
  292. [weakSelf settingAction:type];
  293. }];
  294. }
  295. return _bodyView;
  296. }
  297. - (SettingBottomView *)bottomView {
  298. if (!_bottomView) {
  299. _bottomView = [SettingBottomView shareInstance];
  300. MJWeakSelf;
  301. [_bottomView bottomActionCallback:^{
  302. [weakSelf logoutAction];
  303. }];
  304. }
  305. return _bottomView;
  306. }
  307. #pragma mark --- RSKImageCropViewControllerDelegate
  308. /**
  309. 通知 delegate 取消裁剪图像。
  310. */
  311. - (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller {
  312. self.zh_statusBarHidden = NO;
  313. [controller.navigationController dismissViewControllerAnimated:YES completion:nil];
  314. }
  315. /**
  316. 通知 delegate 原始图像已经被裁剪。此外,还提供了一个裁剪矩形和用于生成图像的旋转角度。
  317. */
  318. - (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect rotationAngle:(CGFloat)rotationAngle {
  319. self.zh_statusBarHidden = NO;
  320. // croppedImage
  321. [self updateWithUserLogo:croppedImage];
  322. [controller.navigationController dismissViewControllerAnimated:YES completion:nil];
  323. }
  324. /*
  325. #pragma mark - Navigation
  326. // In a storyboard-based application, you will often want to do a little preparation before navigation
  327. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  328. // Get the new view controller using [segue destinationViewController].
  329. // Pass the selected object to the new view controller.
  330. }
  331. */
  332. @end