123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- //
- // KMLightMemberManager.swift
- // PDF Master
- //
- // Created by lizhe on 2023/2/22.
- //
- import Cocoa
- let USER_LOGINSTATE_NOTIFICATION = "kUserLoginStateNotification" //用户登录状态改变通知
- let USER_INFO_CHANGE_NOTIFICATION = "kUserInfoChangeNotification" //用户信息改变通知
- let ADVANCED_NOTIFICATION = "kAdvancedContentViewShowNotification" //用户高级引导显示
- enum KMLightMemberUserState: String, CaseIterable {
- case unknown = "unknown"
- case login = "login" //登录
- case logOut = "logOut" //登出
- case cancellation = "cancellation" //注销
- }
- @objcMembers class KMLightMemberManager: NSObject {
- static let manager = KMLightMemberManager()
- var config: KMLightMemberConfig = KMLightMemberConfig()
- var info: KMLightMemberUserInfo = KMLightMemberUserInfo()
- var token: KMLightMemberToken = KMLightMemberToken()
-
- var userState: KMLightMemberUserState = .unknown //用户状态
- var subscriptionState: KMSubscriptionState { //订阅状态
- get {
- return KMSubscriptionManager.manager.state
- }
- }
-
- //定时器
- // var timer: Timer = Timer()
-
- /**
- 是否显示高级功能视图
- */
- var canShowAdvancedView: Bool = true {
- didSet {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil)
- }
- }
- // {
- // get {
- // if UserDefaults.standard.object(forKey: "kAdvancedContentViewShow") != nil {
- // return false
- // } else {
- // return true
- // }
- // }
- //
- // set {
- // UserDefaults.standard.set("kAdvancedContentViewShow", forKey: "kAdvancedContentViewShow")
- // UserDefaults.standard.synchronize()
- // NotificationCenter.default.post(name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil)
- // }
- // }
-
- override init() {
- super.init()
- self.reloadUserInfo()
- }
-
- func logOut() {
- DispatchQueue.main.async {
- self.userState = .logOut
- KMLightMemberCache.cache.clean()
- self.reloadUserInfo()
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
- }
- }
-
- func logIn() {
- self.userState = .login
- self.reloadUserInfo()
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
- }
-
- func cancellation() {
- self.userState = .cancellation
- self.reloadUserInfo()
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil, userInfo: nil)
- }
- }
- //MARK: - 数据
- extension KMLightMemberManager {
- /**
- 刷新本地用户数据
- */
- func reloadUserInfo() {
- //获取用户信息
- let userInfo = KMLightMemberCache.cache.readData(type: .info)
- if userInfo.count != 0 {
- KMLightMemberUserInfo.parseData(data: userInfo, needSave: false) { [unowned self] data in
- self.info = data
- }
- } else {
- self.info = KMLightMemberUserInfo()
- }
-
- let token = KMLightMemberCache.cache.readData(type: .token)
- if token.count != 0 {
- KMLightMemberToken.parseData(data: token) { [unowned self] data in
- self.token = data
- }
- } else {
- self.token = KMLightMemberToken()
- }
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: USER_INFO_CHANGE_NOTIFICATION), object: nil, userInfo: nil)
- }
-
- /**
- 刷新本地用户数据
- */
- func updateNetworkingUserInfo(completion: @escaping (_ isDidLogout: Bool) -> Void) {
- KMRequestServerManager.manager.getUserInfo { [unowned self] success, data, error, isLocal in
- if success {
- self.reloadUserInfo()
- completion(false)
- } else {
- if error?.code == 304 {
- KMLightMemberManager.manager.logOut()
- completion(true)
- }
- completion(false)
- }
- }
- }
-
- /**
- token是否生效
- */
- func isTokenValid() async -> Bool {
- var result = false
-
- if self.isLogin() {
- do {
- let (success, error) = try await KMRequestServerManager.manager.getUserInfo2()
- // Thread.sleep(forTimeInterval: 20.0)
- if success {
- result = true
- } else {
- if error.code == 304 {
- KMLightMemberManager.manager.logOut()
- result = false
- } else {
- result = true
- }
- }
- return result
- } catch {
- return result
- }
- }
- return result
- }
- }
- //MARK: - 快捷方法
- extension KMLightMemberManager {
- //是否已经登录
- func isLogin() -> Bool {
- var result: Bool = false
- if token.access_token != "" {
- result = true
- }
- return result
- }
-
- //是否已经注销
- func isLogOff() -> Bool {
- var result: Bool = false
- return result
- }
-
- //是否能使用高级功能
- func canUseAdvanced(needNetworking: Bool = false) async -> Bool {
- var result: Bool = false
-
- var tokenValid = true
- if needNetworking {
- tokenValid = await self.isTokenValid()
- }
- print(tokenValid)
- if tokenValid && info.isInFreeUseTime {
- result = true
- }
- return result
- }
- }
|