123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- //
- // AccountTools.swift
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2024/11/5.
- //
- import Cocoa
- @objcMembers class AccountTools: NSObject {
- @available(macOS 10.15.0, iOS 13.0, *)
- public class func canUseAdvance() async -> Bool {
- return await withCheckedContinuation({ continuation in
- self.canUseAdvance { success, err in
- continuation.resume(returning: success)
- }
- })
- }
-
- public class func canUseAdvance(callback: @escaping ((Bool, String?)->Void)) {
- if AccountManager.manager.isTrialing {
- callback(true, nil)
- return
- }
-
- guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
- return callback(false, nil)
- }
- let state = Self._isConnectionAvailable()
- if !state {
- // self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
- return callback(false, nil)
- }
-
- self.refreshRights(isActivaDevice: true, isDealCode: true) { model in
- guard let _ = model else { // 刷新权益失败
- callback(false, nil)
- return
- }
-
- let man = AccountManager.manager
- // 试用
- if man.isTrialing {
- callback(true, "")
- return
- }
- // 非登陆 或 非Vip
- if man.isLogin == false || man.isVip == false{
- callback(false, nil)
- return
- }
- // 登陆 & Vip 绑定设备
- self.bindDevice { success, dataModel in
- callback(success, "")
- }
- }
- }
-
- public class func canUseAdvance_success(callback: @escaping ((Bool)->Void)) {
- if AccountManager.manager.isTrialing {
- callback(true)
- return
- }
-
- guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
- self.gotoCompare()
- return callback(false)
- }
- let state = Self._isConnectionAvailable()
- if !state {
- // self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
- self.gotoCompare()
- return callback(false)
- }
-
- self.refreshRights(isActivaDevice: true, isDealCode: true) { model in
- guard let _ = model else { // 刷新权益失败
- callback(false)
-
- self.gotoCompare()
- return
- }
-
- let man = AccountManager.manager
- // 试用
- if man.isTrialing {
- callback(true)
- return
- }
- // 非登陆 或 非Vip
- if man.isLogin == false || man.isVip == false{
- callback(false)
-
- self.gotoCompare()
- return
- }
- // 登陆 & Vip 绑定设备
- self.bindDevice { success, dataModel in
- if success == false {
- self.gotoCompare()
- }
-
- callback(success)
- }
- }
- }
-
- public class func bindDevice(callback: ((Bool,KMRequestResultModel?)->Void)?) {
- guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
- callback?(false, nil)
- return
- }
- let header = ["Token" : token]
-
- let uuid = GetHardwareUUID()
- let params = ["device_code" : uuid]
-
- KMHTTP.OEM_POST(urlString: kURLAPI_oemBind, parameter: params, headers: header) { success, dataModel, err in
- callback?(success, dataModel)
- }
- }
-
- public class func refreshRights(isActivaDevice: Bool, isDealCode: Bool, callback: ((_ model: KMRequestResultModel?)->Void)?) {
- guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
- callback?(nil)
- return
- }
- let header = ["Token" : token]
- KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
- if success == false {
- // 清空 Token
- KMDataManager.ud_set("", forKey: kAccountTokenKey)
- // 清空 登陆 & 权益 数据
- AccountManager.manager.saveRights(model: nil)
- // 发布通知 登陆状态改变
- NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
- // 解除设备激活
- if isActivaDevice {
- self.unactivateDevice {}
- }
- // 处理 Code
- if let codeV = dataModel?.code, isDealCode {
- self.dealCode(.init(rawValue: codeV))
- }
-
- callback?(nil)
- return
- }
- let model = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
- // 保存 登陆 & 权益 数据
- AccountManager.manager.saveRights(model: model)
- // 发布通知 登陆状态改变
- NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
-
- if isActivaDevice == false {
- callback?(dataModel)
- return
- }
-
- // 设备激活
- if let data = model.memberInfo?.email, data.isEmpty == false {
- self.activateDevice(email: data) {
- callback?(dataModel)
- }
- } else {
- callback?(dataModel)
- }
- }
- }
-
- // MARK: - 处理 Code
-
- public class func dealCode(_ code: KMHttpReponseCode?) {
- if code == .passwordChanged {
- self._showHud(msg: NSLocalizedString("Your password has been changed. Please login again.", comment: ""))
- } else if code == .accountAtOtherDeviceLogin {
- self._showPwdChange()
- }
- }
-
- // MARK: - 激活设备
-
- public class func activateDevice(email: String, callback: (()->Void)?) {
- VerificationManager.default().activateDevice(withInfo: ["email" : email]) { status, info, err in
- VerificationManager.default().verification { status, info, err in
- callback?()
- }
- }
- }
-
- public class func unactivateDevice(callback: (()->Void)?) {
- VerificationManager.default().unactivateDeviceWithcomplention { status, infoDict, err in
- VerificationManager.default().verification {status1 , infoDict1, err1 in
- callback?()
- }
- }
- }
-
- class func gotoCompare() {
- KMMainThreadExecute {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- }
- }
- private class func _isConnectionAvailable() -> Bool {
- if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
- return false
- }
- return true
- }
-
- private class func _showHud(msg: String) {
- DispatchQueue.main.async {
- if let data = NSApp.mainWindow?.contentView {
- if let sheetV = NSApp.mainWindow?.attachedSheet?.contentView {
- _ = CustomAlertView.alertView(message: msg, fromView: sheetV, withStyle: .black)
- } else {
- _ = CustomAlertView.alertView(message: msg, fromView: data, withStyle: .black)
- }
- }
- }
- }
-
- private class func _showPwdChange() {
- DispatchQueue.main.async {
- let winC = AccountPwdChangedWindowController.shared
- winC.window?.makeKeyAndOrderFront(nil)
- }
- }
- }
|