QSLCacheManager.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // QSLCacheManager.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/12/4.
  6. //
  7. import Foundation
  8. class QSLCacheManager {
  9. // 缓存用户信息模型
  10. static func cacheModel(_ model: QSLUserModel) {
  11. // QSLApi.updateToken(token: model.authToken)
  12. let encoder = JSONEncoder()
  13. do {
  14. let data = try encoder.encode(model)
  15. UserDefaults.standard.set(data, forKey: "QSLUserModel")
  16. } catch {
  17. debugPrint("Encode wrong: \(error)")
  18. }
  19. }
  20. // 获取缓存的用户信息模型
  21. static func getModel() -> QSLUserModel? {
  22. if let data = UserDefaults.standard.data(forKey: "QSLUserModel") {
  23. let decoder = JSONDecoder()
  24. do {
  25. let model = try decoder.decode(QSLUserModel.self, from: data)
  26. // QSLApi.updateToken(token: model.authToken)
  27. return model
  28. } catch {
  29. debugPrint("Decode wrong: \(error)")
  30. }
  31. }
  32. return nil
  33. }
  34. // 清空缓存,模型清空
  35. static func clearCache() {
  36. if let _ = UserDefaults.standard.data(forKey: "QSLUserModel") {
  37. // QSLB.shared.userModel = HolaUserModel()
  38. UserDefaults.standard.removeObject(forKey: "QSLUserModel")
  39. }
  40. }
  41. // 缓存用配置模型
  42. static func cacheConfModel(_ model: QSLConfModel) {
  43. let encoder = JSONEncoder()
  44. do {
  45. let data = try encoder.encode(model)
  46. UserDefaults.standard.set(data, forKey: "QSLConfModel")
  47. } catch {
  48. debugPrint("Encode wrong: \(error)")
  49. }
  50. }
  51. // 获取
  52. static func getConfModel() -> QSLConfModel? {
  53. if let data = UserDefaults.standard.data(forKey: "QSLConfModel") {
  54. let decoder = JSONDecoder()
  55. do {
  56. let model = try decoder.decode(QSLConfModel.self, from: data)
  57. return model
  58. } catch {
  59. debugPrint("Decode wrong: \(error)")
  60. }
  61. }
  62. return nil
  63. }
  64. }
  65. // 订单模型
  66. extension QSLCacheManager {
  67. // 缓存订单模型
  68. static func cacheOrderModel(_ model: QSLOrderModel) {
  69. let encoder = JSONEncoder()
  70. do {
  71. let data = try encoder.encode(model)
  72. UserDefaults.standard.set(data, forKey: "QSLOrderModel")
  73. UserDefaults.standard.synchronize()
  74. } catch {
  75. debugPrint("Encode wrong: \(error)")
  76. }
  77. }
  78. // 获取缓存的模型
  79. static func getOrderModel() -> QSLOrderModel? {
  80. if let data = UserDefaults.standard.data(forKey: "QSLOrderModel") {
  81. let decoder = JSONDecoder()
  82. do {
  83. let model = try decoder.decode(QSLOrderModel.self, from: data)
  84. return model
  85. } catch {
  86. debugPrint("Decode wrong: \(error)")
  87. }
  88. }
  89. return nil
  90. }
  91. // 清空缓存,模型清空
  92. static func clearOrderCache() {
  93. if let _ = UserDefaults.standard.data(forKey: "QSLOrderModel") {
  94. UserDefaults.standard.removeObject(forKey: "QSLOrderModel")
  95. }
  96. }
  97. }