You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
12 KiB

//
// EditMeViewController.m
// watch
//
// Created by xTT on 2017/7/13.
// Copyright © 2017xTT. All rights reserved.
//
#import "EditMeViewController.h"
#import "ShowWXViewController.h"
#import <WXApi.h>
//#import "WXApiObject.h"
#import "WechatAuthSDK.h"
#import "AppDelegate.h"
#import "WatchHomeViewController.h"
#import "CommonPopView.h"
@interface EditMeViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) User *editUser;
/// 头像
@property (nonatomic ,weak) UIImageView *iconImg;
/// 昵称
@property (nonatomic ,weak) UITextField *nameField;
/// 手机号码
@property (nonatomic ,weak) UITextField *phoneField;
@end
@implementation EditMeViewController
- (User *)editUser{
if (!_editUser) {
_editUser = [[User alloc] init];
_editUser.openid = cUser.openid;
_editUser.image = cUser.image;
_editUser.name = cUser.name;
_editUser.phone = cUser.phone;
}
return _editUser;
}
-(void)viewWillAppear:(BOOL)animated{
[self refreshData];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
WEAKSELF
self.zx_navTitle = @"我的账号";
[self zx_setRightBtnWithText:@"保存" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
[weakSelf rightBarItemClick];
}];
NSArray *titleArr = @[@"头像",@"昵称",@"手机号码"];
UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:KKWhiteColorColor cornerRadius:12 borderWidth:0 borderColor:KKWhiteColorColor];
[self.view addSubview:bgView];
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view).inset(15);
make.top.equalTo(self.view).offset(iPhoneX_NavHeight+15);
make.height.mas_equalTo(titleArr.count*Adapted(55));
}];
for (int i = 0; i < titleArr.count; i++)
{
UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:Font_(16) textColor:KKBlack20 text:titleArr[i] Radius:0];
[bgView addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(bgView).offset(i*Adapted(55));
make.left.right.equalTo(bgView).inset(15);
make.height.mas_equalTo(Adapted(55));
}];
if (i == 0)
{
/// 右箭头
UIImageView *rightImg = [UICommon ui_imageView:CGRectZero fileName:@"icon_enter_gray"];
[bgView addSubview:rightImg];
[rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(titleLabel);
make.right.equalTo(bgView.mas_right).inset(15);
make.size.mas_equalTo(rightImg.image.size);
}];
UIImageView *iconImg = [UIImageView new];
iconImg.layer.cornerRadius = Adapted(16);
iconImg.layer.masksToBounds = YES;
[iconImg sd_setImageWithURL:[NSURL URLWithString:cUser.avator]
placeholderImage:[myHelper getImageWithName:@"默认头像"]
options:SDWebImageRetryFailed];
self.iconImg = iconImg;
[bgView addSubview:iconImg];
[iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(titleLabel);
make.right.equalTo(rightImg.mas_left).inset(10);
make.size.mas_equalTo(CGSizeMake(Adapted(32), Adapted(32)));
}];
titleLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconGuestures:)];
[titleLabel addGestureRecognizer:tapGesture];
}
else
{
UITextField *fieldView = [UICommon ui_textField:CGRectZero textColor:KKBlack20 backColor:KKClearColor font:FontBold_(14) maxTextNum:i == 1 ? 30 : 11 placeholderColor:KKGrey153 placeholder:@"" toMaxNum:nil change:nil];
if (i == 1)
{
fieldView.text = self.editUser.name;
self.nameField = fieldView;
}
else
{
fieldView.text = self.editUser.phone;
self.phoneField = fieldView;
}
[bgView addSubview:fieldView];
fieldView.textAlignment = NSTextAlignmentRight;
[fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(bgView.mas_right).inset(15);
make.centerY.equalTo(titleLabel);
make.size.mas_equalTo(CGSizeMake(150, 40));
}];
}
if (i < titleArr.count-1)
{
UIImageView *line = [UIImageView new];
line.backgroundColor = RGB(240, 240, 240);
[bgView addSubview:line];
[line mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(bgView).inset(15);
make.top.equalTo(titleLabel.mas_bottom);
make.height.mas_equalTo(0.4);
}];
}
}
UIButton *outLogin = [UICommon ui_buttonSimple:CGRectZero font:Font_(16) normalColor:RGB(102, 102, 100) normalText:@"退出登录" click:^(id x) {
[weakSelf signOutClick];
}];
outLogin.backgroundColor = KKWhiteColorColor;
outLogin.layer.cornerRadius = Adapted(26);
outLogin.layer.masksToBounds = YES;
[self.view addSubview:outLogin];
[outLogin mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(15);
make.top.equalTo(bgView.mas_bottom).offset(12);
make.right.equalTo(self.view.mas_centerX).inset(7.5);
make.height.mas_equalTo(Adapted(52));
}];
UIButton *cancellation = [UICommon ui_buttonSimple:CGRectZero font:Font_(16) normalColor:RGB(254, 83, 71) normalText:@"注销用户 (谨慎)" click:^(id x) {
[weakSelf unregister];
}];
cancellation.backgroundColor = KKWhiteColorColor;
cancellation.layer.cornerRadius = Adapted(26);
cancellation.layer.masksToBounds = YES;
[self.view addSubview:cancellation];
[cancellation mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view.mas_right).inset(15);
make.top.equalTo(outLogin.mas_top);
make.left.equalTo(self.view.mas_centerX).offset(7.5);
make.height.mas_equalTo(Adapted(52));
}];
}
-(void)refreshData{
WEAKSELF
[cUser getUserInfoSuccess:^(id responseObject) {
} failure:^(id faiObject) {
}];
}
- (void)rightBarItemClick
{
self.editUser.phone = self.phoneField.text;
self.editUser.name = self.nameField.text;
[self.view endEditing:YES]; //取消 所有的输入框的焦点
if (self.editUser.phone.length != 11) {
[SVProgressHUD showErrorWithStatus:@"请输入11位号码"];
}else if(self.editUser.name.length == 0){
[SVProgressHUD showErrorWithStatus:@"请输入昵称"];
}else if(self.editUser.name.length >8) {
[SVProgressHUD showErrorWithStatus:@"昵称不能大于8个字符"];
} else{
[self.editUser saveUserInfoSuccess:^(id responseObject) {
// cUser.image = self.editUser.image;
[SVProgressHUD showInfoWithStatus:@"保存成功"];
if (self.willLoadBlack)
self.willLoadBlack();
[self refreshData];
cUser.phone = self.editUser.phone;
cUser.name = self.editUser.name;
cUser.avator = self.editUser.avator;
} failure:^(id faiObject) {
}];
}
}
#pragma mark UIImagePickerControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([UIDevice currentDevice].systemVersion.floatValue < 11) {
return;
}
if ([viewController isKindOfClass:NSClassFromString(@"PUPhotoPickerHostViewController")]) {
[viewController.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.frame.size.width < 42) {
[viewController.view sendSubviewToBack:obj];
*stop = YES;
}
}];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{}];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
self.editUser.image = image;
self.iconImg.image = image;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/// 注销
- (void)unregister
{
CommonPopView *popView = [CommonPopView new];
[popView subPopView:popViewType_binding PopTitle:@"警告" PopText:@"注销用户信息将被清空无法恢复,请确认是否注销!" okTouchBlock:^{
AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
UITabBarController *tabBarVC = (UITabBarController*)appDelegate.window.rootViewController;
UINavigationController *navVC = (UINavigationController*)tabBarVC.viewControllers.firstObject;
for (UIViewController *vc in navVC.viewControllers)
{
if([vc isKindOfClass:[WatchHomeViewController class]])
{
WatchHomeViewController *tempVC = (WatchHomeViewController*)vc;
tempVC.closeFocusTips = NO;
}
}
[User unregisterWithSuccess:^(id responseObject)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"outLogin" object:nil];
[User setCurrentUser:nil];
[UserDefaults removeObjectForKey:FirstEveryDayKey];
} failure:^(id faiObject) {
}];
}];
}
/// 退出登录
- (void)signOutClick
{
CommonPopView *popView = [CommonPopView new];
[popView subPopView:popViewType_outLogin PopTitle:@"是否退出当前账号?" PopText:@"" okTouchBlock:^{
AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
UITabBarController *tabBarVC = (UITabBarController*)appDelegate.window.rootViewController;
UINavigationController *navVC = (UINavigationController*)tabBarVC.viewControllers.firstObject;
for (UIViewController *vc in navVC.viewControllers)
{
if([vc isKindOfClass:[WatchHomeViewController class]])
{
WatchHomeViewController *tempVC = (WatchHomeViewController*)vc;
tempVC.closeFocusTips = NO;
}
}
[User logoutWithSuccess:^(id responseObject)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"outLogin"
object:nil];
[User setCurrentUser:nil];
//退出登录,清除每天第一次登陆的记录
[UserDefaults removeObjectForKey:FirstEveryDayKey];
} failure:^(id faiObject) {
}];
}];
}
/// 选择头像
- (void)iconGuestures:(UITapGestureRecognizer *)sender
{
UIAlertController *sheet = [myHelper getAlertWithPicture:^(UIImagePickerController *imagePickerVC) {
imagePickerVC.delegate = self;
[self presentViewController:imagePickerVC animated:YES completion:^{}];
}];
if(sheet){
[self presentViewController:sheet animated:YES completion:nil];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end