KMUserInfoVCModel.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // KMUserInfoVCModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/10/31.
  6. //
  7. import Foundation
  8. import Combine
  9. @objc enum memberEquityType : Int {
  10. case advancedSixMonthPlan = 0 // 高级版半年订阅
  11. case advancedYearMonthPlan // 高级版年订阅
  12. }
  13. typealias UserInfoComplete = (_ success: Bool,_ msg: String) -> Void
  14. @objcMembers
  15. class KMUserInfoVCModel: ObservableObject {
  16. /**
  17. @abstract 刷新个人权益
  18. @param
  19. */
  20. func refreshUserInfo(_ complete: @escaping UserInfoComplete) -> Void {
  21. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  22. let alert = NSAlert()
  23. alert.alertStyle = .critical
  24. alert.messageText = NSLocalizedString("Error Information", comment: "")
  25. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  26. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  27. alert.runModal()
  28. return
  29. }
  30. KMMemberCenterManager.manager.userInfo { success, result in
  31. guard let result = result else { return }
  32. let resultDict = result as KMMemberCenterResult
  33. let msg = resultDict.msg
  34. if success {
  35. let userInfo = resultDict.userInfo
  36. guard let userInfo = resultDict.userInfo else { return }
  37. KMMemberInfo.shared.memberUserInfo(model: userInfo)
  38. complete(true, "")
  39. } else {
  40. complete(false, "")
  41. }
  42. }
  43. }
  44. /**
  45. @abstract 根据用户支持的平台返回外部视图需要的平台图片数组
  46. @param
  47. */
  48. func getPlatformsImages() -> [NSImage] {
  49. var images: [NSImage] = []
  50. for platform in userPlatforms() {
  51. if KMMemberInfo.shared.userScenarioType == .lite_type4 ||
  52. KMMemberInfo.shared.userScenarioType == .lite_type5 ||
  53. KMMemberInfo.shared.userScenarioType == .lite_type6 ||
  54. KMMemberInfo.shared.userScenarioType == .lite_type8 ||
  55. KMMemberInfo.shared.userScenarioType == .lite_type12 ||
  56. KMMemberInfo.shared.userScenarioType == .lite_type13 ||
  57. KMMemberInfo.shared.userScenarioType == .pro_type1 ||
  58. KMMemberInfo.shared.userScenarioType == .pro_type2 ||
  59. KMMemberInfo.shared.userScenarioType == .pro_type3 {
  60. if platform == "mac" {
  61. images.append(NSImage(named: "MacOSPlatformImage2")!)
  62. } else if platform == "ios" {
  63. images.append(NSImage(named: "iOSPlatformImage2")!)
  64. } else if platform == "windows" {
  65. images.append(NSImage(named: "WindowsPlatformImage2")!)
  66. } else if platform == "android" {
  67. images.append(NSImage(named: "AndroidPlatformImage2")!)
  68. }
  69. } else {
  70. if platform == "mac" {
  71. images.append(NSImage(named: "MacOSPlatformImage3")!)
  72. } else if platform == "ios" {
  73. images.append(NSImage(named: "iOSPlatformImage3")!)
  74. } else if platform == "windows" {
  75. images.append(NSImage(named: "WindowsPlatformImage3")!)
  76. } else if platform == "android" {
  77. images.append(NSImage(named: "AndroidPlatformImage3")!)
  78. }
  79. }
  80. }
  81. return images
  82. }
  83. /**
  84. @abstract 跳转官网用户中心;1、官网未登录,则跳转到登录页面;2、官网已登录,则跳转到官网-个人中心;
  85. */
  86. func skipAccountCenter() -> Void {
  87. var urlPath = ""
  88. #if DEBUG
  89. if kTestMode == 1{
  90. urlPath = "http://test-pdf-pro.kdan.cn:3021/user-center/account-benefits"
  91. } else {
  92. urlPath = "https://pdfreaderpro.com/user-center/account-benefits"
  93. }
  94. #else
  95. urlPath = "https://pdfreaderpro.com/user-center/account-benefits"
  96. #endif
  97. NSWorkspace.shared.open(URL(string: urlPath)!)
  98. }
  99. /**
  100. @abstract 退出登录;
  101. */
  102. func signOutAction() -> Void {
  103. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  104. KMMemberPromptWC.shared.showWindow(nil)
  105. KMMemberPromptWC.shared.tipType = .logout
  106. }
  107. /**
  108. @abstract 注销账户;
  109. */
  110. func closeAccountAction() -> Void {
  111. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  112. let alert = NSAlert()
  113. alert.alertStyle = .critical
  114. alert.messageText = NSLocalizedString("Error Information", comment: "")
  115. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  116. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  117. alert.runModal()
  118. return
  119. }
  120. if KMMemberInfo.shared.validFlag == "5" {
  121. // 撤销注销申请
  122. KMMemberCenterManager.manager.userRevokeCancel { success, result in
  123. guard let result = result else { return }
  124. let resultDict = result as KMMemberCenterResult
  125. let msg = resultDict.msg
  126. if success {
  127. self.refreshUserInfo { success, msg in
  128. KMMemberPromptWC.shared.showWindow(nil)
  129. KMMemberPromptWC.shared.tipType = .cancelSignout
  130. }
  131. } else {
  132. print(msg as Any)
  133. }
  134. }
  135. } else {
  136. if KMMemberInfo.shared.vip_status == 1 {
  137. // 订阅用户提示退订
  138. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  139. KMMemberPromptWC.shared.showWindow(nil)
  140. KMMemberPromptWC.shared.tipType = .unsubscribe
  141. } else {
  142. // 手动注销
  143. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  144. KMMemberPromptWC.shared.showWindow(nil)
  145. KMMemberPromptWC.shared.tipType = .signout
  146. }
  147. }
  148. }
  149. /**
  150. @abstract 展开个人中心;
  151. */
  152. func expandPersonalCenter() -> Void {
  153. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ExpandPersonalCenter"), object: nil)
  154. }
  155. /**
  156. @abstract 确认退出;
  157. */
  158. func confirmExitAction() -> Void {
  159. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  160. let alert = NSAlert()
  161. alert.alertStyle = .critical
  162. alert.messageText = NSLocalizedString("Error Information", comment: "")
  163. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  164. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  165. alert.runModal()
  166. return
  167. }
  168. KMMemberCenterManager.manager.userLogout { success, result in
  169. guard let result = result else { return }
  170. let resultDict = result as KMMemberCenterResult
  171. let msg = resultDict.msg
  172. if success {
  173. let islogout: Bool = resultDict.result!
  174. if islogout {
  175. KMMemberInfo.shared.isLogin = false
  176. KMMemberInfo.shared.access_token = ""
  177. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MemberCenterLogoutSuccess"), object: nil)
  178. }
  179. } else {
  180. print(msg as Any)
  181. }
  182. }
  183. }
  184. /**
  185. @abstract 注销账户 警告弹窗;
  186. */
  187. func closeAccountWarningWC() -> Void {
  188. KMCloseAccountWC.shared.showWindow(nil)
  189. }
  190. /**
  191. @abstract 删除我的账户 弹窗;
  192. */
  193. func deleteAccountWC() -> Void {
  194. KMCloseVerificationWC.shared.showWindow(nil)
  195. }
  196. /**
  197. @abstract 注销账户申请 弹窗;
  198. */
  199. func closeAccountApplyWC(code: String) -> Void {
  200. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  201. let alert = NSAlert()
  202. alert.alertStyle = .critical
  203. alert.messageText = NSLocalizedString("Error Information", comment: "")
  204. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  205. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  206. alert.runModal()
  207. return
  208. }
  209. KMMemberCenterManager.manager.userLogOffForUser(code: code) { success, result in
  210. guard let result = result else { return }
  211. let resultDict = result as KMMemberCenterResult
  212. let msg = resultDict.msg
  213. if success {
  214. let logOff: KMMemberLogOff = resultDict.logOff!
  215. if let token = logOff.currentTime { KMCloseApplyWC.shared.currentTime = token }
  216. if let token = logOff.logOffTime { KMCloseApplyWC.shared.logOffTime = token }
  217. KMCloseApplyWC.shared.showWindow(nil)
  218. } else {
  219. print(msg as Any)
  220. }
  221. }
  222. }
  223. /**
  224. @abstract 跳转比较表;
  225. */
  226. func skipCompare(_ type: KMCompareTableType) -> Void {
  227. KMProductCompareWC.shared.orientation = true
  228. KMProductCompareWC.shared.orientationType = type
  229. KMProductCompareWC.shared.showWindow(nil)
  230. }
  231. /**
  232. @abstract 购买全平台年订阅;
  233. */
  234. func buyFullPlatformSubscription(_ type: memberEquityType) -> Void {
  235. if type == .advancedSixMonthPlan {
  236. IAPProductsManager.default().makeSubProduct(IAPProductsManager.default().fourDevicesAllAccessPackNew6Months_lite, discount: IAPProductsManager.default().isCancelAutoRenew())
  237. } else if type == .advancedYearMonthPlan {
  238. IAPProductsManager.default().makeSubProduct(IAPProductsManager.default().fourDevicesAllAccessPackNew12months_lite, discount: IAPProductsManager.default().isCancelAutoRenew())
  239. }
  240. }
  241. // MARK: Private Method
  242. /**
  243. @abstract 获取用户支持的最大设备数
  244. @param
  245. */
  246. private func maxDeviceNum() -> Int {
  247. return KMMemberInfo.shared.vip_maxDeviceNum
  248. }
  249. /**
  250. @abstract 用户支持的平台
  251. @param
  252. */
  253. private func userPlatforms() -> [String] {
  254. let platforms = KMMemberInfo.shared.vip_platforms
  255. let platformsArray = platforms
  256. .components(separatedBy: ",")
  257. .map { $0.trimmingCharacters(in: .whitespaces) }
  258. return platformsArray
  259. }
  260. }