O2MoyaProvider.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // O2MoyaProvider.swift
  3. // O2OA_SDK_Framwork
  4. //
  5. // Created by FancyLou on 2018/11/8.
  6. //
  7. import Foundation
  8. import Moya
  9. class O2MoyaProvider<Target>: MoyaProvider<Target> where Target: TargetType {
  10. // MARK:- 打印出来的JSON格式化
  11. class func JSONResponseDataFormatter(_ data: Data) -> Data {
  12. do {
  13. let dataAsJSON = try JSONSerialization.jsonObject(with: data)
  14. let prettyData = try JSONSerialization.data(withJSONObject: dataAsJSON, options: .prettyPrinted)
  15. return prettyData
  16. } catch {
  17. return data // fallback to original data if it can't be serialized.
  18. }
  19. }
  20. /// 网络请求状态改变插件
  21. var networkIndicatorPlugin:NetworkActivityPlugin = {
  22. return NetworkActivityPlugin(networkActivityClosure: { change,arg in
  23. switch change {
  24. case .began:
  25. DispatchQueue.main.async {
  26. UIApplication.shared.isNetworkActivityIndicatorVisible = true
  27. }
  28. break
  29. case .ended:
  30. DispatchQueue.main.async {
  31. UIApplication.shared.isNetworkActivityIndicatorVisible = false
  32. }
  33. break
  34. }
  35. })
  36. }()
  37. init() {
  38. // let policy = ServerTrustPolicyManager.serverTrustPolicy(
  39. // let manager = Manager(configuration: .default, serverTrustPolicyManager: ServerTrustPolicyManager)
  40. // (
  41. // configuration: URLSessionConfiguration.default,
  42. // serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
  43. // )
  44. let loggerConfig = NetworkLoggerPlugin.Configuration(logOptions: .verbose)
  45. let networkLogger = NetworkLoggerPlugin(configuration: loggerConfig)
  46. super.init(plugins: [
  47. // 日志打印插件
  48. // NetworkLoggerPlugin(
  49. // verbose: true,
  50. // responseDataFormatter: O2MoyaProvider<Target>.JSONResponseDataFormatter
  51. // ),
  52. networkLogger,
  53. // 网络请求指示器插件
  54. networkIndicatorPlugin,
  55. // o2oa 认证插件
  56. O2AccessTokenPlugin()
  57. ])
  58. }
  59. }