KMLightMemberManager.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. @objc enum KMLightMemberUserState: Int, CaseIterable {
  12. case unknown = 0//,"unknown"
  13. case login = 1//"login" //登录
  14. case logOut = 2//"logOut" //登出
  15. case cancellation = 3//"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 purchaseState: KMPurchaseManagerState { //订阅状态
  24. get {
  25. return KMPurchaseManager.manager.state
  26. }
  27. }
  28. /**
  29. 是否显示高级功能视图
  30. */
  31. var canShowAdvancedView: Bool = true {
  32. didSet {
  33. NotificationCenter.default.post(name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil)
  34. }
  35. }
  36. override init() {
  37. super.init()
  38. self.reloadUserInfo()
  39. }
  40. func logOut() {
  41. DispatchQueue.main.async {
  42. self.userState = .logOut
  43. KMLightMemberCache.cache.clean()
  44. self.reloadUserInfo()
  45. NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
  46. }
  47. }
  48. func logIn() {
  49. self.userState = .login
  50. self.reloadUserInfo()
  51. NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
  52. }
  53. func cancellation() {
  54. self.userState = .cancellation
  55. self.reloadUserInfo()
  56. NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
  57. }
  58. }
  59. //MARK: - 数据
  60. extension KMLightMemberManager {
  61. /**
  62. 刷新本地用户数据
  63. */
  64. func reloadUserInfo() {
  65. //获取用户信息
  66. let userInfo = KMLightMemberCache.cache.readData(type: .info)
  67. if userInfo.count != 0 {
  68. KMLightMemberUserInfo.parseData(data: userInfo, needSave: false) { [unowned self] data in
  69. self.info = data
  70. }
  71. } else {
  72. self.info = KMLightMemberUserInfo()
  73. }
  74. let token = KMLightMemberCache.cache.readData(type: .token)
  75. if token.count != 0 {
  76. KMLightMemberToken.parseData(data: token) { [unowned self] data in
  77. self.token = data
  78. }
  79. } else {
  80. self.token = KMLightMemberToken()
  81. }
  82. NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_INFO_CHANGE_NOTIFICATION), object: nil, userInfo: nil)
  83. }
  84. /**
  85. 刷新本地用户数据
  86. */
  87. func updateNetworkingUserInfo(completion: @escaping (_ isDidLogout: Bool) -> Void) {
  88. KMRequestServerManager.manager.getUserInfo { [unowned self] success, data, error, isLocal in
  89. if success {
  90. self.reloadUserInfo()
  91. completion(false)
  92. } else {
  93. if error?.code == 304 {
  94. KMLightMemberManager.manager.logOut()
  95. completion(true)
  96. }
  97. completion(false)
  98. }
  99. }
  100. }
  101. /**
  102. token是否生效
  103. */
  104. func isTokenValid() async -> Bool {
  105. var result = false
  106. if self.isLogin() {
  107. do {
  108. let (success, error) = try await KMRequestServerManager.manager.getUserInfo2()
  109. // Thread.sleep(forTimeInterval: 20.0)
  110. if success {
  111. result = true
  112. } else {
  113. if error.code == 304 {
  114. KMLightMemberManager.manager.logOut()
  115. result = false
  116. } else {
  117. result = true
  118. }
  119. }
  120. return result
  121. } catch {
  122. return result
  123. }
  124. }
  125. return result
  126. }
  127. }
  128. //MARK: - 快捷方法
  129. extension KMLightMemberManager {
  130. //是否已经登录
  131. func isLogin() -> Bool {
  132. var result: Bool = false
  133. if token.access_token != "" {
  134. result = true
  135. }
  136. return result
  137. }
  138. //是否已经注销
  139. func isLogOff() -> Bool {
  140. var result: Bool = false
  141. return result
  142. }
  143. //是否能使用高级功能
  144. func canUseAdvanced(needNetworking: Bool = false) async -> Bool {
  145. var result: Bool = false
  146. var tokenValid = true
  147. if needNetworking {
  148. tokenValid = await self.isTokenValid()
  149. }
  150. KMPrint(tokenValid)
  151. if tokenValid && info.isInFreeUseTime {
  152. result = true
  153. }
  154. return result
  155. }
  156. //是否能使用付费功能
  157. func canPayFunction(needNetworking: Bool = false) async -> Bool {
  158. var result: Bool = false
  159. //先确认可以使用高级功能,再确认是否已经订阅
  160. if await self.canUseAdvanced(needNetworking: needNetworking) && (self.purchaseState == .subscription || self.purchaseState == .trial) {
  161. result = true
  162. }
  163. return result
  164. }
  165. //是否显示订单信息
  166. func existOrderInfo() -> Bool {
  167. return self.info.existOrderInfo ?? false
  168. }
  169. }
  170. //MARK: 登录注册界面弹出逻辑
  171. /*
  172. 弹出策略:
  173. 每日首次进入阅读页,自动弹出【注册弹窗】;关闭弹窗后,当日不再弹出,次日零点重置
  174. 注意:首次进入阅读页之前,通过其他方式弹出过【注册弹窗】,则进入阅读页不再主动弹出
  175. **/
  176. extension KMLightMemberManager {
  177. //检查是否需要弹起注册登录框
  178. func checkPopupRegister() -> Bool {
  179. var needPop = false
  180. self.resetRegisterPopupStatus()
  181. var info: [String : Any] = [:]
  182. if (UserDefaults.standard.value(forKey: "CheckPopupRegistration") == nil) {
  183. } else {
  184. info = (UserDefaults.standard.value(forKey: "CheckPopupRegistration") as! [String : Any])
  185. }
  186. let isPopupShownToday = info["isPopupShownToday"] as? Bool ?? false
  187. var lastPopupDate = info["lastPopupDate"] as? Date
  188. // 检查是否已经弹出过注册弹窗
  189. if (!isPopupShownToday) {
  190. // 获取当前日期
  191. let currentDate = Date()
  192. // 检查上次弹窗显示的日期是否为空,或者不是当天
  193. if lastPopupDate == nil || (lastPopupDate != nil &&
  194. !Calendar.current.isDate(lastPopupDate!, inSameDayAs: currentDate)) {
  195. // 更新上次弹窗显示的日期为当前日期
  196. lastPopupDate = currentDate
  197. info.updateValue(lastPopupDate!, forKey: "lastPopupDate")
  198. info.updateValue(true, forKey: "isPopupShownToday")
  199. UserDefaults.standard.set(info, forKey: "CheckPopupRegistration")
  200. UserDefaults.standard.synchronize()
  201. needPop = true
  202. }
  203. }
  204. return needPop
  205. }
  206. func updatePopupShownToday(isPop: Bool) {
  207. var info: [String : Any] = [:]
  208. if (UserDefaults.standard.value(forKey: "CheckPopupRegistration") == nil) {
  209. } else {
  210. info = (UserDefaults.standard.value(forKey: "CheckPopupRegistration") as! [String : Any])
  211. }
  212. info.updateValue(isPop, forKey: "isPopupShownToday")
  213. UserDefaults.standard.set(info, forKey: "CheckPopupRegistration")
  214. UserDefaults.standard.synchronize()
  215. }
  216. func resetRegisterPopupStatus() {
  217. var info: [String : Any] = [:]
  218. if (UserDefaults.standard.value(forKey: "CheckPopupRegistration") == nil) {
  219. } else {
  220. info = (UserDefaults.standard.value(forKey: "CheckPopupRegistration") as! [String : Any])
  221. }
  222. var isPopupShownToday = info["isPopupShownToday"]
  223. let lastPopupDate = info["lastPopupDate"] as? Date
  224. // 获取当前日期和时间
  225. let currentDate = Date()
  226. // 获取当前日历
  227. let calendar = Calendar.current
  228. // 获取当天的午夜时间
  229. let midnight = calendar.startOfDay(for: currentDate)
  230. // 检查上次弹窗显示的日期是否早于当天的午夜时间
  231. if let lastPopupDate = lastPopupDate, lastPopupDate < midnight {
  232. // 重置弹窗状态
  233. isPopupShownToday = false
  234. info.updateValue(isPopupShownToday!, forKey: "isPopupShownToday")
  235. // info.updateValue(NSDate(), forKey: "lastPopupDate")
  236. UserDefaults.standard.set(info, forKey: "CheckPopupRegistration")
  237. UserDefaults.standard.synchronize()
  238. }
  239. }
  240. }