IMALoginParam.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // IMALoginParam.m
  3. // TIMChat
  4. //
  5. // Created by AlexiChen on 16/2/26.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "IMALoginParam.h"
  9. @implementation TIMLoginParam (PlatformConfig)
  10. - (IMAPlatformConfig *)config
  11. {
  12. return nil;
  13. }
  14. - (void)saveToLocal
  15. {
  16. // do nothing
  17. }
  18. @end
  19. @implementation IMALoginParam
  20. #define kDictUserKey @"kDictUserKey"
  21. #define kDaysInSeconds(x) (x * 24 * 60 * 60) // 过期时间
  22. - (instancetype)init
  23. {
  24. if (self = [super init])
  25. {
  26. NSString *sdkAppIdStr = TXYSdkAppId;
  27. self.appidAt3rd = sdkAppIdStr;
  28. // self.sdkAppId = [sdkAppIdStr intValue];
  29. // self.accountType = TXYSdkAccountType;
  30. self.config = [[IMAPlatformConfig alloc] init];
  31. }
  32. return self;
  33. }
  34. + (instancetype)loadFromLocal
  35. {
  36. NSString *userloginKey = [[NSUserDefaults standardUserDefaults] objectForKey:kIMALoginParamUserKey];
  37. if (userloginKey)
  38. {
  39. // 说明本地有存储
  40. IMALoginParam *param = [IMALoginParam loadInfo:[IMALoginParam class] withKey:userloginKey];
  41. return param;
  42. }
  43. else
  44. {
  45. return [[IMALoginParam alloc] init];
  46. }
  47. }
  48. - (void)saveToLocal
  49. {
  50. if (self.tokenTime == 0)
  51. {
  52. self.tokenTime = [[NSDate date] timeIntervalSince1970];
  53. }
  54. if ([self isVailed])
  55. {
  56. NSString *useridKey = [NSString stringWithFormat:@"%@_LoginParam", self.identifier];
  57. [[NSUserDefaults standardUserDefaults] setObject:useridKey forKey:kIMALoginParamUserKey];
  58. [IMALoginParam saveInfo:self withKey:useridKey];
  59. }
  60. }
  61. - (BOOL)isExpired
  62. {
  63. time_t curTime = [[NSDate date] timeIntervalSince1970];
  64. BOOL expired = curTime - self.tokenTime > kDaysInSeconds(30);
  65. return expired;
  66. }
  67. - (BOOL)isVailed
  68. {
  69. return ![NSString isEmpty:self.identifier] && ![NSString isEmpty:self.userSig] && ![self isExpired];
  70. }
  71. @end