RequestPlugin

importUIKit

import Moya

/// 請求是否顯示HUD

var SHOW_HUD: Bool = true

class RequestPlugin: PluginType {


? ? funcwillSend(_request:RequestType, target:TargetType) {

? ? ? ? if SHOW_HUD {

? ? ? ? ? ? HUDManager.showLoading()

? ? ? ? }else{

? ? ? ? ? ? HUDManager.dismiss()

? ? ? ? }

? ? }


? ? func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {

? ? ? ? HUDManager.dismiss()


? ? ? ? switch result {

? ? ? ? ? ? case let .success(response):

? ? ? ? ? ? ? ? #if DEBUG || DEBUG_DEV || DEBUG_SIT || DEBUG_UAT || DEBUG_PRD

? ? ? ? ? ? ? ? if let jsonString =try? response.mapString() {

? ? ? ? ? ? ? ? ? ? Log(response.response?.url ??"")

? ? ? ? ? ? ? ? ? ? Log(jsonString)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? #endif

? ? ? ? ? ? ? ? guard((200...209) ~= response.statusCode)else{

? ? ? ? ? ? ? ? ? ? HUDManager.showTipText("netError".localized())

? ? ? ? ? ? ? ? ? ? return

? ? ? ? ? ? ? ? }

? ? ? ? ? ? case.failure(_):

? ? ? ? ? ? ? ? HUDManager.showTipText("netError".localized())

? ? ? ? ? ? ? ? break

? ? ? ? }

? ? }

}


~service

import Foundation

import Moya

import Alamofire

struct Service {

? ? private init() {}

private static let userProvider= MoyaProvider(session:AlamofireManager.shared, plugins: [RequestPlugin()])

? ? //用戶provider

? ? static func UserProvider(_ show: Bool = true) -> MoyaProvider<UserService> {

? ? ? ? SHOW_HUD = show

? ? ? ? return userProvider

? ? }

private static let trendProvider= MoyaProvider(session:AlamofireManager.shared, plugins: [RequestPlugin()])

? ? //社區(qū)provider

? ? static func TrendProvider(_ show: Bool = true) -> MoyaProvider<TrendService> {

? ? ? ? SHOW_HUD = show

? ? ? ? return trendProvider

? ? }

}

class AlamofireManager: Alamofire.Session {

? ? static let shared: AlamofireManager = {

? ? ? ? let configuration = URLSessionConfiguration.default

? ? ? ? configuration.httpAdditionalHeaders=? HTTPHeaders.default.dictionary

? ? ? ? configuration.timeoutIntervalForRequest=30

? ? ? ? configuration.timeoutIntervalForResource = 60

? ? ? ? return AlamofireManager(configuration: configuration)

? ? }()

}

------------------usrservice -- start ---------------------------

importFoundation

import Moya

enum UserService {

? ? ///登錄注冊

? ? caseuserLogin(mobilePhone:String, validateCode:String)

}

extension UserService: TargetType {

? ? var baseURL:URL{

? ? ? ? return URL(string: ServerMgr.shared.config.UserHost + ServerMgr.shared.config.UserGateway)!

? ? }

? ? var path:String{

? ? ? ? switch self{

? ? ? ? case.userLogin:

? ? ? ? ? ? return "user/app/login"

? ? ? ? case .vertifyPhone:

? ? ? ? ? ? return "validateCode/getCode"

? ? ? ?}

?}


? ? var method:Moya.Method{

? ? ? ? switch self{

? ? ? ? case .vertifyPhone,

?? ? ? ? ? ? .queryUser,

?? ? ? ? ? ? .testDriveList:

? ? ? ? ? ? return .get

? ? ? ? case .userLogin:

? ? ? ? ? ? ?return .post

? ? ? ? }

? ? }

var task:Task{

? ? ? ? switch self{

? ? ? ? //發(fā)送短信驗證碼:

? ? ? ? case .vertifyPhone(let mobilePhone,let messageType):

? ? ? ? ? ? return .requestParameters(parameters: ["mobilePhone": mobilePhone,"messageType":messageType], encoding: URLEncoding.default)

? ? ? ? case .sendSmsPhone(_):

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

}


------------------usrservice -- end ---------------------------

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

相關(guān)閱讀更多精彩內(nèi)容

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