MeCenterVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //
  2. // MeCenterVC.m
  3. // LNManager
  4. //
  5. // Created by EchoShacolee on 2017/4/6.
  6. // Copyright © 2017年 lee. All rights reserved.
  7. //
  8. #import "MeCenterVC.h"
  9. #import "LoginVC.h"
  10. #import "NSString+ex.h"
  11. #import "InputView.h"
  12. #import "AboutVC.h"
  13. #import "MyMessageVC.h"
  14. //客服
  15. #import "QMChatRoomViewController.h"
  16. @interface MeCenterVC ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate, UIImagePickerControllerDelegate,InputViewDelegate,UINavigationControllerDelegate>
  17. {
  18. NSArray *_contentArr; //要显示的内容
  19. InputView *_inputVi; //照片选择
  20. UIImageView *_headImgV; //头像
  21. //客服
  22. BOOL _isFirstClick; // 判断点击状态
  23. BOOL _isPushed; // 判断跳转状态
  24. }
  25. @end
  26. @implementation MeCenterVC
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. self.navigationItem.title = @"我的";
  30. self.navigationController.navigationBar.translucent = NO;
  31. _contentArr = @[@[@""],
  32. @[@"我的资料",@"修改密码"],//,@"绑定手机"
  33. @[@"联系客服",@"关于我们"],//@"使用手册",
  34. @[@"退出登录"]
  35. ];
  36. [self myInit];
  37. }
  38. -(void)viewWillDisappear:(BOOL)animated{
  39. [super viewWillDisappear:animated];
  40. _isPushed = YES;
  41. }
  42. - (void)viewWillAppear:(BOOL)animated {
  43. [super viewWillAppear:animated];
  44. _isPushed = NO;
  45. self.navigationController.navigationBar.barTintColor = nil;
  46. }
  47. -(void)myInit{
  48. UITableView * tableview = [[UITableView alloc]initWithFrame:kFrame style:UITableViewStyleGrouped];
  49. [tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"me_cellID"];
  50. tableview.delegate = self;
  51. tableview.dataSource = self;
  52. tableview.showsVerticalScrollIndicator = NO;
  53. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 60)];
  54. tableview.tableFooterView = view;
  55. [self.view addSubview:tableview];
  56. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:CUSTOM_LOGIN_SUCCEED object:nil];
  57. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginFaile:) name:CUSTOM_LOGIN_ERROR_USER object:nil];
  58. _isFirstClick = YES;
  59. }
  60. #pragma mark 照片选择
  61. -(void)tapClick{
  62. InputView* vi = [[InputView alloc] initWithTitle:@"请选择头像"];
  63. [vi setDelegate:self];
  64. [vi show];
  65. _inputVi = vi;
  66. }
  67. -(void)InputView:(InputView *)view didGetImage:(UIImage *)image {
  68. [self upLoadImgWithImage:image];
  69. }
  70. -(void)InputViewWillPickImage:(InputView *)view
  71. {
  72. UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
  73. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
  74. pickerImage.sourceType = view.type;
  75. // pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  76. pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
  77. }
  78. pickerImage.delegate = self;
  79. pickerImage.allowsEditing = NO;
  80. [self presentViewController:pickerImage animated:YES completion:nil];
  81. }
  82. - (void) imagePickerController:(UIImagePickerController *)picker
  83. didFinishPickingMediaWithInfo:(NSDictionary *)info
  84. {
  85. [_inputVi cancelAction];
  86. [picker dismissViewControllerAnimated:YES completion:^{
  87. UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
  88. [self upLoadImgWithImage:originalImage];
  89. }];
  90. }
  91. #pragma mark 头像上传
  92. -(void)upLoadImgWithImage:(UIImage *)image
  93. {
  94. if (!image) {
  95. return;
  96. }
  97. NSData * data;
  98. if (UIImageJPEGRepresentation(image, .5))
  99. {
  100. //图片质量 压缩成多少
  101. data = UIImageJPEGRepresentation(image, .5);
  102. }else{
  103. data = UIImagePNGRepresentation(image);
  104. }
  105. NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  106. NSMutableDictionary * mDic = [NSMutableDictionary new];
  107. [mDic setValue:@"1" forKey:@"type"];
  108. [mDic setValue:MYAPPDELEGATE.userDic[@"id"] forKey:@"keyword"];
  109. [mDic setValue:encodedImageStr forKey:@"content"];
  110. [self getDataWithDic:mDic method:@"upload-1" block:^(NSDictionary *successDic) {
  111. [self showMsgByMBWithString:@"修改头像成功"];
  112. [_headImgV sd_setImageWithURL:[NSURL URLWithString:successDic[@"body"]] placeholderImage:[UIImage imageNamed:@"me_DefaultHead"]];
  113. [MYAPPDELEGATE.userDic setValue:successDic[@"body"] forKey:@"photo"];
  114. }];
  115. }
  116. #pragma mark tableview代理相关
  117. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  118. return _contentArr.count;
  119. }
  120. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  121. return [_contentArr[section] count];
  122. }
  123. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  124. if (indexPath.section == 0) {
  125. return 120;
  126. }
  127. return 50;
  128. }
  129. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  130. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"me_cellID" forIndexPath:indexPath];
  131. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  132. if (indexPath.section == 0) {
  133. //
  134. if (!_headImgV) {
  135. //
  136. _headImgV = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 90, 90)];
  137. _headImgV.layer.masksToBounds = YES;
  138. _headImgV.layer.cornerRadius = 45;
  139. NSString *imgStr = MYAPPDELEGATE.userDic[@"photo"];
  140. if (!imgStr) {
  141. imgStr = @"";
  142. }
  143. [_headImgV sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"me_DefaultHead"]];
  144. [cell.contentView addSubview:_headImgV];
  145. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
  146. tap.numberOfTapsRequired = 1;
  147. _headImgV.userInteractionEnabled = YES;
  148. [_headImgV addGestureRecognizer:tap];
  149. //
  150. UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(120, 35, 200, 50)];
  151. label.text = MYAPPDELEGATE.userDic[@"realName"];
  152. [cell.contentView addSubview:label];
  153. cell.accessoryType = UITableViewCellAccessoryNone;
  154. }
  155. }else{
  156. cell.textLabel.text = _contentArr[indexPath.section][indexPath.row];
  157. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  158. }
  159. return cell;
  160. }
  161. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  162. if (indexPath.section == 1){
  163. if (indexPath.row == 0) {
  164. //我的资料
  165. MyMessageVC *vc = [[MyMessageVC alloc]init];
  166. [self.navigationController pushViewController:vc animated:YES];
  167. return;
  168. return;
  169. }
  170. if (indexPath.row == 1) {
  171. //修改密码
  172. [self fixKey];
  173. return;
  174. }
  175. }else if (indexPath.section == 2){
  176. if (indexPath.row == 0) {
  177. //联系客服
  178. if (_isFirstClick == NO) {
  179. return;
  180. }
  181. _isFirstClick = NO;
  182. [MBProgressHUD showHUDAddedTo:self.view animated:NO];
  183. // userId 只能使用 数字 字母(包括大小写) 下划线
  184. NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  185. NSString *name = [NSString stringWithFormat:@"%@[辽I监管_%@]",MYAPPDELEGATE.userDic[@"realName"],version];
  186. [QMConnect registerSDKWithAppKey:@"32958350-4f40-11e7-a416-f12e96c638e0" userName:name userId:[NSString stringWithFormat:@"%@",MYAPPDELEGATE.userDic[@"id"]]];
  187. return;
  188. }
  189. if (indexPath.row == 1) {
  190. AboutVC *vc = [[AboutVC alloc]init];
  191. [self.navigationController pushViewController:vc animated:YES];
  192. return;
  193. }
  194. }else if (indexPath.section == 3 && indexPath.row == 0){
  195. //退出登录
  196. UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:nil message:@"您确定要退出登录吗" preferredStyle:UIAlertControllerStyleAlert];
  197. [alertFind addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  198. }]];
  199. [alertFind addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  200. [self loginAciton];
  201. }]];
  202. dispatch_async(dispatch_get_main_queue(), ^{
  203. [self.tabBarController presentViewController:alertFind animated:true completion:nil];
  204. });
  205. }
  206. }
  207. #pragma mark 修改密码
  208. -(void)fixKey
  209. {
  210. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证原密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
  211. [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
  212. textField.placeholder = @"请输入原密码";
  213. }];
  214. [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
  215. [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  216. NSString *text = [[alert.textFields firstObject] text];
  217. NSString *key = MYAPPDELEGATE.userDic[@"password"];
  218. if (![[text md5Encrypt] isEqualToString:key]) {
  219. [self showMsgByMBWithString:@"原密码错误!"];
  220. return;
  221. }
  222. [self willModifyPwd];
  223. }]];
  224. dispatch_async(dispatch_get_main_queue(), ^{
  225. [self.tabBarController presentViewController:alert animated:true completion:nil];
  226. });
  227. }
  228. -(void)willModifyPwd{
  229. UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:@"修改密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
  230. [alertFind addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  231. textField.placeholder = @"请输入新密码";
  232. textField.secureTextEntry = YES;
  233. }];
  234. [alertFind addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  235. textField.placeholder = @"请确认密码";
  236. textField.secureTextEntry = YES;
  237. }];
  238. [alertFind addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
  239. [alertFind addAction:[UIAlertAction actionWithTitle:@"确定修改" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  240. // 读取文本框的值显示出来
  241. UITextField *newtf1 = alertFind.textFields[0];
  242. UITextField *newtf = alertFind.textFields[1];
  243. if (newtf1.text.length < 6 || newtf.text.length < 6) {
  244. [self showMsgByMBWithString:@"密码长度不能少于6个字符"];
  245. return;
  246. }
  247. if (![newtf1.text isEqualToString:newtf.text])
  248. {
  249. UIAlertController *noname = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不一致,请您核查" preferredStyle:UIAlertControllerStyleAlert];
  250. [noname addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
  251. dispatch_async(dispatch_get_main_queue(), ^{
  252. [self.tabBarController presentViewController:noname animated:true completion:nil];
  253. });
  254. return;
  255. }
  256. [self chanagePasswordWithOldStr:MYAPPDELEGATE.userDic[@"password"] newStr:[newtf.text md5Encrypt]];
  257. }]];
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. [self.tabBarController presentViewController:alertFind animated:true completion:nil];
  260. });
  261. }
  262. -(void)chanagePasswordWithOldStr:(NSString *)oldpwd newStr:(NSString *)newpwd{
  263. NSMutableDictionary * mdic = [NSMutableDictionary new];
  264. [mdic setValue:MYAPPDELEGATE.userDic[@"id"] forKey:@"id"];
  265. [mdic setValue:oldpwd forKey:@"oldPwd"];
  266. [mdic setValue:newpwd forKey:@"newPwd"];
  267. [self getDataWithDic:mdic method:@"modifyPwd" block:^(NSDictionary *successDic) {
  268. //修改成功,返回登录界面,重新登录
  269. UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:@"修改成功,请重新登录" preferredStyle:UIAlertControllerStyleAlert];
  270. [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  271. NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
  272. [ud setObject:@"" forKey:@"userPassWord"];
  273. [ud synchronize];//先这样吧。如果非要清除显示占用的密码再做处理
  274. [self.tabBarController dismissViewControllerAnimated:YES completion:nil];
  275. }]];
  276. [self.tabBarController presentViewController:alert animated:YES completion:nil];
  277. }];
  278. }
  279. -(void)loginAciton{
  280. MYAPPDELEGATE.userDic = nil;
  281. [LoginVC saveAppIosImei];
  282. NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
  283. [defatluts removeObjectForKey:@"userPassWord"];
  284. [defatluts synchronize];
  285. LoginVC * loginvc = [[LoginVC alloc]init];
  286. MYAPPDELEGATE.window.rootViewController = loginvc;
  287. }
  288. #pragma mark 客服
  289. // - 登陆成功状态
  290. - (void)loginSuccess:(NSNotification *)sender {
  291. NSLog(@"连接成功");
  292. if (_isPushed == YES) {
  293. return;
  294. }
  295. [MBProgressHUD hideHUDForView:self.view animated:YES];
  296. [QMConnect sdkGetPeers:^(NSArray * _Nonnull peerArray) {
  297. dispatch_async(dispatch_get_main_queue(), ^{
  298. NSArray *peers = peerArray;
  299. if (peers.count == 1 && peers.count != 0) {
  300. [self showChatRoomViewController:[peers.firstObject objectForKey:@"id"]];
  301. }else {
  302. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择您咨询的类型或业务部门(对应技能组)" preferredStyle:UIAlertControllerStyleAlert];
  303. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  304. }];
  305. [alertController addAction:cancelAction];
  306. for (NSDictionary *index in peers) {
  307. UIAlertAction *surelAction = [UIAlertAction actionWithTitle:[index objectForKey:@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  308. [self showChatRoomViewController:[index objectForKey:@"id"]];
  309. }];
  310. [alertController addAction:surelAction];
  311. }
  312. [self presentViewController:alertController animated:YES completion:nil];
  313. }
  314. _isFirstClick = YES;
  315. });
  316. } failureBlock:^{
  317. _isFirstClick = YES;
  318. }];
  319. }
  320. // - 登陆失败状态
  321. - (void)loginFaile:(NSNotification *)sender {
  322. NSLog(@"客服登录失败---->%@",sender.object);
  323. _isFirstClick = YES;
  324. [MBProgressHUD hideHUDForView:self.view animated:YES];
  325. }
  326. // - 跳转聊天界面
  327. - (void)showChatRoomViewController:(NSString *)peerId {
  328. QMChatRoomViewController *_chatRoomViewController = [[QMChatRoomViewController alloc] init];
  329. _chatRoomViewController.peerId = peerId;
  330. _chatRoomViewController.isPush = NO;
  331. NSString *imgStr = MYAPPDELEGATE.userDic[@"photo"];
  332. if (!imgStr) {
  333. imgStr = @"";
  334. }
  335. _chatRoomViewController.avaterStr = imgStr;
  336. [self.navigationController pushViewController:_chatRoomViewController animated:YES];
  337. }
  338. //- (BOOL)canBecomeFirstResponder {
  339. // return NO;
  340. //}
  341. - (void)dealloc {
  342. [[NSNotificationCenter defaultCenter] removeObserver:self];
  343. }
  344. - (void)didReceiveMemoryWarning {
  345. [super didReceiveMemoryWarning];
  346. // Dispose of any resources that can be recreated.
  347. }
  348. @end