KMMemberInfo.swift 21 KB

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