AccountCenterWindowController.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. VerificationManager.default().activateDevice(withInfo: ["email" : model.email]) { status, infoDict, err in
  89. let header = ["Token" : model.token ?? ""]
  90. KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
  91. if success == false {
  92. return
  93. }
  94. let rmodel = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  95. AccountManager.manager.isVip = rmodel.isVip == 1
  96. self.gotoCenter(model: rmodel, infoModel: model)
  97. }
  98. }
  99. }
  100. }
  101. func gotoSignin() {
  102. self.closeButton_.isHidden = false
  103. self.inputC_ = AccountInputController()
  104. self.contentBox.contentView = self.inputC_?.view
  105. self.inputC_?.goToSignIn()
  106. self.inputC_?.rightDatas = self.rightDatas_
  107. self.inputC_?.itemClick = { idx, params in
  108. guard let model = params.first as? AccountInfoModel else {
  109. return
  110. }
  111. if let data = model.token, data.isEmpty == false {
  112. KMDataManager.ud_set(data, forKey: kAccountTokenKey)
  113. }
  114. if let data = model.email, data.isEmpty == false {
  115. KMDataManager.ud_set(data, forKey: kAccountEmailKey)
  116. }
  117. let state = self._isConnectionAvailable()
  118. if !state {
  119. self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: ""))
  120. return
  121. }
  122. VerificationManager.default().activateDevice(withInfo: ["email" : model.email]) { status, infoDict, err in
  123. let header = ["Token" : model.token ?? ""]
  124. KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
  125. if success == false {
  126. return
  127. }
  128. let rmodel = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  129. AccountManager.manager.isVip = rmodel.isVip == 1
  130. self.gotoCenter(model: rmodel, infoModel: model)
  131. }
  132. }
  133. }
  134. }
  135. func gotoCenter(model: AccountRightModel?, infoModel: AccountInfoModel?) {
  136. self.closeButton_.isHidden = true
  137. self.centerC = AccountCenterController()
  138. self.centerC?.rightDatas = self.rightDatas_
  139. self.centerC?.model = model
  140. self.centerC?.infoModel = infoModel
  141. self.contentBox.contentView = self.centerC?.view
  142. }
  143. // func rightDatas() {
  144. // let header = ["Token" : "7130d162c7533b5a51daefdd4faf17ea1729747785"]
  145. // KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataDict, err in
  146. // let model = AccountRightModel(dict: dataDict ?? [:])
  147. // KMPrint("")
  148. // }
  149. // }
  150. // func productDatas() {
  151. // let header = ["Token" : "7130d162c7533b5a51daefdd4faf17ea1729747785"]
  152. // KMHTTP.OEM_POST(urlString: kURLAPI_oemGetProductData, parameter: nil, headers: header) { success, dataDict, err in
  153. // let array = dataDict?["list"] as? [Any]
  154. // var datas: [ProductListModel] = []
  155. // for data in array ?? [] {
  156. // guard let dict = data as? [String : Any] else {
  157. // continue
  158. // }
  159. // let model = ProductListModel(dict: dict)
  160. // datas.append(model)
  161. // }
  162. // KMPrint("")
  163. // }
  164. // }
  165. //
  166. // func loginIn() {
  167. // let params = ["email" : "aaaa1@qq.com", "password" : "e10adc3949ba59abbe56e057f20f883e", "device_code" : "weqwerwer23243435435wedsf"]
  168. // KMHTTP.OEM_POST(urlString: kURLAPI_oemLogin, parameter: params, headers: nil) { success , dataDict, err in
  169. // let model = AccountInfoModel(dict: dataDict ?? [:])
  170. // KMPrint("")
  171. // }
  172. // }
  173. //
  174. // func signIn() {
  175. // let params = ["email" : "aaaa5@qq.com", "password" : "e10adc3949ba59abbe56e057f20f883e", "device_code" : "weqwerwer23243435435wedsf"]
  176. // KMHTTP.OEM_POST(urlString: kURLAPI_oemRegister, parameter: params, headers: nil) { success , dataDict, err in
  177. // let model = AccountInfoModel(dict: dataDict ?? [:])
  178. // KMPrint("")
  179. // }
  180. // }
  181. // MARK: - Private Methods
  182. @objc private func _closeAction() {
  183. self.km_quick_endSheet()
  184. }
  185. private func _isConnectionAvailable() -> Bool {
  186. if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
  187. return false
  188. }
  189. return true
  190. }
  191. private func _showHud(msg: String) {
  192. if let data = self.window?.contentView {
  193. // _ = CustomAlertView(message: msg, from: data, with: .black)
  194. CustomAlertView.alertView(message: msg, fromView: data, withStyle: .black)
  195. }
  196. }
  197. }