KMVerficationCodeWindowController.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. KMPrint("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.km_init(hex: "#FFFFFF"))
  39. self.verificationCodeView.closeAction = { view in
  40. KMPrint("关闭")
  41. verficationCodeMainWindow?.endSheet(view.window!)
  42. view.window?.close()
  43. verficationCodeController = nil
  44. verficationCodeMainWindow = nil
  45. }
  46. self.verificationCodeView.cancelAction = { view in
  47. KMPrint("登出")
  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. KMPrint("注销")
  55. KMRequestServerManager.manager.logOff(verifyCode: data.verifyCode, complete: { [weak self] success, result in
  56. if self?.verificationCodeView != nil {
  57. if success {
  58. verficationCodeMainWindow?.endSheet(view.window!)
  59. view.window?.close()
  60. verficationCodeController = nil
  61. verficationCodeMainWindow = nil
  62. self?.didCancellation?()
  63. } else {
  64. self?.verificationCodeView.showAlert(result: result)
  65. }
  66. self?.verificationCodeView.changeDoneButtonState(enable: true)
  67. }
  68. })
  69. }
  70. self.verificationCodeView.reSendAction = {[weak self] view, sender in
  71. self?.sendVerifyCode(sender: sender)
  72. }
  73. self.verificationCodeView.verificationCodeAction = { [weak self] (view, data, codeString) in
  74. KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: codeString, verifyCodeType: view.verifyCodeType) { success, result in
  75. if self?.verificationCodeView != nil {
  76. if success {
  77. self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: true)
  78. } else {
  79. self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: false)
  80. self?.verificationCodeView.showAlert(result: result)
  81. }
  82. }
  83. }
  84. }
  85. KMRequestServer.requestServer.reachabilityStatusChange { [weak self] status in
  86. if status == .notReachable {
  87. KMPrint("无网络")
  88. self?.networkView.isHidden = false
  89. } else {
  90. KMPrint("有网络")
  91. self?.networkView.isHidden = true
  92. }
  93. }
  94. }
  95. func sendVerifyCode(sender: NSTextView?) {
  96. KMRequestServerManager.manager.getVerifyCode(verifyCodeType: self.verificationCodeView.verifyCodeType, email: self.verificationCodeView.model.email) { [weak self] success, result in
  97. if self?.verificationCodeView != nil {
  98. if success {
  99. self?.verificationCodeView.resetTimer()
  100. } else {
  101. self?.verificationCodeView.showAlert(result: result)
  102. self?.verificationCodeView.time = (self?.verificationCodeView!.startTime)!
  103. self?.verificationCodeView.updateLanguage()
  104. }
  105. if sender != nil {
  106. sender!.isSelectable = true
  107. }
  108. }
  109. }
  110. }
  111. }