AccountCenterWindowController.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // AccountCenterWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/22.
  6. //
  7. import Cocoa
  8. let kAccountTokenKey = "AccountToken"
  9. let kAccountEmailKey = "AccountEmail"
  10. let kTermsOfUseUrlString = "https://www.anyrecover.com/company/terms-conditions/"
  11. let kPrivacyPolicyUrlString = "https://www.anyrecover.com/company/privacy-policy/"
  12. let kResetpasswordUrlString = "https://account.anyrecover.com/reset-password/"
  13. let kCancelSubscriptionUrlString = "https://www.anyrecover.com/support/cancel-subscription/"
  14. let kPwdInputStrings = "!\"#$%&'()*+,-./:;<>=?@[\\]^_`{|}~0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
  15. class AccountCenterWindowController: NSWindowController {
  16. @IBOutlet weak var contentBox: NSBox!
  17. private lazy var rightDatas_: [String] = {
  18. return [
  19. NSLocalizedString("Handle PDF Documents with AI", comment: ""),
  20. NSLocalizedString("Unlimited file conversion", comment: ""),
  21. NSLocalizedString("PDF text and image editing", comment: ""),
  22. NSLocalizedString("Batch PDF processing", comment: ""),
  23. NSLocalizedString("Advanced PDF management", comment: ""),
  24. NSLocalizedString("PDF annotations", comment: ""),
  25. NSLocalizedString("Create&fill forms", comment: ""),
  26. NSLocalizedString("PDF Protect", comment: ""),
  27. NSLocalizedString("Advanced OCR technology", comment: "")]
  28. }()
  29. private var inputC_: AccountInputController?
  30. private var centerC: AccountCenterController?
  31. private lazy var closeButton_: NSButton = {
  32. let view = NSButton()
  33. view.isBordered = false
  34. view.title = ""
  35. return view
  36. }()
  37. override func windowDidLoad() {
  38. super.windowDidLoad()
  39. self.window?.appearance = NSAppearance(named: .aqua)
  40. self.contentBox.borderWidth = 0
  41. self.window?.contentView?.addSubview(self.closeButton_)
  42. self.closeButton_.km_add_right_constraint(constant: -10)
  43. self.closeButton_.km_add_top_constraint(constant: 10)
  44. self.closeButton_.km_add_size_constraint(size: .init(width: 24, height: 24))
  45. self.closeButton_.target = self
  46. self.closeButton_.action = #selector(_closeAction)
  47. self.closeButton_.image = NSImage(named: "KMImageNameAccountClose")
  48. if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
  49. let header = ["Token" : token]
  50. KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
  51. if success == false {
  52. self.gotoLogin()
  53. return
  54. }
  55. let model = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  56. AccountManager.manager.isVip = model.isVip == 1
  57. self.gotoCenter(model: model, infoModel: nil)
  58. }
  59. } else {
  60. if let token = KMDataManager.ud_string(forKey: kAccountEmailKey), token.isEmpty == false {
  61. self.gotoLogin()
  62. } else {
  63. self.gotoSignin()
  64. }
  65. }
  66. }
  67. func gotoLogin() {
  68. self.closeButton_.isHidden = false
  69. self.inputC_ = AccountInputController()
  70. self.inputC_?.rightDatas = self.rightDatas_
  71. self.contentBox.contentView = self.inputC_?.view
  72. self.inputC_?.gotoLogin()
  73. self.inputC_?.itemClick = { idx, params in
  74. guard let model = params.first as? AccountInfoModel else {
  75. return
  76. }
  77. if let data = model.token, data.isEmpty == false {
  78. KMDataManager.ud_set(data, forKey: kAccountTokenKey)
  79. }
  80. if let data = model.email, data.isEmpty == false {
  81. KMDataManager.ud_set(data, forKey: kAccountEmailKey)
  82. }
  83. let state = self._isConnectionAvailable()
  84. if !state {
  85. self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  86. return
  87. }
  88. let header = ["Token" : model.token ?? ""]
  89. KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
  90. if success == false {
  91. return
  92. }
  93. let rmodel = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  94. AccountManager.manager.isVip = rmodel.isVip == 1
  95. self.gotoCenter(model: rmodel, infoModel: model)
  96. }
  97. }
  98. }
  99. func gotoSignin() {
  100. self.closeButton_.isHidden = false
  101. self.inputC_ = AccountInputController()
  102. self.contentBox.contentView = self.inputC_?.view
  103. self.inputC_?.goToSignIn()
  104. self.inputC_?.rightDatas = self.rightDatas_
  105. self.inputC_?.itemClick = { idx, params in
  106. guard let model = params.first as? AccountInfoModel else {
  107. return
  108. }
  109. if let data = model.token, data.isEmpty == false {
  110. KMDataManager.ud_set(data, forKey: kAccountTokenKey)
  111. }
  112. if let data = model.email, data.isEmpty == false {
  113. KMDataManager.ud_set(data, forKey: kAccountEmailKey)
  114. }
  115. let state = self._isConnectionAvailable()
  116. if !state {
  117. self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  118. return
  119. }
  120. let header = ["Token" : model.token ?? ""]
  121. KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
  122. if success == false {
  123. return
  124. }
  125. let rmodel = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  126. AccountManager.manager.isVip = rmodel.isVip == 1
  127. self.gotoCenter(model: rmodel, infoModel: model)
  128. }
  129. }
  130. }
  131. func gotoCenter(model: AccountRightModel?, infoModel: AccountInfoModel?) {
  132. self.closeButton_.isHidden = true
  133. self.centerC = AccountCenterController()
  134. self.centerC?.rightDatas = self.rightDatas_
  135. self.centerC?.model = model
  136. self.centerC?.infoModel = infoModel
  137. self.contentBox.contentView = self.centerC?.view
  138. }
  139. // func rightDatas() {
  140. // let header = ["Token" : "7130d162c7533b5a51daefdd4faf17ea1729747785"]
  141. // KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataDict, err in
  142. // let model = AccountRightModel(dict: dataDict ?? [:])
  143. // KMPrint("")
  144. // }
  145. // }
  146. // func productDatas() {
  147. // let header = ["Token" : "7130d162c7533b5a51daefdd4faf17ea1729747785"]
  148. // KMHTTP.OEM_POST(urlString: kURLAPI_oemGetProductData, parameter: nil, headers: header) { success, dataDict, err in
  149. // let array = dataDict?["list"] as? [Any]
  150. // var datas: [ProductListModel] = []
  151. // for data in array ?? [] {
  152. // guard let dict = data as? [String : Any] else {
  153. // continue
  154. // }
  155. // let model = ProductListModel(dict: dict)
  156. // datas.append(model)
  157. // }
  158. // KMPrint("")
  159. // }
  160. // }
  161. //
  162. // func loginIn() {
  163. // let params = ["email" : "aaaa1@qq.com", "password" : "e10adc3949ba59abbe56e057f20f883e", "device_code" : "weqwerwer23243435435wedsf"]
  164. // KMHTTP.OEM_POST(urlString: kURLAPI_oemLogin, parameter: params, headers: nil) { success , dataDict, err in
  165. // let model = AccountInfoModel(dict: dataDict ?? [:])
  166. // KMPrint("")
  167. // }
  168. // }
  169. //
  170. // func signIn() {
  171. // let params = ["email" : "aaaa5@qq.com", "password" : "e10adc3949ba59abbe56e057f20f883e", "device_code" : "weqwerwer23243435435wedsf"]
  172. // KMHTTP.OEM_POST(urlString: kURLAPI_oemRegister, parameter: params, headers: nil) { success , dataDict, err in
  173. // let model = AccountInfoModel(dict: dataDict ?? [:])
  174. // KMPrint("")
  175. // }
  176. // }
  177. // MARK: - Private Methods
  178. @objc private func _closeAction() {
  179. self.km_quick_endSheet()
  180. }
  181. private func _isConnectionAvailable() -> Bool {
  182. if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
  183. return false
  184. }
  185. return true
  186. }
  187. private func _showHud(msg: String) {
  188. if let data = self.window?.contentView {
  189. // _ = CustomAlertView(message: msg, from: data, with: .black)
  190. CustomAlertView.alertView(message: msg, fromView: data, withStyle: .black)
  191. }
  192. }
  193. }