KMLoginWindowController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // KMLoginWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/22.
  6. //
  7. import Cocoa
  8. var loginWindowController: KMLoginWindowController?
  9. var loginMainWindow: NSWindow?
  10. class KMLoginWindowController: NSWindowController {
  11. @IBOutlet weak var noNetworkView: KMLightNoNetworkView!
  12. @IBOutlet weak var loginView: KMLoginView!
  13. @IBOutlet weak var leftView: KMLoginLeftImageView!
  14. @IBOutlet weak var registerView: KMRegisterView!
  15. @IBOutlet weak var verificationCodeView: KMVerificationCodeView!
  16. @IBOutlet weak var loginInputPasswordView: KMLoginInputPasswordView!
  17. @IBOutlet weak var registerSuccessView: KMRegisterSuccessView!
  18. @IBOutlet weak var resetPasswordView: KMResetPasswordView!
  19. @IBOutlet weak var rightContentView: NSView!
  20. var inputType: DataNavigationViewButtonActionType?
  21. var model: KMRegisterModel = KMRegisterModel()
  22. var currentView: NSView?
  23. var logType: KMRegisterLogType = .login {
  24. didSet {
  25. self.reloadData()
  26. }
  27. }
  28. deinit {
  29. print("KMLoginWindowController 释放")
  30. }
  31. override func windowDidLoad() {
  32. super.windowDidLoad()
  33. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  34. self.setup()
  35. }
  36. //MARK: 打开文件
  37. static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMLoginWindowController? {
  38. var logType = logType
  39. // let count = UserDefaults.standard.value(forKey: "kFirstOpenAppCount")
  40. //
  41. // if (count as? Int == 1) {
  42. // if logType == .login {
  43. // logType = .register
  44. // }
  45. // }
  46. let controller: KMLoginWindowController = KMLoginWindowController.init(windowNibName: "KMLoginWindowController")
  47. controller.inputType = type
  48. controller.logType = logType
  49. window.beginSheet(controller.window!)
  50. loginMainWindow = window
  51. loginWindowController = controller
  52. return controller
  53. }
  54. override func mouseDown(with event: NSEvent) {
  55. super.mouseDown(with: event)
  56. self.window?.makeFirstResponder(self)
  57. }
  58. func setup() {
  59. self.window?.contentView?.backgroundColor(NSColor(hex: "#FFFFFF"))
  60. KMRequestServer.requestServer.reachabilityStatusChange { [weak self] status in
  61. if status == .notReachable {
  62. print("无网络")
  63. self?.noNetworkView.isHidden = false
  64. } else {
  65. print("有网络")
  66. self?.noNetworkView.isHidden = true
  67. }
  68. }
  69. //MARK: - 左侧订阅相关
  70. self.leftView.cancelAction = { view in
  71. loginMainWindow?.endSheet(view.window!)
  72. view.window!.close()
  73. loginWindowController = nil
  74. loginMainWindow = nil
  75. }
  76. self.leftView.subscriptionAction = { view in
  77. loginMainWindow?.endSheet(view.window!)
  78. view.window!.close()
  79. loginWindowController = nil
  80. loginMainWindow = nil
  81. //跳转订阅比较表
  82. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
  83. }
  84. //MARK: - 登录界面
  85. //登录界面点击注册按钮
  86. self.loginView.registerAction = { [weak self] (view, data) in
  87. self?.logType = .register
  88. }
  89. //登录界面点击下一步按钮
  90. self.loginView.nextAction = { [unowned self] (view, data, sender) in
  91. KMRequestServerManager.manager.verificationEmail(userName: data.email, verifyEmailType: .login) { [weak self] success, result in
  92. if success {
  93. self?.model.email = data.email
  94. self?.logType = .loginInputPassword
  95. } else {
  96. self?.loginView.showAlert(result: result)
  97. }
  98. self?.loginView.changeNextButtonState(enable: true)
  99. }
  100. }
  101. //MARK: - 注册界面
  102. //注册界面点击登录按钮
  103. self.registerView.loginAction = { [unowned self] (view, data) in
  104. self.logType = .login
  105. }
  106. //注册界面点击下一步按钮
  107. self.registerView.nextAction = { [unowned self] (view, data, sender) in
  108. KMRequestServerManager.manager.verificationEmail(userName: data.email, verifyEmailType: .register) { [weak self] success, result in
  109. if success {
  110. self?.verificationCodeView.inputType = .register
  111. self?.verificationCodeView.model.email = data.email
  112. self?.verificationCodeView.model.password = data.password
  113. self?.verificationCodeView.cleanTimer()
  114. self?.logType = .verificationCode
  115. self?.verificationCodeView.sendVerificationCode()
  116. } else {
  117. self?.registerView.showEmailAlert(result: result!)
  118. }
  119. self?.registerView.changeNextButtonState(enable: true)
  120. }
  121. }
  122. //注册成功界面点击完成按钮
  123. self.registerSuccessView.doneAction = { [unowned self] view in
  124. loginMainWindow?.endSheet(view.window!)
  125. view.window!.close()
  126. loginWindowController = nil
  127. loginMainWindow = nil
  128. print("注册成功")
  129. }
  130. //MARK: - 验证码界面
  131. //验证码界面点击取消按钮
  132. self.verificationCodeView.cancelAction = { [unowned self] view in
  133. self.logType = view.inputType
  134. }
  135. //验证码界面点击完成按钮
  136. self.verificationCodeView.doneAction = { [unowned self] (view, data, sender) in
  137. if view.inputType == .register {
  138. KMRequestServerManager.manager.register(userName: data.email, password: data.password, verifyCode: data.verifyCode) { [weak self] (success, requestData, result) in
  139. if success {
  140. KMRequestServerManager.manager.getUserInfo { success, data, error, isLocal in
  141. if success {
  142. self?.logType = .registerSuccess
  143. } else {
  144. self?.verificationCodeView.showAlert(result: result)
  145. }
  146. }
  147. } else {
  148. self?.verificationCodeView.showAlert(result: result)
  149. }
  150. self?.verificationCodeView.changeDoneButtonState(enable: true)
  151. }
  152. } else if view.inputType == .loginInputPassword {
  153. KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: data.verifyCode, verifyCodeType: .resetPassword) { [weak self] success, result in
  154. if success {
  155. self?.resetPasswordView.model.verifyCode = data.verifyCode
  156. self?.resetPasswordView.model.email = data.email
  157. self?.logType = .resetPassword
  158. } else {
  159. self?.verificationCodeView.showAlert(result: result)
  160. }
  161. self?.verificationCodeView.changeDoneButtonState(enable: true)
  162. }
  163. }
  164. }
  165. self.verificationCodeView.verificationCodeAction = { [unowned self] (view, data, codeString) in
  166. KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: codeString, verifyCodeType: view.verifyCodeType) { [weak self] success, result in
  167. if success {
  168. self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: true)
  169. if view.inputType == .register {
  170. KMRequestServerManager.manager.register(userName: data.email, password: data.password, verifyCode: data.verifyCode) { [weak self] (success, requestData, result) in
  171. if success {
  172. KMRequestServerManager.manager.getUserInfo { [weak self] success, data, error, isLocal in
  173. if success {
  174. self?.logType = .registerSuccess
  175. } else {
  176. self?.verificationCodeView.showAlert(result: result)
  177. }
  178. }
  179. } else {
  180. self?.verificationCodeView.showAlert(result: result)
  181. }
  182. self?.verificationCodeView.changeDoneButtonState(enable: true)
  183. }
  184. } else if view.inputType == .loginInputPassword {
  185. KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: data.verifyCode, verifyCodeType: .resetPassword) { [weak self] success, result in
  186. if success {
  187. self?.resetPasswordView.model.verifyCode = data.verifyCode
  188. self?.resetPasswordView.model.email = data.email
  189. self?.logType = .resetPassword
  190. } else {
  191. self?.verificationCodeView.showAlert(result: result)
  192. }
  193. self?.verificationCodeView.changeDoneButtonState(enable: true)
  194. }
  195. }
  196. } else {
  197. self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: false)
  198. self?.verificationCodeView.showAlert(result: result)
  199. }
  200. }
  201. }
  202. //验证码界面点击重新发送验证码按钮
  203. self.verificationCodeView.reSendAction = { [unowned self] (view, textView) in
  204. KMRequestServerManager.manager.getVerifyCode(verifyCodeType: self.verificationCodeView.verifyCodeType, email: self.verificationCodeView.model.email) { [weak self] success, result in
  205. if success {
  206. self?.verificationCodeView.resetTimer()
  207. } else {
  208. self?.verificationCodeView.showAlert(result: result!)
  209. }
  210. textView.isSelectable = true
  211. textView.alphaValue = 1
  212. }
  213. }
  214. //MARK: - 输入密码界面
  215. //登录输入密码界面点击登录按钮
  216. self.loginInputPasswordView.loginAction = { [unowned self] (view, data, sender) in
  217. KMRequestServerManager.manager.login(email: data.email, password: data.password) { [weak self] success, requestData, result in
  218. if success {
  219. KMRequestServerManager.manager.getUserInfo { success, data, error, isLocal in
  220. if success {
  221. view.window!.close()
  222. loginMainWindow?.endSheet(view.window!)
  223. loginWindowController = nil
  224. loginMainWindow = nil
  225. }
  226. }
  227. } else {
  228. self?.loginInputPasswordView.showAlert(result: result)
  229. }
  230. self?.loginInputPasswordView.changeLoginButtonState(enable: true)
  231. }
  232. }
  233. //登录输入密码界面点击忘记密码按钮
  234. self.loginInputPasswordView.forgotPasswordAction = { [unowned self] (view, data) in
  235. // KMRequestServerManager.manager.getVerifyCode(verifyCodeType: .resetPassword, email: data.email) { success, result in
  236. // if success {
  237. self.verificationCodeView.inputType = .loginInputPassword
  238. self.verificationCodeView.model.email = data.email
  239. self.verificationCodeView.cleanTimer()
  240. self.logType = .verificationCode
  241. self.verificationCodeView.sendVerificationCode()
  242. // } else {
  243. // self.loginInputPasswordView.showAlert(result: result)
  244. // }
  245. // }
  246. }
  247. //登录输入密码界面点击调整邮箱按钮
  248. self.loginInputPasswordView.changeEmailAction = { [unowned self] view in
  249. self.logType = .login
  250. }
  251. //MARK: - 重置密码界面
  252. //重置密码界面点击完成按钮
  253. self.resetPasswordView.doneAction = { [unowned self] (view, data, sender) in
  254. KMRequestServerManager.manager.resetPassword(account: data.email, firstPassword: data.password, secondPassword: data.rePassword, verifyCode: data.verifyCode) {[weak self] (success, result) in
  255. if success {
  256. self?.model.email = data.email
  257. self?.logType = .login
  258. } else {
  259. self?.verificationCodeView.showAlert(result: result!)
  260. }
  261. self?.resetPasswordView.changeDoneButtonState(enable: true)
  262. }
  263. }
  264. self.reloadData()
  265. }
  266. func reloadData() {
  267. guard self.rightContentView != nil else {
  268. return
  269. }
  270. for view in self.rightContentView.subviews {
  271. view.isHidden = true
  272. }
  273. switch self.logType {
  274. case .login:
  275. self.loginView.isHidden = false
  276. self.loginView.model.email = self.model.email
  277. self.window?.makeFirstResponder(self.loginView.emailTextField)
  278. case .register:
  279. self.registerView.isHidden = false
  280. self.window?.makeFirstResponder(self.registerView.emailTextField)
  281. case .resetPassword:
  282. self.resetPasswordView.resetData()
  283. self.resetPasswordView.email = self.model.email
  284. self.resetPasswordView.isHidden = false
  285. self.window?.makeFirstResponder(self.resetPasswordView.passwordSecureTextField)
  286. case .verificationCode:
  287. self.verificationCodeView.isHidden = false
  288. self.window?.makeFirstResponder(self.verificationCodeView.code1TextField.textField)
  289. case .registerSuccess:
  290. self.registerSuccessView.isHidden = false
  291. case .loginInputPassword:
  292. self.loginInputPasswordView.email = self.model.email
  293. self.loginInputPasswordView.isHidden = false
  294. if self.loginInputPasswordView.model.secure {
  295. self.window?.makeFirstResponder(self.loginInputPasswordView.passwordSecureTextField)
  296. } else {
  297. self.window?.makeFirstResponder(self.loginInputPasswordView.passwordTextField)
  298. }
  299. default:
  300. break
  301. }
  302. }
  303. }
  304. protocol KMLoginWindowControllerNetworking: KMLoginWindowController {}
  305. extension KMLoginWindowController: KMLoginWindowControllerNetworking {
  306. }