KMAccountInfoWindowController.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. let controller: KMAccountInfoWindowController = KMAccountInfoWindowController.fetchSampleController()
  28. controller.inputType = type
  29. window.beginSheet(controller.window!)
  30. return controller
  31. } else {
  32. let controller: KMAccountInfoWindowController = KMAccountInfoWindowController.init(windowNibName: "KMAccountInfoWindowController")
  33. controller.inputType = type
  34. controller.cancellAtionAction = { controller in
  35. let verficationCodeWindowController = KMVerficationCodeWindowController.show(window: controller.window!)
  36. verficationCodeWindowController.didCancellation = {
  37. accountInfoMainWindow?.endSheet(controller.window!)
  38. controller.window?.close()
  39. if accountInfoMainWindow != nil {
  40. let cancellationWindowController = KMCancellationWindowController.show(window: accountInfoMainWindow!)
  41. }
  42. accountInfoController = nil
  43. accountInfoMainWindow = nil
  44. }
  45. }
  46. window.beginSheet(controller.window!)
  47. accountInfoController = controller
  48. accountInfoMainWindow = window
  49. return controller
  50. }
  51. }
  52. static func isSampleController() -> Bool {
  53. for window in NSApp.windows {
  54. let controller = window.windowController
  55. if controller is KMAccountInfoWindowController {
  56. return true
  57. }
  58. }
  59. return false
  60. }
  61. static func fetchSampleController() -> KMAccountInfoWindowController {
  62. for window in NSApp.windows {
  63. let controller = window.windowController
  64. if controller is KMAccountInfoWindowController {
  65. return controller as! KMAccountInfoWindowController
  66. }
  67. }
  68. return NSWindowController() as! KMAccountInfoWindowController
  69. }
  70. func setup() {
  71. self.window?.contentView?.backgroundColor(NSColor(hex: "#FFFFFF"))
  72. self.accountInfoView.closeAction = { view in
  73. print("关闭")
  74. accountInfoMainWindow?.endSheet(view.window!)
  75. view.window?.close()
  76. accountInfoController = nil
  77. accountInfoMainWindow = nil
  78. }
  79. self.accountInfoView.logOutAction = { view in
  80. print("登出")
  81. KMRequestServerManager.manager.logout { success, result in
  82. if success {
  83. print("登出成功")
  84. }
  85. }
  86. KMLightMemberManager.manager.logOut()
  87. accountInfoMainWindow?.endSheet(view.window!)
  88. view.window?.close()
  89. accountInfoController = nil
  90. accountInfoMainWindow = nil
  91. }
  92. self.accountInfoView.cancellationAction = { [unowned self] view in
  93. print("注销")
  94. let alert = NSAlert()
  95. alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "")
  96. 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: "")
  97. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  98. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  99. alert.beginSheetModal(for: view.window!) { [unowned self] result in
  100. if (result == .alertFirstButtonReturn) { /// 取消
  101. return
  102. } else if result == .alertSecondButtonReturn {
  103. guard let callBack = cancellAtionAction else { return }
  104. // accountInfoMainWindow?.endSheet(view.window!)
  105. // view.window?.close()
  106. // accountInfoController = nil
  107. // accountInfoMainWindow = nil
  108. callBack(self)
  109. }
  110. }
  111. }
  112. }
  113. func reloadData() {
  114. KMRequestServerManager.manager.getUserInfo { [unowned self] success, data, error, isLocal in
  115. if success {
  116. self.accountInfoView.userInfo = data
  117. KMLightMemberManager.manager.reloadUserInfo()
  118. } else {
  119. if error?.code == 304 {
  120. accountInfoMainWindow?.endSheet(self.window!)
  121. self.window?.close()
  122. KMLightMemberManager.manager.logOut()
  123. accountInfoController = nil
  124. accountInfoMainWindow = nil
  125. }
  126. }
  127. }
  128. }
  129. }