KMLoginWindowController.swift 16 KB

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