QSLCacheManager.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // 订单模型
  43. extension QSLCacheManager {
  44. // 缓存订单模型
  45. static func cacheOrderModel(_ model: QSLOrderModel) {
  46. let encoder = JSONEncoder()
  47. do {
  48. let data = try encoder.encode(model)
  49. UserDefaults.standard.set(data, forKey: "QSLOrderModel")
  50. UserDefaults.standard.synchronize()
  51. } catch {
  52. debugPrint("Encode wrong: \(error)")
  53. }
  54. }
  55. // 获取缓存的模型
  56. static func getOrderModel() -> QSLOrderModel? {
  57. if let data = UserDefaults.standard.data(forKey: "QSLOrderModel") {
  58. let decoder = JSONDecoder()
  59. do {
  60. let model = try decoder.decode(QSLOrderModel.self, from: data)
  61. return model
  62. } catch {
  63. debugPrint("Decode wrong: \(error)")
  64. }
  65. }
  66. return nil
  67. }
  68. // 清空缓存,模型清空
  69. static func clearOrderCache() {
  70. if let _ = UserDefaults.standard.data(forKey: "QSLOrderModel") {
  71. UserDefaults.standard.removeObject(forKey: "QSLOrderModel")
  72. }
  73. }
  74. }