KWMUserModel.m 2.8 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9
//
//  KWMUserModel.m
//  iCemarose
//
//  Created by 陈荣科 on 16/9/12.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMUserModel.h"
lee committed
10
#import <SAMKeychain/SAMKeychain.h>
houweibin committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67


@implementation KWMUserModel

static KWMUserModel *_shareUser;

+ (instancetype)shareUser{
    
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        _shareUser = [[self alloc] init];
        
        NSDictionary * dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"customer"];
        //    BUYCustomer *customer = [[BUYModelManager alloc] insertCustomerWithJSONDictionary:dict];
        if (dict != nil) {
            _shareUser.status = 1;
            NSString *phoneNum = [[NSUserDefaults standardUserDefaults] objectForKey:@"PhoneNum"];
            if (phoneNum != nil) {
                _shareUser.name = phoneNum;
            }else{
                _shareUser.name = [dict objectForKey:@"email"];
            }
            if ([dict objectForKey:@"email"] != nil) {
                _shareUser.email = [dict objectForKey:@"email"];
            }
        }
        
    });
    return  _shareUser;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        _shareUser = [super allocWithZone:zone];
    });
    return _shareUser;
}

+ (instancetype)copyWithZone:(struct _NSZone *)zone{
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        _shareUser = [super copyWithZone:zone];
    });
    return _shareUser;
}

- (void)login{
    self.status = 1;
    [self saveUser];
}

- (void)logout{
    self.status = 0;
    [self deleteUser];
}

lee committed
68 69 70 71 72 73 74 75 76 77 78 79
+ (void) saveAccountByCredentials:(BUYAccountCredentials *) credentials {
    NSString *account = nil,*password = nil;
    for (BUYAccountCredentialItem *item in credentials.items) {
        if ([item.key isEqualToString:BUYAccountEmailKey]) {
            account = item.value.lowercaseString;
        } else if ([item.key isEqualToString:BUYAccountPasswordKey]) {
            password = item.value;
        }
    }
    [SAMKeychain setPassword:password forService:@"cemarose.account" account:account];
}

houweibin committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
- (void)deleteUser{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"KeyUser"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"customer"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"PhoneNum"];
    NSString * path = NSHomeDirectory();
    NSLog(@"path = %@",path);
}

- (void)saveUser{
    NSDictionary *userDict = self.toDictionary;
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    [defaults setObject:userDict forKey:@"KeyUser"];
    [defaults synchronize];
    NSString * path = NSHomeDirectory();
    NSLog(@"path = %@",path);
}
@end