Swift Moya 的詳細(xì)使用

IOS高級(jí)工程師賈正亮?

好久沒有更新簡書了Swift Moya 的使用

import Moya

importHandyJSON

importMBProgressHUD

letLoadingPlugin =NetworkActivityPlugin{ (type, target)in

? ? guard let vc = topVC else { return }

? ? switchtype {

? ? case.began:

? ? ? ? MBProgressHUD.hide(for: vc.view, animated:false)

? ? ? ? MBProgressHUD.showAdded(to: vc.view, animated:true)

? ? case.ended:

? ? ? ? MBProgressHUD.hide(for: vc.view, animated:true)

? ? }

}

lettimeoutClosure = {(endpoint:Endpoint, closure:MoyaProvider.RequestResultClosure) ->Voidin

? ??ifvarurlRequest =try? endpoint.urlRequest() {

? ? ? ? urlRequest.timeoutInterval=20

? ? ? ? closure(.success(urlRequest))

? ? }else{

? ? ? ? closure(.failure(MoyaError.requestMapping(endpoint.url)))

? ? }

}

letApiProvider =MoyaProvider(requestClosure:timeoutClosure)

letApiLoadingProvider =MoyaProvider(requestClosure:timeoutClosure, plugins: [LoadingPlugin])

請(qǐng)求分類

enum?UApi {

? ? casesearchHot//搜索熱門

? ? casesearchRelative(inputText:String)//相關(guān)搜索

? ? casesearchResult(argCon:Int, q:String)//搜索結(jié)果


? ? caseboutiqueList(sexType:Int)//推薦列表

? ? casespecial(argCon:Int, page:Int)//專題

? ? casevipList//VIP列表

? ? casesubscribeList//訂閱列表

? ? caserankList//排行列表

? ? casecateList//分類列表


? ? casecomicList(argCon:Int, argName:String, argValue:Int, page:Int)//漫畫列表


? ? caseguessLike//猜你喜歡


? ? casedetailStatic(comicid:Int)//詳情(基本)

? ? casedetailRealtime(comicid:Int)//詳情(實(shí)時(shí))

? ? casecommentList(object_id:Int, thread_id:Int, page:Int)//評(píng)論


? ? casechapter(chapter_id:Int)//章節(jié)內(nèi)容

}

//請(qǐng)求配置 實(shí)現(xiàn)TargetType協(xié)議

extension UApi: TargetType {

?//服務(wù)器地址

? ? var baseURL: URL { return URL(string: "http://app.u17.com/v3/appV3_3/ios/phone")! }

//各個(gè)請(qǐng)求的具體路徑

? ? varpath:String{

? ? ? ? switchself{

? ? ? ? case .searchHot: return "search/hotkeywordsnew"

? ? ? ? case .searchRelative: return "search/relative"

? ? ? ? case .searchResult: return "search/searchResult"


? ? ? ? case .boutiqueList: return "comic/boutiqueListNew"

? ? ? ? case .special: return "comic/special"

? ? ? ? case .vipList: return "list/vipList"

? ? ? ? case .subscribeList: return "list/newSubscribeList"

? ? ? ? case.rankList:return"rank/list"


? ? ? ? case .cateList: return "sort/mobileCateList"


? ? ? ? case .comicList: return "list/commonComicList"


? ? ? ? case .guessLike: return "comic/guessLike"


? ? ? ? case .detailStatic: return "comic/detail_static_new"

? ? ? ? case .detailRealtime: return "comic/detail_realtime"

? ? ? ? case .commentList: return "comment/list"


? ? ? ? case .chapter: return "comic/chapterNew"

? ? ? ? }

? ? }

//請(qǐng)求類型

? ? varmethod:Moya.Method{return.get}

//請(qǐng)求任務(wù)事件(這里附帶上參數(shù))

? ? vartask:Task{

? ? ? ? var?parmeters: [String:Any] = [:]

? ? ? ? switch?self{

? ? ? ? case.searchRelative(letinputText):

? ? ? ? ? ? parmeters["inputText"] = inputText


? ? ? ? case.searchResult(letargCon,letq):

? ? ? ? ? ? parmeters["argCon"] = argCon

? ? ? ? ? ? parmeters["q"] = q


? ? ? ? case.boutiqueList(letsexType):

? ? ? ? ? ? parmeters["sexType"] = sexType


? ? ? ? case.special(letargCon,letpage):

? ? ? ? ? ? parmeters["argCon"] = argCon

? ? ? ? ? ? parmeters["page"] =max(1, page)


? ? ? ? case.comicList(letargCon,letargName,letargValue,letpage):

? ? ? ? ? ? parmeters["argCon"] = argCon

? ? ? ? ? ? ifargName.count>0{ parmeters["argName"] = argName }

? ? ? ? ? ? parmeters["argValue"] = argValue

? ? ? ? ? ? parmeters["page"] =max(1, page)


? ? ? ? case.detailStatic(letcomicid),

?? ? ? ? ? ? .detailRealtime(letcomicid):

? ? ? ? ? ? parmeters["comicid"] = comicid


? ? ? ? case.commentList(letobject_id,letthread_id,letpage):

? ? ? ? ? ? parmeters["object_id"] = object_id

? ? ? ? ? ? parmeters["thread_id"] = thread_id

? ? ? ? ? ? parmeters["page"] = page


? ? ? ? case.chapter(letchapter_id):

? ? ? ? ? ? parmeters["chapter_id"] = chapter_id


? ? ? ? default: break

? ? ? ? }


? ? ? ? return.requestParameters(parameters: parmeters, encoding:URLEncoding.default)

? ? }

?//這個(gè)就是做單元測試模擬的數(shù)據(jù),只會(huì)在單元測試文件中有作用

? ? varsampleData:Data{return"".data(using:String.Encoding.utf8)! }

//請(qǐng)求頭

? ? varheaders: [String:String]? {returnnil}

}

extension Response {

? ? funcmapModel(_type:T.Type)throws->T{

? ? ? ? letjsonString =String(data:data, encoding: .utf8)

? ? ? ? guardletmodel =JSONDeserializer.deserializeFrom(json: jsonString)else{

? ? ? ? ? ? throw MoyaError.jsonMapping(self)

? ? ? ? }

? ? ? ? returnmodel

? ? }


}

MoyaProvider類的擴(kuò)展?

extension MoyaProvider {

? ? @discardableResult

? ? openfuncrequest(_target:Target,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? model:T.Type,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completion: ((_returnData:T?) ->Void)?) ->Cancellable? {


? ? ? ? returnrequest(target, completion: { (result)in

? ? ? ? ? ? guardletcompletion = completionelse{return}

? ? ? ? ? ? guardletreturnData =try? result.value?.mapModel(ResponseData.self)else{

? ? ? ? ? ? ? ? completion(nil)

? ? ? ? ? ? ? ? return

? ? ? ? ? ? }

? ? ? ? ? ? completion(returnData.data?.returnData)

? ? ? ? })

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容