// // KMUserInfoVCModel.swift // PDF Reader Pro // // Created by wanjun on 2024/10/31. // import Foundation import Combine @objc enum memberEquityType : Int { case advancedSixMonthPlan = 0 // 高级版半年订阅 case advancedYearMonthPlan // 高级版年订阅 } typealias UserInfoComplete = (_ success: Bool,_ msg: String) -> Void @objcMembers class KMUserInfoVCModel: ObservableObject { /** @abstract 刷新个人权益 @param */ func refreshUserInfo(_ complete: @escaping UserInfoComplete) -> Void { if KMMemberCenterManager.manager.isConnectionAvailable() == false { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Error Information", comment: "") alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) alert.runModal() return } KMMemberCenterManager.manager.userInfo { success, result in guard let result = result else { return } let resultDict = result as KMMemberCenterResult let msg = resultDict.msg if success { let userInfo = resultDict.userInfo guard let userInfo = resultDict.userInfo else { return } KMMemberInfo.shared.memberUserInfo(model: userInfo) complete(true, "") } else { complete(false, "") } } } /** @abstract 根据用户支持的平台返回外部视图需要的平台图片数组 @param */ func getPlatformsImages() -> [NSImage] { var images: [NSImage] = [] for platform in userPlatforms() { if KMMemberInfo.shared.userScenarioType == .lite_type4 || KMMemberInfo.shared.userScenarioType == .lite_type5 || KMMemberInfo.shared.userScenarioType == .lite_type6 || KMMemberInfo.shared.userScenarioType == .lite_type8 || KMMemberInfo.shared.userScenarioType == .lite_type12 || KMMemberInfo.shared.userScenarioType == .lite_type13 || KMMemberInfo.shared.userScenarioType == .pro_type1 || KMMemberInfo.shared.userScenarioType == .pro_type2 || KMMemberInfo.shared.userScenarioType == .pro_type3 { if platform == "mac" { images.append(NSImage(named: "MacOSPlatformImage2")!) } else if platform == "ios" { images.append(NSImage(named: "iOSPlatformImage2")!) } else if platform == "windows" { images.append(NSImage(named: "WindowsPlatformImage2")!) } else if platform == "android" { images.append(NSImage(named: "AndroidPlatformImage2")!) } } else { if platform == "mac" { images.append(NSImage(named: "MacOSPlatformImage3")!) } else if platform == "ios" { images.append(NSImage(named: "iOSPlatformImage3")!) } else if platform == "windows" { images.append(NSImage(named: "WindowsPlatformImage3")!) } else if platform == "android" { images.append(NSImage(named: "AndroidPlatformImage3")!) } } } return images } /** @abstract 跳转官网用户中心;1、官网未登录,则跳转到登录页面;2、官网已登录,则跳转到官网-个人中心; */ func skipAccountCenter() -> Void { var urlPath = "" #if DEBUG if kTestMode == 1{ urlPath = "http://test-pdf-pro.kdan.cn:3021/user-center/account-benefits" } else { urlPath = "https://pdfreaderpro.com/user-center/account-benefits" } #else urlPath = "https://pdfreaderpro.com/user-center/account-benefits" #endif NSWorkspace.shared.open(URL(string: urlPath)!) } /** @abstract 退出登录; */ func signOutAction() -> Void { NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil) KMMemberPromptWC.shared.showWindow(nil) KMMemberPromptWC.shared.tipType = .logout } /** @abstract 注销账户; */ func closeAccountAction() -> Void { if KMMemberCenterManager.manager.isConnectionAvailable() == false { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Error Information", comment: "") alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) alert.runModal() return } if KMMemberInfo.shared.validFlag == "5" { // 撤销注销申请 KMMemberCenterManager.manager.userRevokeCancel { success, result in guard let result = result else { return } let resultDict = result as KMMemberCenterResult let msg = resultDict.msg if success { self.refreshUserInfo { success, msg in KMMemberPromptWC.shared.showWindow(nil) KMMemberPromptWC.shared.tipType = .cancelSignout } } else { print(msg as Any) } } } else { if KMMemberInfo.shared.vip_status == 1 { // 订阅用户提示退订 NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil) KMMemberPromptWC.shared.showWindow(nil) KMMemberPromptWC.shared.tipType = .unsubscribe } else { // 手动注销 NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil) KMMemberPromptWC.shared.showWindow(nil) KMMemberPromptWC.shared.tipType = .signout } } } /** @abstract 展开个人中心; */ func expandPersonalCenter() -> Void { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ExpandPersonalCenter"), object: nil) } /** @abstract 确认退出; */ func confirmExitAction() -> Void { if KMMemberCenterManager.manager.isConnectionAvailable() == false { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Error Information", comment: "") alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) alert.runModal() return } KMMemberCenterManager.manager.userLogout { success, result in guard let result = result else { return } let resultDict = result as KMMemberCenterResult let msg = resultDict.msg if success { let islogout: Bool = resultDict.result! if islogout { KMMemberInfo.shared.isLogin = false KMMemberInfo.shared.access_token = "" NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MemberCenterLogoutSuccess"), object: nil) } } else { print(msg as Any) } } } /** @abstract 注销账户 警告弹窗; */ func closeAccountWarningWC() -> Void { KMCloseAccountWC.shared.showWindow(nil) } /** @abstract 删除我的账户 弹窗; */ func deleteAccountWC() -> Void { KMCloseVerificationWC.shared.showWindow(nil) } /** @abstract 注销账户申请 弹窗; */ func closeAccountApplyWC(code: String) -> Void { if KMMemberCenterManager.manager.isConnectionAvailable() == false { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Error Information", comment: "") alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) alert.runModal() return } KMMemberCenterManager.manager.userLogOffForUser(code: code) { success, result in guard let result = result else { return } let resultDict = result as KMMemberCenterResult let msg = resultDict.msg if success { let logOff: KMMemberLogOff = resultDict.logOff! if let token = logOff.currentTime { KMCloseApplyWC.shared.currentTime = token } if let token = logOff.logOffTime { KMCloseApplyWC.shared.logOffTime = token } KMCloseApplyWC.shared.showWindow(nil) } else { print(msg as Any) } } } /** @abstract 跳转比较表; */ func skipCompare(_ type: KMCompareTableType) -> Void { KMProductCompareWC.shared.orientation = true KMProductCompareWC.shared.orientationType = type KMProductCompareWC.shared.showWindow(nil) } /** @abstract 购买全平台年订阅; */ func buyFullPlatformSubscription(_ type: memberEquityType) -> Void { if type == .advancedSixMonthPlan { IAPProductsManager.default().makeSubProduct(IAPProductsManager.default().fourDevicesAllAccessPackNew6Months_lite, discount: IAPProductsManager.default().isCancelAutoRenew()) } else if type == .advancedYearMonthPlan { IAPProductsManager.default().makeSubProduct(IAPProductsManager.default().fourDevicesAllAccessPackNew12months_lite, discount: IAPProductsManager.default().isCancelAutoRenew()) } } // MARK: Private Method /** @abstract 获取用户支持的最大设备数 @param */ private func maxDeviceNum() -> Int { return KMMemberInfo.shared.vip_maxDeviceNum } /** @abstract 用户支持的平台 @param */ private func userPlatforms() -> [String] { let platforms = KMMemberInfo.shared.vip_platforms let platformsArray = platforms .components(separatedBy: ",") .map { $0.trimmingCharacters(in: .whitespaces) } return platformsArray } }