AccountProfileController.swift 12 KB

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