SettingViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // SettingViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/21.
  6. //
  7. #import "SettingViewController.h"
  8. #import "SettingBodyView.h"
  9. #import "RCConnectionManager.h"
  10. #import "LoginViewController.h"
  11. #import "AppDelegate.h"
  12. #import "CustomNavViewController.h"
  13. #import "JPUSHService.h"
  14. #import "KSBaseWKWebViewController.h"
  15. #import "ModifyNameViewController.h"
  16. #import "ModifyViewController.h"
  17. #import "ModifyPhoneCheckController.h"
  18. #import "KSMediaManager.h"
  19. #import "FeedbackViewController.h"
  20. #import "AboutUsViewController.h"
  21. #import "UserInfoManager.h"
  22. #import "AddressListViewController.h"
  23. #import "KSFullDatePicker.h"
  24. @interface SettingViewController ()
  25. @property (nonatomic, strong) SettingBodyView *bodyView;
  26. @property (nonatomic, strong) NSString *userSex;
  27. @property (nonatomic, strong) KSMediaManager *mediaManager;
  28. @property (nonatomic, strong) UserInfo *mineInfo;
  29. @property (nonatomic, strong) NSString *birthday;
  30. @end
  31. @implementation SettingViewController
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Do any additional setup after loading the view.
  35. [self allocTitle:@"设置"];
  36. [self configUI];
  37. [self requestUserMessage];
  38. }
  39. - (void)requestUserMessage {
  40. [USER_MANAGER queryUserInfoCallback:^(UserInfo * _Nonnull userInfo) {
  41. self.mineInfo = userInfo;
  42. [self configMessage];
  43. }];
  44. }
  45. - (void)configMessage {
  46. if (![NSString isEmptyString:self.mineInfo.avatar]) {
  47. [self.bodyView.userAvatal sd_setImageWithURL:[NSURL URLWithString:[self.mineInfo.avatar getUrlEndcodeString]] placeholderImage:[UIImage imageNamed:USERDEFAULT_LOGO]];
  48. }
  49. else {
  50. [self.bodyView.userAvatal setImage:[UIImage imageNamed:USERDEFAULT_LOGO]];
  51. }
  52. self.bodyView.nickNameLabel.text = [NSString returnNoNullStringWithString:self.mineInfo.username];
  53. NSString *userSex = @"";
  54. if ([self.mineInfo.gender isEqualToString:@"1"]) {
  55. userSex = @"男";
  56. self.userSex = @"1";
  57. }
  58. else {
  59. userSex = @"女";
  60. self.userSex = @"0";
  61. }
  62. self.bodyView.genderLabel.text = userSex;
  63. self.bodyView.phoneLabel.text = [NSString returnNoNullStringWithString:self.mineInfo.phone];
  64. NSString *authStatus = @"";
  65. if (USER_MANAGER.hasAuth) {
  66. authStatus = @"已认证";
  67. }
  68. else {
  69. authStatus = @"未认证";
  70. }
  71. self.bodyView.authStatusLabel.text = authStatus;
  72. if ([NSString isEmptyString:self.mineInfo.birthdate]) {
  73. self.bodyView.birthdayLabel.text = @"未设置";
  74. }
  75. else {
  76. self.bodyView.birthdayLabel.text = [[self.mineInfo.birthdate componentsSeparatedByString:@" "] firstObject];
  77. }
  78. }
  79. - (void)configUI {
  80. [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
  81. make.left.right.mas_equalTo(self.view);
  82. make.top.mas_equalTo(self.view.mas_top);
  83. make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin);
  84. }];
  85. self.bodyView = [SettingBodyView shareInstance];
  86. CGFloat height = [self.bodyView getViewHeight];
  87. CGFloat viewHeight = height > KPortraitHeight - kNaviBarHeight - iPhoneXSafeBottomMargin ? height : KPortraitHeight - kNaviBarHeight - iPhoneXSafeBottomMargin;
  88. [self.scrollView addSubview:self.bodyView];
  89. [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.top.mas_equalTo(self.scrollView.mas_top);
  91. make.right.left.mas_equalTo(self.view);
  92. make.height.mas_equalTo(viewHeight);
  93. }];
  94. [self.scrollView setContentSize:CGSizeMake(kScreenWidth, viewHeight)];
  95. KSWeakSelf(weakSelf);
  96. [self.bodyView settingAction:^(SETTINGACTION type) {
  97. [weakSelf operationAction:type];
  98. }];
  99. }
  100. - (void)operationAction:(SETTINGACTION)type {
  101. switch (type) {
  102. case SETTINGACTION_AVATAL: // 头像
  103. {
  104. [self chooseImage];
  105. }
  106. break;
  107. case SETTINGACTION_NAME:
  108. {
  109. ModifyNameViewController *nameCtrl = [[ModifyNameViewController alloc] init];
  110. nameCtrl.preNickName = self.mineInfo.username;
  111. MJWeakSelf;
  112. [nameCtrl successCallback:^{
  113. [weakSelf requestUserMessage];
  114. }];
  115. [self.navigationController pushViewController:nameCtrl animated:YES];
  116. }
  117. break;
  118. case SETTINGACTION_SEX:
  119. {
  120. [self showModifySexAlert];
  121. }
  122. break;
  123. case SETTINGACTION_PHONE:
  124. {
  125. ModifyPhoneCheckController *checkCtrl = [[ModifyPhoneCheckController alloc] init];
  126. [self.navigationController pushViewController:checkCtrl animated:YES];
  127. }
  128. break;
  129. case SETTINGACTION_PWD:
  130. {
  131. ModifyViewController *modifyVC = [[ModifyViewController alloc] init];
  132. [self.navigationController pushViewController:modifyVC animated:YES];
  133. }
  134. break;
  135. case SETTINGACTION_VEFI: // 实名认证
  136. {
  137. }
  138. break;
  139. case SETTINGACTION_ADDRESS:
  140. {
  141. AddressListViewController *ctrl = [[AddressListViewController alloc] init];
  142. [self.navigationController pushViewController:ctrl animated:YES];
  143. }
  144. break;
  145. case SETTINGACTION_ONLINECHECK:
  146. {
  147. }
  148. break;
  149. case SETTINGACTION_PRIVACY:
  150. {
  151. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  152. webCtrl.url = [NSString stringWithFormat:@"%@%@",WEBHOST, @"/#/privacyProtocol"];
  153. [self.navigationController pushViewController:webCtrl animated:YES];
  154. }
  155. break;
  156. case SETTINGACTION_FEEEDBACK:
  157. {
  158. FeedbackViewController *ctrl = [[FeedbackViewController alloc] init];
  159. [self.navigationController pushViewController:ctrl animated:YES];
  160. }
  161. break;
  162. case SETTINGACTION_ABOUTUS:
  163. {
  164. AboutUsViewController *aboutUs = [[AboutUsViewController alloc] init];
  165. [self.navigationController pushViewController:aboutUs animated:YES];
  166. }
  167. break;
  168. case SETTINGACTION_LOGOUT: // 退出登录
  169. {
  170. [self logoutAction];
  171. }
  172. break;
  173. case SETTINGACTION_BIRTHDAY:
  174. {
  175. [self chooseBirthDay];
  176. }
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. - (void)chooseBirthDay {
  183. MJWeakSelf;
  184. KSFullDatePicker *datePicker = [[KSFullDatePicker alloc] initWithTitle:@"" date:[NSDate date] pickMode:KSDATEPICKER_MODE_DAY selectDateBlock:^(NSString *date) {
  185. [weakSelf.bodyView.birthdayLabel setText:date];
  186. weakSelf.birthday = date;
  187. [weakSelf modifyUserMessage:nil gender:self.userSex birthday:date];
  188. } cancleBlock:^{
  189. }];
  190. [datePicker show];
  191. }
  192. - (void)chooseImage {
  193. // 调用相册
  194. self.mediaManager = [[KSMediaManager alloc] init];
  195. self.mediaManager.mediaType = MEDIATYPE_PHOTO;
  196. self.mediaManager.maxPhotoNumber = 1;
  197. self.mediaManager.baseCtrl = self;
  198. self.mediaManager.needCropImage = NO;
  199. MJWeakSelf;
  200. [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
  201. UIImage *sendImg = [imageArray lastObject];
  202. [weakSelf updateWithUserLogo:sendImg];
  203. }];
  204. [self.mediaManager pushImagePickerController];
  205. }
  206. - (void)updateWithUserLogo:(UIImage *)image {
  207. NSData *imgData = [UIImage turnsImaegDataByImage:image];
  208. NSString *fileName = @"image";
  209. [[KSUploadManager shareInstance] uploadImage:imgData fileName:fileName successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
  210. NSString *avatarUrl = [fileUrlArray lastObject];
  211. [self modifyUserMessage:avatarUrl gender:nil birthday:nil];
  212. } faliure:^(NSError * _Nullable error, NSString *descMessaeg) {
  213. if ([NSString isEmptyString:descMessaeg]) {
  214. [self MBPShow:descMessaeg];
  215. }
  216. }];
  217. }
  218. - (void)modifyUserMessage:(NSString *)imgUrl gender:(NSString *)gender birthday:(NSString *)birthday {
  219. NSString *userName = nil;
  220. [self showhud];
  221. [KSNetworkingManager modifyUserMessage:KS_POST avatal:imgUrl gender:gender username:userName birthdate:birthday success:^(NSDictionary * _Nonnull dic) {
  222. [self removehub];
  223. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  224. [self MBPShow:@"修改成功"];
  225. [self requestUserMessage];
  226. }
  227. else {
  228. [self MBPShow:MESSAGEKEY];
  229. }
  230. } faliure:^(NSError * _Nonnull error) {
  231. [self removehub];
  232. }];
  233. }
  234. - (void)showModifySexAlert {
  235. NSString *titleString = @"选择性别";
  236. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:titleString preferredStyle:IS_IPAD ? UIAlertControllerStyleAlert : UIAlertControllerStyleActionSheet];
  237. NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:titleString];
  238. [titleAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.0f] range:NSMakeRange(0, titleString.length)];
  239. [titleAtt addAttribute:NSForegroundColorAttributeName value:HexRGB(0x999999) range:NSMakeRange(0, titleString.length)];
  240. [alertVC setValue:titleAtt forKey:@"attributedMessage"];
  241. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  242. self.bodyView.genderLabel.text = @"男";
  243. self.userSex = @"1";
  244. [self modifyUserSex];
  245. }];
  246. [actionOne setValue:THEMECOLOR forKey:@"_titleTextColor"];
  247. [alertVC addAction:actionOne];
  248. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  249. self.bodyView.genderLabel.text = @"女";
  250. self.userSex = @"0";
  251. [self modifyUserSex];
  252. }];
  253. [actionTwo setValue:THEMECOLOR forKey:@"_titleTextColor"];
  254. [alertVC addAction:actionTwo];
  255. UIAlertAction *cancleAlert = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  256. }];
  257. [alertVC addAction:cancleAlert];
  258. [cancleAlert setValue:HexRGB(0x444444) forKey:@"_titleTextColor"];
  259. alertVC.modalPresentationStyle = UIModalPresentationFullScreen;
  260. [self presentViewController:alertVC animated:true completion:nil];
  261. }
  262. - (void)modifyUserSex {
  263. // 更新性别
  264. [self modifyUserMessage:nil gender:self.userSex birthday:nil];
  265. }
  266. - (void)logoutAction {
  267. [KSNetworkingManager logoutRequest:KS_POST success:^(NSDictionary * _Nonnull dic) {
  268. [self clearSource];
  269. } faliure:^(NSError * _Nonnull error) {
  270. [self clearSource];
  271. }];
  272. }
  273. - (void)clearSource {
  274. [RCConnectionManager shareManager].isNeedJoin = NO;
  275. [RCConnectionManager shareManager].isNeedShowMessage = NO;
  276. [[RCIM sharedRCIM] logout];
  277. [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  278. // 取消推送别名
  279. [JPUSHService deleteAlias:nil seq:0];
  280. [[NSUserDefaults standardUserDefaults] removeObjectForKey:TokenKey];
  281. [[NSUserDefaults standardUserDefaults] removeObjectForKey:Token_type];
  282. [[NSUserDefaults standardUserDefaults] removeObjectForKey:RefreshToken];
  283. [[NSUserDefaults standardUserDefaults] removeObjectForKey:RongTokenKey];
  284. [[NSUserDefaults standardUserDefaults] synchronize];
  285. [KSNetworkingManager clearRequestHeader];
  286. LoginViewController *loginVC = [[LoginViewController alloc] init];
  287. CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:loginVC];
  288. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  289. appDelegate.window.rootViewController = navCtrl;
  290. }
  291. /*
  292. #pragma mark - Navigation
  293. // In a storyboard-based application, you will often want to do a little preparation before navigation
  294. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  295. // Get the new view controller using [segue destinationViewController].
  296. // Pass the selected object to the new view controller.
  297. }
  298. */
  299. @end