UserViewController.m 13.4 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"
13 14
#import "JPUSHService.h"

曹云霄's avatar
曹云霄 committed
15

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

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
24 25
    
    [self uiConfigAction];
26
    [self addGestureRecognizer];
27 28 29 30 31
}

#pragma mark -UI
- (void)uiConfigAction
{
曹云霄's avatar
曹云霄 committed
32 33 34 35 36 37 38 39
    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;
40 41 42 43
    self.switchUser.layer.masksToBounds = YES;
    self.switchUser.layer.cornerRadius = kCornerRadius;
    self.changePassword.layer.masksToBounds = YES;
    self.changePassword.layer.cornerRadius = kCornerRadius;
曹云霄's avatar
曹云霄 committed
44
    self.changePassword.layer.borderWidth = 0.5;
45
    self.changePassword.layer.borderColor = kMainBlueColor.CGColor;
46 47 48 49
    self.rebateBackView.layer.masksToBounds = YES;
    self.rebateBackView.layer.borderWidth = 1.0f;
    self.rebateBackView.layer.borderColor = kMainBlueColor.CGColor;
    self.rebateBackView.layer.cornerRadius = 20;
50 51 52 53
    self.guideIntegralView.layer.masksToBounds = YES;
    self.guideIntegralView.layer.borderWidth = 1.0f;
    self.guideIntegralView.layer.borderColor = kMainBlueColor.CGColor;
    self.guideIntegralView.layer.cornerRadius = 20;
曹云霄's avatar
曹云霄 committed
54 55
    self.userName.text = [Shoppersmanager manager].userNameString;
    self.passWord.text = [Shoppersmanager manager].passWordString;
56 57 58 59 60
    self.userHeader.userInteractionEnabled = YES;
    NSString *headerurl = [Shoppersmanager manager].Shoppers.employee.picture;
    [self.userHeader sd_setImageWithURL:[NSURL URLWithString:headerurl] placeholderImage:ReplaceImage];
}

61 62
#pragma mark - 添加添加手势
- (void)addGestureRecognizer
63
{
64 65
    [self.rebateBackView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rebateViewClickAction:)]];
    [self.guideIntegralView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(guideIntegralViewClickAction:)]];
66
    [self.userHeader addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userHeaderClickAction:)]];
67 68
}

69
#pragma mark -我的账户点击
70 71
- (void)rebateViewClickAction:(UITapGestureRecognizer *)tap
{
72 73 74 75 76 77 78 79 80
    SHARED_APPDELEGATE.tabBarController.selectedIndex = 3;
    [SHARED_APPDELEGATE.tabBarController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 我的积分
- (void)guideIntegralViewClickAction:(UITapGestureRecognizer *)sender
{
    SHARED_APPDELEGATE.tabBarController.selectedIndex = 4;
    [SHARED_APPDELEGATE.tabBarController dismissViewControllerAnimated:YES completion:nil];
81 82
}

83 84 85
#pragma mark -用户头像点击
- (void)userHeaderClickAction:(UITapGestureRecognizer *)tap
{
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    // 判断应用是否有使用相机的权限
    if(![BaseViewController determineCameraPermissions]){
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在iPad的“设置-隐私-相机”选项中,允许欧立方访问你的相机" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alertVC animated:YES completion:nil];
        
    }else{
        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
            {
                [DeviceDirectionManager instance].isHorizontal=NO;
曹云霄's avatar
曹云霄 committed
107
                [XBLoadingView showHUDViewWithText:@"相机无法使用"];
108 109 110 111 112 113 114 115 116 117 118 119
            }
        }]];
        [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
            {
                [DeviceDirectionManager instance].isHorizontal=NO;
曹云霄's avatar
曹云霄 committed
120
                [XBLoadingView showHUDViewWithText:@"相册无法打开"];
121 122 123
            }
        }]];
        [alertView addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
曹云霄's avatar
曹云霄 committed
124
            [DeviceDirectionManager instance].isHorizontal=NO;
125 126 127 128 129
            [alertView dismissViewControllerAnimated:YES completion:nil];
        }]];
        [self presentViewController:alertView animated:YES completion:nil];
    }
 }
130 131 132 133 134

#pragma mark -拍照、从相册选择
#pragma -mark -UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
135
    [DeviceDirectionManager instance].isHorizontal=NO;
