123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- //
- // KMLoginWindowController.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/2/22.
- //
- import Cocoa
- var loginWindowController: KMLoginWindowController?
- var loginMainWindow: NSWindow?
- typealias KMLoginWindowControllerLoginAction = (_ controller: KMLoginWindowController, _ isSuccess: Bool) -> Void
- typealias KMLoginWindowControllerRegisterAction = (_ controller: KMLoginWindowController, _ isSuccess: Bool) -> Void
- class KMLoginWindowController: NSWindowController {
- @IBOutlet weak var noNetworkView: KMLightNoNetworkView!
- @IBOutlet weak var loginView: KMLoginView!
- @IBOutlet weak var leftView: KMLoginLeftImageView!
- @IBOutlet weak var registerView: KMRegisterView!
- @IBOutlet weak var verificationCodeView: KMVerificationCodeView!
-
- @IBOutlet weak var loginInputPasswordView: KMLoginInputPasswordView!
- @IBOutlet weak var registerSuccessView: KMRegisterSuccessView!
-
- @IBOutlet weak var resetPasswordView: KMResetPasswordView!
-
- @IBOutlet weak var rightContentView: NSView!
-
- var loginAction: KMLoginWindowControllerLoginAction?
- var registerAction: KMLoginWindowControllerRegisterAction?
-
- var inputType: DataNavigationViewButtonActionType?
- var model: KMRegisterModel = KMRegisterModel()
-
- var currentView: NSView?
- var logType: KMRegisterLogType = .login {
- didSet {
- self.reloadData()
- }
- }
-
- deinit {
- KMPrint("KMLoginWindowController 释放")
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
- self.setup()
- }
-
- //MARK: 打开文件
- static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMLoginWindowController? {
-
- var logType = logType
- // let count = UserDefaults.standard.value(forKey: "kFirstOpenAppCount")
- //
- // if (count as? Int == 1) {
- // if logType == .login {
- // logType = .register
- // }
- // }
-
- let controller: KMLoginWindowController = KMLoginWindowController.init(windowNibName: "KMLoginWindowController")
- controller.inputType = type
- controller.logType = logType
- window.beginSheet(controller.window!)
- loginMainWindow = window
- loginWindowController = controller
- return controller
- }
-
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
- self.window?.makeFirstResponder(self)
- }
-
- func setup() {
- self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
- KMRequestServer.requestServer.reachabilityStatusChange { [weak self] status in
- if status == .notReachable {
- KMPrint("无网络")
- self?.noNetworkView.isHidden = false
- } else {
- KMPrint("有网络")
- self?.noNetworkView.isHidden = true
- }
- }
-
- //MARK: - 左侧订阅相关
- self.leftView.cancelAction = { [weak self] view in
- self?.closeWindow()
-
- if self?.inputType == .ComparativeTable {
- //跳转订阅比较表
- let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow(),.loginCancel)
- }
- }
-
- self.leftView.subscriptionAction = { [weak self] view in
- self?.closeWindow()
-
- //跳转订阅比较表
- let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
- }
-
- //MARK: - 登录界面
- //登录界面点击注册按钮
- self.loginView.registerAction = { [weak self] (view, data) in
- self?.logType = .register
- }
- //登录界面点击下一步按钮
- self.loginView.nextAction = { [weak self] (view, data, sender) in
- KMRequestServerManager.manager.verificationEmail(userName: data.email, verifyEmailType: .login) { [weak self] success, result in
- if success {
- self?.model.email = data.email
- self?.logType = .loginInputPassword
- } else {
- self?.loginView.showAlert(result: result)
- }
- self?.loginView.changeNextButtonState(enable: true)
- }
- }
-
- //MARK: - 注册界面
- //注册界面点击登录按钮
- self.registerView.loginAction = { [weak self] (view, data) in
- self?.logType = .login
- }
- //注册界面点击下一步按钮
- self.registerView.nextAction = { [weak self] (view, data, sender) in
- KMRequestServerManager.manager.verificationEmail(userName: data.email, verifyEmailType: .register) { [weak self] success, result in
- if success {
- self?.verificationCodeView.inputType = .register
- self?.verificationCodeView.model.email = data.email
- self?.verificationCodeView.model.password = data.password
- self?.verificationCodeView.cleanTimer()
- self?.logType = .verificationCode
- self?.verificationCodeView.sendVerificationCode()
- } else {
- self?.registerView.showEmailAlert(result: result!)
- }
- self?.registerView.changeNextButtonState(enable: true)
- }
- }
-
- //注册成功界面点击完成按钮
- self.registerSuccessView.doneAction = { [weak self] view in
- self?.closeWindow()
- KMPrint("注册成功")
- //跳转订阅比较表
- let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
- }
-
- self.registerSuccessView.subscribeAction = { [weak self] view in
- self?.closeWindow()
- KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
- KMPrint("注册成功")
- }
-
- //MARK: - 验证码界面
- //验证码界面点击取消按钮
- self.verificationCodeView.cancelAction = { [weak self] view in
- self?.logType = view.inputType
- }
-
- //验证码界面点击完成按钮
- self.verificationCodeView.doneAction = { [weak self] (view, data, sender) in
- if view.inputType == .register {
- KMRequestServerManager.manager.register(userName: data.email, password: data.password, verifyCode: data.verifyCode) { [weak self] (success, requestData, result) in
- if success {
- KMRequestServerManager.manager.getUserInfo { success, data, error, isLocal in
- if success {
- self?.logType = .registerSuccess
- } else {
- self?.verificationCodeView.showAlert(result: result)
- }
- }
-
- //restore 注册成功后校验权益
- // KMPurchaseManager.manager.restorePurchases { isSuccess, error in
- //
- // }
- } else {
- self?.verificationCodeView.showAlert(result: result)
- }
- self?.verificationCodeView.changeDoneButtonState(enable: true)
- }
- } else if view.inputType == .loginInputPassword {
- KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: data.verifyCode, verifyCodeType: .resetPassword) { [weak self] success, result in
- if success {
- self?.resetPasswordView.model.verifyCode = data.verifyCode
- self?.resetPasswordView.model.email = data.email
- self?.logType = .resetPassword
- } else {
- self?.verificationCodeView.showAlert(result: result)
- }
- self?.verificationCodeView.changeDoneButtonState(enable: true)
- }
- }
- }
-
- self.verificationCodeView.verificationCodeAction = { [weak self] (view, data, codeString) in
- KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: codeString, verifyCodeType: view.verifyCodeType) { [weak self] success, result in
- if success {
- self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: true)
- if view.inputType == .register {
- KMRequestServerManager.manager.register(userName: data.email, password: data.password, verifyCode: data.verifyCode) { [weak self] (success, requestData, result) in
- if success {
- KMRequestServerManager.manager.getUserInfo { [weak self] success, data, error, isLocal in
- if success {
- self?.logType = .registerSuccess
- } else {
- self?.verificationCodeView.showAlert(result: result)
- }
- }
-
- //restore 注册成功后校验权益
- // KMPurchaseManager.manager.restorePurchases { isSuccess, error in
- //
- // }
- } else {
- self?.verificationCodeView.showAlert(result: result)
- }
- self?.verificationCodeView.changeDoneButtonState(enable: true)
- }
- } else if view.inputType == .loginInputPassword {
- KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: data.verifyCode, verifyCodeType: .resetPassword) { [weak self] success, result in
- if success {
- self?.resetPasswordView.model.verifyCode = data.verifyCode
- self?.resetPasswordView.model.email = data.email
- self?.logType = .resetPassword
- } else {
- self?.verificationCodeView.showAlert(result: result)
- }
- self?.verificationCodeView.changeDoneButtonState(enable: true)
- }
- }
- } else {
- self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: false)
- self?.verificationCodeView.showAlert(result: result)
- }
- }
- }
-
- //验证码界面点击重新发送验证码按钮
- self.verificationCodeView.reSendAction = { [unowned self] (view, textView) in
- KMRequestServerManager.manager.getVerifyCode(verifyCodeType: self.verificationCodeView.verifyCodeType, email: self.verificationCodeView.model.email) { [weak self] success, result in
- if success {
- self?.verificationCodeView.resetTimer()
- } else {
- self?.verificationCodeView.showAlert(result: result!)
- }
- textView.isSelectable = true
- textView.alphaValue = 1
- }
- }
-
-
- //MARK: - 输入密码界面
- //登录输入密码界面点击登录按钮
- self.loginInputPasswordView.loginAction = { [weak self] (view, data, sender) in
- KMRequestServerManager.manager.login(email: data.email, password: data.password) { [weak self] success, requestData, result in
- if success {
- KMRequestServerManager.manager.getUserInfo { success, data, error, isLocal in
- if success {
- self?.closeWindow()
- if self?.inputType == .ComparativeTable && (KMLightMemberManager.manager.purchaseState != .subscription &&
- KMLightMemberManager.manager.purchaseState != .trial) {
- //跳转订阅比较表
- let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow(), .loginSuccess)
- }
- //restore 登录成功后校验权益
- // KMPurchaseManager.manager.restorePurchases { isSuccess, error in
- //
- // }
- }
- }
- } else {
- self?.loginInputPasswordView.showAlert(result: result)
- }
- self?.loginInputPasswordView.changeLoginButtonState(enable: true)
- }
- }
-
- //登录输入密码界面点击忘记密码按钮
- self.loginInputPasswordView.forgotPasswordAction = { [weak self] (view, data) in
- // KMRequestServerManager.manager.getVerifyCode(verifyCodeType: .resetPassword, email: data.email) { success, result in
- // if success {
- self?.verificationCodeView.inputType = .loginInputPassword
- self?.verificationCodeView.model.email = data.email
- self?.verificationCodeView.cleanTimer()
- self?.logType = .verificationCode
- self?.verificationCodeView.sendVerificationCode()
- // } else {
- // self.loginInputPasswordView.showAlert(result: result)
- // }
- // }
- }
-
- //登录输入密码界面点击调整邮箱按钮
- self.loginInputPasswordView.changeEmailAction = { [weak self] view in
- self?.logType = .login
- }
-
- //MARK: - 重置密码界面
- //重置密码界面点击完成按钮
- self.resetPasswordView.doneAction = { [weak self] (view, data, sender) in
- KMRequestServerManager.manager.resetPassword(account: data.email, firstPassword: data.password, secondPassword: data.rePassword, verifyCode: data.verifyCode) {[weak self] (success, result) in
- if success {
- self?.model.email = data.email
- self?.logType = .login
- } else {
- self?.verificationCodeView.showAlert(result: result!)
- }
- self?.resetPasswordView.changeDoneButtonState(enable: true)
- }
- }
-
- self.reloadData()
- }
-
- func reloadData() {
- guard self.rightContentView != nil else {
- return
- }
-
- for view in self.rightContentView.subviews {
- view.isHidden = true
- }
-
- self.leftView.logType = self.logType
-
- switch self.logType {
- case .login:
- self.loginView.isHidden = false
- self.loginView.model.email = self.model.email
- self.window?.makeFirstResponder(self.loginView.emailTextField)
- case .register:
- self.registerView.isHidden = false
- self.window?.makeFirstResponder(self.registerView.emailTextField)
- case .resetPassword:
- self.resetPasswordView.resetData()
- self.resetPasswordView.email = self.model.email
- self.resetPasswordView.isHidden = false
- self.window?.makeFirstResponder(self.resetPasswordView.passwordSecureTextField)
- case .verificationCode:
- self.verificationCodeView.isHidden = false
- self.window?.makeFirstResponder(self.verificationCodeView.code1TextField.textField)
- case .registerSuccess:
- self.registerSuccessView.isHidden = false
- case .loginInputPassword:
- self.loginInputPasswordView.email = self.model.email
- self.loginInputPasswordView.isHidden = false
- if self.loginInputPasswordView.model.secure {
- self.window?.makeFirstResponder(self.loginInputPasswordView.passwordSecureTextField)
- } else {
- self.window?.makeFirstResponder(self.loginInputPasswordView.passwordTextField)
- }
- default:
- break
- }
- }
-
- func closeWindow() {
- if loginWindowController != nil && loginMainWindow != nil {
- loginWindowController!.window!.close()
- loginMainWindow?.endSheet(loginWindowController!.window!)
- }
-
- loginWindowController = nil
- loginMainWindow = nil
- }
-
- static func fetchSampleController() -> KMLoginWindowController? {
- for window in NSApp.windows {
- let controller = window.windowController
- if controller is KMLoginWindowController {
- return controller as! KMLoginWindowController
- }
- }
-
- return nil
- }
- }
- protocol KMLoginWindowControllerNetworking: KMLoginWindowController {}
- extension KMLoginWindowController: KMLoginWindowControllerNetworking {
-
- }
|