feat: 品牌化那拉提景区收款页
This commit is contained in:
51
suixinkan/Features/Payment/Services/PayPageConfigCache.swift
Normal file
51
suixinkan/Features/Payment/Services/PayPageConfigCache.swift
Normal file
@ -0,0 +1,51 @@
|
||||
//
|
||||
// PayPageConfigCache.swift
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// 收款页品牌配置缓存协议,供 ViewModel 注入持久化实现或测试替身。
|
||||
protocol PayPageConfigCaching {
|
||||
/// 读取指定景区的品牌配置。
|
||||
func config(scenicId: Int) -> PayPageConfig?
|
||||
|
||||
/// 保存指定景区的完整品牌配置。
|
||||
func save(_ config: PayPageConfig, scenicId: Int)
|
||||
}
|
||||
|
||||
/// 使用 UserDefaults 按接口环境与景区隔离的收款页品牌配置缓存。
|
||||
final class PayPageConfigCache: PayPageConfigCaching {
|
||||
private static let version = "v1"
|
||||
|
||||
private let defaults: UserDefaults
|
||||
private let environmentNamespace: String
|
||||
private let encoder = JSONEncoder()
|
||||
private let decoder = JSONDecoder()
|
||||
|
||||
/// 使用指定持久化容器和 API 环境创建缓存。
|
||||
init(
|
||||
defaults: UserDefaults = .standard,
|
||||
environment: APIEnvironment = .current
|
||||
) {
|
||||
self.defaults = defaults
|
||||
environmentNamespace = environment.baseURL.host ?? environment.baseURL.absoluteString
|
||||
}
|
||||
|
||||
func config(scenicId: Int) -> PayPageConfig? {
|
||||
guard scenicId > 0,
|
||||
let data = defaults.data(forKey: key(scenicId: scenicId)) else {
|
||||
return nil
|
||||
}
|
||||
return try? decoder.decode(PayPageConfig.self, from: data)
|
||||
}
|
||||
|
||||
func save(_ config: PayPageConfig, scenicId: Int) {
|
||||
guard scenicId > 0, let data = try? encoder.encode(config) else { return }
|
||||
defaults.set(data, forKey: key(scenicId: scenicId))
|
||||
}
|
||||
|
||||
private func key(scenicId: Int) -> String {
|
||||
"payment.pay_page_config.\(Self.version).\(environmentNamespace).\(scenicId)"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user