AccountProfileController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // AccountProfileController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/29.
  6. //
  7. import Cocoa
  8. class AccountLoadingView: NSView {
  9. private lazy var contentBox_: NSBox = {
  10. let view = NSBox()
  11. view.boxType = .custom
  12. view.titlePosition = .noTitle
  13. view.contentViewMargins = .zero
  14. view.borderWidth = 0
  15. return view
  16. }()
  17. // private lazy var titleLabel_: NSTextField = {
  18. // let view = NSTextField(labelWithString: "")
  19. // view.font = .systemFont(ofSize: 16)
  20. // view.textColor = KMAppearance.titleColor()
  21. // return view
  22. // }()
  23. private lazy var iconIv_: NSImageView = {
  24. let view = NSImageView()
  25. view.image = NSImage(named: "KMImageNameAccountRefreshLoading")
  26. return view
  27. }()
  28. convenience init() {
  29. self.init(frame: .init(x: 0, y: 0, width: 160, height: 118))
  30. self.initSubviews()
  31. }
  32. override func awakeFromNib() {
  33. super.awakeFromNib()
  34. self.initSubviews()
  35. }
  36. func initSubviews() {
  37. self.addSubview(self.contentBox_)
  38. self.contentBox_.km_add_inset_constraint()
  39. self.contentBox_.contentView?.addSubview(self.iconIv_)
  40. self.iconIv_.km_add_size_constraint(size: .init(width: 40, height: 40))
  41. self.iconIv_.km_add_centerX_constraint()
  42. self.iconIv_.km_add_centerY_constraint()
  43. self.contentBox_.fillColor = .black
  44. self.contentBox_.cornerRadius = 10
  45. }
  46. }
  47. class AccountProfileController: NSViewController {
  48. @IBOutlet weak var headerBox: NSBox!
  49. @IBOutlet weak var mainBox: NSBox!
  50. @IBOutlet weak var iv: NSImageView!
  51. @IBOutlet weak var nameLabel: NSTextField!
  52. @IBOutlet weak var refreshButton: NSButton!
  53. @IBOutlet weak var tipLabel: NSTextField!
  54. @IBOutlet weak var logoutButton: NSButton!
  55. @IBOutlet weak var hLineView: NSView!
  56. @IBOutlet weak var bottomLineView: NSView!
  57. var rightDatas: [String] = []
  58. var userInfoModel: AccountInfoModel?
  59. var model: AccountRightModel?
  60. var itemClick: KMCommonClickBlock?
  61. private var benefitsC_: AccountBenefitsController?
  62. private var rightC_: AccountRightController?
  63. private var winC: AccountLogoutWindowController?
  64. private var unSubwinC_: AccountUnsubscribeWindowController?
  65. private var pwdChangedwinC_: AccountPwdChangedWindowController?
  66. private var loadingView_: AccountLoadingView = {
  67. let view = AccountLoadingView()
  68. return view
  69. }()
  70. convenience init() {
  71. self.init(nibName: "AccountProfileController", bundle: MainBundle)
  72. }
  73. override func viewDidLoad() {
  74. super.viewDidLoad()
  75. // Do view setup here.
  76. self.headerBox.borderWidth = 0
  77. self.mainBox.borderWidth = 0
  78. self.iv.image = NSImage(named: "KMImageNameAccountIcon")
  79. self.nameLabel.font = .systemFont(ofSize: 18)
  80. self.nameLabel.textColor = KMAppearance.titleColor()
  81. self.refreshButton.image = NSImage(named: "KMImageNameAccountRefresh")
  82. self.refreshButton.target = self
  83. self.refreshButton.action = #selector(refreshAction)
  84. self.tipLabel.textColor = KMAppearance.themeColor()
  85. self.logoutButton.image = NSImage(named: "KMImageNameAccountLogout")
  86. self.hLineView.wantsLayer = true
  87. self.hLineView.layer?.backgroundColor = NSColor(hex: "#E5E5E5").cgColor
  88. self.bottomLineView.wantsLayer = true
  89. self.bottomLineView.layer?.backgroundColor = NSColor(hex: "#F4F4F4").cgColor
  90. self.logoutButton.target = self
  91. self.logoutButton.action = #selector(logoutAction)
  92. if let name = userInfoModel?.email, name.isEmpty == false {
  93. self.nameLabel.stringValue = String(format: NSLocalizedString("Hi,%@", comment: ""), name)
  94. } else {
  95. if let data = KMDataManager.ud_string(forKey: kAccountEmailKey) {
  96. self.nameLabel.stringValue = String(format: NSLocalizedString("Hi,%@", comment: ""), data)
  97. }
  98. }
  99. let status = self.model?.trialData?.status ?? 0
  100. if status == 1 {
  101. self.tipLabel.isHidden = false
  102. self.tipLabel.stringValue = String(format: NSLocalizedString("You have tried it for %d days.", comment: ""), self.model?.trialData?.triedDay ?? 0)
  103. } else {
  104. self.tipLabel.isHidden = false
  105. self.tipLabel.stringValue = NSLocalizedString("Your trial has ended.", comment: "")
  106. }
  107. let isVip = model?.isVip ?? 0
  108. self.tipLabel.isHidden = isVip == 1
  109. if isVip == 1 {
  110. self.showRight()
  111. } else {
  112. self.showBenefit()
  113. }
  114. self.activateDevice()
  115. }
  116. func showBenefit() {
  117. let vc = AccountBenefitsController()
  118. vc.rightDatas = self.rightDatas
  119. vc.datas = self.model?.moreBenefits ?? []
  120. self.benefitsC_ = vc
  121. self.mainBox.contentView = vc.view
  122. }
  123. func showRight() {
  124. let vc = AccountRightController()
  125. self.rightC_ = vc
  126. vc.rightDatas = self.model?.rightsInterestsData ?? []
  127. vc.expiredDatas = self.model?.expiredBenefits ?? []
  128. self.mainBox.contentView = vc.view
  129. vc.itemClick = { [weak self] idx, _ in
  130. if idx == 1 { // 更多权益
  131. self?.itemClick?(idx)
  132. } else if idx == 2 { // 取消订阅
  133. self?.unSubcribeAction()
  134. }
  135. }
  136. }
  137. @objc func refreshAction() {
  138. if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
  139. let header = ["Token" : token]
  140. self.showLoading()
  141. KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
  142. self.hideLoading()
  143. if success == false {
  144. self.showBenefit()
  145. return
  146. }
  147. let model = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  148. self.model = model
  149. let isVip = model.isVip
  150. AccountManager.manager.isLogin = true
  151. AccountManager.manager.isVip = model.isVip == 1
  152. AccountManager.manager.hasAiPermissions = model.hasAiPermissions == 1
  153. AccountManager.manager.aiBuyUrl = model.aiBuyUrl
  154. NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
  155. if isVip == 1 {
  156. self.showRight()
  157. } else {
  158. self.showBenefit()
  159. }
  160. self.activateDevice()
  161. }
  162. }
  163. }
  164. @objc func logoutAction() {
  165. if let _ = self.winC {
  166. NSSound.beep()
  167. return
  168. }
  169. let winC = AccountLogoutWindowController()
  170. self.winC = winC
  171. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  172. let winFrame = self.view.window?.sheetParent?.frame ?? .zero
  173. var frame = self.winC?.window?.frame ?? .zero
  174. frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
  175. frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
  176. self.winC?.window?.setFrame(frame, display: true)
  177. winC.itemClick = { [weak self] idx, _ in
  178. for win in self?.view.window?.childWindows ?? [] {
  179. if win.isEqual(to: self?.winC?.window) {
  180. self?.view.window?.removeChildWindow(win)
  181. break
  182. }
  183. }
  184. self?.winC?.window?.orderOut(nil)
  185. self?.winC = nil
  186. if idx == 2 {
  187. if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
  188. let header = ["Token" : token]
  189. KMHTTP.OEM_POST(urlString: kURLAPI_oemLogout, parameter: nil, headers: header) { success, dataDict, err in
  190. if success == false {
  191. return
  192. }
  193. // 登出
  194. VerificationManager.default().unactivateDeviceWithcomplention { status, infoDict, err in
  195. VerificationManager.default().verification {status1 , infoDict1, err1 in
  196. }
  197. self?.view.window?.windowController?.km_quick_endSheet()
  198. KMDataManager.ud_set("", forKey: kAccountTokenKey)
  199. AccountManager.manager.isLogin = false
  200. AccountManager.manager.isVip = false
  201. AccountManager.manager.hasAiPermissions = false
  202. AccountManager.manager.aiBuyUrl = nil
  203. NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
  204. }
  205. }
  206. } else {
  207. // self.gotoSignin()
  208. }
  209. return
  210. }
  211. }
  212. }
  213. func unSubcribeAction() {
  214. if let _ = self.unSubwinC_ {
  215. NSSound.beep()
  216. return
  217. }
  218. let winC = AccountUnsubscribeWindowController()
  219. self.unSubwinC_ = winC
  220. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  221. let winFrame = self.view.window?.sheetParent?.frame ?? .zero
  222. var frame = self.unSubwinC_?.window?.frame ?? .zero
  223. frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
  224. frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
  225. self.unSubwinC_?.window?.setFrame(frame, display: true)
  226. winC.itemClick = { [weak self] idx, _ in
  227. for win in self?.view.window?.childWindows ?? [] {
  228. if win.isEqual(to: self?.unSubwinC_?.window) {
  229. self?.view.window?.removeChildWindow(win)
  230. break
  231. }
  232. }
  233. self?.unSubwinC_?.window?.orderOut(nil)
  234. self?.unSubwinC_ = nil
  235. if idx == 1 {
  236. KMTools.openURL(urlString: kCancelSubscriptionUrlString)
  237. return
  238. }
  239. }
  240. }
  241. func pwdChangedAction() {
  242. if let _ = self.pwdChangedwinC_ {
  243. NSSound.beep()
  244. return
  245. }
  246. let winC = AccountPwdChangedWindowController()
  247. self.pwdChangedwinC_ = winC
  248. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  249. let winFrame = self.view.window?.sheetParent?.frame ?? .zero
  250. var frame = self.pwdChangedwinC_?.window?.frame ?? .zero
  251. frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
  252. frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
  253. self.pwdChangedwinC_?.window?.setFrame(frame, display: true)
  254. winC.itemClick = { [weak self] idx, _ in
  255. for win in self?.view.window?.childWindows ?? [] {
  256. if win.isEqual(to: self?.unSubwinC_?.window) {
  257. self?.view.window?.removeChildWindow(win)
  258. break
  259. }
  260. }
  261. self?.pwdChangedwinC_?.window?.orderOut(nil)
  262. self?.pwdChangedwinC_ = nil
  263. self?.view.window?.windowController?.km_quick_endSheet()
  264. }
  265. }
  266. func showLoading() {
  267. if let _ = self.loadingView_.superview {
  268. self.hideLoading()
  269. }
  270. let view = self.loadingView_
  271. self.view.addSubview(view)
  272. let viewFrame = self.view.frame
  273. var frame = view.frame
  274. frame.origin.x = (viewFrame.size.width-frame.size.width)*0.5
  275. frame.origin.y = (viewFrame.size.height-frame.size.height)*0.5
  276. view.frame = frame
  277. }
  278. func hideLoading() {
  279. self.loadingView_.removeFromSuperview()
  280. }
  281. func activateDevice() {
  282. // if IAPProductsManager.default().isAvailableAllFunction() {
  283. // return
  284. // }
  285. var licence: String?
  286. for model in self.model?.rightsInterestsData ?? [] {
  287. if let data = model.product_code, data.isEmpty == false {
  288. licence = data
  289. break
  290. }
  291. }
  292. if let data = licence {
  293. // VerificationManager.default().activateDevice(withInfo: ["cdkey" : data]) { status, info, err in
  294. // KMPrint("")
  295. // }
  296. }
  297. }
  298. }