O2AccessTokenAuthorizable.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // O2AccessTokenAuthorizable.swift
  3. // O2OA_SDK_Framwork
  4. //
  5. // Created by FancyLou on 2018/11/7.
  6. //
  7. import Foundation
  8. import Moya
  9. public protocol O2APIContextCapable {
  10. var apiContextKey:String { get }
  11. }
  12. public protocol O2AccessTokenAuthorizable {
  13. var shouldAuthorize: Bool { get }
  14. }
  15. public class O2AccessTokenPlugin:PluginType {
  16. public init() {}
  17. var tokenVal:String {
  18. get {
  19. guard let myInfo = O2UserDefaults.shared.myInfo else {
  20. return ""
  21. }
  22. return myInfo.token ?? ""
  23. }
  24. }
  25. var clientVal:String {
  26. get {
  27. let version = ProcessInfo.processInfo.operatingSystemVersion
  28. let versionString = "\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
  29. return "iOS \(versionString)"
  30. }
  31. }
  32. public func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
  33. var request = request
  34. //加上通用头
  35. request.addValue(clientVal, forHTTPHeaderField: "x-client")
  36. if let authorizable = target as? O2AccessTokenAuthorizable,authorizable.shouldAuthorize == false {
  37. return request
  38. }
  39. //加上token
  40. request.addValue(tokenVal, forHTTPHeaderField: "x-token")
  41. return request
  42. }
  43. }