SettingViewController.m 12 KB

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