swift网络框架配置(三)

1.WMGetApiManager(get请求)

import UIKit
import Moya

enum WMGetApiManager {
    //获取配置
    case appConfig
    //获取app信息
    case getAppInfo(phone: String)
}

extension WMGetApiManager: TargetType {
    var baseURL: URL {
        switch self {
        case .appConfig,
             .getAppInfo:
            return URL(string: "https://www.baidu.com/1")!
        }
    }
    
    var headers: [String : String]? {
        let header = [
            "Content-Type": "application/x-www-form-urlencoded"
        ]
        return header
    }
    
    var method: Moya.Method {
        return .get
    }
    
    var path: String {
        switch self {
        case .appConfig:
            return "/getappconfig"
        case let .getAppInfo(phone):
            return "/getAppInfo?phone=\(phone)"
        }
    }
    
    var task: Moya.Task {
        let params = [String: Any]()
        return .requestParameters(parameters: params, encoding: URLEncoding.default)
    }
    
    
}

2.调用

func netWorkRequestAppInfo(phone: String) {
        let api = WMGetApiManager.getAppInfo(phone: phone)
        VToast.showLoading()
        Network.request(api, dataType: Model.self, showErrorMsg: true).do(onSuccess: { [weak self] (data) in
            //返回数据
        }, onError: { error in
            VToast.showToast(msg: (error as? APIError)?.message)
        }, onDispose: {
            VToast.hideLoading()
        }).subscribe().disposed(by: disposeBag)
        
    }

  

posted @ 2023-12-04 12:22  哇哈爽  阅读(32)  评论(0编辑  收藏  举报