优惠券swift版马甲包

XLProvider.swift 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // XLProvider.swift
  3. // YLBaseComponent
  4. //
  5. // Created by chenxiaofeng on 2018/8/29.
  6. // Copyright © 2018年 xuanlv.cgs.com. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. import ObjectMapper
  11. import Result
  12. /// A protocol representing a minimal interface for a MoyaProvider.
  13. /// Used by the reactive provider extensions.
  14. public protocol XLProviderType: AnyObject {
  15. associatedtype Target: XLTargetType
  16. /// Designated request-making method. Returns a `Cancellable` token to cancel the request later.
  17. func request<ModelType: Mappable>(_ target: Target, callbackQueue: DispatchQueue?, progress: Moya.ProgressBlock?, completion: @escaping (_ result: Result<ModelType, AnyError>, _ isCache: Bool) -> Void ) -> Cancellable
  18. }
  19. final class XLProvider<Target: XLTargetType>: XLProviderType{
  20. /// 修改这个变量添加默认plugin
  21. private var defaultPlugins: [PluginType] = [
  22. NetworkLoggerPlugin(verbose: false),
  23. JoinVeriSignaturePlugin(),
  24. AddTokenPlugin(),
  25. CheckErrorCodePlugin()
  26. ]
  27. private let moyaProvider: MoyaProvider<Target>
  28. // 这里最好替换成YYCache, 并做成单例的cache,这里只是做个样子
  29. private let cache = NSCache<NSString, NSData>()
  30. public init(plugins: [PluginType] = []) {
  31. moyaProvider = MoyaProvider<Target>.init(
  32. plugins: defaultPlugins + plugins
  33. )
  34. }
  35. // (_ result: Result<ModelType, AnyError>, _ isCache: Bool) -> Void
  36. // Result<ModelType, AnyError>
  37. @discardableResult
  38. public func request<ModelType: Mappable>(_ target: Target,
  39. callbackQueue: DispatchQueue? = .none,
  40. progress: ProgressBlock? = .none,
  41. completion: @escaping (_ result: Result<ModelType, AnyError>, _ isCache: Bool) -> Void) -> Cancellable {
  42. typealias RequestResult = Result<ModelType, AnyError>
  43. if target.needCache {
  44. if target.cachePolicy != .reloadIgnoringLocalCacheData{
  45. if let cacheData = self.cache.object(forKey: NSString(string: target.cacheId())){
  46. let fakeResponse = Response(statusCode: 0, data: cacheData as Data)
  47. self.handle(response: fakeResponse, isCache: true, completion: completion)
  48. }else{
  49. completion(RequestResult.init(error: AnyError(YLError.notFoundCacheError)), true)
  50. }
  51. }
  52. if target.cachePolicy == .retutnCacheDataDontLoad{
  53. return NoCancellable()
  54. }
  55. }
  56. return moyaProvider.request(target, callbackQueue: callbackQueue, progress: progress) { [weak self] (result) in
  57. guard let `self` = self else { return }
  58. switch result {
  59. case .success(let response):
  60. self.handle(response: response, isCache: false, completion: completion)
  61. self.handle(response: response, isCache: false, completion: { (result: Result<ModelType, AnyError>, iscache: Bool) in
  62. completion(result, iscache)
  63. switch result{
  64. case .success(_):
  65. // 只有成功的情况下 保存缓存数据
  66. if target.needCache{
  67. self.cache.setObject(NSData(data: response.data) , forKey: NSString(string: target.cacheId()))
  68. }
  69. default:
  70. break
  71. }
  72. })
  73. case .failure(let error):
  74. completion( RequestResult.init(error: AnyError(error)), false)
  75. }
  76. }
  77. }
  78. // 处理reponse
  79. private func handle<ModelType: Mappable>(response: Response, isCache: Bool, completion: @escaping (_ result: Result<ModelType, AnyError>, _ isCache: Bool) -> Void) {
  80. do {
  81. let model = try response.mapCommonModel() as ModelType
  82. completion( Result<ModelType, AnyError>.init(value: model), isCache)
  83. }catch{
  84. completion( Result<ModelType, AnyError>.init(error: AnyError(error) ), isCache)
  85. }
  86. }
  87. }