宽窄优行-由【嘉易行】项目成品而来
无故事王国
2023-05-25 dc1998fc1ac124f6b9a0e434ccf91103dd936409
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
//  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)
        }
    }
}