KMUserInfoVCModel.swift 14 KB

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