KMUserInfoVCModel.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. var urlPath = ""
  81. #if DEBUG
  82. if kTestMode == 1{
  83. urlPath = "http://test-pdf-pro.kdan.cn:3021/user-center/account-benefits"
  84. } else {
  85. urlPath = "https://pdfreaderpro.com/user-center/account-benefits"
  86. }
  87. #else
  88. urlPath = "https://pdfreaderpro.com/user-center/account-benefits"
  89. #endif
  90. NSWorkspace.shared.open(URL(string: urlPath)!)
  91. }
  92. /**
  93. @abstract 退出登录;
  94. */
  95. func signOutAction() -> Void {
  96. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  97. KMMemberPromptWC.shared.tipType = .logout
  98. KMMemberPromptWC.shared.showWindow(nil)
  99. }
  100. /**
  101. @abstract 注销账户;
  102. */
  103. func closeAccountAction() -> Void {
  104. if KMMemberInfo.shared.validFlag == "5" {
  105. // 撤销注销申请
  106. KMMemberCenterManager.manager.userRevokeCancel { success, result in
  107. guard let result = result else { return }
  108. let resultDict = result as KMMemberCenterResult
  109. let msg = resultDict.msg
  110. if success {
  111. self.refreshUserInfo { success, msg in
  112. // 撤销注销申请后刷新用户数据
  113. }
  114. } else {
  115. print(msg as Any)
  116. }
  117. }
  118. } else {
  119. if KMMemberInfo.shared.vip_status == 1 {
  120. // 订阅用户提示退订
  121. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  122. KMMemberPromptWC.shared.tipType = .unsubscribe
  123. KMMemberPromptWC.shared.showWindow(nil)
  124. } else {
  125. // 手动注销
  126. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  127. KMMemberPromptWC.shared.tipType = .signout
  128. KMMemberPromptWC.shared.showWindow(nil)
  129. }
  130. }
  131. }
  132. /**
  133. @abstract 展开个人中心;
  134. */
  135. func expandPersonalCenter() -> Void {
  136. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ExpandPersonalCenter"), object: nil)
  137. }
  138. /**
  139. @abstract 确认退出;
  140. */
  141. func confirmExitAction() -> Void {
  142. KMMemberCenterManager.manager.userLogout { success, result in
  143. guard let result = result else { return }
  144. let resultDict = result as KMMemberCenterResult
  145. let msg = resultDict.msg
  146. if success {
  147. let islogout: Bool = resultDict.result!
  148. if islogout {
  149. KMMemberInfo.shared.isLogin = false
  150. KMMemberInfo.shared.access_token = ""
  151. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MemberCenterLogoutSuccess"), object: nil)
  152. }
  153. } else {
  154. print(msg as Any)
  155. }
  156. }
  157. }
  158. /**
  159. @abstract 注销账户 警告弹窗;
  160. */
  161. func closeAccountWarningWC() -> Void {
  162. KMCloseAccountWC.shared.showWindow(nil)
  163. }
  164. /**
  165. @abstract 删除我的账户 弹窗;
  166. */
  167. func deleteAccountWC() -> Void {
  168. KMCloseVerificationWC.shared.showWindow(nil)
  169. }
  170. /**
  171. @abstract 注销账户申请 弹窗;
  172. */
  173. func closeAccountApplyWC(code: String) -> Void {
  174. KMMemberCenterManager.manager.userLogOffForUser(code: code) { success, result in
  175. guard let result = result else { return }
  176. let resultDict = result as KMMemberCenterResult
  177. let msg = resultDict.msg
  178. if success {
  179. let logOff: KMMemberLogOff = resultDict.logOff!
  180. if let token = logOff.currentTime { KMCloseApplyWC.shared.currentTime = token }
  181. if let token = logOff.logOffTime { KMCloseApplyWC.shared.logOffTime = token }
  182. KMCloseApplyWC.shared.showWindow(nil)
  183. } else {
  184. print(msg as Any)
  185. }
  186. }
  187. }
  188. // MARK: Private Method
  189. /**
  190. @abstract 获取用户支持的最大设备数
  191. @param
  192. */
  193. private func maxDeviceNum() -> Int {
  194. return 4
  195. // return 0
  196. }
  197. /**
  198. @abstract 用户支持的平台
  199. @param
  200. */
  201. private func userPlatforms() -> [String] {
  202. let platforms = "android, ios, mac, windows"
  203. let platformsArray = platforms
  204. .components(separatedBy: ",")
  205. .map { $0.trimmingCharacters(in: .whitespaces) }
  206. return platformsArray
  207. }
  208. }