KMUserInfoVCModel.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 KMUserState : Int {
  10. case unPay_UnTrial = 0 // 未付费且未试用
  11. case permanentAccount // 永久账户用户
  12. case unAutomatic_RemainValid// 取消自动续费,但仍在有效期内
  13. case expired // 已到期用户
  14. }
  15. typealias UserInfoComplete = (_ success: Bool,_ msg: String) -> Void
  16. class KMUserInfoVCModel: ObservableObject {
  17. /**
  18. @abstract 刷新个人权益
  19. @param
  20. */
  21. func refreshUserInfo(_ complete: @escaping UserInfoComplete) -> Void {
  22. KMMemberCenterManager.manager.userInfo { success, result in
  23. guard let result = result else { return }
  24. let resultDict = result as KMMemberCenterResult
  25. let msg = resultDict.msg
  26. if success {
  27. let userInfo = resultDict.userInfo
  28. guard let userInfo = resultDict.userInfo else { return }
  29. KMMemberInfo.shared.memberUserInfo(model: userInfo)
  30. complete(true, "")
  31. } else {
  32. complete(false, "")
  33. }
  34. }
  35. }
  36. /**
  37. @abstract 刷新用户状态 0(未付费且未试用)、1(永久账户用户)、2(取消自动续费,但仍在有效期内)、3(已到期用户)
  38. @param
  39. */
  40. func refreshUserState() -> KMUserState {
  41. return .unPay_UnTrial
  42. }
  43. /**
  44. @abstract 根据用户支持的平台返回外部视图需要的平台图片数组
  45. @param
  46. */
  47. func getPlatformsImages() -> [NSImage] {
  48. var images: [NSImage] = []
  49. for platform in userPlatforms() {
  50. if refreshUserState() == .permanentAccount ||
  51. refreshUserState() == .unAutomatic_RemainValid {
  52. if platform == "mac" {
  53. images.append(NSImage(named: "MacOSPlatformImage2")!)
  54. } else if platform == "ios" {
  55. images.append(NSImage(named: "iOSPlatformImage2")!)
  56. } else if platform == "windows" {
  57. images.append(NSImage(named: "WindowsPlatformImage2")!)
  58. } else if platform == "android" {
  59. images.append(NSImage(named: "AndroidPlatformImage2")!)
  60. }
  61. } else {
  62. if platform == "mac" {
  63. images.append(NSImage(named: "MacOSPlatformImage")!)
  64. } else if platform == "ios" {
  65. images.append(NSImage(named: "iOSPlatformImage")!)
  66. } else if platform == "windows" {
  67. images.append(NSImage(named: "WindowsPlatformImage")!)
  68. } else if platform == "android" {
  69. images.append(NSImage(named: "AndroidPlatformImage")!)
  70. }
  71. }
  72. }
  73. return images
  74. }
  75. /**
  76. @abstract 跳转官网用户中心;1、官网未登录,则跳转到登录页面;2、官网已登录,则跳转到官网-个人中心;
  77. */
  78. func skipAccountCenter() -> Void {
  79. }
  80. /**
  81. @abstract 退出登录;
  82. */
  83. func signOutAction() -> Void {
  84. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  85. KMMemberPromptWC.shared.tipType = .logout
  86. KMMemberPromptWC.shared.showWindow(nil)
  87. }
  88. /**
  89. @abstract 注销账户;
  90. */
  91. func closeAccountAction() -> Void {
  92. if KMMemberInfo.shared.vip_status == 1 {
  93. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  94. KMMemberPromptWC.shared.tipType = .unsubscribe
  95. KMMemberPromptWC.shared.showWindow(nil)
  96. } else {
  97. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  98. KMMemberPromptWC.shared.tipType = .signout
  99. KMMemberPromptWC.shared.showWindow(nil)
  100. }
  101. }
  102. /**
  103. @abstract 展开个人中心;
  104. */
  105. func expandPersonalCenter() -> Void {
  106. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ExpandPersonalCenter"), object: nil)
  107. }
  108. /**
  109. @abstract 确认退出;
  110. */
  111. func confirmExitAction() -> Void {
  112. KMMemberCenterManager.manager.userLogout { success, result in
  113. guard let result = result else { return }
  114. let resultDict = result as KMMemberCenterResult
  115. let msg = resultDict.msg
  116. if success {
  117. let islogout: Bool = resultDict.result!
  118. if islogout {
  119. KMMemberInfo.shared.isLogin = false
  120. KMMemberInfo.shared.access_token = ""
  121. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MemberCenterLogoutSuccess"), object: nil)
  122. }
  123. } else {
  124. print(msg as Any)
  125. }
  126. }
  127. }
  128. /**
  129. @abstract 注销账户 警告弹窗;
  130. */
  131. func closeAccountWarningWC() -> Void {
  132. KMCloseAccountWC.shared.showWindow(nil)
  133. }
  134. /**
  135. @abstract 删除我的账户 弹窗;
  136. */
  137. func deleteAccountWC() -> Void {
  138. KMCloseVerificationWC.shared.showWindow(nil)
  139. }
  140. /**
  141. @abstract 注销账户申请 弹窗;
  142. */
  143. func closeAccountApplyWC(code: String) -> Void {
  144. KMMemberCenterManager.manager.userLogOffForUser(code: code) { success, result in
  145. guard let result = result else { return }
  146. let resultDict = result as KMMemberCenterResult
  147. let msg = resultDict.msg
  148. if success {
  149. let logOff: KMMemberLogOff = resultDict.logOff!
  150. if let token = logOff.currentTime { KMCloseApplyWC.shared.currentTime = token }
  151. if let token = logOff.logOffTime { KMCloseApplyWC.shared.logOffTime = token }
  152. KMCloseApplyWC.shared.showWindow(nil)
  153. } else {
  154. print(msg as Any)
  155. }
  156. }
  157. }
  158. // MARK: Private Method
  159. /**
  160. @abstract 获取用户支持的最大设备数
  161. @param
  162. */
  163. private func maxDeviceNum() -> Int {
  164. return 4
  165. // return 0
  166. }
  167. /**
  168. @abstract 用户支持的平台
  169. @param
  170. */
  171. private func userPlatforms() -> [String] {
  172. let platforms = "android, ios, mac, windows"
  173. let platformsArray = platforms
  174. .components(separatedBy: ",")
  175. .map { $0.trimmingCharacters(in: .whitespaces) }
  176. return platformsArray
  177. }
  178. }