| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- //
- // TCUserInfoModel+TCUserInfoModel.m
- // TCLVBIMDemo
- //
- // Created by jemilyzhou on 16/8/2.
- // Copyright © 2016年 tencent. All rights reserved.
- //
- #import "TCUserInfoModel.h"
- //#import "TCLoginModel.h"
- #ifndef APP_EXT
- #import <UIImageView+WebCache.h>
- #endif
- #import "TCConstants.h"
- #import "TCUtil.h"
- #define kUserInfoKey @"kUserInfoKey"
- static TCUserInfoModel *_shareInstance = nil;
- @implementation TCUserInfoData
- - (instancetype)init
- {
- if (self = [super init])
- {
-
- }
- return self;
- }
- @end
- @interface TCUserInfoModel()
- @property(nonatomic,strong) TCUserInfoData *userInfo;
- //@property(nonatomic,strong) TCLoginParam *loginParam;
- @end
- @implementation TCUserInfoModel
- - (instancetype)init
- {
- self = [super init];
- if (self)
- {
- // _loginParam = [TCLoginParam shareInstance];
- _userInfo = [self loadUserProfile];
- }
-
- return self;
- }
- - (void)dealloc
- {
- }
- + (instancetype)sharedInstance;
- {
- static dispatch_once_t predicate;
-
- dispatch_once(&predicate, ^{
- _shareInstance = [[TCUserInfoModel alloc] init];
- });
- return _shareInstance;
- }
- - (void)setBucket:(NSString *)bucket secretId:(NSString*)secretId appid:(long long)appid region:(NSString *)region accountType:(NSString *)accountType
- {
- _userInfo.bucket = bucket;
- _userInfo.secretId = secretId;
- _userInfo.appid = [NSString stringWithFormat:@"%lld",appid];
- _userInfo.region = region;
- _userInfo.accountType = accountType;
- }
- #pragma mark 从服务器上拉取信息
- #ifndef APP_EXT
- /**
- * 通过id信息从服务器上拉取用户信息
- */
- -(void)fetchUserInfo
- {
- DebugLog(@"开始通过用户id拉取用户资料信息");
- __weak typeof(self) weakSelf = self;
- // NSDictionary* params = @{@"userid": _loginParam.identifier, @"timestamp":@([[NSDate date] timeIntervalSince1970] * 1000), @"expires":@(_loginParam.expires)};
- // [TCUtil asyncSendHttpRequest:@"get_user_info" token:_loginParam.token params:params handler:^(int resultCode, NSString *message, NSDictionary *resultDict) {
- // if (resultCode == 200) {
- // DebugLog(@"从服务器上拉取用户资料信息成功%@", resultDict);
- // weakSelf.userInfo.identifier = weakSelf.loginParam.identifier;
- // weakSelf.userInfo.nickName = resultDict[@"nickname"];
- // weakSelf.userInfo.gender = [((NSNumber*)resultDict[@"sex"]) intValue];
- // weakSelf.userInfo.faceURL = resultDict[@"avatar"];
- // weakSelf.userInfo.coverURL = resultDict[@"frontcover"];
- //
- // [weakSelf saveToLocal];
- // }
- // else {
- // DebugLog(@"从服务器上拉取用户资料信息失败 errCode = %d, errMsg = %@", resultCode, message);
- // }
- // }];
- }
- - (void)uploadUserInfo:(TCUserInfoSaveHandle)handle
- {
- NSString* nickname = _userInfo.nickName.length < 1 ? _userInfo.identifier : _userInfo.nickName;
- NSString* avatar = _userInfo.faceURL == nil ? @"" : _userInfo.faceURL;
- NSString* frontcover = _userInfo.coverURL == nil ? @"" : _userInfo.coverURL;
-
- // NSDictionary* params = @{@"userid": _loginParam.identifier, @"timestamp":@([[NSDate date] timeIntervalSince1970] * 1000), @"expires":@(_loginParam.expires), @"nickname": nickname, @"avatar": avatar, @"frontcover": frontcover, @"sex": @(_userInfo.gender)};
- // [TCUtil asyncSendHttpRequest:@"upload_user_info" token:_loginParam.token params:params handler:^(int resultCode, NSString *message, NSDictionary *resultDict) {
- //
- // handle(resultCode, message);
- // }];
- }
- #pragma mark 上传更改后的用户信息
- /**
- * saveUserNickName 用户保存用户修改后的昵称到服务器上
- *
- * @param nickName 用户的昵称
- * @param handle 保存成功或者失败后的回调block
- */
- -(void)saveUserNickName:(NSString*)nickName handler:(TCUserInfoSaveHandle)handle
- {
- DebugLog(@"开始保存用户昵称信息到服务器 \n");
- __weak typeof(self) weakSelf = self;
-
- NSString* oldNickname = _userInfo.nickName;
- _userInfo.nickName = nickName;
- [self uploadUserInfo:^(int errCode, NSString *strMsg) {
- if (errCode != ERROR_SUCESS) {
- weakSelf.userInfo.nickName = oldNickname;
- }
- handle(errCode, strMsg);
- }];
- }
- /**
- * saveUserFace 当吧用户修改后的头像图片上传到服务器后会返回头像url地址信息
- 此时再把头像的url地址上传到服务器上
- *
- * @param faceURL 用户头像的url地址
- * @param handle 保存成功或者失败后的回调block
- */
- -(void)saveUserFace:(NSString*)faceURL handler:(TCUserInfoSaveHandle)handle
- {
- DebugLog(@"开始保存用户头像Url地址到服务器 \n");
- __weak typeof(self) weakSelf = self;
-
- NSString* oldFaceURL = _userInfo.faceURL;
- _userInfo.faceURL = faceURL;
- [self uploadUserInfo:^(int errCode, NSString *strMsg) {
- if (errCode != ERROR_SUCESS) {
- weakSelf.userInfo.faceURL = oldFaceURL;
- }
- handle(errCode, strMsg);
- }];
- }
- /**
- * saveUserGender 用于保存用户性别到服务器
- *
- * @param gender 用户性别信息,根据男 or 女取不同结构体,可查询TIMGender结构体定义
- * @param handle 保存成功或者失败后的回调block
- */
- -(void)saveUserGender:(int)gender handler:(TCUserInfoSaveHandle)handle
- {
- DebugLog(@"开始保存用户性别信息到服务器 \n");
-
- __weak typeof(self) weakSelf = self;
-
- int oldGender = _userInfo.gender;
- _userInfo.gender = gender;
- [self uploadUserInfo:^(int errCode, NSString *strMsg) {
- if (errCode != ERROR_SUCESS) {
- weakSelf.userInfo.gender = oldGender;
- }
- handle(errCode, strMsg);
- }];
- }
- #endif
- #pragma mark 内存中查询或者修改数据
- /**
- * 用于获取用户资料信息借口
- *
- * @return 用户资料信息结构体指针
- */
- - (TCUserInfoData*)getUserProfile
- {
- return _userInfo;
- }
- - (TCUserInfoData*)loadUserProfile {
- TCUserInfoData *info = [[TCUserInfoData alloc] init];
-
- // 从文件中读取
- NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:APP_GROUP];
- if (defaults == nil) {
- defaults = [NSUserDefaults standardUserDefaults];
- }
-
- // NSString *useridKey = [NSString stringWithFormat:@"%@_UserInfo", _loginParam.identifier];
- // if (useridKey) {
- // NSString *strUserInfo = [defaults objectForKey:useridKey];
- // NSDictionary *dic = [TCUtil jsonData2Dictionary: strUserInfo];
- // if (dic) {
- // info.identifier = [_loginParam.identifier copy];
- // info.nickName = [dic objectForKey:@"nickName"];
- // info.faceURL = [dic objectForKey:@"faceURL"];
- // info.coverURL = [dic objectForKey:@"coverURL"];
- // info.gender = [[dic objectForKey:@"gender"] intValue];
- // }
- // }
- return info;
- }
- - (void)saveToLocal {
- // // 保存昵称,头像,封页, 性别 到本地,方便其他进程读取
- // NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
- // [dic setObject:_userInfo.nickName forKey:@"nickName"];
- // [dic setObject:_userInfo.faceURL forKey:@"faceURL"];
- // [dic setObject:_userInfo.coverURL forKey:@"coverURL"];
- // [dic setObject:@(_userInfo.gender) forKey:@"gender"];
- //
- // NSData *data = [TCUtil dictionary2JsonData: dic];
- // NSString *strUserInfo = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- // NSString *useridKey = [NSString stringWithFormat:@"%@_UserInfo", _loginParam.identifier];
- //
- // NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:APP_GROUP];
- // if (defaults == nil) {
- // defaults = [NSUserDefaults standardUserDefaults];
- // }
- // [defaults setObject:useridKey forKey:kUserInfoKey];
- //
- // [defaults setObject:strUserInfo forKey:useridKey];
- // [defaults synchronize];
- }
- @end
|