// // 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? var goLoginBlock: KMCommonBlock? 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.iv.imageScaling = .scaleAxesIndependently 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 let datas = (model?.rightsInterestsData ?? []) + (model?.expiredBenefits ?? []) if datas.isEmpty == false { 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: "")) KMDataManager.ud_set("", forKey: kAccountTokenKey) AccountManager.manager.saveRights(model: nil) NotificationCenter.default.post(name: .loginStatusChanged, object: nil) self.goLoginBlock?() } else if dataModel?.code == KMHttpReponseCode.accountAtOtherDeviceLogin.rawValue { self.pwdChangedAction() KMDataManager.ud_set("", forKey: kAccountTokenKey) AccountManager.manager.saveRights(model: nil) NotificationCenter.default.post(name: .loginStatusChanged, object: nil) self.goLoginBlock?() } 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) let datas = model.rightsInterestsData + model.expiredBenefits if datas.isEmpty == false { self.showRight() } else { self.showBenefit() } if let data = model.memberInfo?.email, data.isEmpty == false { AccountTools.activateDevice(email: data, callback: nil) } } } } @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() } private func _showHud(msg: String) { DispatchQueue.main.async { _ = CustomAlertView.alertView(message: msg, fromView: self.view, withStyle: .black) } } }