Moya+Rx.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Moya+Rx.swift
  3. // SwiftBilibili
  4. //
  5. // Created by 罗文 on 2021/1/13.
  6. // Copyright © 2021年 罗文. All rights reserved.
  7. //
  8. import Moya
  9. import RxSwift
  10. import SwiftyJSON
  11. import ObjectMapper
  12. extension PrimitiveSequence where Trait == SingleTrait, Element == Moya.Response {
  13. func map<T: ImmutableMappable>(_ type: T.Type) -> PrimitiveSequence<Trait, T> {
  14. return self
  15. .map { (response) -> T in
  16. let json = try JSON(data: response.data)
  17. guard let code = json[RESULT_CODE].int else { throw RequestError.noCodeKey }
  18. if code != StatusCode.success.rawValue { throw RequestError.sysError(statusCode:"\(code)" , errorMsg: json[RESULT_MESSAGE].string) }
  19. if(T.self == TraineeDataModel.self){
  20. if let data = json.dictionaryObject {
  21. return try Mapper<T>().map(JSON: data)
  22. }
  23. }
  24. if let data = json[RESULT_DATA].dictionaryObject {
  25. return try Mapper<T>().map(JSON: data)
  26. } else if let data = json[RESULT_RESULT].dictionaryObject {
  27. return try Mapper<T>().map(JSON: data)
  28. }
  29. throw RequestError.noDataKey
  30. }.do(onSuccess: { _ in
  31. }, onError: { error in
  32. if error is MapError {
  33. log.error(error)
  34. }
  35. })
  36. }
  37. func map<T: ImmutableMappable>(_ type: T.Type,isModel:Bool) -> PrimitiveSequence<Trait, T> {
  38. return self
  39. .map { (response) -> T in
  40. if(isModel){
  41. let json = try JSON(data: response.data)
  42. guard let code = json[RESULT_CODE].int else { throw RequestError.noCodeKey }
  43. if code != StatusCode.success.rawValue { throw RequestError.sysError(statusCode:"\(code)" , errorMsg: json[RESULT_MESSAGE].string) }
  44. if let data = json.dictionaryObject {
  45. return try Mapper<T>().map(JSON: data)
  46. }
  47. }else {
  48. let json = try JSON(data: response.data)
  49. guard let code = json[RESULT_CODE].int else { throw RequestError.noCodeKey }
  50. if code != StatusCode.success.rawValue { throw RequestError.sysError(statusCode:"\(code)" , errorMsg: json[RESULT_MESSAGE].string) }
  51. if(T.self == TraineeDataModel.self){
  52. if let data = json.dictionaryObject {
  53. return try Mapper<T>().map(JSON: data)
  54. }
  55. }
  56. if let data = json[RESULT_DATA].dictionaryObject {
  57. return try Mapper<T>().map(JSON: data)
  58. } else if let data = json[RESULT_RESULT].dictionaryObject {
  59. return try Mapper<T>().map(JSON: data)
  60. }
  61. }
  62. throw RequestError.noDataKey
  63. }.do(onSuccess: { _ in
  64. }, onError: { error in
  65. if error is MapError {
  66. log.error(error)
  67. }
  68. })
  69. }
  70. func map<T: ImmutableMappable>(_ type: T.Type) -> PrimitiveSequence<Trait, [T]> {
  71. return self
  72. .map { response -> [T] in
  73. let json = try JSON(data: response.data)
  74. guard let code = json[RESULT_CODE].int else { throw RequestError.noCodeKey }
  75. if code != StatusCode.success.rawValue { throw RequestError.sysError(statusCode:"\(code)" , errorMsg: json[RESULT_MESSAGE].string) }
  76. var jsonArray: [Any] = []
  77. if let array = json[RESULT_DATA].arrayObject {
  78. jsonArray = array
  79. } else if let array = json[RESULT_RESULT].arrayObject {
  80. jsonArray = array
  81. } else {
  82. throw RequestError.noDataKey
  83. }
  84. guard let data = try? JSONSerialization.data(withJSONObject: jsonArray, options: JSONSerialization.WritingOptions.prettyPrinted),
  85. let jsonString = String(data: data, encoding: String.Encoding.utf8)
  86. else { throw RequestError.wrongData }
  87. return try Mapper<T>().mapArray(JSONString: jsonString)
  88. }.do(onSuccess: { _ in
  89. }, onError: { error in
  90. if error is MapError {
  91. log.error(error)
  92. }
  93. })
  94. }
  95. }