O2DesktopAPI.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // O2DesktopAPI.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/12/29.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. // MARK:- 所有调用的API枚举
  11. enum O2DesktopAPI {
  12. case todoItemDetail(String)
  13. case todoedItemDetail(String)
  14. case bbsItemDetail(String)
  15. case cmsItemDetail(String)
  16. case appItemDetail(String)
  17. }
  18. // MARK:- 上下文实现
  19. extension O2DesktopAPI:OOAPIContextCapable {
  20. var apiContextKey: String {
  21. return "x_desktop"
  22. }
  23. }
  24. // MARK: - 是否需要加入x-token访问头
  25. extension O2DesktopAPI:OOAccessTokenAuthorizable {
  26. public var shouldAuthorize: Bool {
  27. return true
  28. }
  29. }
  30. extension O2DesktopAPI:TargetType {
  31. var baseURL: URL {
  32. let model = O2AuthSDK.shared.centerServerInfo()?.webServer
  33. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 80)/\(apiContextKey)"
  34. return URL(string: baseURLString)!
  35. }
  36. var path: String {
  37. switch self {
  38. case .todoItemDetail(let workid):
  39. return "/workmobilewithaction.html?workid=\(workid)&\(String.randomString(length: 6))"
  40. case .todoedItemDetail(let workCompletedId):
  41. return "/workmobilewithaction.html?workcompletedid=\(workCompletedId)&\(String.randomString(length: 6))"
  42. case .bbsItemDetail(let subjectId):
  43. return "/forumdocMobile.html?id=\(subjectId)&\(String.randomString(length: 6))"
  44. case .cmsItemDetail(let documentId):
  45. return "/cmsdocMobile.html?id=\(documentId)&\(String.randomString(length: 6))"
  46. case .appItemDetail(let status):
  47. return "/appMobile.html?app=portal.Portal&status=\(status)&\(String.randomString(length: 6))"
  48. }
  49. }
  50. var method: Moya.Method {
  51. return .get
  52. }
  53. var sampleData: Data {
  54. return "".data(using: String.Encoding.utf8)!
  55. }
  56. var task: Task {
  57. return .requestPlain
  58. }
  59. var headers: [String : String]? {
  60. return nil
  61. }
  62. func getCompleteURL() -> URL {
  63. return URL(string:baseURL.absoluteString+path)!
  64. }
  65. }