OOMoyaProvider.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // OOMoyaProvider.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/8/18.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. // MARK:- 自定义Provider
  11. public class OOMoyaProvider<Target>: MoyaProvider<Target> where Target: TargetType {
  12. // MARK:- 打印出来的JSON格式化
  13. class func JSONResponseDataFormatter(_ data: Data) -> Data {
  14. do {
  15. let dataAsJSON = try JSONSerialization.jsonObject(with: data)
  16. let prettyData = try JSONSerialization.data(withJSONObject: dataAsJSON, options: .prettyPrinted)
  17. return prettyData
  18. } catch {
  19. return data // fallback to original data if it can't be serialized.
  20. }
  21. }
  22. private let networkActivityPlugin = NetworkActivityPlugin(networkActivityClosure: { change,arg in
  23. switch change {
  24. case .began:
  25. UIApplication.shared.isNetworkActivityIndicatorVisible = true
  26. break
  27. case .ended:
  28. UIApplication.shared.isNetworkActivityIndicatorVisible = false
  29. break
  30. }
  31. })
  32. /// 网络请求状态改变插件
  33. var netPlugin:NetworkActivityPlugin = {
  34. return NetworkActivityPlugin(networkActivityClosure: { change,arg in
  35. switch change {
  36. case .began:
  37. UIApplication.shared.isNetworkActivityIndicatorVisible = true
  38. break
  39. case .ended:
  40. UIApplication.shared.isNetworkActivityIndicatorVisible = false
  41. break
  42. }
  43. })
  44. }()
  45. init() {
  46. let loggerConfig = NetworkLoggerPlugin.Configuration(logOptions: .verbose)
  47. let networkLogger = NetworkLoggerPlugin(configuration: loggerConfig)
  48. // let verbose = NetworkLoggerPlugin.Configuration(formatter: OOMoyaProvider<Target>.JSONResponseDataFormatter, output: O2AccessTokenPlugin(), logOptions: .verbose)
  49. super.init(plugins: [networkLogger, O2AccessTokenPlugin()])
  50. }
  51. }