AccountCenterWindowController.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // AccountCenterWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/22.
  6. //
  7. import Cocoa
  8. let kAccountTokenKey = "AccountToken"
  9. let kAccountEmailKey = "AccountEmail"
  10. let kTermsOfUseUrlString = "https://www.anyrecover.com/company/terms-conditions/"
  11. let kPrivacyPolicyUrlString = "https://www.anyrecover.com/company/privacy-policy/"
  12. let kResetpasswordUrlString = "https://account.anyrecover.com/reset-password/"
  13. let kCancelSubscriptionUrlString = "https://www.anyrecover.com/support/cancel-subscription/"
  14. let kPwdInputStrings = "!\"#$%&'()*+,-./:;<>=?@[\\]^_`{|}~0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
  15. extension NSNotification.Name {
  16. static let loginStatusChanged = NSNotification.Name("KMLoginStatusChangedNotification")
  17. }
  18. class AccountCenterWindowController: NSWindowController {
  19. @IBOutlet weak var contentBox: NSBox!
  20. private lazy var rightDatas_: [String] = {
  21. return [
  22. NSLocalizedString("Handle PDF Documents with AI", comment: ""),
  23. NSLocalizedString("Unlimited file conversion", comment: ""),
  24. NSLocalizedString("PDF text and image editing", comment: ""),
  25. NSLocalizedString("Batch PDF processing", comment: ""),
  26. NSLocalizedString("Advanced PDF management", comment: ""),
  27. NSLocalizedString("PDF annotations", comment: ""),
  28. NSLocalizedString("Create&fill forms", comment: ""),
  29. NSLocalizedString("PDF Protect", comment: ""),
  30. NSLocalizedString("Advanced OCR technology", comment: "")]
  31. }()
  32. private var inputC_: AccountInputController?
  33. private var centerC: AccountCenterController?
  34. private var pwdChangedwinC_: AccountPwdChangedWindowController?
  35. private lazy var closeButton_: NSButton = {
  36. let view = NSButton()
  37. view.isBordered = false
  38. view.title = ""
  39. return view
  40. }()
  41. convenience init() {
  42. self.init(windowNibName: "AccountCenterWindowController")
  43. }
  44. override func windowDidLoad() {
  45. super.windowDidLoad()
  46. self.window?.appearance = NSAppearance(named: .aqua)
  47. self.contentBox.borderWidth = 0
  48. self.window?.contentView?.addSubview(self.closeButton_)
  49. self.closeButton_.km_add_right_constraint(constant: -10)
  50. self.closeButton_.km_add_top_constraint(constant: 10)
  51. self.closeButton_.km_add_size_constraint(size: .init(width: 24, height: 24))
  52. self.closeButton_.target = self
  53. self.closeButton_.action = #selector(_closeAction)
  54. self.closeButton_.image = NSImage(named: "KMImageNameAccountClose")
  55. if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
  56. // 外面处理激活设备
  57. AccountTools.refreshRights(isActivaDevice: false, isDealCode: true) { model in
  58. guard let _ = model else {
  59. self.gotoLogin()
  60. return
  61. }
  62. let rightM = AccountRightModel(dict: (model?.data as? [String : Any]) ?? [:])
  63. self.gotoCenter(model: rightM, infoModel: nil)
  64. // 激活设备
  65. if let data = rightM.memberInfo?.email, data.isEmpty == false {
  66. AccountTools.activateDevice(email: data, callback: nil)
  67. }
  68. }
  69. } else {
  70. self.gotoLogin()
  71. }
  72. }
  73. func gotoLogin() {
  74. self.closeButton_.isHidden = false
  75. self.inputC_ = AccountInputController()
  76. self.inputC_?.rightDatas = self.rightDatas_
  77. self.contentBox.contentView = self.inputC_?.view
  78. self.inputC_?.gotoLogin()
  79. self.inputC_?.itemClick = { idx, params in
  80. guard let model = params.first as? AccountInfoModel else {
  81. return
  82. }
  83. if let data = model.token, data.isEmpty == false {
  84. KMDataManager.ud_set(data, forKey: kAccountTokenKey)
  85. }
  86. if let data = model.email, data.isEmpty == false {
  87. KMDataManager.ud_set(data, forKey: kAccountEmailKey)
  88. }
  89. let state = self._isConnectionAvailable()
  90. if !state {
  91. self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  92. return
  93. }
  94. AccountTools.refreshRights(isActivaDevice: true, isDealCode: true) { dataModel in
  95. guard let _ = dataModel else {
  96. return
  97. }
  98. let rmodel = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  99. self.gotoCenter(model: rmodel, infoModel: model)
  100. }
  101. }
  102. }
  103. func gotoSignin() {
  104. self.closeButton_.isHidden = false
  105. self.inputC_ = AccountInputController()
  106. self.contentBox.contentView = self.inputC_?.view
  107. self.inputC_?.goToSignIn()
  108. self.inputC_?.rightDatas = self.rightDatas_
  109. self.inputC_?.itemClick = { idx, params in
  110. guard let model = params.first as? AccountInfoModel else {
  111. return
  112. }
  113. if let data = model.token, data.isEmpty == false {
  114. KMDataManager.ud_set(data, forKey: kAccountTokenKey)
  115. }
  116. if let data = model.email, data.isEmpty == false {
  117. KMDataManager.ud_set(data, forKey: kAccountEmailKey)
  118. }
  119. let state = self._isConnectionAvailable()
  120. if !state {
  121. self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  122. return
  123. }
  124. AccountTools.refreshRights(isActivaDevice: true, isDealCode: true) { dataModel in
  125. guard let _ = dataModel else {
  126. return
  127. }
  128. let rmodel = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  129. self.gotoCenter(model: rmodel, infoModel: model)
  130. }
  131. }
  132. }
  133. func gotoCenter(model: AccountRightModel?, infoModel: AccountInfoModel?) {
  134. self.closeButton_.isHidden = true
  135. self.centerC = AccountCenterController()
  136. self.centerC?.rightDatas = self.rightDatas_
  137. self.centerC?.model = model
  138. self.centerC?.infoModel = infoModel
  139. self.centerC?.goLoginBlock = { [weak self] _ in
  140. self?.gotoLogin()
  141. }
  142. self.contentBox.contentView = self.centerC?.view
  143. }
  144. func pwdChangedAction() {
  145. if let _ = self.pwdChangedwinC_ {
  146. NSSound.beep()
  147. return
  148. }
  149. KMDataManager.ud_set("", forKey: kAccountTokenKey)
  150. AccountManager.manager.saveRights(model: nil)
  151. NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
  152. let winC = AccountPwdChangedWindowController()
  153. self.pwdChangedwinC_ = winC
  154. self.window?.addChildWindow(winC.window!, ordered: .above)
  155. let winFrame = self.window?.sheetParent?.frame ?? .zero
  156. var frame = self.pwdChangedwinC_?.window?.frame ?? .zero
  157. frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
  158. frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
  159. self.pwdChangedwinC_?.window?.setFrame(frame, display: true)
  160. winC.itemClick = { [weak self] idx, _ in
  161. for win in self?.window?.childWindows ?? [] {
  162. if win.isEqual(to: self?.pwdChangedwinC_?.window) {
  163. self?.window?.removeChildWindow(win)
  164. break
  165. }
  166. }
  167. self?.pwdChangedwinC_?.window?.orderOut(nil)
  168. self?.pwdChangedwinC_ = nil
  169. self?.window?.windowController?.km_quick_endSheet()
  170. }
  171. }
  172. // MARK: - Private Methods
  173. @objc private func _closeAction() {
  174. self.km_quick_endSheet()
  175. }
  176. private func _isConnectionAvailable() -> Bool {
  177. if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
  178. return false
  179. }
  180. return true
  181. }
  182. private func _showHud(msg: String) {
  183. DispatchQueue.main.async {
  184. if let data = self.window?.contentView {
  185. _ = CustomAlertView.alertView(message: msg, fromView: data, withStyle: .black)
  186. }
  187. }
  188. }
  189. }