KMAccountInfoWindowController.swift 5.3 KB

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