OOContactAPI.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // OOContactAPI.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/11/20.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. // MARK: - 所有调用的API
  11. enum OOContactAPI {
  12. //根据组织ID返回组织对象
  13. case unitList([String])
  14. //所有顶层单元
  15. case listTop
  16. //所有顶层单元子单元
  17. case listSubDirect(String)
  18. //根据组织类型查询组织,第一个参数是type 第二个参数是上级组织 可以多值
  19. case unitListByType(String, [String])
  20. //单元信息
  21. case getUnit(String)
  22. //个人信息(包括部门,群组等)
  23. case getPerson(String)
  24. //查找
  25. case iconByPerson(String)
  26. case unitLike(String)
  27. case groupLike(String)
  28. case personLike(String)
  29. case personListNext(String,Int)
  30. case groupListNext(String, Int)
  31. //根据组织查询身份列表
  32. case identityListByUnit(String)
  33. }
  34. // MARK: - 通讯录上下文
  35. extension OOContactAPI:OOAPIContextCapable {
  36. var apiContextKey: String {
  37. return "x_organization_assemble_control"
  38. }
  39. }
  40. // MARK: - 是否需要加入x-token访问头
  41. extension OOContactAPI:OOAccessTokenAuthorizable {
  42. public var shouldAuthorize: Bool {
  43. return true
  44. }
  45. }
  46. // MARK: - 扩展API
  47. extension OOContactAPI:TargetType {
  48. var baseURL: URL {
  49. let model = O2AuthSDK.shared.o2APIServer(context: .x_organization_assemble_control)
  50. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 80)\(model?.context ?? "")"
  51. if let trueUrl = O2AuthSDK.shared.bindUnitTransferUrl2Mapping(url: baseURLString) {
  52. return URL(string: trueUrl)!
  53. }
  54. return URL(string: baseURLString)!
  55. }
  56. var path: String {
  57. switch self {
  58. case .getPerson(let flag):
  59. return "/jaxrs/person/\(flag)"
  60. case .getUnit(let unitFlag):
  61. return "/jaxrs/unit/\(unitFlag)"
  62. case .listSubDirect(let unitFlag):
  63. return "/jaxrs/unit/list/\(unitFlag)/sub/direct"
  64. case .listTop:
  65. return "/jaxrs/unit/list/top"
  66. case .unitList(_):
  67. return "/jaxrs/unit/list"
  68. case .unitListByType(_,_):
  69. return "/jaxrs/unit/list/unit/type"
  70. case .iconByPerson(let pid):
  71. return "/jaxrs/person/\(pid)/icon"
  72. case .unitLike(_):
  73. return "/jaxrs/unit/list/like"
  74. case .groupLike(_):
  75. return "/jaxrs/group/list/like"
  76. case .personLike(_):
  77. return "/jaxrs/person/list/like"
  78. case .personListNext(let flag, let count):
  79. return "/jaxrs/person/list/\(flag)/next/\(count)"
  80. case .groupListNext(let flag, let count):
  81. return "/jaxrs/group/list/\(flag)/next/\(count)"
  82. case .identityListByUnit(let unit):
  83. return "/jaxrs/identity/list/unit/\(unit)"
  84. }
  85. }
  86. var method: Moya.Method {
  87. switch self{
  88. case .getPerson(_),.getUnit(_),.listTop,.listSubDirect(_),.iconByPerson(_),.personListNext(_, _),.groupListNext(_, _),.identityListByUnit(_):
  89. return .get
  90. case .unitLike(_),.groupLike(_),.personLike(_),.unitListByType(_, _):
  91. return .put
  92. case .unitList(_):
  93. return .post
  94. }
  95. }
  96. var sampleData: Data {
  97. return "".data(using: String.Encoding.utf8)!
  98. }
  99. var task: Task {
  100. switch self{
  101. case .getPerson(_),.getUnit(_),.listTop,.listSubDirect(_),.personListNext(_,_),.groupListNext(_, _),.identityListByUnit(_):
  102. return .requestPlain
  103. case .iconByPerson(_):
  104. return .requestPlain
  105. case .groupLike(let searchText):
  106. return .requestParameters(parameters: ["key":searchText], encoding: JSONEncoding.default)
  107. case .unitLike(let searchText):
  108. return .requestParameters(parameters: ["key":searchText], encoding: JSONEncoding.default)
  109. case .unitListByType(let type, let parentList):
  110. return .requestParameters(parameters: ["type": type, "unitList": parentList], encoding: JSONEncoding.default)
  111. case .unitList(let idList):
  112. return .requestParameters(parameters: ["unitList": idList], encoding: JSONEncoding.default)
  113. case .personLike(let searchText):
  114. return .requestParameters(parameters: ["key":searchText], encoding: JSONEncoding.default)
  115. }
  116. }
  117. public var headers: [String : String]? {
  118. return nil
  119. }
  120. }