KMAccountInfoWindowController.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // KMAccountInfoWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/24.
  6. //
  7. import Cocoa
  8. var accountInfoController: KMAccountInfoWindowController?
  9. var accountInfoMainWindow: NSWindow?
  10. typealias KMAccountInfoWindowControllerCancellationAction = (_ controller: KMAccountInfoWindowController) -> Void
  11. class KMAccountInfoWindowController: NSWindowController {
  12. @IBOutlet weak var accountInfoView: KMAccountInfoView!
  13. @IBOutlet weak var accountViewHeightConstraint: NSLayoutConstraint!
  14. var inputType: DataNavigationViewButtonActionType?
  15. var cancellAtionAction: KMAccountInfoWindowControllerCancellationAction?
  16. deinit {
  17. print("KMAccountInfoWindowController 释放")
  18. }
  19. override func windowDidLoad() {
  20. super.windowDidLoad()
  21. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  22. self.setup()
  23. self.reloadData()
  24. }
  25. //MARK: 打开文件
  26. static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMAccountInfoWindowController {
  27. if KMAccountInfoWindowController.isSampleController() {
  28. return accountInfoController ?? KMAccountInfoWindowController()
  29. } else {
  30. let controller: KMAccountInfoWindowController = KMAccountInfoWindowController.init(windowNibName: "KMAccountInfoWindowController")
  31. controller.inputType = type
  32. controller.cancellAtionAction = { controller in
  33. let verficationCodeWindowController = KMVerficationCodeWindowController.show(window: controller.window!)
  34. verficationCodeWindowController.didCancellation = {
  35. accountInfoMainWindow?.endSheet(controller.window!)
  36. controller.window?.close()
  37. if accountInfoMainWindow != nil {
  38. let cancellationWindowController = KMCancellationWindowController.show(window: accountInfoMainWindow!)
  39. }
  40. accountInfoController = nil
  41. accountInfoMainWindow = nil
  42. }
  43. }
  44. window.beginSheet(controller.window!)
  45. accountInfoController = controller
  46. accountInfoMainWindow = window
  47. return controller
  48. }
  49. }
  50. static func isSampleController() -> Bool {
  51. for window in NSApp.windows {
  52. let controller = window.windowController
  53. if controller is KMAccountInfoWindowController {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. static func fetchSampleController() -> KMAccountInfoWindowController {
  60. for window in NSApp.windows {
  61. let controller = window.windowController
  62. if controller is KMAccountInfoWindowController {
  63. return controller as! KMAccountInfoWindowController
  64. }
  65. }
  66. return NSWindowController() as! KMAccountInfoWindowController
  67. }
  68. func setup() {
  69. self.window?.contentView?.backgroundColor(NSColor(hex: "#FFFFFF"))
  70. self.accountInfoView.closeAction = { view in
  71. print("关闭")
  72. accountInfoMainWindow?.endSheet(view.window!)
  73. view.window?.close()
  74. accountInfoController = nil
  75. accountInfoMainWindow = nil
  76. }
  77. self.accountInfoView.logOutAction = { view in
  78. print("登出")
  79. KMRequestServerManager.manager.logout { success, result in
  80. if success {
  81. print("登出成功")
  82. }
  83. }
  84. KMLightMemberManager.manager.logOut()
  85. accountInfoMainWindow?.endSheet(view.window!)
  86. view.window?.close()
  87. accountInfoController = nil
  88. accountInfoMainWindow = nil
  89. }
  90. self.accountInfoView.cancellationAction = { [unowned self] view in
  91. print("注销")
  92. let alert = NSAlert()
  93. alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "")
  94. alert.informativeText = NSLocalizedString("Cancellation cannot be withdrawn, it will be completed within 3 working days, until then your account will be frozen and cannot be logged in, are you sure you want to delete your account?", comment: "")
  95. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  96. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  97. alert.beginSheetModal(for: view.window!) { [unowned self] result in
  98. if (result == .alertFirstButtonReturn) { /// 取消
  99. return
  100. } else if result == .alertSecondButtonReturn {
  101. guard let callBack = cancellAtionAction else { return }
  102. // accountInfoMainWindow?.endSheet(view.window!)
  103. // view.window?.close()
  104. // accountInfoController = nil
  105. // accountInfoMainWindow = nil
  106. callBack(self)
  107. }
  108. }
  109. }
  110. self.accountInfoView.purchaseInfoAction = { view in
  111. NSWorkspace.shared.open(URL(string: "https://www.pdfreaderpro.com/store")!)
  112. }
  113. self.accountInfoView.subcriptionAction = { view in
  114. #if VERSION_DMG
  115. KMPurchaseManager.manager.purchaseProduct(productIdentifier: "") { isSuccess, error in
  116. }
  117. #endif
  118. }
  119. }
  120. func reloadData() {
  121. let state = KMLightMemberManager.manager.purchaseState
  122. if state == .unknow {
  123. self.accountViewHeightConstraint.constant = 274
  124. } else if state == .subscription ||
  125. state == .trial {
  126. self.accountViewHeightConstraint.constant = 411
  127. } else if state == .unknow {
  128. self.accountViewHeightConstraint.constant = 483
  129. }
  130. self.accountInfoView.purchaseState = state
  131. KMRequestServerManager.manager.getUserInfo { [weak self] success, data, error, isLocal in
  132. if success {
  133. self?.accountInfoView.userInfo = data
  134. KMLightMemberManager.manager.reloadUserInfo()
  135. } else {
  136. if error?.code == 304 {
  137. if self?.window != nil {
  138. accountInfoMainWindow?.endSheet((self?.window)!)
  139. }
  140. self?.window?.close()
  141. KMLightMemberManager.manager.logOut()
  142. accountInfoController = nil
  143. accountInfoMainWindow = nil
  144. }
  145. }
  146. }
  147. }
  148. }