AccountTools.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. self.refreshRights(isActivaDevice: true, isDealCode: true) { model in
  31. guard let _ = model else { // 刷新权益失败
  32. callback(false, nil)
  33. return
  34. }
  35. let man = AccountManager.manager
  36. // 试用
  37. if man.isTrialing {
  38. callback(true, "")
  39. return
  40. }
  41. // 非登陆 或 非Vip
  42. if man.isLogin == false || man.isVip == false{
  43. callback(false, nil)
  44. return
  45. }
  46. // 登陆 & Vip 绑定设备
  47. self.bindDevice { success, dataModel in
  48. callback(success, "")
  49. }
  50. }
  51. }
  52. public class func canUseAdvance_success(callback: @escaping ((Bool)->Void)) {
  53. if AccountManager.manager.isTrialing {
  54. callback(true)
  55. return
  56. }
  57. guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
  58. self.gotoCompare()
  59. return callback(false)
  60. }
  61. let state = Self._isConnectionAvailable()
  62. if !state {
  63. // self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  64. self.gotoCompare()
  65. return callback(false)
  66. }
  67. self.refreshRights(isActivaDevice: true, isDealCode: true) { model in
  68. guard let _ = model else { // 刷新权益失败
  69. callback(false)
  70. self.gotoCompare()
  71. return
  72. }
  73. let man = AccountManager.manager
  74. // 试用
  75. if man.isTrialing {
  76. callback(true)
  77. return
  78. }
  79. // 非登陆 或 非Vip
  80. if man.isLogin == false || man.isVip == false{
  81. callback(false)
  82. self.gotoCompare()
  83. return
  84. }
  85. // 登陆 & Vip 绑定设备
  86. self.bindDevice { success, dataModel in
  87. if success == false {
  88. self.gotoCompare()
  89. }
  90. callback(success)
  91. }
  92. }
  93. }
  94. public class func bindDevice(callback: ((Bool,KMRequestResultModel?)->Void)?) {
  95. guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
  96. callback?(false, nil)
  97. return
  98. }
  99. let header = ["Token" : token]
  100. let uuid = GetHardwareUUID()
  101. let params = ["device_code" : uuid]
  102. KMHTTP.OEM_POST(urlString: kURLAPI_oemBind, parameter: params, headers: header) { success, dataModel, err in
  103. callback?(success, dataModel)
  104. }
  105. }
  106. public class func refreshRights(isActivaDevice: Bool, isDealCode: Bool, callback: ((_ model: KMRequestResultModel?)->Void)?) {
  107. guard let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false else {
  108. callback?(nil)
  109. return
  110. }
  111. let header = ["Token" : token]
  112. KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
  113. if success == false {
  114. // 清空 Token
  115. KMDataManager.ud_set("", forKey: kAccountTokenKey)
  116. // 清空 登陆 & 权益 数据
  117. AccountManager.manager.saveRights(model: nil)
  118. // 发布通知 登陆状态改变
  119. NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
  120. // 解除设备激活
  121. if isActivaDevice {
  122. self.unactivateDevice {}
  123. }
  124. // 处理 Code
  125. if let codeV = dataModel?.code, isDealCode {
  126. self.dealCode(.init(rawValue: codeV))
  127. }
  128. callback?(nil)
  129. return
  130. }
  131. let model = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  132. // 保存 登陆 & 权益 数据
  133. AccountManager.manager.saveRights(model: model)
  134. // 发布通知 登陆状态改变
  135. NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
  136. if isActivaDevice == false {
  137. callback?(dataModel)
  138. return
  139. }
  140. // 设备激活
  141. if let data = model.memberInfo?.email, data.isEmpty == false {
  142. self.activateDevice(email: data) {
  143. callback?(dataModel)
  144. }
  145. } else {
  146. callback?(dataModel)
  147. }
  148. }
  149. }
  150. // MARK: - 处理 Code
  151. public class func dealCode(_ code: KMHttpReponseCode?) {
  152. if code == .passwordChanged {
  153. self._showHud(msg: NSLocalizedString("Your password has been changed. Please login again.", comment: ""))
  154. } else if code == .accountAtOtherDeviceLogin {
  155. self._showPwdChange()
  156. }
  157. }
  158. // MARK: - 激活设备
  159. public class func activateDevice(email: String, callback: (()->Void)?) {
  160. VerificationManager.default().activateDevice(withInfo: ["email" : email]) { status, info, err in
  161. VerificationManager.default().verification { status, info, err in
  162. callback?()
  163. }
  164. }
  165. }
  166. public class func unactivateDevice(callback: (()->Void)?) {
  167. VerificationManager.default().unactivateDeviceWithcomplention { status, infoDict, err in
  168. VerificationManager.default().verification {status1 , infoDict1, err1 in
  169. callback?()
  170. }
  171. }
  172. }
  173. class func gotoCompare() {
  174. KMMainThreadExecute {
  175. let winC = KMPurchaseCompareWindowController.sharedInstance()
  176. winC?.showWindow(nil)
  177. }
  178. }
  179. private class func _isConnectionAvailable() -> Bool {
  180. if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
  181. return false
  182. }
  183. return true
  184. }
  185. private class func _showHud(msg: String) {
  186. DispatchQueue.main.async {
  187. if let data = NSApp.mainWindow?.contentView {
  188. if let sheetV = NSApp.mainWindow?.attachedSheet?.contentView {
  189. _ = CustomAlertView.alertView(message: msg, fromView: sheetV, withStyle: .black)
  190. } else {
  191. _ = CustomAlertView.alertView(message: msg, fromView: data, withStyle: .black)
  192. }
  193. }
  194. }
  195. }
  196. private class func _showPwdChange() {
  197. DispatchQueue.main.async {
  198. let winC = AccountPwdChangedWindowController.shared
  199. winC.window?.makeKeyAndOrderFront(nil)
  200. }
  201. }
  202. }