SwiftMoyaServiceCommunityApi.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // SwiftMoyaServiceCommunityApi.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/6/24.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. // MARK: - 话题组列表
  11. /// 话题组列表
  12. public let kCommunityTopicCategoryApi = "/community/topicCategory"
  13. // MARK: - 关注推荐话题
  14. /// 关注推荐话题
  15. public let kCommunityMemberFollowTopicApi = "/community/memberFollowTopic"
  16. // MARK: - 个人中心内容
  17. /// 个人中心内容
  18. public let kCommunityPostMyApi = "/community/post/my"
  19. // MARK: - 话题列表
  20. /// 话题列表
  21. public let kCommunityTopicsApi = "/community/topic"
  22. // MARK: - 内容列表
  23. /// 内容列表
  24. public let kCommunityPostsApi = "/community/post"
  25. /// 社区
  26. ///
  27. /// - communityTopicCategory: 话题组列表
  28. /// - communityMemberFollowTopic: 关注推荐话题
  29. /// - communityPostMy: 个人中心内容
  30. /// - communityTopics: 话题列表
  31. /// - communityPosts: 内容列表
  32. public enum SwiftMoyaServiceCommunityApi {
  33. case communityTopicCategory(parameters:Dictionary<String, Any>)
  34. case communityMemberFollowTopic(parameters:Dictionary<String, Any>)
  35. case communityPostMy(parameters:Dictionary<String, Any>)
  36. case communityTopics(parameters:Dictionary<String, Any>)
  37. case communityPosts(parameters:Dictionary<String, Any>)
  38. }
  39. extension SwiftMoyaServiceCommunityApi: TargetType {
  40. public var baseURL: URL {
  41. switch self {
  42. case .communityTopicCategory,
  43. .communityMemberFollowTopic,
  44. .communityPostMy,
  45. .communityTopics,
  46. .communityPosts
  47. :
  48. return URL(string: kApiDataPrefix())!
  49. }
  50. }
  51. public var path: String {
  52. switch self {
  53. case .communityTopicCategory:
  54. return kCommunityTopicCategoryApi
  55. case .communityMemberFollowTopic:
  56. return kCommunityMemberFollowTopicApi
  57. case .communityPostMy:
  58. return kCommunityPostMyApi
  59. case .communityTopics:
  60. return kCommunityTopicsApi
  61. case .communityPosts:
  62. return kCommunityPostsApi
  63. }
  64. }
  65. public var method: Moya.Method {
  66. switch self {
  67. case .communityMemberFollowTopic
  68. :
  69. return .post
  70. case .communityTopicCategory,
  71. .communityPostMy,
  72. .communityTopics,
  73. .communityPosts
  74. :
  75. return .get
  76. }
  77. }
  78. // MARK: - 请求任务事件(这里附带上参数)
  79. public var task: Task {
  80. switch self {
  81. case .communityTopicCategory(var parameters),
  82. .communityMemberFollowTopic(var parameters),
  83. .communityPostMy(var parameters),
  84. .communityTopics(var parameters),
  85. .communityPosts(var parameters)
  86. :
  87. let sign = SwiftSign.shared().sign(signType:.AccessToken, parameters: parameters)
  88. parameters.updateValue(sign, forKey: "sign")
  89. return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
  90. }
  91. }
  92. // MARK: - 是否执行Alamofire验证
  93. public var validate: Bool {
  94. return false
  95. }
  96. // MARK: - 这个就是做单元测试模拟的数据,只会在单元测试文件中有作用
  97. public var sampleData: Data {
  98. return "{}".data(using: String.Encoding.utf8)!
  99. }
  100. // MARK: - 请求头
  101. public var headers: [String : String]? {
  102. //同task,具体选择看后台 有application/x-www-form-urlencoded 、application/json
  103. switch self {
  104. case .communityTopicCategory,
  105. .communityMemberFollowTopic,
  106. .communityPostMy,
  107. .communityTopics,
  108. .communityPosts
  109. :
  110. return (headerParameters(headerType: .tokenHeader) as! [String : String])
  111. }
  112. }
  113. }