后台返回时的处理方法

2019-04-26  本文已影响0人  舟_e9ce

当后台返回<null>时,需要先转化成AnyObject,然后判断通过isEqual是否为NSNull()

static func getRequest(url : String, params : [String : Any]? = nil, finshCallBack : @escaping (T) -> ()){
        let requestUrl : String = Server_URL + url
        HUD.show(.labeledProgress(title: "请求中...", subtitle: ""))
        
        NetworkSessionManager.sharedSessionManager.request(requestUrl, method: HTTPMethod.get, parameters: params, encoding: URLEncoding.default, headers: UserDefaults.getRequestHeaders())
            .validate({ (request, respnse, data) -> Request.ValidationResult in
                return .success
            })
            .responseJSON { (response) in
                HUD.hide()
                switch response.result {
                case .success(let data): do {
                    switch response.response?.statusCode {
                    case 200: do {
                        if ((data as! [String : Any])["data"] as AnyObject).isEqual(NSNull()) {
                            return
                        }
                        let model = Mapper<T>().map(JSON: (data as! [String : Any])["data"] as! [String : Any])!
                        finshCallBack(model)
                        }
                    default: do{
                        let messageM = Mapper<Success>().map(JSON: data as! [String : Any])
                        HUD.flash(.label(messageM?.message), delay: 0.5)
                        }
                    }
                    }
                case .failure(let error): do{
                    switch response.response?.statusCode {
                    case 403: do {
                        HUD.flash(.label("请不要点击太快"), delay: 0.5)
                        }
                    default: do{
                        print("=== \(error)")
                        //                    HUD.flash(.label(error.localizedDescription), delay: 0.5)
                        }
                    }
                    }
                }
        }
上一篇下一篇

猜你喜欢

热点阅读