KMLightMemberManager.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // KMLightMemberManager.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/22.
  6. //
  7. import Cocoa
  8. let USER_LOGINSTATE_NOTIFICATION = "kUserLoginStateNotification" //用户登录状态改变通知
  9. let USER_INFO_CHANGE_NOTIFICATION = "kUserInfoChangeNotification" //用户信息改变通知
  10. let ADVANCED_NOTIFICATION = "kAdvancedContentViewShowNotification" //用户高级引导显示
  11. enum KMLightMemberUserState: String, CaseIterable {
  12. case unknown = "unknown"
  13. case login = "login" //登录
  14. case logOut = "logOut" //登出
  15. case cancellation = "cancellation" //注销
  16. }
  17. @objcMembers class KMLightMemberManager: NSObject {
  18. static let manager = KMLightMemberManager()
  19. var config: KMLightMemberConfig = KMLightMemberConfig()
  20. var info: KMLightMemberUserInfo = KMLightMemberUserInfo()
  21. var token: KMLightMemberToken = KMLightMemberToken()
  22. var userState: KMLightMemberUserState = .unknown //用户状态
  23. var subscriptionState: KMSubscriptionState { //订阅状态
  24. get {
  25. return KMSubscriptionManager.manager.state
  26. }
  27. }
  28. //定时器
  29. // var timer: Timer = Timer()
  30. /**
  31. 是否显示高级功能视图
  32. */
  33. var canShowAdvancedView: Bool = true {
  34. didSet {
  35. NotificationCenter.default.post(name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil)
  36. }
  37. }
  38. // {
  39. // get {
  40. // if UserDefaults.standard.object(forKey: "kAdvancedContentViewShow") != nil {
  41. // return false
  42. // } else {
  43. // return true
  44. // }
  45. // }
  46. //
  47. // set {
  48. // UserDefaults.standard.set("kAdvancedContentViewShow", forKey: "kAdvancedContentViewShow")
  49. // UserDefaults.standard.synchronize()
  50. // NotificationCenter.default.post(name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil)
  51. // }
  52. // }
  53. override init() {
  54. super.init()
  55. self.reloadUserInfo()
  56. }
  57. func logOut() {
  58. DispatchQueue.main.async {
  59. self.userState = .logOut
  60. KMLightMemberCache.cache.clean()
  61. self.reloadUserInfo()
  62. NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
  63. }
  64. }
  65. func logIn() {
  66. self.userState = .login
  67. self.reloadUserInfo()
  68. NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
  69. }
  70. func cancellation() {
  71. self.userState = .cancellation
  72. self.reloadUserInfo()
  73. NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
  74. }
  75. }
  76. //MARK: - 数据
  77. extension KMLightMemberManager {
  78. /**
  79. 刷新本地用户数据
  80. */
  81. func reloadUserInfo() {
  82. //获取用户信息
  83. let userInfo = KMLightMemberCache.cache.readData(type: .info)
  84. if userInfo.count != 0 {
  85. KMLightMemberUserInfo.parseData(data: userInfo, needSave: false) { [unowned self] data in
  86. self.info = data
  87. }
  88. } else {
  89. self.info = KMLightMemberUserInfo()
  90. }
  91. let token = KMLightMemberCache.cache.readData(type: .token)
  92. if token.count != 0 {
  93. KMLightMemberToken.parseData(data: token) { [unowned self] data in
  94. self.token = data
  95. }
  96. } else {
  97. self.token = KMLightMemberToken()
  98. }
  99. NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_INFO_CHANGE_NOTIFICATION), object: nil, userInfo: nil)
  100. }
  101. /**
  102. 刷新本地用户数据
  103. */
  104. func updateNetworkingUserInfo(completion: @escaping (_ isDidLogout: Bool) -> Void) {
  105. KMRequestServerManager.manager.getUserInfo { [unowned self] success, data, error, isLocal in
  106. if success {
  107. self.reloadUserInfo()
  108. completion(false)
  109. } else {
  110. if error?.code == 304 {
  111. KMLightMemberManager.manager.logOut()
  112. completion(true)
  113. }
  114. completion(false)
  115. }
  116. }
  117. }
  118. /**
  119. token是否生效
  120. */
  121. func isTokenValid() async -> Bool {
  122. var result = false
  123. if self.isLogin() {
  124. do {
  125. let (success, error) = try await KMRequestServerManager.manager.getUserInfo2()
  126. // Thread.sleep(forTimeInterval: 20.0)
  127. if success {
  128. result = true
  129. } else {
  130. if error.code == 304 {
  131. KMLightMemberManager.manager.logOut()
  132. result = false
  133. } else {
  134. result = true
  135. }
  136. }
  137. return result
  138. } catch {
  139. return result
  140. }
  141. }
  142. return result
  143. }
  144. }
  145. //MARK: - 快捷方法
  146. extension KMLightMemberManager {
  147. //是否已经登录
  148. func isLogin() -> Bool {
  149. var result: Bool = false
  150. if token.access_token != "" {
  151. result = true
  152. }
  153. return result
  154. }
  155. //是否已经注销
  156. func isLogOff() -> Bool {
  157. var result: Bool = false
  158. return result
  159. }
  160. //是否能使用高级功能
  161. func canUseAdvanced(needNetworking: Bool = false) async -> Bool {
  162. var result: Bool = false
  163. var tokenValid = true
  164. if needNetworking {
  165. tokenValid = await self.isTokenValid()
  166. }
  167. print(tokenValid)
  168. if tokenValid && info.isInFreeUseTime {
  169. result = true
  170. }
  171. return result
  172. }
  173. }