KMUserInfoVCModel.swift 8.7 KB

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