142 lines
3.6 KiB
Objective-C
142 lines
3.6 KiB
Objective-C
//
|
|
// GiGaUserManager.m
|
|
// GIGA
|
|
//
|
|
// Created by lianxiang on 2018/8/15.
|
|
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
|
//
|
|
|
|
#import "GiGaUserManager.h"
|
|
#import "YTKKeyValueStore.h"
|
|
#import "GiGaUserDefault.h"
|
|
#import "GiGaBaseAPiRequest.h"
|
|
|
|
NSString *const GIGA_USER_TABLE = @"user_table";
|
|
const static NSInteger GIGADB_USER_VER = 0;//当前数据库版本号
|
|
|
|
@interface GiGaUserManager()
|
|
|
|
@property (nonatomic,strong) YTKKeyValueStore *store;
|
|
@property (nonatomic,strong) NSMutableArray *userArr;
|
|
@property (nonatomic,copy) NSString *userTabId;
|
|
|
|
@end
|
|
|
|
@implementation GiGaUserManager
|
|
|
|
+ (instancetype)shareUser{
|
|
static GiGaUserManager *userManager = nil;
|
|
static dispatch_once_t once;
|
|
dispatch_once(&once, ^{
|
|
userManager = [[self alloc] init];
|
|
|
|
});
|
|
|
|
return userManager;
|
|
}
|
|
|
|
-(instancetype)init{
|
|
|
|
self = [super init];
|
|
if (self) {
|
|
// 打开名为GiGa.db的数据库,如果该文件不存在,则创新一个新的。
|
|
self.store = [[YTKKeyValueStore alloc] initDBWithName:@"GiGa.db"];
|
|
[self creatTableByName:GIGA_USER_TABLE];
|
|
_userArr = [NSMutableArray new];
|
|
[self detectionUserUpgrade];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
//登录成功后请求用户数据
|
|
-(void)synsisUserInfo:(userDataresponseBlock)block userErrorMsgBlock:(userErrorMsgBlock)errorCodeMsg{
|
|
|
|
GiGaBaseAPiRequest *requst = [GiGaBaseAPiRequest initWithRequestPath:kApiUserInfo method:RequestPostMethod parms:nil];
|
|
weakify(self);
|
|
[requst requstDataWithResult:^(GiGaAPIResult *result) {
|
|
|
|
if (result.success) {
|
|
//更新用户信息
|
|
NSDictionary *userDic = result.dic[@"userInfo"];
|
|
GiGaUser *user = [[GiGaUser alloc] initWithDictionary:userDic error:nil];
|
|
self.user = user;
|
|
[weakSelf saveUser:user];
|
|
if (block) {
|
|
block(user);
|
|
}
|
|
NSLog(@"user dic ***\n%@",result.dic);
|
|
}else{
|
|
if (errorCodeMsg) {
|
|
errorCodeMsg(requst.resultDict);
|
|
}
|
|
}
|
|
}];
|
|
|
|
}
|
|
|
|
//建表
|
|
-(void)creatTableByName:(NSString *)tableName{
|
|
[_store createTableWithName:tableName];
|
|
}
|
|
|
|
- (void)saveUser:(GiGaUser *)user{
|
|
|
|
self.user = user;
|
|
NSString *userTableId = [NSString stringWithFormat:@"%@",user.userId];
|
|
self.userTabId = userTableId;
|
|
|
|
//暂定只保留一个用户
|
|
NSDictionary *userDic = [user toDictionary];
|
|
[_userArr removeAllObjects];
|
|
[_userArr addObject:userDic];
|
|
|
|
[_store putObject:_userArr withId:userTableId intoTable:GIGA_USER_TABLE];
|
|
[GiGaUserDefault saveUserId:userTableId];
|
|
|
|
}
|
|
|
|
//已登录
|
|
-(GiGaUser *)getCurrentUser{
|
|
|
|
NSString *useriD = [GiGaUserDefault getCurentUserId];
|
|
NSArray *usrA= [_store getObjectById:useriD fromTable:GIGA_USER_TABLE];
|
|
if (usrA && usrA.count > 0) {
|
|
//self.user = usrA[0];
|
|
NSDictionary *userdic = usrA[0];
|
|
GiGaUser *user = [[GiGaUser alloc] initWithDictionary:userdic error:nil];;
|
|
self.user = user;
|
|
}
|
|
return self.user;
|
|
}
|
|
|
|
//
|
|
|
|
-(void)loginOut{
|
|
self.user = nil;
|
|
[GiGaUserDefault userLogOut];
|
|
|
|
|
|
}
|
|
|
|
- (void)detectionUserUpgrade{
|
|
|
|
NSInteger oldVerion = [GiGaUserDefault getOldVersion];
|
|
if (GIGADB_USER_VER <= oldVerion) {
|
|
return;
|
|
}
|
|
[self upgradUserDBToNew];
|
|
[GiGaUserDefault saveDBVersion:GIGADB_USER_VER];
|
|
}
|
|
|
|
-(void)upgradUserDBToNew{
|
|
|
|
if (_userArr.count <= 0) {
|
|
GILog(@"there is no user yet");
|
|
return;
|
|
}
|
|
[_store putObject:_userArr withId:self.userTabId intoTable:GIGA_USER_TABLE];
|
|
|
|
}
|
|
|
|
@end
|