KMUserInfoVCModel.swift 7.3 KB

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