TCUserInfoModel.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // TCUserInfoModel+TCUserInfoModel.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by jemilyzhou on 16/8/2.
  6. // Copyright © 2016年 tencent. All rights reserved.
  7. //
  8. #import "TCUserInfoModel.h"
  9. //#import "TCLoginModel.h"
  10. #ifndef APP_EXT
  11. #import <UIImageView+WebCache.h>
  12. #endif
  13. #import "TCConstants.h"
  14. #import "TCUtil.h"
  15. #define kUserInfoKey @"kUserInfoKey"
  16. static TCUserInfoModel *_shareInstance = nil;
  17. @implementation TCUserInfoData
  18. - (instancetype)init
  19. {
  20. if (self = [super init])
  21. {
  22. }
  23. return self;
  24. }
  25. @end
  26. @interface TCUserInfoModel()
  27. @property(nonatomic,strong) TCUserInfoData *userInfo;
  28. //@property(nonatomic,strong) TCLoginParam *loginParam;
  29. @end
  30. @implementation TCUserInfoModel
  31. - (instancetype)init
  32. {
  33. self = [super init];
  34. if (self)
  35. {
  36. // _loginParam = [TCLoginParam shareInstance];
  37. _userInfo = [self loadUserProfile];
  38. }
  39. return self;
  40. }
  41. - (void)dealloc
  42. {
  43. }
  44. + (instancetype)sharedInstance;
  45. {
  46. static dispatch_once_t predicate;
  47. dispatch_once(&predicate, ^{
  48. _shareInstance = [[TCUserInfoModel alloc] init];
  49. });
  50. return _shareInstance;
  51. }
  52. - (void)setBucket:(NSString *)bucket secretId:(NSString*)secretId appid:(long long)appid region:(NSString *)region accountType:(NSString *)accountType
  53. {
  54. _userInfo.bucket = bucket;
  55. _userInfo.secretId = secretId;
  56. _userInfo.appid = [NSString stringWithFormat:@"%lld",appid];
  57. _userInfo.region = region;
  58. _userInfo.accountType = accountType;
  59. }
  60. #pragma mark 从服务器上拉取信息
  61. #ifndef APP_EXT
  62. /**
  63. * 通过id信息从服务器上拉取用户信息
  64. */
  65. -(void)fetchUserInfo
  66. {
  67. DebugLog(@"开始通过用户id拉取用户资料信息");
  68. __weak typeof(self) weakSelf = self;
  69. // NSDictionary* params = @{@"userid": _loginParam.identifier, @"timestamp":@([[NSDate date] timeIntervalSince1970] * 1000), @"expires":@(_loginParam.expires)};
  70. // [TCUtil asyncSendHttpRequest:@"get_user_info" token:_loginParam.token params:params handler:^(int resultCode, NSString *message, NSDictionary *resultDict) {
  71. // if (resultCode == 200) {
  72. // DebugLog(@"从服务器上拉取用户资料信息成功%@", resultDict);
  73. // weakSelf.userInfo.identifier = weakSelf.loginParam.identifier;
  74. // weakSelf.userInfo.nickName = resultDict[@"nickname"];
  75. // weakSelf.userInfo.gender = [((NSNumber*)resultDict[@"sex"]) intValue];
  76. // weakSelf.userInfo.faceURL = resultDict[@"avatar"];
  77. // weakSelf.userInfo.coverURL = resultDict[@"frontcover"];
  78. //
  79. // [weakSelf saveToLocal];
  80. // }
  81. // else {
  82. // DebugLog(@"从服务器上拉取用户资料信息失败 errCode = %d, errMsg = %@", resultCode, message);
  83. // }
  84. // }];
  85. }
  86. - (void)uploadUserInfo:(TCUserInfoSaveHandle)handle
  87. {
  88. NSString* nickname = _userInfo.nickName.length < 1 ? _userInfo.identifier : _userInfo.nickName;
  89. NSString* avatar = _userInfo.faceURL == nil ? @"" : _userInfo.faceURL;
  90. NSString* frontcover = _userInfo.coverURL == nil ? @"" : _userInfo.coverURL;
  91. // NSDictionary* params = @{@"userid": _loginParam.identifier, @"timestamp":@([[NSDate date] timeIntervalSince1970] * 1000), @"expires":@(_loginParam.expires), @"nickname": nickname, @"avatar": avatar, @"frontcover": frontcover, @"sex": @(_userInfo.gender)};
  92. // [TCUtil asyncSendHttpRequest:@"upload_user_info" token:_loginParam.token params:params handler:^(int resultCode, NSString *message, NSDictionary *resultDict) {
  93. //
  94. // handle(resultCode, message);
  95. // }];
  96. }
  97. #pragma mark 上传更改后的用户信息
  98. /**
  99. * saveUserNickName 用户保存用户修改后的昵称到服务器上
  100. *
  101. * @param nickName 用户的昵称
  102. * @param handle 保存成功或者失败后的回调block
  103. */
  104. -(void)saveUserNickName:(NSString*)nickName handler:(TCUserInfoSaveHandle)handle
  105. {
  106. DebugLog(@"开始保存用户昵称信息到服务器 \n");
  107. __weak typeof(self) weakSelf = self;
  108. NSString* oldNickname = _userInfo.nickName;
  109. _userInfo.nickName = nickName;
  110. [self uploadUserInfo:^(int errCode, NSString *strMsg) {
  111. if (errCode != ERROR_SUCESS) {
  112. weakSelf.userInfo.nickName = oldNickname;
  113. }
  114. handle(errCode, strMsg);
  115. }];
  116. }
  117. /**
  118. * saveUserFace 当吧用户修改后的头像图片上传到服务器后会返回头像url地址信息
  119. 此时再把头像的url地址上传到服务器上
  120. *
  121. * @param faceURL 用户头像的url地址
  122. * @param handle 保存成功或者失败后的回调block
  123. */
  124. -(void)saveUserFace:(NSString*)faceURL handler:(TCUserInfoSaveHandle)handle
  125. {
  126. DebugLog(@"开始保存用户头像Url地址到服务器 \n");
  127. __weak typeof(self) weakSelf = self;
  128. NSString* oldFaceURL = _userInfo.faceURL;
  129. _userInfo.faceURL = faceURL;
  130. [self uploadUserInfo:^(int errCode, NSString *strMsg) {
  131. if (errCode != ERROR_SUCESS) {
  132. weakSelf.userInfo.faceURL = oldFaceURL;
  133. }
  134. handle(errCode, strMsg);
  135. }];
  136. }
  137. /**
  138. * saveUserGender 用于保存用户性别到服务器
  139. *
  140. * @param gender 用户性别信息,根据男 or 女取不同结构体,可查询TIMGender结构体定义
  141. * @param handle 保存成功或者失败后的回调block
  142. */
  143. -(void)saveUserGender:(int)gender handler:(TCUserInfoSaveHandle)handle
  144. {
  145. DebugLog(@"开始保存用户性别信息到服务器 \n");
  146. __weak typeof(self) weakSelf = self;
  147. int oldGender = _userInfo.gender;
  148. _userInfo.gender = gender;
  149. [self uploadUserInfo:^(int errCode, NSString *strMsg) {
  150. if (errCode != ERROR_SUCESS) {
  151. weakSelf.userInfo.gender = oldGender;
  152. }
  153. handle(errCode, strMsg);
  154. }];
  155. }
  156. #endif
  157. #pragma mark 内存中查询或者修改数据
  158. /**
  159. * 用于获取用户资料信息借口
  160. *
  161. * @return 用户资料信息结构体指针
  162. */
  163. - (TCUserInfoData*)getUserProfile
  164. {
  165. return _userInfo;
  166. }
  167. - (TCUserInfoData*)loadUserProfile {
  168. TCUserInfoData *info = [[TCUserInfoData alloc] init];
  169. // 从文件中读取
  170. NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:APP_GROUP];
  171. if (defaults == nil) {
  172. defaults = [NSUserDefaults standardUserDefaults];
  173. }
  174. // NSString *useridKey = [NSString stringWithFormat:@"%@_UserInfo", _loginParam.identifier];
  175. // if (useridKey) {
  176. // NSString *strUserInfo = [defaults objectForKey:useridKey];
  177. // NSDictionary *dic = [TCUtil jsonData2Dictionary: strUserInfo];
  178. // if (dic) {
  179. // info.identifier = [_loginParam.identifier copy];
  180. // info.nickName = [dic objectForKey:@"nickName"];
  181. // info.faceURL = [dic objectForKey:@"faceURL"];
  182. // info.coverURL = [dic objectForKey:@"coverURL"];
  183. // info.gender = [[dic objectForKey:@"gender"] intValue];
  184. // }
  185. // }
  186. return info;
  187. }
  188. - (void)saveToLocal {
  189. // // 保存昵称,头像,封页, 性别 到本地,方便其他进程读取
  190. // NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
  191. // [dic setObject:_userInfo.nickName forKey:@"nickName"];
  192. // [dic setObject:_userInfo.faceURL forKey:@"faceURL"];
  193. // [dic setObject:_userInfo.coverURL forKey:@"coverURL"];
  194. // [dic setObject:@(_userInfo.gender) forKey:@"gender"];
  195. //
  196. // NSData *data = [TCUtil dictionary2JsonData: dic];
  197. // NSString *strUserInfo = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  198. // NSString *useridKey = [NSString stringWithFormat:@"%@_UserInfo", _loginParam.identifier];
  199. //
  200. // NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:APP_GROUP];
  201. // if (defaults == nil) {
  202. // defaults = [NSUserDefaults standardUserDefaults];
  203. // }
  204. // [defaults setObject:useridKey forKey:kUserInfoKey];
  205. //
  206. // [defaults setObject:strUserInfo forKey:useridKey];
  207. // [defaults synchronize];
  208. }
  209. @end