// // KMAccountInfoWindowController.swift // PDF Reader Pro // // Created by lizhe on 2023/2/24. // import Cocoa var accountInfoController: KMAccountInfoWindowController? var accountInfoMainWindow: NSWindow? typealias KMAccountInfoWindowControllerCancellationAction = (_ controller: KMAccountInfoWindowController) -> Void class KMAccountInfoWindowController: NSWindowController { @IBOutlet weak var accountInfoView: KMAccountInfoView! @IBOutlet weak var accountViewHeightConstraint: NSLayoutConstraint! var inputType: DataNavigationViewButtonActionType? var cancellAtionAction: KMAccountInfoWindowControllerCancellationAction? deinit { KMPrint("KMAccountInfoWindowController 释放") } 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() self.reloadData() } //MARK: 打开文件 static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMAccountInfoWindowController { if KMAccountInfoWindowController.isSampleController() { return accountInfoController ?? KMAccountInfoWindowController() } else { let controller: KMAccountInfoWindowController = KMAccountInfoWindowController.init(windowNibName: "KMAccountInfoWindowController") controller.inputType = type controller.cancellAtionAction = { controller in let verficationCodeWindowController = KMVerficationCodeWindowController.show(window: controller.window!) verficationCodeWindowController.didCancellation = { accountInfoMainWindow?.endSheet(controller.window!) controller.window?.close() if accountInfoMainWindow != nil { let cancellationWindowController = KMCancellationWindowController.show(window: accountInfoMainWindow!) } accountInfoController = nil accountInfoMainWindow = nil } } window.beginSheet(controller.window!) accountInfoController = controller accountInfoMainWindow = window return controller } } static func isSampleController() -> Bool { for window in NSApp.windows { let controller = window.windowController if controller is KMAccountInfoWindowController { return true } } return false } static func fetchSampleController() -> KMAccountInfoWindowController? { for window in NSApp.windows { let controller = window.windowController if controller is KMAccountInfoWindowController { return controller as! KMAccountInfoWindowController } } return nil } func setup() { self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "#FFFFFF")) self.accountInfoView.closeAction = { view in KMPrint("关闭") accountInfoMainWindow?.endSheet(view.window!) view.window?.close() accountInfoController = nil accountInfoMainWindow = nil } self.accountInfoView.logOutAction = { view in KMPrint("登出") KMRequestServerManager.manager.logout { success, result in if success { KMPrint("登出成功") } } KMLightMemberManager.manager.logOut() accountInfoMainWindow?.endSheet(view.window!) view.window?.close() accountInfoController = nil accountInfoMainWindow = nil } self.accountInfoView.cancellationAction = { [unowned self] view in KMPrint("注销") if KMLightMemberManager.manager.purchaseState == .subscription || KMLightMemberManager.manager.purchaseState == .trial { let alert = NSAlert() alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "") alert.informativeText = NSLocalizedString("You are still in the subscription and cannot cancel account. Please click Cancel Account after the subscription period ends.", comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) alert.beginSheetModal(for: view.window!) { [unowned self] result in } } else { let alert = NSAlert() alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "") alert.informativeText = NSLocalizedString("Cancellation cannot be withdrawn, it will be completed within 3 working days, until then your account will be frozen and cannot be logged in, are you sure you want to delete your account?", comment: "") alert.addButton(withTitle: NSLocalizedString("No", comment: "")) alert.addButton(withTitle: NSLocalizedString("Yes", comment: "")) alert.beginSheetModal(for: view.window!) { [unowned self] result in if (result == .alertFirstButtonReturn) { /// 取消 return } else if result == .alertSecondButtonReturn { guard let callBack = cancellAtionAction else { return } // accountInfoMainWindow?.endSheet(view.window!) // view.window?.close() // accountInfoController = nil // accountInfoMainWindow = nil callBack(self) } } } } self.accountInfoView.purchaseInfoAction = { view in KMPurchaseManager.manager.showPurchasesInfo() } self.accountInfoView.subcriptionAction = { view in #if VERSION_DMG KMPurchaseManager.manager.purchaseProduct(productIdentifier: "") { isSuccess, error in } #endif #if VERSION_FREE accountInfoMainWindow?.endSheet(view.window!) view.window?.close() accountInfoController = nil accountInfoMainWindow = nil //跳转订阅比较表 let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow(), .accountInfo) #endif } } func reloadData() { let state = KMDMGPurchaseManager.manager.state if state == .unknow { self.accountViewHeightConstraint.constant = 274 #if VERSION_DMG if KMLightMemberManager.manager.existOrderInfo() { self.accountViewHeightConstraint.constant = 274 + 44 } #endif } else if state == .subscription || state == .trial { self.accountViewHeightConstraint.constant = 411 - 60 #if VERSION_DMG if KMLightMemberManager.manager.info.subscriptionInfoList[0].payType == 1 { self.accountViewHeightConstraint.constant = 483 - 52 - 20 } else { self.accountViewHeightConstraint.constant = 483 } #endif } else if state == .subscriptionExpired || state == .trialExpired { self.accountViewHeightConstraint.constant = 483 - 52 #if VERSION_DMG self.accountViewHeightConstraint.constant = 483 #endif } self.accountInfoView.purchaseState = state KMRequestServerManager.manager.getUserInfo { [weak self] success, data, error, isLocal in if success { self?.accountInfoView.userInfo = data KMLightMemberManager.manager.reloadUserInfo() } else { if error?.code == 304 { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { if self?.window != nil { accountInfoMainWindow?.endSheet((self?.window)!) } self?.window?.close() KMLightMemberManager.manager.logOut() accountInfoController = nil accountInfoMainWindow = nil } } } } } }