O2BBSAPI.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // O2BBSAPI.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/12/26.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. //static let bbsContextKey = "x_bbs_assemble_control"
  11. //static let getCategoryAndSectionQuery = "jaxrs/mobile/view/all" //所有分区及所有子板块
  12. //static let getSectionItemQuery = "jaxrs/section/##id##" //板块详细信息列表
  13. //static let sectionTopItemQuery = "jaxrs/subject/top/##id##" //板块内的置顶帖
  14. //static let subjectByIdQuery = "jaxrs/subject/view/##id##" //获得具体帖子
  15. //static let subjectFromSectionByPageQuery = "jaxrs/subject/filter/list/page/##pageNumber##/count/##pageSize##" //板块帖子分页查询
  16. //static let uploadImageQuery = "servlet/upload/subject" //上传图片
  17. //static let imageDisplayQuery = "servlet/download/subjectattachment/##id##/stream" //图片显示地址
  18. //static let itemCreateQuery = "jaxrs/user/subject"//发帖
  19. //static let itemReplyQuery = "jaxrs/user/reply" //回帖
  20. //static let bbsSectionIconQuery = "servlet/section/##id##/icon"
  21. // MARK:- 所有调用的API枚举
  22. enum O2BBSAPI {
  23. case getCategoryAndSectionQuery
  24. case getSectionItemQuery(String)
  25. case sectionTopItemQuery(String)
  26. case subjectByIdQuery(String)
  27. case subjectFromSectionByPageQuery(SubjectsParameter)
  28. case createSubject
  29. case replySubject
  30. case getAttachment(String)
  31. case uploadAttachForSubject(String)
  32. case downloadAttachForSubject(O2BBSSubjectAttachmentInfo)
  33. case getSubjectAttachmentList(String)
  34. }
  35. // MARK:- 上下文实现
  36. extension O2BBSAPI:OOAPIContextCapable {
  37. var apiContextKey: String {
  38. return "x_bbs_assemble_control"
  39. }
  40. }
  41. // MARK: - 是否需要加入x-token访问头
  42. extension O2BBSAPI:OOAccessTokenAuthorizable {
  43. public var shouldAuthorize: Bool {
  44. return true
  45. }
  46. }
  47. extension O2BBSAPI:TargetType{
  48. var baseURL: URL {
  49. let model = O2AuthSDK.shared.o2APIServer(context: .x_bbs_assemble_control)
  50. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 0)\(model?.context ?? "")"
  51. return URL(string: baseURLString)!
  52. }
  53. var path: String {
  54. switch self {
  55. case .getCategoryAndSectionQuery:
  56. return "/jaxrs/mobile/view/all"
  57. case .getSectionItemQuery(let id):
  58. return "/jaxrs/section/\(id.urlEscaped)"
  59. case .sectionTopItemQuery(let id):
  60. return "/jaxrs/subject/top/\(id.urlEscaped)"
  61. case .subjectByIdQuery(let id):
  62. return "/jaxrs/subject/view/\(id.urlEscaped)"
  63. case .subjectFromSectionByPageQuery(let parameter):
  64. return "/jaxrs/subject/filter/list/page/\(parameter.pageParameter!.currentPageNo)/count/\(parameter.pageParameter!.countByPage)"
  65. case .createSubject:
  66. return "/jaxrs/user/subject"
  67. case .replySubject:
  68. return "/jaxrs/user/reply"
  69. case .getAttachment(let attId):
  70. return "/jaxrs/attachment/\(attId)"
  71. case .uploadAttachForSubject(let subjectId):
  72. return "/jaxrs/attachment/upload/subject/\(subjectId)"
  73. case .downloadAttachForSubject(let att):
  74. return "/jaxrs/attachment/download/\(att.id!)"
  75. case .getSubjectAttachmentList(let subjectId):
  76. return "/jaxrs/subjectattach/list/subject/\(subjectId)"
  77. }
  78. }
  79. var method: Moya.Method {
  80. switch self {
  81. case .getCategoryAndSectionQuery, .getSubjectAttachmentList(_), .getAttachment(_):
  82. return .get
  83. case .getSectionItemQuery(_):
  84. return .get
  85. case .sectionTopItemQuery(_):
  86. return .get
  87. case .subjectByIdQuery(_):
  88. return .get
  89. case .subjectFromSectionByPageQuery(_):
  90. return .put
  91. case .createSubject:
  92. return .post
  93. case .replySubject:
  94. return .post
  95. case .uploadAttachForSubject(_):
  96. return .post
  97. case .downloadAttachForSubject(_):
  98. return .get
  99. }
  100. }
  101. var sampleData: Data {
  102. return "".data(using: String.Encoding.utf8)!
  103. }
  104. var task: Task {
  105. switch self {
  106. case .downloadAttachForSubject(let attachment):
  107. let myDest = getDownDest(filename: attachment.fileName!)
  108. return .downloadDestination(myDest)
  109. case .uploadAttachForSubject(_):
  110. return .requestPlain
  111. case .subjectFromSectionByPageQuery(let parameter):
  112. let param = ["sectionId":parameter.sectionId!,"withTopSubject":parameter.withTopSubject!] as [String : Any]
  113. return .requestParameters(parameters: param, encoding:JSONEncoding.default)
  114. default:
  115. return .requestPlain
  116. }
  117. }
  118. var headers: [String : String]? {
  119. return nil
  120. }
  121. func getDownDest(filename: String) -> DownloadDestination {
  122. let myDest:DownloadDestination = { temporaryURL, response in
  123. let fileURL = O2.cloudFileLocalFolder().appendingPathComponent(filename)
  124. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  125. }
  126. return myDest
  127. }
  128. }