// // Moya+Rx.swift // SwiftBilibili // // Created by 罗文 on 2021/1/13. // Copyright © 2021年 罗文. All rights reserved. // import Moya import RxSwift import SwiftyJSON import ObjectMapper extension PrimitiveSequence where Trait == SingleTrait, Element == Moya.Response { func map(_ type: T.Type) -> PrimitiveSequence { return self .map { (response) -> T in let json = try JSON(data: response.data) guard let code = json[RESULT_CODE].int else { throw RequestError.noCodeKey } if code != StatusCode.success.rawValue { throw RequestError.sysError(statusCode:"\(code)" , errorMsg: json[RESULT_MESSAGE].string) } if(T.self == TraineeDataModel.self){ if let data = json.dictionaryObject { return try Mapper().map(JSON: data) } } if let data = json[RESULT_DATA].dictionaryObject { return try Mapper().map(JSON: data) } else if let data = json[RESULT_RESULT].dictionaryObject { return try Mapper().map(JSON: data) } throw RequestError.noDataKey }.do(onSuccess: { _ in }, onError: { error in if error is MapError { log.error(error) } }) } func map(_ type: T.Type,isModel:Bool) -> PrimitiveSequence { return self .map { (response) -> T in if(isModel){ let json = try JSON(data: response.data) guard let code = json[RESULT_CODE].int else { throw RequestError.noCodeKey } if code != StatusCode.success.rawValue { throw RequestError.sysError(statusCode:"\(code)" , errorMsg: json[RESULT_MESSAGE].string) } if let data = json.dictionaryObject { return try Mapper().map(JSON: data) } }else { let json = try JSON(data: response.data) guard let code = json[RESULT_CODE].int else { throw RequestError.noCodeKey } if code != StatusCode.success.rawValue { throw RequestError.sysError(statusCode:"\(code)" , errorMsg: json[RESULT_MESSAGE].string) } if(T.self == TraineeDataModel.self){ if let data = json.dictionaryObject { return try Mapper().map(JSON: data) } } if let data = json[RESULT_DATA].dictionaryObject { return try Mapper().map(JSON: data) } else if let data = json[RESULT_RESULT].dictionaryObject { return try Mapper().map(JSON: data) } } throw RequestError.noDataKey }.do(onSuccess: { _ in }, onError: { error in if error is MapError { log.error(error) } }) } func map(_ type: T.Type) -> PrimitiveSequence { return self .map { response -> [T] in let json = try JSON(data: response.data) guard let code = json[RESULT_CODE].int else { throw RequestError.noCodeKey } if code != StatusCode.success.rawValue { throw RequestError.sysError(statusCode:"\(code)" , errorMsg: json[RESULT_MESSAGE].string) } var jsonArray: [Any] = [] if let array = json[RESULT_DATA].arrayObject { jsonArray = array } else if let array = json[RESULT_RESULT].arrayObject { jsonArray = array } else { throw RequestError.noDataKey } guard let data = try? JSONSerialization.data(withJSONObject: jsonArray, options: JSONSerialization.WritingOptions.prettyPrinted), let jsonString = String(data: data, encoding: String.Encoding.utf8) else { throw RequestError.wrongData } return try Mapper().mapArray(JSONString: jsonString) }.do(onSuccess: { _ in }, onError: { error in if error is MapError { log.error(error) } }) } }