KMMemberInfo.swift 20 KB

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