UserViewController.m 7.54 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9
//
//  UserViewController.m
//  Lighting
//
//  Created by 曹云霄 on 16/5/8.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "UserViewController.h"
10
#import "ChangePasswordViewController.h"
勾芒's avatar
勾芒 committed
11
#import "LoginViewController.h"
曹云霄's avatar
曹云霄 committed
12 13 14 15 16 17 18 19 20 21

@interface UserViewController ()

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
22 23 24 25 26 27 28
    
    [self uiConfigAction];
}

#pragma mark -UI
- (void)uiConfigAction
{
曹云霄's avatar
曹云霄 committed
29 30 31 32 33 34 35 36
    self.userNameBack.layer.masksToBounds = YES;
    self.userNameBack.layer.cornerRadius = kCornerRadius;
    self.userNameBack.layer.borderWidth = 0.5;
    self.userNameBack.layer.borderColor = kMainBlueColor.CGColor;
    self.passWordBack.layer.masksToBounds = YES;
    self.passWordBack.layer.cornerRadius = kCornerRadius;
    self.passWordBack.layer.borderWidth = 0.5;
    self.passWordBack.layer.borderColor = kMainBlueColor.CGColor;
37 38 39 40 41 42
    self.cancellationButton.layer.masksToBounds = YES;
    self.cancellationButton.layer.cornerRadius = kCornerRadius;
    self.switchUser.layer.masksToBounds = YES;
    self.switchUser.layer.cornerRadius = kCornerRadius;
    self.changePassword.layer.masksToBounds = YES;
    self.changePassword.layer.cornerRadius = kCornerRadius;
曹云霄's avatar
曹云霄 committed
43
    self.changePassword.layer.borderWidth = 0.5;
44 45
    self.changePassword.layer.borderColor = kMainBlueColor.CGColor;
    
曹云霄's avatar
曹云霄 committed
46 47
    self.userName.text = [Shoppersmanager manager].userNameString;
    self.passWord.text = [Shoppersmanager manager].passWordString;
勾芒's avatar
勾芒 committed
48
//    self.userHeader sd_setImageWithURL:[NSURL URLWithString:[Shoppersmanager manager].Shoppers.employee] placeholderImage:<#(UIImage *)#>
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    
}




#pragma mark -修改密码
- (IBAction)ChangPasswordButtonClick:(UIButton *)sender {
    
    
    if ([self.delegate respondsToSelector:@selector(changPasswordButtonClick)]) {
        
        [self.delegate changPasswordButtonClick];
        
    }
曹云霄's avatar
曹云霄 committed
64 65 66
}


67 68
#pragma mark -注销
- (IBAction)cancellationButtonClick:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
69
    
勾芒's avatar
勾芒 committed
70
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"注销将清空用户数据,是否继续?" preferredStyle:UIAlertControllerStyleAlert];
曹云霄's avatar
曹云霄 committed
71
    
勾芒's avatar
勾芒 committed
72 73 74 75 76 77 78 79 80 81 82 83
    __weak typeof(self) weakSelf = self;
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf CreateMBProgressHUDLoding];
        [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/employee/logout"] WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
            
            [weakSelf RemoveMBProgressHUDLoding];
            if ([returnValue[@"code"] isEqualToNumber:@0]) {
                
勾芒's avatar
勾芒 committed
84 85 86 87 88 89 90
                [Shoppersmanager manager].currentCustomer = NO;
                [Customermanager manager].customerID = nil;
                [Customermanager manager].customerName = nil;
                [Customermanager manager].customerPhoneNumber = nil;
                [Customermanager manager].companyName = nil;
                [Customermanager manager].cutomerAddress = nil;
                [Customermanager manager].header = nil;
勾芒's avatar
勾芒 committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                LoginViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"Login"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:USERNAME];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:PASSWORD];
                [weakSelf restoreRootViewController:loginVC];
            }else
            {
                [weakSelf promptCustomerWithString:@"注销失败"];
            }
            
        } WithErrorCodeBlock:^(id errorCodeValue) {
            
        } WithFailureBlock:^(id error) {
            [weakSelf RemoveMBProgressHUDLoding];
        }];
    }]];
    [self presentViewController:alertVC animated:YES completion:nil];
}


#pragma mark -切换rootViewcontroller
- (void)restoreRootViewController:(UIViewController *)rootViewController
{
    typedef void (^Animation)(void);
    UIWindow* window = SHARED_APPDELEGATE.window;
    rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        window.rootViewController = rootViewController;
        [UIView setAnimationsEnabled:oldState];
    };
曹云霄's avatar
曹云霄 committed
123
    
勾芒's avatar
勾芒 committed
124 125 126 127 128
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionFlipFromBottom
                    animations:animation
                    completion:nil];
129 130 131 132 133
}


#pragma mark -切换账户
- (IBAction)switchUserButtonClick:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    
    
    if (self.userName.text.length == 0) {
        
        [self ErrorMBProgressView:@"用户名不能为空"];
        return;
    }
    if (self.passWord.text.length == 0) {
        
        [self ErrorMBProgressView:@"密码不能为空"];
        return;
    }
    
    [self CreateMBProgressHUDLoding];
    NSString *URL = [NSString stringWithFormat:@"%@%@",ServerAddress,@"/employee/login"];
    LoginInfo *login = [[LoginInfo alloc]init];
    login.username = self.userName.text;
    login.password = self.passWord.text;
    
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:URL WithRequestType:0 WithParameter:login WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            LoginResult *result = [[LoginResult alloc] initWithDictionary:returnValue[@"data"] error:nil];
            [Shoppersmanager manager].userNameString = self.userName.text;
            [Shoppersmanager manager].passWordString = self.passWord.text;
勾芒's avatar
勾芒 committed
161
            [Shoppersmanager manager].Shoppers = result;
曹云霄's avatar
曹云霄 committed
162
            
勾芒's avatar
勾芒 committed
163 164 165 166 167
            //保存用户名密码
            [[NSUserDefaults standardUserDefaults] setObject:self.userName.text forKey:USERNAME];
            [[NSUserDefaults standardUserDefaults] setObject:self.passWord.text forKey:PASSWORD];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
曹云霄's avatar
曹云霄 committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        }else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
            [self recoveryUsernamePasswrod];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        NSLog(@"%@",errorCodeValue);
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
        [self recoveryUsernamePasswrod];
        
    }];
}


#pragma mark -切换失败后账户密码复原
- (void)recoveryUsernamePasswrod
{
    self.userName.text = [Shoppersmanager manager].userNameString;
    self.passWord.text = [Shoppersmanager manager].passWordString;
192 193 194 195 196
}




曹云霄's avatar
曹云霄 committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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