UserViewController.m 13.6 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"
12
#import "DeviceDirectionManager.h"
曹云霄's avatar
曹云霄 committed
13

14
@interface UserViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
曹云霄's avatar
曹云霄 committed
15 16 17 18 19 20 21

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
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
    self.switchUser.layer.masksToBounds = YES;
    self.switchUser.layer.cornerRadius = kCornerRadius;
    self.changePassword.layer.masksToBounds = YES;
    self.changePassword.layer.cornerRadius = kCornerRadius;
曹云霄's avatar
曹云霄 committed
41
    self.changePassword.layer.borderWidth = 0.5;
42
    self.changePassword.layer.borderColor = kMainBlueColor.CGColor;
43 44 45 46 47
    self.rebateBackView.layer.masksToBounds = YES;
    self.rebateBackView.layer.borderWidth = 1.0f;
    self.rebateBackView.layer.borderColor = kMainBlueColor.CGColor;
    self.rebateBackView.layer.cornerRadius = 20;
    [self.rebateBackView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rebateViewClickAction:)]];
曹云霄's avatar
曹云霄 committed
48 49
    self.userName.text = [Shoppersmanager manager].userNameString;
    self.passWord.text = [Shoppersmanager manager].passWordString;
50 51 52 53 54 55
    self.userHeader.userInteractionEnabled = YES;
    NSString *headerurl = [Shoppersmanager manager].Shoppers.employee.picture;
    [self.userHeader sd_setImageWithURL:[NSURL URLWithString:headerurl] placeholderImage:ReplaceImage];
    [self.userHeader addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userHeaderClickAction:)]];
}

56 57 58
#pragma mark -查询返利金额
- (void)queryShoppersCommission
{
59 60
    WS(weakSelf);
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(SHOPPERSCOMMISSION),[Shoppersmanager manager].Shoppers.employee.fid] WithCallClass:weakSelf WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
61 62
        
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
曹云霄's avatar
曹云霄 committed
63
//            self.rebateLabe.text = [NSString stringWithFormat:@"¥%.2f",[returnValue[@"data"] floatValue]];
64 65 66 67 68 69 70
        }
    } WithErrorCodeBlock:^(id errorCodeValue) {
    } WithFailureBlock:^(id error) {
    }];
}


71
#pragma mark -我的账户点击
72 73
- (void)rebateViewClickAction:(UITapGestureRecognizer *)tap
{
74
    [[NSNotificationCenter defaultCenter] postNotificationName:OPENCONTROLLER object:@(3)];
75 76
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#pragma mark -用户头像点击
- (void)userHeaderClickAction:(UITapGestureRecognizer *)tap
{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIImagePickerController *PcCamera = [[UIImagePickerController alloc]init];
    PcCamera.delegate = self;
    [DeviceDirectionManager instance].isHorizontal=YES;
    [alertView addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //拍照
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [PcCamera setSourceType:UIImagePickerControllerSourceTypeCamera];
            PcCamera.allowsEditing = YES;
            [self presentViewController:PcCamera animated:YES completion:nil];
        }
        else
        {
曹云霄's avatar
曹云霄 committed
93
            [DeviceDirectionManager instance].isHorizontal=NO;
94 95 96 97 98 99 100 101 102 103 104 105
            [self ErrorMBProgressView:@"相机无法使用"];
        }
    }]];
    [alertView addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册中选择
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [PcCamera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            PcCamera.allowsEditing = YES;
            [self presentViewController:PcCamera animated:YES completion:nil];
        }
        else
        {
曹云霄's avatar
曹云霄 committed
106
            [DeviceDirectionManager instance].isHorizontal=NO;
107 108 109 110
            [self ErrorMBProgressView:@"相册无法打开"];
        }
    }]];
    [alertView addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
曹云霄's avatar
曹云霄 committed
111
        [DeviceDirectionManager instance].isHorizontal=NO;
112 113 114 115 116 117 118 119 120
        [alertView dismissViewControllerAnimated:YES completion:nil];
    }]];
    [self presentViewController:alertView animated:YES completion:nil];
}

#pragma mark -拍照、从相册选择
#pragma -mark -UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
121
    [DeviceDirectionManager instance].isHorizontal=NO;
