KMUserInfoVCModel.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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(networkAlert: Bool = true ,_ complete: @escaping UserInfoComplete) -> Void {
  21. if KMMemberCenterManager.manager.isConnectionAvailable() == false && networkAlert {
  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_type2 ||
  58. KMMemberInfo.shared.userScenarioType == .pro_type4 ||
  59. (KMMemberInfo.shared.userScenarioType == .lite_type9 &&
  60. (KMMemberInfo.shared.vip_paymentModel == "2" && KMMemberInfo.shared.vip_levels != "1")) ||
  61. (KMMemberInfo.shared.userScenarioType == .pro_type1 &&
  62. (KMMemberInfo.shared.vip_paymentModel == "2" && KMMemberInfo.shared.vip_levels != "1")) ||
  63. KMMemberInfo.shared.userScenarioType == .pro_type5 ||
  64. KMMemberInfo.shared.userScenarioType == .dmg_type5 ||
  65. KMMemberInfo.shared.userScenarioType == .dmg_type6 ||
  66. KMMemberInfo.shared.userScenarioType == .dmg_type7 ||
  67. KMMemberInfo.shared.userScenarioType == .dmg_type28 ||
  68. KMMemberInfo.shared.userScenarioType == .dmg_type29 ||
  69. KMMemberInfo.shared.userScenarioType == .dmg_type30 ||
  70. KMMemberInfo.shared.userScenarioType == .dmg_type43 ||
  71. KMMemberInfo.shared.userScenarioType == .dmg_type44 ||
  72. KMMemberInfo.shared.userScenarioType == .dmg_type45 ||
  73. KMMemberInfo.shared.userScenarioType == .dmg_type8 ||
  74. KMMemberInfo.shared.userScenarioType == .dmg_type9 ||
  75. KMMemberInfo.shared.userScenarioType == .dmg_type10 ||
  76. KMMemberInfo.shared.userScenarioType == .dmg_type12 ||
  77. KMMemberInfo.shared.userScenarioType == .dmg_type13 ||
  78. KMMemberInfo.shared.userScenarioType == .dmg_type14 ||
  79. KMMemberInfo.shared.userScenarioType == .dmg_type15 ||
  80. KMMemberInfo.shared.userScenarioType == .dmg_type16 ||
  81. KMMemberInfo.shared.userScenarioType == .dmg_type17 ||
  82. KMMemberInfo.shared.userScenarioType == .dmg_type20 ||
  83. KMMemberInfo.shared.userScenarioType == .dmg_type21 ||
  84. KMMemberInfo.shared.userScenarioType == .dmg_type23 ||
  85. KMMemberInfo.shared.userScenarioType == .dmg_type24 ||
  86. KMMemberInfo.shared.userScenarioType == .dmg_type25 ||
  87. KMMemberInfo.shared.userScenarioType == .dmg_type26 ||
  88. KMMemberInfo.shared.userScenarioType == .dmg_type27 ||
  89. KMMemberInfo.shared.userScenarioType == .dmg_type31 ||
  90. KMMemberInfo.shared.userScenarioType == .dmg_type32 ||
  91. KMMemberInfo.shared.userScenarioType == .dmg_type35 ||
  92. KMMemberInfo.shared.userScenarioType == .dmg_type36 ||
  93. KMMemberInfo.shared.userScenarioType == .dmg_type38 ||
  94. KMMemberInfo.shared.userScenarioType == .dmg_type39 ||
  95. KMMemberInfo.shared.userScenarioType == .dmg_type40 ||
  96. KMMemberInfo.shared.userScenarioType == .dmg_type41 ||
  97. KMMemberInfo.shared.userScenarioType == .dmg_type42 ||
  98. KMMemberInfo.shared.userScenarioType == .dmg_type46 ||
  99. KMMemberInfo.shared.userScenarioType == .dmg_type47 {
  100. if platform == "mac" {
  101. images.append(NSImage(named: "MacOSPlatformImage2")!)
  102. } else if platform == "ios" {
  103. images.append(NSImage(named: "iOSPlatformImage2")!)
  104. } else if platform == "windows" {
  105. images.append(NSImage(named: "WindowsPlatformImage2")!)
  106. } else if platform == "android" {
  107. images.append(NSImage(named: "AndroidPlatformImage2")!)
  108. }
  109. } else {
  110. if platform == "mac" {
  111. images.append(NSImage(named: "MacOSPlatformImage3")!)
  112. } else if platform == "ios" {
  113. images.append(NSImage(named: "iOSPlatformImage3")!)
  114. } else if platform == "windows" {
  115. images.append(NSImage(named: "WindowsPlatformImage3")!)
  116. } else if platform == "android" {
  117. images.append(NSImage(named: "AndroidPlatformImage3")!)
  118. }
  119. }
  120. }
  121. return images
  122. }
  123. /**
  124. @abstract 跳转官网用户中心;1、官网未登录,则跳转到登录页面;2、官网已登录,则跳转到官网-个人中心;
  125. */
  126. func skipAccountCenter() -> Void {
  127. var urlPath = ""
  128. #if DEBUG
  129. if kTestMode == 1{
  130. urlPath = "http://test-pdf-pro.kdan.cn:3021/user-center/account-benefits"
  131. } else {
  132. urlPath = "https://pdfreaderpro.com/user-center/account-benefits"
  133. }
  134. #else
  135. urlPath = "https://pdfreaderpro.com/user-center/account-benefits"
  136. #endif
  137. NSWorkspace.shared.open(URL(string: urlPath)!)
  138. }
  139. /**
  140. @abstract 退出登录;
  141. */
  142. func signOutAction() -> Void {
  143. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  144. KMMemberPromptWC.shared.showWindow(nil)
  145. KMMemberPromptWC.shared.tipType = .logout
  146. }
  147. /**
  148. @abstract 注销账户;
  149. */
  150. func closeAccountAction() -> Void {
  151. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  152. let alert = NSAlert()
  153. alert.alertStyle = .critical
  154. alert.messageText = NSLocalizedString("Error Information", comment: "")
  155. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  156. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  157. alert.runModal()
  158. return
  159. }
  160. if KMMemberInfo.shared.validFlag == "5" {
  161. // 撤销注销申请
  162. KMMemberCenterManager.manager.userRevokeCancel { success, result in
  163. guard let result = result else { return }
  164. let resultDict = result as KMMemberCenterResult
  165. let msg = resultDict.msg
  166. if success {
  167. self.refreshUserInfo { success, msg in
  168. KMMemberPromptWC.shared.showWindow(nil)
  169. KMMemberPromptWC.shared.tipType = .cancelSignout
  170. }
  171. } else {
  172. print(msg as Any)
  173. }
  174. }
  175. } else {
  176. if (KMMemberInfo.shared.vip_paymentModel == "1" && KMMemberInfo.shared.vip_status == 1) || (KMMemberInfo.shared.ai_status == 1 && KMMemberInfo.shared.ai_paymentModel == "1") {
  177. // 订阅用户提示退订
  178. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  179. KMMemberPromptWC.shared.showWindow(nil)
  180. KMMemberPromptWC.shared.tipType = .unsubscribe
  181. } else {
  182. // 手动注销
  183. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  184. KMMemberPromptWC.shared.showWindow(nil)
  185. KMMemberPromptWC.shared.tipType = .signout
  186. }
  187. }
  188. }
  189. /**
  190. @abstract 展开个人中心;
  191. */
  192. func expandPersonalCenter() -> Void {
  193. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ExpandPersonalCenter"), object: nil)
  194. }
  195. /**
  196. @abstract 确认退出;
  197. */
  198. func confirmExitAction() -> Void {
  199. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  200. let alert = NSAlert()
  201. alert.alertStyle = .critical
  202. alert.messageText = NSLocalizedString("Error Information", comment: "")
  203. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  204. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  205. alert.runModal()
  206. return
  207. }
  208. KMMemberCenterManager.manager.userLogout { success, result in
  209. guard let result = result else { return }
  210. let resultDict = result as KMMemberCenterResult
  211. let msg = resultDict.msg
  212. if success {
  213. let islogout: Bool = resultDict.result!
  214. if islogout {
  215. KMMemberInfo.shared.clearMemberInfoCache()
  216. KMMemberInfo.shared.isLogin = false
  217. KMMemberInfo.shared.access_token = ""
  218. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MemberCenterLogoutSuccess"), object: nil)
  219. }
  220. } else {
  221. print(msg as Any)
  222. }
  223. }
  224. }
  225. /**
  226. @abstract 注销账户 警告弹窗;
  227. */
  228. func closeAccountWarningWC() -> Void {
  229. KMCloseAccountWC.shared.showWindow(nil)
  230. }
  231. /**
  232. @abstract 删除我的账户 弹窗;
  233. */
  234. func deleteAccountWC() -> Void {
  235. KMCloseVerificationWC.shared.showWindow(nil)
  236. }
  237. /**
  238. @abstract 注销账户申请 弹窗;
  239. */
  240. func closeAccountApplyWC(code: String,callback: ((Bool?, KMMemberCenterResult?)->Void)?) -> Void {
  241. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  242. let alert = NSAlert()
  243. alert.alertStyle = .critical
  244. alert.messageText = NSLocalizedString("Error Information", comment: "")
  245. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  246. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  247. alert.runModal()
  248. callback?(false,nil)
  249. return
  250. }
  251. KMMemberCenterManager.manager.userLogOffForUser(code: code) { success, result in
  252. guard let result = result else { return }
  253. let resultDict = result as KMMemberCenterResult
  254. callback?(success,resultDict)
  255. }
  256. }
  257. /**
  258. @abstract 跳转比较表;
  259. */
  260. func skipCompare(_ type: KMCompareTableType) -> Void {
  261. KMProductCompareWC.shared.orientation = true
  262. KMProductCompareWC.shared.orientationType = type
  263. KMProductCompareWC.shared.showWindow(nil)
  264. }
  265. /**
  266. @abstract 购买全平台年订阅;
  267. */
  268. func buyFullPlatformSubscription(_ type: memberEquityType) -> Void {
  269. if type == .advancedSixMonthPlan {
  270. IAPProductsManager.default().makeSubProduct(IAPProductsManager.default().fourDevicesAllAccessPackNew6Months_lite, discount: IAPProductsManager.default().isCancelAutoRenew())
  271. } else if type == .advancedYearMonthPlan {
  272. IAPProductsManager.default().makeSubProduct(IAPProductsManager.default().fourDevicesAllAccessPackNew12months_lite, discount: IAPProductsManager.default().isCancelAutoRenew())
  273. }
  274. }
  275. // MARK: Private Method
  276. /**
  277. @abstract 获取用户支持的最大设备数
  278. @param
  279. */
  280. private func maxDeviceNum() -> Int {
  281. return KMMemberInfo.shared.vip_maxDeviceNum
  282. }
  283. /**
  284. @abstract 用户支持的平台
  285. @param
  286. */
  287. private func userPlatforms() -> [String] {
  288. let platforms = KMMemberInfo.shared.vip_platforms
  289. let platformsArray = platforms
  290. .components(separatedBy: ",")
  291. .map { $0.trimmingCharacters(in: .whitespaces) }
  292. let containsMac = platforms.contains("mac")
  293. let containsiOS = platforms.contains("ios")
  294. let containsWin = platforms.contains("windows")
  295. let containsAn = platforms.contains("android")
  296. var newPlatforms:[String] = []
  297. if(containsMac == true){
  298. newPlatforms.append("mac")
  299. }
  300. if(containsWin == true){
  301. newPlatforms.append("windows")
  302. }
  303. if(containsiOS == true){
  304. newPlatforms.append("ios")
  305. }
  306. if(containsAn == true){
  307. newPlatforms.append("android")
  308. }
  309. return newPlatforms
  310. }
  311. }