KMLightMemberManager.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 || (self.purchaseState == .subscription || self.purchaseState == .trial) {
  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 canPayFunction() -> Bool {
  167. var result: Bool = false
  168. //先确认可以使用高级功能,再确认是否已经订阅
  169. if (self.purchaseState == .subscription || self.purchaseState == .trial) {
  170. result = true
  171. }
  172. return result
  173. }
  174. //是否显示订单信息
  175. func existOrderInfo() -> Bool {
  176. return self.info.existOrderInfo ?? false
  177. }
  178. }
  179. //MARK: 登录注册界面弹出逻辑
  180. /*
  181. 弹出策略:
  182. 每日首次进入阅读页,自动弹出【注册弹窗】;关闭弹窗后,当日不再弹出,次日零点重置
  183. 注意:首次进入阅读页之前,通过其他方式弹出过【注册弹窗】,则进入阅读页不再主动弹出
  184. **/
  185. extension KMLightMemberManager {
  186. //检查是否需要弹起注册登录框
  187. func checkPopupRegister() -> Bool {
  188. var needPop = false
  189. self.resetRegisterPopupStatus()
  190. var info: [String : Any] = [:]
  191. if (UserDefaults.standard.value(forKey: "CheckPopupRegistration") == nil) {
  192. } else {
  193. info = (UserDefaults.standard.value(forKey: "CheckPopupRegistration") as! [String : Any])
  194. }
  195. let isPopupShownToday = info["isPopupShownToday"] as? Bool ?? false
  196. var lastPopupDate = info["lastPopupDate"] as? Date
  197. // 检查是否已经弹出过注册弹窗
  198. if (!isPopupShownToday) {
  199. // 获取当前日期
  200. let currentDate = Date()
  201. // 检查上次弹窗显示的日期是否为空,或者不是当天
  202. if lastPopupDate == nil || (lastPopupDate != nil &&
  203. !Calendar.current.isDate(lastPopupDate!, inSameDayAs: currentDate)) {
  204. // 更新上次弹窗显示的日期为当前日期
  205. lastPopupDate = currentDate
  206. info.updateValue(lastPopupDate!, forKey: "lastPopupDate")
  207. info.updateValue(true, forKey: "isPopupShownToday")
  208. UserDefaults.standard.set(info, forKey: "CheckPopupRegistration")
  209. UserDefaults.standard.synchronize()
  210. needPop = true
  211. }
  212. }
  213. return needPop
  214. }
  215. func updatePopupShownToday(isPop: Bool) {
  216. var info: [String : Any] = [:]
  217. if (UserDefaults.standard.value(forKey: "CheckPopupRegistration") == nil) {
  218. } else {
  219. info = (UserDefaults.standard.value(forKey: "CheckPopupRegistration") as! [String : Any])
  220. }
  221. info.updateValue(isPop, forKey: "isPopupShownToday")
  222. UserDefaults.standard.set(info, forKey: "CheckPopupRegistration")
  223. UserDefaults.standard.synchronize()
  224. }
  225. func resetRegisterPopupStatus() {
  226. var info: [String : Any] = [:]
  227. if (UserDefaults.standard.value(forKey: "CheckPopupRegistration") == nil) {
  228. } else {
  229. info = (UserDefaults.standard.value(forKey: "CheckPopupRegistration") as! [String : Any])
  230. }
  231. var isPopupShownToday = info["isPopupShownToday"]
  232. let lastPopupDate = info["lastPopupDate"] as? Date
  233. // 获取当前日期和时间
  234. let currentDate = Date()
  235. // 获取当前日历
  236. let calendar = Calendar.current
  237. // 获取当天的午夜时间
  238. let midnight = calendar.startOfDay(for: currentDate)
  239. // 检查上次弹窗显示的日期是否早于当天的午夜时间
  240. if let lastPopupDate = lastPopupDate, lastPopupDate < midnight {
  241. // 重置弹窗状态
  242. isPopupShownToday = false
  243. info.updateValue(isPopupShownToday!, forKey: "isPopupShownToday")
  244. // info.updateValue(NSDate(), forKey: "lastPopupDate")
  245. UserDefaults.standard.set(info, forKey: "CheckPopupRegistration")
  246. UserDefaults.standard.synchronize()
  247. }
  248. }
  249. }