KMAccountInfoWindowController.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. var inputType: DataNavigationViewButtonActionType?
  14. var cancellAtionAction: KMAccountInfoWindowControllerCancellationAction?
  15. deinit {
  16. print("KMAccountInfoWindowController 释放")
  17. }
  18. override func windowDidLoad() {
  19. super.windowDidLoad()
  20. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  21. self.setup()
  22. self.reloadData()
  23. }
  24. //MARK: 打开文件
  25. static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMAccountInfoWindowController {
  26. if KMAccountInfoWindowController.isSampleController() {
  27. return accountInfoController ?? KMAccountInfoWindowController()
  28. } else {
  29. let controller: KMAccountInfoWindowController = KMAccountInfoWindowController.init(windowNibName: "KMAccountInfoWindowController")
  30. controller.inputType = type
  31. controller.cancellAtionAction = { controller in
  32. let verficationCodeWindowController = KMVerficationCodeWindowController.show(window: controller.window!)
  33. verficationCodeWindowController.didCancellation = {
  34. accountInfoMainWindow?.endSheet(controller.window!)
  35. controller.window?.close()
  36. if accountInfoMainWindow != nil {
  37. let cancellationWindowController = KMCancellationWindowController.show(window: accountInfoMainWindow!)
  38. }
  39. accountInfoController = nil
  40. accountInfoMainWindow = nil
  41. }
  42. }
  43. window.beginSheet(controller.window!)
  44. accountInfoController = controller
  45. accountInfoMainWindow = window
  46. return controller
  47. }
  48. }
  49. static func isSampleController() -> Bool {
  50. for window in NSApp.windows {
  51. let controller = window.windowController
  52. if controller is KMAccountInfoWindowController {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. static func fetchSampleController() -> KMAccountInfoWindowController {
  59. for window in NSApp.windows {
  60. let controller = window.windowController
  61. if controller is KMAccountInfoWindowController {
  62. return controller as! KMAccountInfoWindowController
  63. }
  64. }
  65. return NSWindowController() as! KMAccountInfoWindowController
  66. }
  67. func setup() {
  68. self.window?.contentView?.backgroundColor(NSColor(hex: "#FFFFFF"))
  69. self.accountInfoView.closeAction = { view in
  70. print("关闭")
  71. accountInfoMainWindow?.endSheet(view.window!)
  72. view.window?.close()
  73. accountInfoController = nil
  74. accountInfoMainWindow = nil
  75. }
  76. self.accountInfoView.logOutAction = { view in
  77. print("登出")
  78. KMRequestServerManager.manager.logout { success, result in
  79. if success {
  80. print("登出成功")
  81. }
  82. }
  83. KMLightMemberManager.manager.logOut()
  84. accountInfoMainWindow?.endSheet(view.window!)
  85. view.window?.close()
  86. accountInfoController = nil
  87. accountInfoMainWindow = nil
  88. }
  89. self.accountInfoView.cancellationAction = { [unowned self] view in
  90. print("注销")
  91. let alert = NSAlert()
  92. alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "")
  93. 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: "")
  94. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  95. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  96. alert.beginSheetModal(for: view.window!) { [unowned self] result in
  97. if (result == .alertFirstButtonReturn) { /// 取消
  98. return
  99. } else if result == .alertSecondButtonReturn {
  100. guard let callBack = cancellAtionAction else { return }
  101. // accountInfoMainWindow?.endSheet(view.window!)
  102. // view.window?.close()
  103. // accountInfoController = nil
  104. // accountInfoMainWindow = nil
  105. callBack(self)
  106. }
  107. }
  108. }
  109. }
  110. func reloadData() {
  111. KMRequestServerManager.manager.getUserInfo { [weak self] success, data, error, isLocal in
  112. if success {
  113. self?.accountInfoView.userInfo = data
  114. KMLightMemberManager.manager.reloadUserInfo()
  115. } else {
  116. if error?.code == 304 {
  117. if self?.window != nil {
  118. accountInfoMainWindow?.endSheet((self?.window)!)
  119. }
  120. self?.window?.close()
  121. KMLightMemberManager.manager.logOut()
  122. accountInfoController = nil
  123. accountInfoMainWindow = nil
  124. }
  125. }
  126. }
  127. }
  128. }