136 137
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *Headimage = [info objectForKey:UIImagePickerControllerOriginalImage];
138
    WS(weakSelf);
139 140
    //修改并保存头像
    [self uploadUserHeader:Headimage completeBlock:^(NSString *string) {
141
        weakSelf.userHeader.image = Headimage;
142 143 144 145 146 147 148
        [Shoppersmanager manager].Shoppers.employee.picture = string;
    }];
}

#pragma mark -保存选中头像、并上传
- (void)uploadUserHeader:(UIImage *)image completeBlock:(void(^)(NSString *string))complete
{
149
    WS(weakSelf);
150
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
曹云霄's avatar
曹云霄 committed
151
    XBLoadingView *progressView = [XBLoadingView showHUDViewProgressLabel:@"上传头像中"];
曹云霄's avatar
曹云霄 committed
152
    [HTTP uploadImageWithURL:SERVERREQUESTURL(UPLOADHEADER) withRequestType:ONE withImageDatas:data withParameter:nil withReturnValueBlock:^(id returnValue) {
153
        
曹云霄's avatar
曹云霄 committed
154
        if (RESULT(returnValue)) {
155
            [weakSelf modifyshoppersInformation:returnValue[@"data"] complete:^{
156 157 158 159
                 complete(returnValue[@"data"]);
            }];
        }else
        {
曹云霄's avatar
曹云霄 committed
160
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
161
        }
162
    } withprogressBlock:^(double progress) {
163 164
        
        if (progress >= 1) {
165 166 167 168 169
            dispatch_async(dispatch_get_main_queue(), ^{
                progressView.labelText = @"上传完成";
                [progressView hide:YES afterDelay:1];
            });
 
曹云霄's avatar
曹云霄 committed
170 171
        }else{
            progressView.progress = progress;
172
        }
173
    } withFailureBlock:^(NSError *error) {
174
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
175
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
176
    }];
177 178 179
}


180 181 182
#pragma mark -上传完成修改导购个人信息
- (void)modifyshoppersInformation:(NSString *)headerurl complete:(void(^)())block
{
曹云霄's avatar
曹云霄 committed
183
    [XBLoadingView showHUDViewWithDefault];
184 185 186
    RsEmployeeRequest *employ = [[RsEmployeeRequest alloc]init];
    employ.employee = [Shoppersmanager manager].Shoppers.employee;
    employ.employee.picture = headerurl;
曹云霄's avatar
曹云霄 committed
187
    [HTTP networkRequestWithURL:SERVERREQUESTURL(MODITYSHOPPERSINFORMATION)  withRequestType:ZERO withParameter:employ withReturnValueBlock:^(id returnValue) {
188
        
曹云霄's avatar
曹云霄 committed
189
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
190
        if (RESULT(returnValue)) {
191
            block();
192
            [XBLoadingView showHUDViewWithSuccessText:@"修改成功" completeBlock:nil];
193 194
        }else
        {
曹云霄's avatar
曹云霄 committed
195
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
196
        }
197
    }withFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
198
        [XBLoadingView hideHUDViewWithDefault];
199 200 201
    }];
}

202 203
#pragma mark -修改密码
- (IBAction)ChangPasswordButtonClick:(UIButton *)sender {
204

205 206 207
    if ([self.delegate respondsToSelector:@selector(changPasswordButtonClick)]) {
        [self.delegate changPasswordButtonClick];
    }
曹云霄's avatar
曹云霄 committed
208 209
}

曹云霄's avatar
曹云霄 committed
210
#pragma mark -注销《切换用户》
211
- (IBAction)cancellationButtonClick:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
212
    
曹云霄's avatar
曹云霄 committed
213
    WS(weakSelf);
