KMUserInfoVCModel.swift 15 KB

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