O2InformationAPI.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // O2InformationAPI.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/12/27.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. //static let cmsCategoryQuery = "jaxrs/appinfo/list/user/view"
  11. //static let cmsAttachmentDownloadQuery = "servlet/download/##id##/stream"
  12. //static let cmsCategoryDetailQuery = "jaxrs/document/filter/list/##id##/next/##count##"
  13. //static let cmsAttachmentListQuery = "jaxrs/fileinfo/list/document/##documentId##"
  14. // MARK:- 所有调用的API枚举
  15. enum O2InformationAPI {
  16. case allCategoryList
  17. case categoryDetail(O2InformationCategoryListParameter)
  18. case categoryHomeDetail(O2InformationHomeParameter)
  19. case docAttachList(String)
  20. case docAttachDownload(O2CMSAttachmentInfo)
  21. }
  22. // MARK:- 上下文实现
  23. extension O2InformationAPI:OOAPIContextCapable {
  24. var apiContextKey: String {
  25. return "x_cms_assemble_control"
  26. }
  27. }
  28. // MARK: - 是否需要加入x-token访问头
  29. extension O2InformationAPI:OOAccessTokenAuthorizable {
  30. public var shouldAuthorize: Bool {
  31. return true
  32. }
  33. }
  34. extension O2InformationAPI:TargetType {
  35. var baseURL: URL {
  36. let model = O2AuthSDK.shared.o2APIServer(context: .x_cms_assemble_control)
  37. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 0)\(model?.context ?? "")"
  38. return URL(string: baseURLString)!
  39. }
  40. var path: String {
  41. switch self {
  42. case .allCategoryList:
  43. return "/jaxrs/appinfo/list/user/view"
  44. case .categoryDetail(let parameter):
  45. return "/jaxrs/document/filter/list/\(parameter.pageParameter?.currentPageNo ?? 0)/next/\(parameter.pageParameter?.countByPage ?? 20)"
  46. case .categoryHomeDetail(let parameter):
  47. return "/jaxrs/document/filter/list/\(parameter.pageParameter?.currentPageNo ?? 0)/next/\(parameter.pageParameter?.countByPage ?? 20)"
  48. case .docAttachList(let documentId):
  49. return "/jaxrs/fileinfo/list/document/\(documentId)"
  50. case .docAttachDownload(let attach):
  51. return "/jaxrs/fileinfo/download/document/\(attach.id!)/stream/APPLICATION_OCTET_STREAM"
  52. }
  53. }
  54. var method: Moya.Method {
  55. switch self {
  56. case .allCategoryList:
  57. return .get
  58. case .categoryDetail(_):
  59. return .put
  60. case .categoryHomeDetail(_):
  61. return .put
  62. case .docAttachList(_):
  63. return .get
  64. default:
  65. return .get
  66. }
  67. }
  68. var sampleData: Data {
  69. return "".data(using: String.Encoding.utf8)!
  70. }
  71. var task: Task {
  72. switch self {
  73. case .allCategoryList:
  74. return .requestPlain
  75. case .categoryDetail(let parameter):
  76. let param = parameter.getParamDict()
  77. return .requestParameters(parameters: param, encoding: JSONEncoding.default)
  78. case .categoryHomeDetail(_):
  79. return .requestPlain
  80. case .docAttachList(_):
  81. return .requestPlain
  82. case .docAttachDownload(_):
  83. return .downloadDestination(self.getDownDest()!)
  84. }
  85. }
  86. var headers: [String : String]? {
  87. return nil
  88. }
  89. func getDocAttachURL() -> NSURL? {
  90. switch self {
  91. case .docAttachDownload(let attachmentInfo):
  92. let fileName = attachmentInfo.name
  93. let fileURL = O2.inforCacheLocalFolder().appendingPathComponent(fileName!)
  94. return NSURL(string: fileURL.absoluteString)
  95. default:
  96. return nil
  97. }
  98. }
  99. func getDownDest() -> DownloadDestination? {
  100. switch self {
  101. case .docAttachDownload(let attachmentInfo):
  102. let myDest:DownloadDestination = { temporaryURL, response in
  103. let fileName = attachmentInfo.name
  104. let fileURL = O2.inforCacheLocalFolder().appendingPathComponent(fileName!)
  105. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  106. }
  107. return myDest
  108. default:
  109. return nil
  110. }
  111. }
  112. }