12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- //
- // MCHttpRxExtension.swift
- // HCQuanfangtong
- //
- // Created by Apple on 2021/12/24.
- // Copyright © 2021 Jyp. All rights reserved.
- //
- import Foundation
- import RxSwift
- import RxCocoa
- import RxAlamofire
- import Alamofire
- import HandyJSON
- extension Observable{
-
- func on(success: @escaping ((Element)->Void), error: @escaping (MCHttpResponseError<Any>)->Void) -> Disposable{
-
- return subscribe(onNext: { e in
- success(e)
- }, onError: {e in
- let model = MCHttpResponseError<Any>(data: nil, msg: nil, status: .error, code: nil)
- Mirror(reflecting: e).children.forEach { (child) in
- if let key = child.label{
- if key == "data"{
- model.data = child.value
- }
- if key == "msg"{
- model.msg = child.value as? String
- }
- if key == "status"{
- model.status = child.value as! MCHttpStatus
- }
- if key == "code"{
- model.code = child.value as? Int
- }
- }
- }
-
- error(model)
- })
- }
-
- }
- extension AnyObserver{
-
- /// 观察者
- /// - Parameters:
- /// - success: 成功
- /// - error: 失败
- /// - beforeAspect: 面向切面 - before
- /// - afterAspect: 面向切面 - after
- /// - Returns: 观察者
- static func create(success: @escaping ((Element)->Void), error: @escaping (MCHttpResponseError<Any>)->Void, beforeAspect: @escaping (()->Void) = {}, afterAspect: @escaping (()->Void) = {}) -> AnyObserver<Element>{
-
- return AnyObserver.init {(event) in
- switch event{
- case .next(let e):
- beforeAspect()
- success(e)
- afterAspect()
- case .error(let e):
- beforeAspect()
- let model = MCHttpResponseError<Any>(data: nil, msg: nil, status: .error, code: nil)
- Mirror(reflecting: e).children.forEach { (child) in
- if let key = child.label{
- if key == "data"{
- model.data = child.value
- }
- if key == "msg"{
- model.msg = child.value as? String
- }
- if key == "status"{
- model.status = child.value as! MCHttpStatus
- }
- if key == "code"{
- model.code = child.value as? Int
- }
- }
- }
- error(model)
- afterAspect()
- case .completed: break
- }
- }
-
- }
- }
|