KMVerficationCodeWindowController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // KMVerficationCodeWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/24.
  6. //
  7. import Cocoa
  8. var verficationCodeController: KMVerficationCodeWindowController?
  9. var verficationCodeMainWindow: NSWindow?
  10. typealias KMVerficationCodeWindowControllerDidCancellation = () -> ()
  11. class KMVerficationCodeWindowController: NSWindowController {
  12. @IBOutlet weak var verificationCodeView: KMVerificationCodeView!
  13. @IBOutlet weak var networkView: KMLightNoNetworkView!
  14. var didCancellation: KMVerficationCodeWindowControllerDidCancellation?
  15. var inputType: DataNavigationViewButtonActionType?
  16. deinit {
  17. print("KMVerficationCodeWindowController 释放")
  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. }
  24. //MARK: 打开文件
  25. static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMVerficationCodeWindowController {
  26. let controller: KMVerficationCodeWindowController = KMVerficationCodeWindowController.init(windowNibName: "KMVerficationCodeWindowController")
  27. controller.inputType = type
  28. window.beginSheet(controller.window!)
  29. controller.window?.center()
  30. controller.verificationCodeView.inputType = .accountInfo
  31. controller.verificationCodeView.model.email = KMLightMemberManager.manager.info.email
  32. controller.sendVerifyCode(sender: nil)
  33. verficationCodeController = controller
  34. verficationCodeMainWindow = window
  35. return controller
  36. }
  37. func setup() {
  38. self.window?.contentView?.backgroundColor(NSColor(hex: "#FFFFFF"))
  39. self.verificationCodeView.closeAction = { view in
  40. print("关闭")
  41. verficationCodeMainWindow?.endSheet(view.window!)
  42. view.window?.close()
  43. verficationCodeController = nil
  44. verficationCodeMainWindow = nil
  45. }
  46. self.verificationCodeView.cancelAction = { view in
  47. print("登出")
  48. verficationCodeMainWindow?.endSheet(view.window!)
  49. view.window?.close()
  50. verficationCodeController = nil
  51. verficationCodeMainWindow = nil
  52. }
  53. self.verificationCodeView.doneAction = { [unowned self] (view, data , sender) in
  54. print("注销")
  55. KMRequestServerManager.manager.logOff(verifyCode: data.verifyCode, complete: { [unowned self] success, result in
  56. if success {
  57. verficationCodeMainWindow?.endSheet(view.window!)
  58. view.window?.close()
  59. verficationCodeController = nil
  60. verficationCodeMainWindow = nil
  61. self.didCancellation?()
  62. } else {
  63. self.verificationCodeView.showAlert(result: result)
  64. }
  65. self.verificationCodeView.changeDoneButtonState(enable: true)
  66. })
  67. }
  68. self.verificationCodeView.reSendAction = {[unowned self] view, sender in
  69. self.sendVerifyCode(sender: sender)
  70. }
  71. self.verificationCodeView.verificationCodeAction = { [unowned self] (view, data, codeString) in
  72. KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: codeString, verifyCodeType: view.verifyCodeType) { success, result in
  73. if success {
  74. self.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: true)
  75. } else {
  76. self.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: false)
  77. self.verificationCodeView.showAlert(result: result)
  78. }
  79. }
  80. }
  81. KMRequestServer.requestServer.reachabilityStatusChange { [weak self] status in
  82. if status == .notReachable {
  83. print("无网络")
  84. self?.networkView.isHidden = false
  85. } else {
  86. print("有网络")
  87. self?.networkView.isHidden = true
  88. }
  89. }
  90. }
  91. func sendVerifyCode(sender: NSTextView?) {
  92. KMRequestServerManager.manager.getVerifyCode(verifyCodeType: self.verificationCodeView.verifyCodeType, email: self.verificationCodeView.model.email) { [unowned self] success, result in
  93. if success {
  94. self.verificationCodeView.resetTimer()
  95. } else {
  96. self.verificationCodeView.showAlert(result: result)
  97. self.verificationCodeView.time = self.verificationCodeView.startTime
  98. self.verificationCodeView.updateLanguage()
  99. }
  100. if sender != nil {
  101. sender!.isSelectable = true
  102. }
  103. }
  104. }
  105. }