123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- //
- // AccountProfileController.swift
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2024/10/29.
- //
- import Cocoa
- class AccountProfileController: NSViewController {
- @IBOutlet weak var headerBox: NSBox!
- @IBOutlet weak var mainBox: NSBox!
-
- @IBOutlet weak var iv: NSImageView!
- @IBOutlet weak var nameLabel: NSTextField!
- @IBOutlet weak var refreshButton: NSButton!
- @IBOutlet weak var tipLabel: NSTextField!
- @IBOutlet weak var logoutButton: NSButton!
- @IBOutlet weak var hLineView: NSView!
- @IBOutlet weak var bottomLineView: NSView!
-
- var rightDatas: [String] = []
- var userInfoModel: AccountInfoModel?
- var model: AccountRightModel?
-
- var itemClick: KMCommonClickBlock?
-
- private var benefitsC_: AccountBenefitsController?
- private var rightC_: AccountRightController?
-
- private var winC: AccountLogoutWindowController?
- private var unSubwinC_: AccountUnsubscribeWindowController?
- private var pwdChangedwinC_: AccountPwdChangedWindowController?
-
- private var loadingView_: AccountLoadingView = {
- let view = AccountLoadingView()
- return view
- }()
-
- convenience init() {
- self.init(nibName: "AccountProfileController", bundle: MainBundle)
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- self.headerBox.borderWidth = 0
- self.mainBox.borderWidth = 0
-
- self.iv.image = NSImage(named: "KMImageNameAccountIcon")
- self.nameLabel.font = .systemFont(ofSize: 18)
- self.nameLabel.textColor = KMAppearance.titleColor()
- self.refreshButton.image = NSImage(named: "KMImageNameAccountRefresh")
- self.refreshButton.target = self
- self.refreshButton.action = #selector(refreshAction)
-
- self.tipLabel.textColor = KMAppearance.themeColor()
- self.logoutButton.image = NSImage(named: "KMImageNameAccountLogout")
- self.hLineView.wantsLayer = true
- self.hLineView.layer?.backgroundColor = NSColor(hex: "#E5E5E5").cgColor
- self.bottomLineView.wantsLayer = true
- self.bottomLineView.layer?.backgroundColor = NSColor(hex: "#F4F4F4").cgColor
- self.logoutButton.target = self
- self.logoutButton.action = #selector(logoutAction)
-
- if let name = userInfoModel?.email, name.isEmpty == false {
- self.nameLabel.stringValue = String(format: NSLocalizedString("Hi,%@", comment: ""), name)
- } else {
- if let data = KMDataManager.ud_string(forKey: kAccountEmailKey) {
- self.nameLabel.stringValue = String(format: NSLocalizedString("Hi,%@", comment: ""), data)
- }
- }
-
- let status = self.model?.trialData?.status ?? 0
- if status == 1 {
- self.tipLabel.isHidden = false
- self.tipLabel.stringValue = String(format: NSLocalizedString("You have tried it for %d days.", comment: ""), self.model?.trialData?.triedDay ?? 0)
- } else {
- self.tipLabel.isHidden = false
- self.tipLabel.stringValue = NSLocalizedString("Your trial has ended.", comment: "")
- }
-
- let isVip = model?.isVip ?? 0
- self.tipLabel.isHidden = isVip == 1
- if isVip == 1 {
- self.showRight()
- } else {
- self.showBenefit()
- }
- }
-
- func showBenefit() {
- let vc = AccountBenefitsController()
- vc.rightDatas = self.rightDatas
- vc.datas = self.model?.moreBenefits ?? []
- self.benefitsC_ = vc
- self.mainBox.contentView = vc.view
- }
-
- func showRight() {
- let vc = AccountRightController()
- self.rightC_ = vc
- vc.rightDatas = self.model?.rightsInterestsData ?? []
- vc.expiredDatas = self.model?.expiredBenefits ?? []
- self.mainBox.contentView = vc.view
- vc.itemClick = { [weak self] idx, _ in
- if idx == 1 { // 更多权益
- self?.itemClick?(idx)
- } else if idx == 2 { // 取消订阅
- self?.unSubcribeAction()
- }
- }
- }
-
- @objc func refreshAction() {
- if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
- let header = ["Token" : token]
- self.showLoading()
- KMHTTP.OEM_POST(urlString: kURLAPI_oemGetPermissions, parameter: nil, headers: header) { success, dataModel, err in
- self.hideLoading()
- if success == false {
- if dataModel?.code == KMHttpReponseCode.passwordChanged.rawValue {
- self._showHud(msg: NSLocalizedString("Your password has been changed. Please login again.", comment: ""))
- } else if dataModel?.code == KMHttpReponseCode.accountAtOtherDeviceLogin.rawValue {
- self.pwdChangedAction()
- }
- return
- }
- let model = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:])
- self.model = model
- let isVip = model.isVip
- AccountManager.manager.saveRights(model: model)
-
- NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
- if isVip == 1 {
- self.showRight()
- } else {
- self.showBenefit()
- }
-
- if let data = model.memberInfo?.email, data.isEmpty == false {
- self.activateDevice(email: data)
- }
- }
- }
- }
-
- @objc func logoutAction() {
- if let _ = self.winC {
- NSSound.beep()
- return
- }
-
- let winC = AccountLogoutWindowController()
- self.winC = winC
- self.view.window?.addChildWindow(winC.window!, ordered: .above)
-
- let winFrame = self.view.window?.sheetParent?.frame ?? .zero
- var frame = self.winC?.window?.frame ?? .zero
- frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
- frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
- self.winC?.window?.setFrame(frame, display: true)
-
- winC.itemClick = { [weak self] idx, _ in
- for win in self?.view.window?.childWindows ?? [] {
- if win.isEqual(to: self?.winC?.window) {
- self?.view.window?.removeChildWindow(win)
- break
- }
- }
- self?.winC?.window?.orderOut(nil)
- self?.winC = nil
- if idx == 2 {
- if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false {
- let header = ["Token" : token]
- KMHTTP.OEM_POST(urlString: kURLAPI_oemLogout, parameter: nil, headers: header) { success, dataDict, err in
- if success == false {
- return
- }
-
- // 登出
- VerificationManager.default().unactivateDeviceWithcomplention { status, infoDict, err in
- VerificationManager.default().verification {status1 , infoDict1, err1 in
-
- }
-
- self?.view.window?.windowController?.km_quick_endSheet()
- KMDataManager.ud_set("", forKey: kAccountTokenKey)
- AccountManager.manager.saveRights(model: nil)
-
- NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
- }
- }
- } else {
- // self.gotoSignin()
- }
- return
- }
- }
- }
-
- func unSubcribeAction() {
- if let _ = self.unSubwinC_ {
- NSSound.beep()
- return
- }
-
- let winC = AccountUnsubscribeWindowController()
- self.unSubwinC_ = winC
- self.view.window?.addChildWindow(winC.window!, ordered: .above)
-
- let winFrame = self.view.window?.sheetParent?.frame ?? .zero
- var frame = self.unSubwinC_?.window?.frame ?? .zero
- frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
- frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
- self.unSubwinC_?.window?.setFrame(frame, display: true)
-
- winC.itemClick = { [weak self] idx, _ in
- for win in self?.view.window?.childWindows ?? [] {
- if win.isEqual(to: self?.unSubwinC_?.window) {
- self?.view.window?.removeChildWindow(win)
- break
- }
- }
- self?.unSubwinC_?.window?.orderOut(nil)
- self?.unSubwinC_ = nil
- if idx == 1 {
- KMTools.openURL(urlString: kCancelSubscriptionUrlString)
- return
- }
- }
- }
-
- func pwdChangedAction() {
- if let _ = self.pwdChangedwinC_ {
- NSSound.beep()
- return
- }
-
- KMDataManager.ud_set("", forKey: kAccountTokenKey)
- AccountManager.manager.saveRights(model: nil)
- NotificationCenter.default.post(name: .loginStatusChanged, object: nil)
-
- let winC = AccountPwdChangedWindowController()
- self.pwdChangedwinC_ = winC
- self.view.window?.addChildWindow(winC.window!, ordered: .above)
-
- let winFrame = self.view.window?.sheetParent?.frame ?? .zero
- var frame = self.pwdChangedwinC_?.window?.frame ?? .zero
- frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5
- frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5
- self.pwdChangedwinC_?.window?.setFrame(frame, display: true)
-
- winC.itemClick = { [weak self] idx, _ in
- for win in self?.view.window?.childWindows ?? [] {
- if win.isEqual(to: self?.pwdChangedwinC_?.window) {
- self?.view.window?.removeChildWindow(win)
- break
- }
- }
- self?.pwdChangedwinC_?.window?.orderOut(nil)
- self?.pwdChangedwinC_ = nil
- self?.view.window?.windowController?.km_quick_endSheet()
- }
- }
-
- func showLoading() {
- if let _ = self.loadingView_.superview {
- self.hideLoading()
- }
-
- let view = self.loadingView_
- self.view.addSubview(view)
-
- let viewFrame = self.view.frame
- var frame = view.frame
- frame.origin.x = (viewFrame.size.width-frame.size.width)*0.5
- frame.origin.y = (viewFrame.size.height-frame.size.height)*0.5
-
- view.frame = frame
-
- view.startAnimation()
- }
-
- func hideLoading() {
- self.loadingView_.stopAnimation()
- self.loadingView_.removeFromSuperview()
- }
-
- func activateDevice(email: String) {
- // if IAPProductsManager.default().isAvailableAllFunction() {
- // return
- // }
-
- VerificationManager.default().activateDevice(withInfo: ["email" : email]) { status, info, err in
- VerificationManager.default().verification { status, info, err in
-
- }
- }
- }
-
- private func _showHud(msg: String) {
- DispatchQueue.main.async {
- _ = CustomAlertView.alertView(message: msg, fromView: self.view, withStyle: .black)
- }
- }
- }
|