美文网首页
后台返回时的处理方法

后台返回时的处理方法

作者: 舟_e9ce | 来源:发表于2019-04-26 10:03 被阅读0次

    当后台返回<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)
                            }
                        }
                        }
                    }
            }
    

    相关文章

      网友评论

          本文标题:后台返回时的处理方法

          本文链接:https://www.haomeiwen.com/subject/kraxnqtx.html