//
|
// APIValidate.swift
|
// OKProject
|
//
|
// Created by alvin_y on 2020/5/28.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import Foundation
|
import RxSwift
|
import Moya
|
import HandyJSON
|
|
extension String: HandyJSON{
|
|
}
|
|
extension PrimitiveSequence where Trait == SingleTrait, Element: Mappable {
|
|
func validate() -> Single<Element> {
|
return flatMap { response -> Single<Element> in
|
|
guard response.code == 0 || response.code == 200 else {
|
if response.code == 600 || response.msg == "TOKEN_INVALID"{
|
// token 过期
|
NotificationCenter.default.post(name: NSNotification.Name.init(TokenInvalid), object: nil)
|
throw MoyaError.underlying(NSError(domain: "com.yy.ok.error", code: -9999, userInfo: [NSLocalizedDescriptionKey: "您的身份过期,请重新登录"]), nil)
|
}else{
|
throw MoyaError.underlying(NSError(domain: "com.yy.ok.error", code: -9999, userInfo: [NSLocalizedDescriptionKey: response.msg]), nil)
|
}
|
|
}
|
|
return Single.just(response)
|
}
|
}
|
}
|