122 123 124 125 126 127 128 129 130 131 132 133 134
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *Headimage = [info objectForKey:UIImagePickerControllerOriginalImage];
    __weak typeof(self) weskSelf = self;
    //修改并保存头像
    [self uploadUserHeader:Headimage completeBlock:^(NSString *string) {
        weskSelf.userHeader.image = Headimage;
        [Shoppersmanager manager].Shoppers.employee.picture = string;
    }];
}

#pragma mark -保存选中头像、并上传
- (void)uploadUserHeader:(UIImage *)image completeBlock:(void(^)(NSString *string))complete
{
135
    WS(weakSelf);
136
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
137
    [[NetworkRequestClassManager Manager] UploadImageWithURL:SERVERREQUESTURL(UPLOADHEADER) WithRequestType:1 WithImageDatas:data WithParameter:nil WithReturnValueBlock:^(id returnValue) {
138 139
        
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
140
            [weakSelf modifyshoppersInformation:returnValue[@"data"] complete:^{
141 142 143 144
                 complete(returnValue[@"data"]);
            }];
        }else
        {
145
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
146 147 148 149
        }
    } WithprogressBlock:^(double progress) {
        
        if (progress >= 1) {
150 151
            weakSelf.TCHud.labelText = @"上传完成";
            [weakSelf RemoveMBProgressHUDLoding];
152 153
        }else
        {
154
            [weakSelf ShowProgressView:progress];
155 156
        }
    } WithErrorCodeBlock:^(id errorCodeValue) {
157 158
        weakSelf.TCHud.labelText = @"上传失败";
        [weakSelf RemoveMBProgressHUDLoding];
159
    } WithFailureBlock:^(NSError *error) {
160 161
        weakSelf.TCHud.labelText = @"上传失败";
        [weakSelf RemoveMBProgressHUDLoding];
162
    }];
163 164 165
}


166 167 168 169 170 171 172
#pragma mark -上传完成修改导购个人信息
- (void)modifyshoppersInformation:(NSString *)headerurl complete:(void(^)())block
{
    [self CreateMBProgressHUDLoding];
    RsEmployeeRequest *employ = [[RsEmployeeRequest alloc]init];
    employ.employee = [Shoppersmanager manager].Shoppers.employee;
    employ.employee.picture = headerurl;
曹云霄's avatar
曹云霄 committed
173
    WS(weakSelf);
174
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(MODITYSHOPPERSINFORMATION) WithCallClass:weakSelf WithRequestType:0 WithParameter:employ WithReturnValueBlock:^(id returnValue) {
175
        
曹云霄's avatar
曹云霄 committed
176
        [weakSelf RemoveMBProgressHUDLoding];
177 178
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            block();
曹云霄's avatar
曹云霄 committed
179
            [weakSelf SuccessMBProgressView:@"修改成功"];
180 181
        }else
        {
曹云霄's avatar
曹云霄 committed
182
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
183 184 185
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
曹云霄's avatar
曹云霄 committed
186
        [weakSelf RemoveMBProgressHUDLoding];
187
    } WithFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
188
        [weakSelf RemoveMBProgressHUDLoding];
189 190 191
    }];
}

192 193
#pragma mark -修改密码
- (IBAction)ChangPasswordButtonClick:(UIButton *)sender {
194

195 196 197
    if ([self.delegate respondsToSelector:@selector(changPasswordButtonClick)]) {
        [self.delegate changPasswordButtonClick];
    }
曹云霄's avatar
曹云霄 committed
198 199
}

曹云霄's avatar
曹云霄 committed
200
#pragma mark -注销《切换用户》
201
- (IBAction)cancellationButtonClick:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
202
    
曹云霄's avatar
曹云霄 committed
203
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"切换用户将清空用户数据,是否继续?" preferredStyle:UIAlertControllerStyleAlert];
曹云霄's avatar
曹云霄 committed
204
    WS(weakSelf);
勾芒's avatar
勾芒 committed
205 206 207 208 209 210
    [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];
