KMUserInfoVCModel.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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,_ resDic:KMMemberCenterResult?) -> 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, "",resultDict)
  40. } else {
  41. complete(false, "",resultDict)
  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. DispatchQueue.main.async {
  152. KMMemberPromptWC.shared.showWindow(nil)
  153. KMMemberPromptWC.shared.window?.makeKeyAndOrderFront(nil)
  154. KMMemberPromptWC.shared.tipType = .logout
  155. }
  156. }
  157. /**
  158. @abstract 注销账户;
  159. */
  160. func closeAccountAction() -> Void {
  161. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  162. let alert = NSAlert()
  163. alert.alertStyle = .critical
  164. alert.messageText = NSLocalizedString("Error Information", comment: "")
  165. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  166. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  167. alert.runModal()
  168. return
  169. }
  170. if KMMemberInfo.shared.validFlag == "5" {
  171. // 撤销注销申请
  172. KMMemberCenterManager.manager.userRevokeCancel { success, result in
  173. guard let result = result else { return }
  174. let resultDict = result as KMMemberCenterResult
  175. let msg = resultDict.msg
  176. if success {
  177. self.refreshUserInfo { success, msg,resdic in
  178. DispatchQueue.main.async {
  179. KMMemberPromptWC.shared.showWindow(nil)
  180. KMMemberPromptWC.shared.window?.makeKeyAndOrderFront(nil)
  181. KMMemberPromptWC.shared.tipType = .cancelSignout
  182. }
  183. }
  184. } else {
  185. print(msg as Any)
  186. KMMemberCenterManager.showAlert(code: .init(rawValue: result.code), message: result.msg, callback: nil)
  187. }
  188. }
  189. } else {
  190. if (KMMemberInfo.shared.vip_payType == 1) ||
  191. (KMMemberInfo.shared.ai_payType == 1) {
  192. // 在续订中用户提示退订
  193. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  194. DispatchQueue.main.async {
  195. KMMemberPromptWC.shared.showWindow(nil)
  196. KMMemberPromptWC.shared.window?.makeKeyAndOrderFront(nil)
  197. KMMemberPromptWC.shared.tipType = .unsubscribe
  198. }
  199. } else {
  200. // 手动注销
  201. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  202. DispatchQueue.main.async {
  203. KMMemberPromptWC.shared.showWindow(nil)
  204. KMMemberPromptWC.shared.window?.makeKeyAndOrderFront(nil)
  205. KMMemberPromptWC.shared.tipType = .signout
  206. }
  207. }
  208. }
  209. }
  210. /**
  211. @abstract 展开个人中心;
  212. */
  213. func expandPersonalCenter() -> Void {
  214. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ExpandPersonalCenter"), object: nil)
  215. }
  216. /**
  217. @abstract 确认退出;
  218. */
  219. func confirmExitAction() -> Void {
  220. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  221. let alert = NSAlert()
  222. alert.alertStyle = .critical
  223. alert.messageText = NSLocalizedString("Error Information", comment: "")
  224. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  225. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  226. alert.runModal()
  227. return
  228. }
  229. KMMemberCenterManager.manager.userLogout { success, result in
  230. guard let result = result else { return }
  231. let resultDict = result as KMMemberCenterResult
  232. let msg = resultDict.msg
  233. if success {
  234. let islogout: Bool = resultDict.result!
  235. if islogout {
  236. KMMemberInfo.shared.clearMemberInfoCache()
  237. KMMemberInfo.shared.isLogin = false
  238. KMMemberInfo.shared.access_token = ""
  239. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MemberCenterLogoutSuccess"), object: nil)
  240. }
  241. } else {
  242. print(msg as Any)
  243. KMMemberCenterManager.showAlert(code: .init(rawValue: result.code), message: result.msg, callback: nil)
  244. }
  245. }
  246. }
  247. /**
  248. @abstract 注销账户 警告弹窗;
  249. */
  250. func closeAccountWarningWC() -> Void {
  251. KMCloseAccountWC.shared.showWindow(nil)
  252. }
  253. /**
  254. @abstract 删除我的账户 弹窗;
  255. */
  256. func deleteAccountWC() -> Void {
  257. KMCloseVerificationWC.shared.showWindow(nil)
  258. }
  259. /**
  260. @abstract 注销账户申请 弹窗;
  261. */
  262. func closeAccountApplyWC(code: String,callback: ((Bool?, KMMemberCenterResult?)->Void)?) -> Void {
  263. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  264. let alert = NSAlert()
  265. alert.alertStyle = .critical
  266. alert.messageText = NSLocalizedString("Error Information", comment: "")
  267. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  268. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  269. alert.runModal()
  270. callback?(false,nil)
  271. return
  272. }
  273. KMMemberCenterManager.manager.userLogOffForUser(code: code) { success, result in
  274. guard let result = result else { return }
  275. let resultDict = result as KMMemberCenterResult
  276. callback?(success,resultDict)
  277. }
  278. }
  279. /**
  280. @abstract 跳转比较表;
  281. */
  282. func skipCompare(_ type: KMCompareTableType) -> Void {
  283. NotificationCenter.default.post(name: NSNotification.Name("CloseMenuNotification"), object: nil)
  284. KMProductCompareWC.shared.orientation = true
  285. KMProductCompareWC.shared.orientationType = type
  286. KMProductCompareWC.shared.showWindow(nil)
  287. }
  288. /**
  289. @abstract 购买全平台年订阅;
  290. */
  291. func buyFullPlatformSubscription(_ type: memberEquityType) -> Void {
  292. if type == .advancedSixMonthPlan {
  293. IAPProductsManager.default().makeSubProduct(IAPProductsManager.default().fourDevicesAllAccessPackNew6Months_lite, discount: IAPProductsManager.default().isCancelAutoRenew())
  294. } else if type == .advancedYearMonthPlan {
  295. IAPProductsManager.default().makeSubProduct(IAPProductsManager.default().fourDevicesAllAccessPackNew12months_lite, discount: IAPProductsManager.default().isCancelAutoRenew())
  296. }
  297. }
  298. // MARK: Private Method
  299. /**
  300. @abstract 获取用户支持的最大设备数
  301. @param
  302. */
  303. private func maxDeviceNum() -> Int {
  304. return KMMemberInfo.shared.vip_maxDeviceNum
  305. }
  306. /**
  307. @abstract 用户支持的平台
  308. @param
  309. */
  310. private func userPlatforms() -> [String] {
  311. let platforms = KMMemberInfo.shared.vip_platforms
  312. let platformsArray = platforms
  313. .components(separatedBy: ",")
  314. .map { $0.trimmingCharacters(in: .whitespaces) }
  315. let containsMac = platforms.contains("mac")
  316. let containsiOS = platforms.contains("ios")
  317. let containsWin = platforms.contains("windows")
  318. let containsAn = platforms.contains("android")
  319. var newPlatforms:[String] = []
  320. if(containsMac == true){
  321. newPlatforms.append("mac")
  322. }
  323. if(containsWin == true){
  324. newPlatforms.append("windows")
  325. }
  326. if(containsiOS == true){
  327. newPlatforms.append("ios")
  328. }
  329. if(containsAn == true){
  330. newPlatforms.append("android")
  331. }
  332. return newPlatforms
  333. }
  334. }