KMLoginWindowController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //
  2. // KMLoginWindowController.swift
  3. // PDF Reader Pro
  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. KMPrint("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.km_init(hex: "#FFFFFF"))
  64. KMRequestServer.requestServer.reachabilityStatusChange { [weak self] status in
  65. if status == .notReachable {
  66. KMPrint("无网络")
  67. self?.noNetworkView.isHidden = false
  68. } else {
  69. KMPrint("有网络")
  70. self?.noNetworkView.isHidden = true
  71. }
  72. }
  73. //MARK: - 左侧订阅相关
  74. self.leftView.cancelAction = { [weak self] view in
  75. self?.closeWindow()
  76. if self?.inputType == .ComparativeTable {
  77. //跳转订阅比较表
  78. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow(),.loginCancel)
  79. }
  80. }
  81. self.leftView.subscriptionAction = { [weak self] view in
  82. self?.closeWindow()
  83. //跳转订阅比较表
  84. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
  85. }
  86. //MARK: - 登录界面
  87. //登录界面点击注册按钮
  88. self.loginView.registerAction = { [weak self] (view, data) in
  89. self?.logType = .register
  90. }
  91. //登录界面点击下一步按钮
  92. self.loginView.nextAction = { [weak self] (view, data, sender) in
  93. KMRequestServerManager.manager.verificationEmail(userName: data.email, verifyEmailType: .login) { [weak self] success, result in
  94. if success {
  95. self?.model.email = data.email
  96. self?.logType = .loginInputPassword
  97. } else {
  98. self?.loginView.showAlert(result: result)
  99. }
  100. self?.loginView.changeNextButtonState(enable: true)
  101. }
  102. }
  103. //MARK: - 注册界面
  104. //注册界面点击登录按钮
  105. self.registerView.loginAction = { [weak self] (view, data) in
  106. self?.logType = .login
  107. }
  108. //注册界面点击下一步按钮
  109. self.registerView.nextAction = { [weak self] (view, data, sender) in
  110. KMRequestServerManager.manager.verificationEmail(userName: data.email, verifyEmailType: .register) { [weak self] success, result in
  111. if success {
  112. self?.verificationCodeView.inputType = .register
  113. self?.verificationCodeView.model.email = data.email
  114. self?.verificationCodeView.model.password = data.password
  115. self?.verificationCodeView.cleanTimer()
  116. self?.logType = .verificationCode
  117. self?.verificationCodeView.sendVerificationCode()
  118. } else {
  119. self?.registerView.showEmailAlert(result: result!)
  120. }
  121. self?.registerView.changeNextButtonState(enable: true)
  122. }
  123. }
  124. //注册成功界面点击完成按钮
  125. self.registerSuccessView.doneAction = { [weak self] view in
  126. self?.closeWindow()
  127. KMPrint("注册成功")
  128. //跳转订阅比较表
  129. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
  130. }
  131. self.registerSuccessView.subscribeAction = { [weak self] view in
  132. self?.closeWindow()
  133. KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow())
  134. KMPrint("注册成功")
  135. }
  136. //MARK: - 验证码界面
  137. //验证码界面点击取消按钮
  138. self.verificationCodeView.cancelAction = { [weak self] view in
  139. self?.logType = view.inputType
  140. }
  141. //验证码界面点击完成按钮
  142. self.verificationCodeView.doneAction = { [weak self] (view, data, sender) in
  143. if view.inputType == .register {
  144. KMRequestServerManager.manager.register(userName: data.email, password: data.password, verifyCode: data.verifyCode) { [weak self] (success, requestData, result) in
  145. if success {
  146. KMRequestServerManager.manager.getUserInfo { success, data, error, isLocal in
  147. if success {
  148. self?.logType = .registerSuccess
  149. } else {
  150. self?.verificationCodeView.showAlert(result: result)
  151. }
  152. }
  153. //restore 注册成功后校验权益
  154. // KMPurchaseManager.manager.restorePurchases { isSuccess, error in
  155. //
  156. // }
  157. } else {
  158. self?.verificationCodeView.showAlert(result: result)
  159. }
  160. self?.verificationCodeView.changeDoneButtonState(enable: true)
  161. }
  162. } else if view.inputType == .loginInputPassword {
  163. KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: data.verifyCode, verifyCodeType: .resetPassword) { [weak self] success, result in
  164. if success {
  165. self?.resetPasswordView.model.verifyCode = data.verifyCode
  166. self?.resetPasswordView.model.email = data.email
  167. self?.logType = .resetPassword
  168. } else {
  169. self?.verificationCodeView.showAlert(result: result)
  170. }
  171. self?.verificationCodeView.changeDoneButtonState(enable: true)
  172. }
  173. }
  174. }
  175. self.verificationCodeView.verificationCodeAction = { [weak self] (view, data, codeString) in
  176. KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: codeString, verifyCodeType: view.verifyCodeType) { [weak self] success, result in
  177. if success {
  178. self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: true)
  179. if view.inputType == .register {
  180. KMRequestServerManager.manager.register(userName: data.email, password: data.password, verifyCode: data.verifyCode) { [weak self] (success, requestData, result) in
  181. if success {
  182. KMRequestServerManager.manager.getUserInfo { [weak self] success, data, error, isLocal in
  183. if success {
  184. self?.logType = .registerSuccess
  185. } else {
  186. self?.verificationCodeView.showAlert(result: result)
  187. }
  188. }
  189. //restore 注册成功后校验权益
  190. // KMPurchaseManager.manager.restorePurchases { isSuccess, error in
  191. //
  192. // }
  193. } else {
  194. self?.verificationCodeView.showAlert(result: result)
  195. }
  196. self?.verificationCodeView.changeDoneButtonState(enable: true)
  197. }
  198. } else if view.inputType == .loginInputPassword {
  199. KMRequestServerManager.manager.verificationCode(account: data.email, verifyCode: data.verifyCode, verifyCodeType: .resetPassword) { [weak self] success, result in
  200. if success {
  201. self?.resetPasswordView.model.verifyCode = data.verifyCode
  202. self?.resetPasswordView.model.email = data.email
  203. self?.logType = .resetPassword
  204. } else {
  205. self?.verificationCodeView.showAlert(result: result)
  206. }
  207. self?.verificationCodeView.changeDoneButtonState(enable: true)
  208. }
  209. }
  210. } else {
  211. self?.verificationCodeView.updateNetworkingState(complete: true, codeIsTure: false)
  212. self?.verificationCodeView.showAlert(result: result)
  213. }
  214. }
  215. }
  216. //验证码界面点击重新发送验证码按钮
  217. self.verificationCodeView.reSendAction = { [unowned self] (view, textView) in
  218. KMRequestServerManager.manager.getVerifyCode(verifyCodeType: self.verificationCodeView.verifyCodeType, email: self.verificationCodeView.model.email) { [weak self] success, result in
  219. if success {
  220. self?.verificationCodeView.resetTimer()
  221. } else {
  222. self?.verificationCodeView.showAlert(result: result!)
  223. }
  224. textView.isSelectable = true
  225. textView.alphaValue = 1
  226. }
  227. }
  228. //MARK: - 输入密码界面
  229. //登录输入密码界面点击登录按钮
  230. self.loginInputPasswordView.loginAction = { [weak self] (view, data, sender) in
  231. KMRequestServerManager.manager.login(email: data.email, password: data.password) { [weak self] success, requestData, result in
  232. if success {
  233. KMRequestServerManager.manager.getUserInfo { success, data, error, isLocal in
  234. if success {
  235. self?.closeWindow()
  236. if self?.inputType == .ComparativeTable && (KMLightMemberManager.manager.purchaseState != .subscription &&
  237. KMLightMemberManager.manager.purchaseState != .trial) {
  238. //跳转订阅比较表
  239. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow(), .loginSuccess)
  240. }
  241. //restore 登录成功后校验权益
  242. // KMPurchaseManager.manager.restorePurchases { isSuccess, error in
  243. //
  244. // }
  245. }
  246. }
  247. } else {
  248. self?.loginInputPasswordView.showAlert(result: result)
  249. }
  250. self?.loginInputPasswordView.changeLoginButtonState(enable: true)
  251. }
  252. }
  253. //登录输入密码界面点击忘记密码按钮
  254. self.loginInputPasswordView.forgotPasswordAction = { [weak self] (view, data) in
  255. // KMRequestServerManager.manager.getVerifyCode(verifyCodeType: .resetPassword, email: data.email) { success, result in
  256. // if success {
  257. self?.verificationCodeView.inputType = .loginInputPassword
  258. self?.verificationCodeView.model.email = data.email
  259. self?.verificationCodeView.cleanTimer()
  260. self?.logType = .verificationCode
  261. self?.verificationCodeView.sendVerificationCode()
  262. // } else {
  263. // self.loginInputPasswordView.showAlert(result: result)
  264. // }
  265. // }
  266. }
  267. //登录输入密码界面点击调整邮箱按钮
  268. self.loginInputPasswordView.changeEmailAction = { [weak self] view in
  269. self?.logType = .login
  270. }
  271. //MARK: - 重置密码界面
  272. //重置密码界面点击完成按钮
  273. self.resetPasswordView.doneAction = { [weak self] (view, data, sender) in
  274. KMRequestServerManager.manager.resetPassword(account: data.email, firstPassword: data.password, secondPassword: data.rePassword, verifyCode: data.verifyCode) {[weak self] (success, result) in
  275. if success {
  276. self?.model.email = data.email
  277. self?.logType = .login
  278. } else {
  279. self?.verificationCodeView.showAlert(result: result!)
  280. }
  281. self?.resetPasswordView.changeDoneButtonState(enable: true)
  282. }
  283. }
  284. self.reloadData()
  285. }
  286. func reloadData() {
  287. guard self.rightContentView != nil else {
  288. return
  289. }
  290. for view in self.rightContentView.subviews {
  291. view.isHidden = true
  292. }
  293. self.leftView.logType = self.logType
  294. switch self.logType {
  295. case .login:
  296. self.loginView.isHidden = false
  297. self.loginView.model.email = self.model.email
  298. self.window?.makeFirstResponder(self.loginView.emailTextField)
  299. case .register:
  300. self.registerView.isHidden = false
  301. self.window?.makeFirstResponder(self.registerView.emailTextField)
  302. case .resetPassword:
  303. self.resetPasswordView.resetData()
  304. self.resetPasswordView.email = self.model.email
  305. self.resetPasswordView.isHidden = false
  306. self.window?.makeFirstResponder(self.resetPasswordView.passwordSecureTextField)
  307. case .verificationCode:
  308. self.verificationCodeView.isHidden = false
  309. self.window?.makeFirstResponder(self.verificationCodeView.code1TextField.textField)
  310. case .registerSuccess:
  311. self.registerSuccessView.isHidden = false
  312. case .loginInputPassword:
  313. self.loginInputPasswordView.email = self.model.email
  314. self.loginInputPasswordView.isHidden = false
  315. if self.loginInputPasswordView.model.secure {
  316. self.window?.makeFirstResponder(self.loginInputPasswordView.passwordSecureTextField)
  317. } else {
  318. self.window?.makeFirstResponder(self.loginInputPasswordView.passwordTextField)
  319. }
  320. default:
  321. break
  322. }
  323. }
  324. func closeWindow() {
  325. if loginWindowController != nil && loginMainWindow != nil {
  326. loginWindowController!.window!.close()
  327. loginMainWindow?.endSheet(loginWindowController!.window!)
  328. }
  329. loginWindowController = nil
  330. loginMainWindow = nil
  331. }
  332. static func fetchSampleController() -> KMLoginWindowController? {
  333. for window in NSApp.windows {
  334. let controller = window.windowController
  335. if controller is KMLoginWindowController {
  336. return controller as! KMLoginWindowController
  337. }
  338. }
  339. return nil
  340. }
  341. }
  342. protocol KMLoginWindowControllerNetworking: KMLoginWindowController {}
  343. extension KMLoginWindowController: KMLoginWindowControllerNetworking {
  344. }