211
        [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:SERVERREQUESTURL(CANCELLOGIN) WithCallClass:weakSelf WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
212 213 214 215
            
            [weakSelf RemoveMBProgressHUDLoding];
            if ([returnValue[@"code"] isEqualToNumber:@0]) {
                
勾芒's avatar
勾芒 committed
216
                [Shoppersmanager manager].currentCustomer = NO;
勾芒's avatar
勾芒 committed
217
                [Customermanager manager].model = nil;
曹云霄's avatar
曹云霄 committed
218
                LoginViewController *loginVC = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"Login"];
勾芒's avatar
勾芒 committed
219 220 221 222 223
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:USERNAME];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:PASSWORD];
                [weakSelf restoreRootViewController:loginVC];
            }else
            {
曹云霄's avatar
曹云霄 committed
224
                [weakSelf promptCustomerWithString:@"切换用户失败"];
勾芒's avatar
勾芒 committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            }
            
        } 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
249
    
勾芒's avatar
勾芒 committed
250 251 252 253 254
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionFlipFromBottom
                    animations:animation
                    completion:nil];
255 256 257 258 259
}


#pragma mark -切换账户
- (IBAction)switchUserButtonClick:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    
    
    if (self.userName.text.length == 0) {
        
        [self ErrorMBProgressView:@"用户名不能为空"];
        return;
    }
    if (self.passWord.text.length == 0) {
        
        [self ErrorMBProgressView:@"密码不能为空"];
        return;
    }
    
    [self CreateMBProgressHUDLoding];
    LoginInfo *login = [[LoginInfo alloc]init];
    login.username = self.userName.text;
    login.password = self.passWord.text;
曹云霄's avatar
曹云霄 committed
277
    WS(weakSelf);
278
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(LOGIN) WithCallClass:weakSelf WithRequestType:0 WithParameter:login WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
279
        
曹云霄's avatar
曹云霄 committed
280
        [weakSelf RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
281 282
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
曹云霄's avatar
曹云霄 committed
283
            [weakSelf SuccessMBProgressView:@"登陆成功"];
曹云霄's avatar
曹云霄 committed
284
            LoginResult *result = [[LoginResult alloc] initWithDictionary:returnValue[@"data"] error:nil];
曹云霄's avatar
曹云霄 committed
285 286
            [Shoppersmanager manager].userNameString = weakSelf.userName.text;
            [Shoppersmanager manager].passWordString = weakSelf.passWord.text;
287
            [Shoppersmanager manager].currentCustomer = NO;
勾芒's avatar
勾芒 committed
288
            [Shoppersmanager manager].Shoppers = result;
289
            [[NSNotificationCenter defaultCenter] postNotificationName:CHANGESHOPPERSNAME object:result.employee.realName];
曹云霄's avatar
曹云霄 committed
290
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGECUSTOMERNAME" object:nil];
291
            [[NSNotificationCenter defaultCenter] postNotificationName:@"EMPTYCUSTOMERNAME" object:nil];//情况当前客户数据
勾芒's avatar
勾芒 committed
292
            //保存用户名密码
曹云霄's avatar
曹云霄 committed
293 294
            [[NSUserDefaults standardUserDefaults] setObject:weakSelf.userName.text forKey:USERNAME];
            [[NSUserDefaults standardUserDefaults] setObject:weakSelf.passWord.text forKey:PASSWORD];
勾芒's avatar
勾芒 committed
295
            [[NSUserDefaults standardUserDefaults] synchronize];
曹云霄's avatar
曹云霄 committed
296
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
勾芒's avatar
勾芒 committed
297
            
曹云霄's avatar
曹云霄 committed
298 299
        }else
        {
曹云霄's avatar
曹云霄 committed
300 301
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
            [weakSelf recoveryUsernamePasswrod];
曹云霄's avatar
曹云霄 committed
302 303 304 305 306 307 308 309
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        NSLog(@"%@",errorCodeValue);
        
    } WithFailureBlock:^(id error) {
        
曹云霄's avatar
曹云霄 committed
310 311
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf recoveryUsernamePasswrod];
曹云霄's avatar
曹云霄 committed
312 313 314 315 316 317 318 319 320 321
        
    }];
}


#pragma mark -切换失败后账户密码复原
- (void)recoveryUsernamePasswrod
{
    self.userName.text = [Shoppersmanager manager].userNameString;
    self.passWord.text = [Shoppersmanager manager].passWordString;
322 323 324 325 326
}




曹云霄's avatar
曹云霄 committed
327 328 329 330 331 332 333

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

@end