AccountTools.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // AccountTools.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/11/5.
  6. //
  7. import Cocoa
  8. @objcMembers class AccountTools: NSObject {
  9. @available(macOS 10.15.0, iOS 13.0, *)
  10. public class func canUseAdvance() async -> Bool {
  11. return await withCheckedContinuation({ continuation in
  12. self.canUseAdvance { success, err in
  13. continuation.resume(returning: success)
  14. }
  15. })
  16. }
  17. public class func canUseAdvance(callback: @escaping ((Bool, String?)->Void)) {
  18. if AccountManager.manager.isTrialing {
  19. callback(true, nil)
  20. return
  21. }
  22. guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
  23. return callback(false, nil)
  24. }
  25. let state = Self._isConnectionAvailable()
  26. if !state {
  27. // self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  28. return callback(false, nil)
  29. }
  30. let header = ["Token" : token]
  31. let uuid = GetHardwareUUID()
  32. let params = ["device_code" : uuid]
  33. // 绑定设备
  34. KMHTTP.OEM_POST(urlString: kURLAPI_oemBind, parameter: params, headers: header) { success, dataModel, err in
  35. callback(success, "")
  36. if dataModel?.code == KMHttpReponseCode.passwordChanged.rawValue {
  37. DispatchQueue.main.async {
  38. self._showHud(msg: NSLocalizedString("Your password has been changed. Please login again.", comment: ""))
  39. }
  40. }
  41. }
  42. }
  43. public class func canUseAdvance_success(callback: @escaping ((Bool)->Void)) {
  44. if AccountManager.manager.isTrialing {
  45. callback(true)
  46. return
  47. }
  48. guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
  49. self.gotoCompare()
  50. return callback(false)
  51. }
  52. let state = Self._isConnectionAvailable()
  53. if !state {
  54. // self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  55. self.gotoCompare()
  56. return callback(false)
  57. }
  58. let header = ["Token" : token]
  59. let uuid = GetHardwareUUID()
  60. let params = ["device_code" : uuid]
  61. // 绑定设备
  62. KMHTTP.OEM_POST(urlString: kURLAPI_oemBind, parameter: params, headers: header) { success, dataModel, err in
  63. if success == false {
  64. self.gotoCompare()
  65. if dataModel?.code == KMHttpReponseCode.passwordChanged.rawValue {
  66. DispatchQueue.main.async {
  67. self._showHud(msg: NSLocalizedString("Your password has been changed. Please login again.", comment: ""))
  68. }
  69. }
  70. }
  71. callback(success)
  72. }
  73. }
  74. class func gotoCompare() {
  75. KMMainThreadExecute {
  76. let winC = KMPurchaseCompareWindowController.sharedInstance()
  77. winC?.showWindow(nil)
  78. }
  79. }
  80. private class func _isConnectionAvailable() -> Bool {
  81. if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
  82. return false
  83. }
  84. return true
  85. }
  86. private class func _showHud(msg: String) {
  87. if let data = NSApp.mainWindow?.contentView {
  88. _ = CustomAlertView.alertView(message: msg, fromView: data, withStyle: .black)
  89. }
  90. }
  91. }