KMUserInfoVCModel.swift 15 KB

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