214 215 216
    ShowDefaultAlertView(self, @"提示", @"切换用户将清空用户数据,是否继续?", UIAlertControllerStyleAlert, ^{
        //清除极光别名
        [JPUSHService setTags:nil alias:@"" callbackSelector:nil object:nil];
勾芒's avatar
勾芒 committed
217
        
曹云霄's avatar
曹云霄 committed
218
        [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
219
        [HTTP networkWithDictionaryRequestWithURL:SERVERREQUESTURL(CANCELLOGIN)  withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
220
            
曹云霄's avatar
曹云霄 committed
221
            [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
222
            if (RESULT(returnValue)) {
勾芒's avatar
勾芒 committed
223
                
勾芒's avatar
勾芒 committed
224
                [Shoppersmanager manager].currentCustomer = NO;
勾芒's avatar
勾芒 committed
225
                [Customermanager manager].model = nil;
226 227
                LoginViewController *loginVC = [[[weakSelf class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"Login"];
                [[weakSelf class] deleteAccountAndPassWord];
勾芒's avatar
勾芒 committed
228
                [weakSelf restoreRootViewController:loginVC];
229
                
勾芒's avatar
勾芒 committed
230 231
            }else
            {
曹云霄's avatar
曹云霄 committed
232
                ShowAlertView(self, nil, @"切换用户失败", nil, @"我知道了", UIAlertControllerStyleActionSheet, nil, nil);
勾芒's avatar
勾芒 committed
233
            }
234
        } withFailureBlock:^(id error) {
曹云霄's avatar
曹云霄 committed
235
            [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
236
        }];
237 238
        
    }, nil);
勾芒's avatar
勾芒 committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}


#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
254
    
勾芒's avatar
勾芒 committed
255 256 257 258 259
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionFlipFromBottom
                    animations:animation
                    completion:nil];
260 261 262 263 264
}


#pragma mark -切换账户
- (IBAction)switchUserButtonClick:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
265
    
266
    if ([[self class] isBlankString:self.userName.text]) {
曹云霄's avatar
曹云霄 committed
267
        
268
        [XBLoadingView showHUDViewWithText:@"用户名不能为空"];return;
曹云霄's avatar
曹云霄 committed
269
    }
270
    if ([[self class] isBlankString:self.passWord.text]) {
曹云霄's avatar
曹云霄 committed
271
        
272
        [XBLoadingView showHUDViewWithText:@"密码不能为空"];return;
曹云霄's avatar
曹云霄 committed
273
    }
曹云霄's avatar
曹云霄 committed
274
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
275 276 277
    LoginInfo *login = [[LoginInfo alloc]init];
    login.username = self.userName.text;
    login.password = self.passWord.text;
曹云霄's avatar
曹云霄 committed
278
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
279
    [HTTP networkRequestWithURL:SERVERREQUESTURL(LOGIN)  withRequestType:ZERO withParameter:login withReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
280
        
曹云霄's avatar
曹云霄 committed
281
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
282
        if (RESULT(returnValue)) {
曹云霄's avatar
曹云霄 committed
283
            
284
            [XBLoadingView showHUDViewWithSuccessText:@"登陆成功" completeBlock:nil];
曹云霄's avatar
曹云霄 committed
285
            LoginResult *result = [[LoginResult alloc] initWithDictionary:returnValue[@"data"] error:nil];
曹云霄's avatar
曹云霄 committed
286 287
            [Shoppersmanager manager].userNameString = weakSelf.userName.text;
            [Shoppersmanager manager].passWordString = weakSelf.passWord.text;
288
            [Shoppersmanager manager].currentCustomer = NO;
勾芒's avatar
勾芒 committed
289
            [Shoppersmanager manager].Shoppers = result;
290 291 292
            [Notification postNotificationName:CHANGESHOPPERSNAME object:result.employee.realName];
            [Notification postNotificationName:@"CHANGECUSTOMERNAME" object:nil];
            [Notification postNotificationName:@"EMPTYCUSTOMERNAME" object:nil];//清空当前客户数据
勾芒's avatar
勾芒 committed
293
            //保存用户名密码
294
            [[weakSelf class] saveAccountAndPassWord:weakSelf.userName.text AndPassword:weakSelf.passWord.text];
曹云霄's avatar
曹云霄 committed
295
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
勾芒's avatar
勾芒 committed
296
            
曹云霄's avatar
曹云霄 committed
297 298
        }else
        {
曹云霄's avatar
曹云霄 committed
299
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
300
            [weakSelf recoveryUsernamePasswrod];
曹云霄's avatar
曹云霄 committed
301
        }
302
    } withFailureBlock:^(id error) {
曹云霄's avatar
曹云霄 committed
303
        
曹云霄's avatar
曹云霄 committed
304
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
305
        [weakSelf recoveryUsernamePasswrod];
曹云霄's avatar
曹云霄 committed
306 307 308 309 310 311 312 313 314 315
        
    }];
}


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


曹云霄's avatar
曹云霄 committed
319 320 321 322 323 324
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end