KMAccountInfoWindowController.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // KMAccountInfoWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/24.
  6. //
  7. import Cocoa
  8. var accountInfoController: KMAccountInfoWindowController?
  9. var accountInfoMainWindow: NSWindow?
  10. typealias KMAccountInfoWindowControllerCancellationAction = (_ controller: KMAccountInfoWindowController) -> Void
  11. class KMAccountInfoWindowController: NSWindowController {
  12. @IBOutlet weak var accountInfoView: KMAccountInfoView!
  13. @IBOutlet weak var accountViewHeightConstraint: NSLayoutConstraint!
  14. var inputType: DataNavigationViewButtonActionType?
  15. var cancellAtionAction: KMAccountInfoWindowControllerCancellationAction?
  16. deinit {
  17. KMPrint("KMAccountInfoWindowController 释放")
  18. }
  19. override func windowDidLoad() {
  20. super.windowDidLoad()
  21. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  22. self.setup()
  23. self.reloadData()
  24. }
  25. //MARK: 打开文件
  26. static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMAccountInfoWindowController {
  27. if KMAccountInfoWindowController.isSampleController() {
  28. return accountInfoController ?? KMAccountInfoWindowController()
  29. } else {
  30. let controller: KMAccountInfoWindowController = KMAccountInfoWindowController.init(windowNibName: "KMAccountInfoWindowController")
  31. controller.inputType = type
  32. controller.cancellAtionAction = { controller in
  33. let verficationCodeWindowController = KMVerficationCodeWindowController.show(window: controller.window!)
  34. verficationCodeWindowController.didCancellation = {
  35. accountInfoMainWindow?.endSheet(controller.window!)
  36. controller.window?.close()
  37. if accountInfoMainWindow != nil {
  38. let cancellationWindowController = KMCancellationWindowController.show(window: accountInfoMainWindow!)
  39. }
  40. accountInfoController = nil
  41. accountInfoMainWindow = nil
  42. }
  43. }
  44. window.beginSheet(controller.window!)
  45. accountInfoController = controller
  46. accountInfoMainWindow = window
  47. return controller
  48. }
  49. }
  50. static func isSampleController() -> Bool {
  51. for window in NSApp.windows {
  52. let controller = window.windowController
  53. if controller is KMAccountInfoWindowController {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. static func fetchSampleController() -> KMAccountInfoWindowController? {
  60. for window in NSApp.windows {
  61. let controller = window.windowController
  62. if controller is KMAccountInfoWindowController {
  63. return controller as! KMAccountInfoWindowController
  64. }
  65. }
  66. return nil
  67. }
  68. func setup() {
  69. self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
  70. self.accountInfoView.closeAction = { view in
  71. KMPrint("关闭")
  72. accountInfoMainWindow?.endSheet(view.window!)
  73. view.window?.close()
  74. accountInfoController = nil
  75. accountInfoMainWindow = nil
  76. }
  77. self.accountInfoView.logOutAction = { view in
  78. KMPrint("登出")
  79. KMRequestServerManager.manager.logout { success, result in
  80. if success {
  81. KMPrint("登出成功")
  82. }
  83. }
  84. KMLightMemberManager.manager.logOut()
  85. accountInfoMainWindow?.endSheet(view.window!)
  86. view.window?.close()
  87. accountInfoController = nil
  88. accountInfoMainWindow = nil
  89. }
  90. self.accountInfoView.cancellationAction = { [unowned self] view in
  91. KMPrint("注销")
  92. if KMLightMemberManager.manager.purchaseState == .subscription ||
  93. KMLightMemberManager.manager.purchaseState == .trial {
  94. let alert = NSAlert()
  95. alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "")
  96. alert.informativeText = NSLocalizedString("You are still in the subscription and cannot cancel account. Please click Cancel Account after the subscription period ends.", comment: "")
  97. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  98. alert.beginSheetModal(for: view.window!) { [unowned self] result in
  99. }
  100. } else {
  101. let alert = NSAlert()
  102. alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "")
  103. 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: "")
  104. alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
  105. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  106. alert.beginSheetModal(for: view.window!) { [unowned self] result in
  107. if (result == .alertFirstButtonReturn) { /// 取消
  108. return
  109. } else if result == .alertSecondButtonReturn {
  110. guard let callBack = cancellAtionAction else { return }
  111. // accountInfoMainWindow?.endSheet(view.window!)
  112. // view.window?.close()
  113. // accountInfoController = nil
  114. // accountInfoMainWindow = nil
  115. callBack(self)
  116. }
  117. }
  118. }
  119. }
  120. self.accountInfoView.purchaseInfoAction = { view in
  121. KMPurchaseManager.manager.showPurchasesInfo()
  122. }
  123. self.accountInfoView.subcriptionAction = { view in
  124. #if VERSION_DMG
  125. KMPurchaseManager.manager.purchaseProduct(productIdentifier: "") { isSuccess, error in
  126. }
  127. #endif
  128. #if VERSION_FREE
  129. accountInfoMainWindow?.endSheet(view.window!)
  130. view.window?.close()
  131. accountInfoController = nil
  132. accountInfoMainWindow = nil
  133. //跳转订阅比较表
  134. let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow(), .accountInfo)
  135. #endif
  136. }
  137. }
  138. func reloadData() {
  139. let state = KMDMGPurchaseManager.manager.state
  140. if state == .unknow {
  141. self.accountViewHeightConstraint.constant = 274
  142. #if VERSION_DMG
  143. if KMLightMemberManager.manager.existOrderInfo() {
  144. self.accountViewHeightConstraint.constant = 274 + 44
  145. }
  146. #endif
  147. } else if state == .subscription ||
  148. state == .trial {
  149. self.accountViewHeightConstraint.constant = 411 - 60
  150. #if VERSION_DMG
  151. if KMLightMemberManager.manager.info.subscriptionInfoList[0].payType == 1 {
  152. self.accountViewHeightConstraint.constant = 483 - 52 - 20
  153. } else {
  154. self.accountViewHeightConstraint.constant = 483
  155. }
  156. #endif
  157. } else if state == .subscriptionExpired ||
  158. state == .trialExpired {
  159. self.accountViewHeightConstraint.constant = 483 - 52
  160. #if VERSION_DMG
  161. self.accountViewHeightConstraint.constant = 483
  162. #endif
  163. }
  164. self.accountInfoView.purchaseState = state
  165. KMRequestServerManager.manager.getUserInfo { [weak self] success, data, error, isLocal in
  166. if success {
  167. self?.accountInfoView.userInfo = data
  168. KMLightMemberManager.manager.reloadUserInfo()
  169. } else {
  170. if error?.code == 304 {
  171. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  172. if self?.window != nil {
  173. accountInfoMainWindow?.endSheet((self?.window)!)
  174. }
  175. self?.window?.close()
  176. KMLightMemberManager.manager.logOut()
  177. accountInfoController = nil
  178. accountInfoMainWindow = nil
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }