KMMemberInfo.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. //
  2. // KMMemberInfo.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/10/22.
  6. //
  7. import Cocoa
  8. // 用户场景类型
  9. @objc enum KMUserScenarioType : Int {
  10. case lite_type1 = 0 // 免费版 无旧买断 免费未试用 (右上角导航栏按钮 - “登录领福利” + “试用”)
  11. case lite_type2 // 免费版 无旧买断 免费试用中 (右上角导航栏按钮 - “登录领福利”)
  12. case lite_type3 // 免费版 无旧买断 试用过期或取消 (右上角导航栏按钮 - “登录领福利” + 开通会员)
  13. case lite_type4 // 免费版 无旧买断 可升级订阅 (右上角导航栏按钮 - “登录领福利” + 升级)
  14. case lite_type5 // 免费版 无旧买断 多平台永久 (右上角导航栏按钮 - “登录领福利” + 升级全平台)
  15. case lite_type6 // 免费版 无旧买断 全平台高级年订阅 (右上角导航栏按钮 - “登录领福利”)
  16. case lite_type7 // 免费版 无旧买断 会员到期 (右上角导航栏按钮 - “登录领福利” + 续费)
  17. case lite_type8 // 免费版 无旧买断 订阅中取消 (右上角导航栏按钮 - “登录领福利” + 续费)
  18. case lite_type9 // 免费版 有旧买断 免费未试用 (右上角导航栏按钮 - “登录领福利” + “升级至Pro” + 立即试用)
  19. case lite_type10 // 免费版 有旧买断 免费试用中 (右上角导航栏按钮 - “登录领福利”)
  20. case lite_type11 // 免费版 有旧买断 试用过期或取消 (右上角导航栏按钮 - “登录领福利” + 开通会员)
  21. case lite_type12 // 免费版 有旧买断 全平台高级版年订阅 (右上角导航栏按钮 - “登录领福利”)
  22. case lite_type13 // 免费版 有旧买断 订阅中取消 (右上角导航栏按钮 - “登录领福利” + 续费)
  23. case pro_type1 // 付费版 有旧PDF To Office (右上角导航栏按钮 - “登录领福利” + 升级(全平台))
  24. case pro_type2 // 付费版 无旧PDF To Office (右上角导航栏按钮 - “登录领福利” + 升级(全平台/高级版永久))
  25. case pro_type3 // 付费版 无旧PDF To Office 高级版永久 (右上角导航栏按钮 - Info + 升级(全平台))
  26. }
  27. // 当前权益类型
  28. @objc enum KMCurrentUserEquityType : Int {
  29. case lite_free = 0 // 免费版 免费
  30. case lite_old
  31. }
  32. @objcMembers class KMMemberInfo: NSObject {
  33. static let shared = KMMemberInfo()
  34. var isLogin: Bool = false
  35. /**
  36. 登录
  37. */
  38. private var _refresh_token: String = "" // 刷新Token
  39. private var _access_token: String = "" // 访问Token
  40. private var _token_type: String = "" // Token 类型
  41. private var _expires_in: String = ""
  42. private var _scope: String = ""
  43. /**
  44. 登录
  45. */
  46. var userID: String = "" // 用户ID
  47. var userEmail: String = "" // 用户邮箱
  48. var fullName: String = "" // 用户名称
  49. var _activeVips: [KMMemberUserInfoActiveVIP] = [] // 活跃的会员
  50. var _activeAis: [KMMemberUserInfoActiveVIP] = [] // 活跃的AI
  51. var vip_id: String = "" // id
  52. var vip_userId: String = "" // 用户id
  53. var vip_status: Int = 0 // 状态 0注册1订阅中2订阅过期3试用中4试用过期
  54. var vip_endDate: String = "" // 到期时间
  55. var vip_payType: Int = 0 // 0未开启自动续订 1自动续订
  56. var vip_productName: String = "" // 产品名字
  57. var vip_levels: String = "" // 会员等级 1是免费 2是标准 3是高级
  58. var vip_platforms: String = "" // 支持的平台
  59. var vip_point: Int = 0 // 剩余ai点数(svip才有)
  60. var vip_maxDeviceNum: Int = 0 // 最大登录设备限制数
  61. var vip_paymentModel: String = "" // 支付方式 付费模式(1自动续订(vip svip) 2单次付费(永久会员))
  62. var vip_isAi: String = "" // 是否是ai产品(0否1是)
  63. var vip_cycle: Int = 0 // 订阅周期:1、月(30天);2、季(90天);3、半年(183天);4、年(365天)
  64. var ai_id: String = "" // id
  65. var ai_userId: String = "" // 用户id
  66. var ai_status: Int = 0 // 状态 0注册1订阅中2订阅过期3试用中4试用过期
  67. var ai_endDate: String = "" // 到期时间
  68. var ai_payType: Int = 0 // 0未开启自动续订 1自动续订
  69. var ai_productName: String = "" // 产品名字
  70. var ai_levels: String = "" // 会员等级 1是免费 2是标准 3是高级
  71. var ai_platforms: String = "" // 支持的平台
  72. var ai_point: Int = 0 // 剩余ai点数(svip才有)
  73. var ai_maxDeviceNum: Int = 0 // 最大登录设备限制数
  74. var ai_paymentModel: String = "" // 支付方式 付费模式(1自动续订(vip svip) 2单次付费(永久会员))
  75. var ai_isAi: String = "" // 是否是ai产品(0否1是)
  76. var ai_cycle: Int = 0 // 订阅周期:1、月(30天);2、季(90天);3、半年(183天);4、年(365天)
  77. var aiPoint: Int = 0 // AI 点数
  78. var validFlag: String = "" // 注销(0), * 正常(1), * 停用(2), * 注销中(5);
  79. var isHaveAIDiscount: String = "" // 是否有ai折扣
  80. var canTrail: Bool = false // 是否能试用
  81. // var currentTime: String = "" // 当前注销时间
  82. // var logOffTime: String = "" // 注销时间
  83. // MARK: Get & Set
  84. var refresh_token: String {
  85. get {
  86. let token = UserDefaults.standard.value(forKey: "MemberRefreshToken")
  87. if token is String {
  88. return token as! String
  89. }
  90. return _refresh_token
  91. }
  92. set {
  93. _refresh_token = newValue
  94. UserDefaults.standard.setValue(newValue, forKey: "MemberRefreshToken")
  95. UserDefaults.standard.synchronize()
  96. }
  97. }
  98. var access_token: String {
  99. get {
  100. let token = UserDefaults.standard.value(forKey: "MemberAccessToken")
  101. if token is String {
  102. return token as! String
  103. }
  104. return _access_token
  105. }
  106. set {
  107. _access_token = newValue
  108. UserDefaults.standard.setValue(newValue, forKey: "MemberAccessToken")
  109. UserDefaults.standard.synchronize()
  110. }
  111. }
  112. var token_type: String {
  113. get {
  114. return _token_type
  115. }
  116. set {
  117. _token_type = newValue
  118. }
  119. }
  120. var activeVips: [KMMemberUserInfoActiveVIP] {
  121. get {
  122. return _activeVips
  123. }
  124. set {
  125. _activeVips = newValue
  126. for vip in _activeVips {
  127. if vip_levels == "" {
  128. vip_id = vip.id
  129. vip_userId = vip.userId
  130. vip_status = vip.status
  131. vip_endDate = vip.endDate
  132. vip_payType = vip.payType
  133. vip_productName = vip.productName
  134. vip_levels = vip.levels
  135. vip_platforms = vip.platforms
  136. vip_point = vip.point
  137. vip_maxDeviceNum = vip.maxDeviceNum
  138. vip_paymentModel = vip.paymentModel
  139. vip_isAi = vip.isAi
  140. vip_cycle = vip.cycle
  141. } else {
  142. if let vipLevelsInt = Int(vip_levels), let vipOtherLevelsInt = Int(vip.levels), vipLevelsInt < vipOtherLevelsInt {
  143. vip_id = vip.id
  144. vip_userId = vip.userId
  145. vip_status = vip.status
  146. vip_endDate = vip.endDate
  147. vip_payType = vip.payType
  148. vip_productName = vip.productName
  149. vip_levels = vip.levels
  150. vip_platforms = vip.platforms
  151. vip_point = vip.point
  152. vip_maxDeviceNum = vip.maxDeviceNum
  153. vip_paymentModel = vip.paymentModel
  154. vip_isAi = vip.isAi
  155. vip_cycle = vip.cycle
  156. }
  157. }
  158. }
  159. }
  160. }
  161. var activeAis: [KMMemberUserInfoActiveVIP] {
  162. get {
  163. return _activeAis
  164. }
  165. set {
  166. _activeAis = newValue
  167. for vip in _activeAis {
  168. if ai_levels == "" {
  169. ai_id = vip.id
  170. ai_userId = vip.userId
  171. ai_status = vip.status
  172. ai_endDate = vip.endDate
  173. ai_payType = vip.payType
  174. ai_productName = vip.productName
  175. ai_levels = vip.levels
  176. ai_platforms = vip.platforms
  177. ai_point = vip.point
  178. ai_maxDeviceNum = vip.maxDeviceNum
  179. ai_paymentModel = vip.paymentModel
  180. ai_isAi = vip.isAi
  181. ai_cycle = vip.cycle
  182. } else {
  183. if let vipLevelsInt = Int(ai_levels), let vipOtherLevelsInt = Int(vip.levels), vipLevelsInt < vipOtherLevelsInt {
  184. ai_id = vip.id
  185. ai_userId = vip.userId
  186. ai_status = vip.status
  187. ai_endDate = vip.endDate
  188. ai_payType = vip.payType
  189. ai_productName = vip.productName
  190. ai_levels = vip.levels
  191. ai_platforms = vip.platforms
  192. ai_point = vip.point
  193. ai_maxDeviceNum = vip.maxDeviceNum
  194. ai_paymentModel = vip.paymentModel
  195. ai_isAi = vip.isAi
  196. ai_cycle = vip.cycle
  197. }
  198. }
  199. }
  200. }
  201. }
  202. // 用户当前的状态
  203. var userScenarioType: KMUserScenarioType {
  204. get {
  205. let oldAIPoints = AIInfoManager.default().aiInfo.totalToken - AIInfoManager.default().aiInfo.usedTimes // 旧版AI点数
  206. let dateFormatter = DateFormatter()
  207. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  208. var startTime = Date()
  209. if KMMemberInfo.shared.vip_endDate.count > 0 {
  210. startTime = dateFormatter.date(from: KMMemberInfo.shared.vip_endDate)!
  211. }
  212. let currentTime = Date()
  213. let platforms = KMMemberInfo.shared.vip_platforms
  214. let platformsArray = platforms
  215. .components(separatedBy: ",")
  216. .map { $0.trimmingCharacters(in: .whitespaces) }
  217. #if VERSION_FREE
  218. #if VERSION_DMG
  219. // DMG
  220. if KMMemberInfo.shared.isLogin {
  221. } else {
  222. }
  223. #else
  224. // AppStore 免费版本
  225. if IAPProductsManager.default().allAccessProduct.isSubscribed == true {
  226. // 有旧买断
  227. if KMMemberInfo.shared.canTrail {
  228. return .lite_type9
  229. }
  230. if KMMemberInfo.shared.vip_status == 3 {
  231. return .lite_type10
  232. } else if KMMemberInfo.shared.vip_status == 4 {
  233. return .lite_type11
  234. }
  235. if KMMemberInfo.shared.vip_paymentModel == "1" && KMMemberInfo.shared.vip_cycle == 4 && platformsArray.count == 4 {
  236. return .lite_type12
  237. }
  238. if KMMemberInfo.shared.vip_paymentModel == "1" && KMMemberInfo.shared.vip_payType == 0 {
  239. return .lite_type13
  240. }
  241. } else {
  242. // 无旧买断
  243. if KMMemberInfo.shared.canTrail && KMMemberInfo.shared.vip_levels == "1" {
  244. return .lite_type1
  245. }
  246. if KMMemberInfo.shared.vip_status == 3 {
  247. return .lite_type2
  248. } else if KMMemberInfo.shared.vip_status == 4 {
  249. return .lite_type3
  250. } else if KMMemberInfo.shared.vip_status == 2 {
  251. return .lite_type7
  252. } else if KMMemberInfo.shared.vip_paymentModel == "1" && KMMemberInfo.shared.vip_payType == 0 {
  253. return .lite_type8
  254. }
  255. if KMMemberInfo.shared.vip_paymentModel == "2" {
  256. return .lite_type5
  257. } else {
  258. if KMMemberInfo.shared.vip_cycle == 4 && platformsArray.count == 4 {
  259. return .lite_type6
  260. } else {
  261. return .lite_type4
  262. }
  263. }
  264. }
  265. #endif
  266. #else
  267. // AppStore 付费版
  268. if IAPProductsManager.default().pdfToOfficeProduct.isSubscribed == true {
  269. return .pro_type1
  270. } else {
  271. if KMMemberInfo.shared.vip_paymentModel == "2" {
  272. return .pro_type3
  273. } else {
  274. return .pro_type2
  275. }
  276. }
  277. #endif
  278. return .lite_type1
  279. }
  280. }
  281. // 是否允许全部功能
  282. var isMemberAllFunction: Bool {
  283. get {
  284. if KMMemberInfo.shared.isLogin {
  285. if IAPProductsManager.default().allAccessProduct.isSubscribed == true {
  286. return true
  287. } else {
  288. if userScenarioType == .lite_type2 || userScenarioType == .lite_type4 || userScenarioType == .lite_type5 || userScenarioType == .lite_type6 {
  289. return true
  290. }
  291. return false
  292. }
  293. } else {
  294. if IAPProductsManager.default().allAccessProduct.isSubscribed == true {
  295. return true
  296. }
  297. return false
  298. }
  299. }
  300. }
  301. // 是否允许使用转档
  302. var isPermitConvert: Bool {
  303. if KMMemberInfo.shared.isLogin {
  304. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  305. return true
  306. } else {
  307. if userScenarioType == .lite_type4 || userScenarioType == .lite_type5 || userScenarioType == .lite_type6 || userScenarioType == .lite_type12 {
  308. return true
  309. } else {
  310. return false
  311. }
  312. }
  313. } else {
  314. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  315. return true
  316. }
  317. return false
  318. }
  319. }
  320. // 是否允许使用AI
  321. var isPermitAI: Bool {
  322. let oldAIPoints = AIInfoManager.default().aiInfo.totalToken - AIInfoManager.default().aiInfo.usedTimes // 旧版AI点数
  323. if KMMemberInfo.shared.isLogin {
  324. if oldAIPoints > 0 {
  325. return true
  326. } else {
  327. if userScenarioType == .lite_type4 || userScenarioType == .lite_type5 || userScenarioType == .lite_type6 || userScenarioType == .lite_type12 {
  328. if KMMemberInfo.shared.aiPoint > 0 {
  329. return true
  330. } else {
  331. return false
  332. }
  333. }
  334. return false
  335. }
  336. } else {
  337. if AIInfoManager.default().aiInfoValid && oldAIPoints > 0 {
  338. return true
  339. }
  340. return false
  341. }
  342. }
  343. // MARK: Public Method
  344. func memberUserInfo(model: KMMemberUserInfo) -> Void {
  345. userID = model.id
  346. userEmail = model.email
  347. fullName = model.fullName
  348. aiPoint = model.aiPoint
  349. validFlag = model.validFlag
  350. isHaveAIDiscount = model.isHaveAIDiscount
  351. if model.canTrail == "1" {
  352. canTrail = true
  353. } else {
  354. canTrail = false
  355. }
  356. activeVips = model.activeVIP
  357. activeAis = model.activeAI
  358. }
  359. func advancedFunctionUsage() -> Void {
  360. let platforms = KMMemberInfo.shared.vip_platforms
  361. let platformsArray = platforms
  362. .components(separatedBy: ",")
  363. .map { $0.trimmingCharacters(in: .whitespaces) }
  364. if KMMemberInfo.shared.isLogin {
  365. if userScenarioType == .lite_type1 {
  366. KMProductCompareWC.shared.orientation = true
  367. KMProductCompareWC.shared.orientationType = .trial
  368. KMProductCompareWC.shared.showWindow(nil)
  369. } else if userScenarioType == .lite_type2 || userScenarioType == .lite_type6 || userScenarioType == .lite_type10 || userScenarioType == .lite_type12 {
  370. } else if userScenarioType == .lite_type3 || userScenarioType == .lite_type11 {
  371. KMProductCompareWC.shared.orientation = true
  372. KMProductCompareWC.shared.orientationType = .lite_Base
  373. KMProductCompareWC.shared.showWindow(nil)
  374. } else if userScenarioType == .lite_type4 || userScenarioType == .lite_type5 || userScenarioType == .lite_type9 || userScenarioType == .lite_type13 {
  375. KMProductCompareWC.shared.orientation = true
  376. KMProductCompareWC.shared.orientationType = .lite_MacWindows
  377. KMProductCompareWC.shared.showWindow(nil)
  378. } else if userScenarioType == .lite_type7 || userScenarioType == .lite_type8 {
  379. KMProductCompareWC.shared.orientation = true
  380. if KMMemberInfo.shared.vip_paymentModel == "1" && KMMemberInfo.shared.vip_cycle == 4 && platformsArray.count == 4 {
  381. KMProductCompareWC.shared.orientationType = .lite_MacWindows
  382. } else {
  383. KMProductCompareWC.shared.orientationType = .lite_Base
  384. }
  385. KMProductCompareWC.shared.showWindow(nil)
  386. }
  387. } else {
  388. KMLoginWindowsController.shared.openWindow() { [weak self] success in
  389. guard let self = self else { return }
  390. if success {
  391. if self.userScenarioType == .lite_type1 {
  392. KMProductCompareWC.shared.orientation = true
  393. KMProductCompareWC.shared.orientationType = .trial
  394. KMProductCompareWC.shared.showWindow(nil)
  395. } else if self.userScenarioType == .lite_type2 || self.userScenarioType == .lite_type6 || self.userScenarioType == .lite_type10 || self.userScenarioType == .lite_type12 {
  396. } else if self.userScenarioType == .lite_type3 || self.userScenarioType == .lite_type11 {
  397. KMProductCompareWC.shared.orientation = true
  398. KMProductCompareWC.shared.orientationType = .lite_Base
  399. KMProductCompareWC.shared.showWindow(nil)
  400. } else if self.userScenarioType == .lite_type4 || self.userScenarioType == .lite_type5 || self.userScenarioType == .lite_type9 || self.userScenarioType == .lite_type13 {
  401. KMProductCompareWC.shared.orientation = true
  402. KMProductCompareWC.shared.orientationType = .lite_MacWindows
  403. KMProductCompareWC.shared.showWindow(nil)
  404. } else if self.userScenarioType == .lite_type7 || self.userScenarioType == .lite_type8 {
  405. KMProductCompareWC.shared.orientation = true
  406. if KMMemberInfo.shared.vip_paymentModel == "1" && KMMemberInfo.shared.vip_cycle == 4 && platformsArray.count == 4 {
  407. KMProductCompareWC.shared.orientationType = .lite_MacWindows
  408. } else {
  409. KMProductCompareWC.shared.orientationType = .lite_Base
  410. }
  411. KMProductCompareWC.shared.showWindow(nil)
  412. }
  413. }
  414. }
  415. }
  416. }
  417. func aiSubscription() -> Bool {
  418. let oldAIPoints = AIInfoManager.default().aiInfo.totalToken - AIInfoManager.default().aiInfo.usedTimes // 旧版AI点数
  419. if oldAIPoints > 0 {
  420. return true
  421. } else {
  422. if KMMemberInfo.shared.isLogin {
  423. if KMMemberInfo.shared.aiPoint > 0 {
  424. return true
  425. } else {
  426. AIPurchaseWindowController.currentWC().showWindow(nil)
  427. return false
  428. }
  429. } else {
  430. KMLoginWindowsController.shared.openWindow() { success in
  431. if success {
  432. AIPurchaseWindowController.currentWC().showWindow(nil)
  433. }
  434. }
  435. return false
  436. }
  437. }
  438. }
  439. }