AccountProfileController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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?.first_name, 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 = true
  105. }
  106. let isVip = model?.isVip ?? 0
  107. self.tipLabel.isHidden = isVip == 1
  108. if isVip == 1 {
  109. self.showRight()
  110. } else {
  111. self.showBenefit()
  112. }
  113. self.activateDevice()
  114. }
  115. func showBenefit() {
  116. let vc = AccountBenefitsController()
  117. vc.rightDatas = self.rightDatas
  118. vc.datas = self.model?.moreBenefits ?? []
  119. self.benefitsC_ = vc
  120. self.mainBox.contentView = vc.view
  121. }
  122. func showRight() {
  123. let vc = AccountRightController()
  124. self.rightC_ = vc
  125. vc.rightDatas = self.model?.rightsInterestsData ?? []
  126. vc.expiredDatas = self.model?.expiredBenefits ?? []
  127. self.mainBox.contentView = vc.view
  128. vc.itemClick = { [weak self] idx, _ in
  129. if idx == 1 { // 更多权益
  130. self?.itemClick?(idx)
  131. } else if idx == 2 { // 取消订阅
  132. self?.unSubcribeAction()
  133. }
  134. }
  135. }
  136. @objc func refreshAction() {
  137. if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
  138. let header = ["Token" : token]
  139. self.showLoading()
  140. KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
  141. self.hideLoading()
  142. if success == false {
  143. self.showBenefit()
  144. return
  145. }
  146. let model = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
  147. self.model = model
  148. let isVip = model.isVip
  149. AccountManager.manager.isVip = model.isVip == 1
  150. if isVip == 1 {
  151. self.showRight()
  152. } else {
  153. self.showBenefit()
  154. }
  155. self.activateDevice()
  156. }
  157. }
  158. }
  159. @objc func logoutAction() {
  160. if let _ = self.winC {
  161. NSSound.beep()
  162. return
  163. }
  164. let winC = AccountLogoutWindowController()
  165. self.winC = winC
  166. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  167. let winFrame = self.view.window?.sheetParent?.frame ?? .zero
  168. var frame = self.winC?.window?.frame ?? .zero
  169. frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
  170. frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
  171. self.winC?.window?.setFrame(frame, display: true)
  172. winC.itemClick = { [weak self] idx, _ in
  173. for win in self?.view.window?.childWindows ?? [] {
  174. if win.isEqual(to: self?.winC?.window) {
  175. self?.view.window?.removeChildWindow(win)
  176. break
  177. }
  178. }
  179. self?.winC?.window?.orderOut(nil)
  180. self?.winC = nil
  181. if idx == 2 {
  182. if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
  183. let header = ["Token" : token]
  184. KMHTTP.OEM_POST(urlString: kURLAPI_oemLogout, parameter: nil, headers: header) { success, dataDict, err in
  185. if success == false {
  186. return
  187. }
  188. // 登出
  189. VerificationManager.default().unactivateDeviceWithcomplention { status, infoDict, err in
  190. self?.view.window?.windowController?.km_quick_endSheet()
  191. KMDataManager.ud_set("", forKey: kAccountTokenKey)
  192. AccountManager.manager.isVip = false
  193. }
  194. }
  195. } else {
  196. // self.gotoSignin()
  197. }
  198. return
  199. }
  200. }
  201. }
  202. func unSubcribeAction() {
  203. if let _ = self.unSubwinC_ {
  204. NSSound.beep()
  205. return
  206. }
  207. let winC = AccountUnsubscribeWindowController()
  208. self.unSubwinC_ = winC
  209. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  210. let winFrame = self.view.window?.sheetParent?.frame ?? .zero
  211. var frame = self.unSubwinC_?.window?.frame ?? .zero
  212. frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
  213. frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
  214. self.unSubwinC_?.window?.setFrame(frame, display: true)
  215. winC.itemClick = { [weak self] idx, _ in
  216. for win in self?.view.window?.childWindows ?? [] {
  217. if win.isEqual(to: self?.unSubwinC_?.window) {
  218. self?.view.window?.removeChildWindow(win)
  219. break
  220. }
  221. }
  222. self?.unSubwinC_?.window?.orderOut(nil)
  223. self?.unSubwinC_ = nil
  224. if idx == 1 {
  225. KMTools.openURL(urlString: kCancelSubscriptionUrlString)
  226. return
  227. }
  228. }
  229. }
  230. func pwdChangedAction() {
  231. if let _ = self.pwdChangedwinC_ {
  232. NSSound.beep()
  233. return
  234. }
  235. let winC = AccountPwdChangedWindowController()
  236. self.pwdChangedwinC_ = winC
  237. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  238. let winFrame = self.view.window?.sheetParent?.frame ?? .zero
  239. var frame = self.pwdChangedwinC_?.window?.frame ?? .zero
  240. frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
  241. frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
  242. self.pwdChangedwinC_?.window?.setFrame(frame, display: true)
  243. winC.itemClick = { [weak self] idx, _ in
  244. for win in self?.view.window?.childWindows ?? [] {
  245. if win.isEqual(to: self?.unSubwinC_?.window) {
  246. self?.view.window?.removeChildWindow(win)
  247. break
  248. }
  249. }
  250. self?.pwdChangedwinC_?.window?.orderOut(nil)
  251. self?.pwdChangedwinC_ = nil
  252. self?.view.window?.windowController?.km_quick_endSheet()
  253. }
  254. }
  255. func showLoading() {
  256. if let _ = self.loadingView_.superview {
  257. self.hideLoading()
  258. }
  259. let view = self.loadingView_
  260. self.view.addSubview(view)
  261. let viewFrame = self.view.frame
  262. var frame = view.frame
  263. frame.origin.x = (viewFrame.size.width-frame.size.width)*0.5
  264. frame.origin.y = (viewFrame.size.height-frame.size.height)*0.5
  265. view.frame = frame
  266. }
  267. func hideLoading() {
  268. self.loadingView_.removeFromSuperview()
  269. }
  270. func activateDevice() {
  271. // if IAPProductsManager.default().isAvailableAllFunction() {
  272. // return
  273. // }
  274. var licence: String?
  275. for model in self.model?.rightsInterestsData ?? [] {
  276. if let data = model.product_code, data.isEmpty == false {
  277. licence = data
  278. break
  279. }
  280. }
  281. if let data = licence {
  282. // VerificationManager.default().activateDevice(withInfo: ["cdkey" : data]) { status, info, err in
  283. // KMPrint("")
  284. // }
  285. }
  286. }
  287. }