KMVerficationCodeWindowController.swift 4.8 KB

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