swift网络框架配置(二)
1.WMPostApiManager(post请求)
import Moya enum WMPostApiManager { //登录 case login(login_type: String, id: String, password: String) } extension WMPostApiManager: TargetType { var baseURL: URL { return URL(string: "https://api.socialnb.work/")! } var path: String { switch self { case .login: return "api/user/isLoginNew" } } var method: Moya.Method { return .post } var task: Moya.Task { var params = [String: Any]() switch self { case let .login(login_type, id, password): params["login_type"] = login_type params["id"] = id params["password"] = password } return .requestParameters(parameters: params, encoding: URLEncoding.default) } var headers: [String : String]? { let header = [ "Content-Type": "application/x-www-form-urlencoded" ] return header } public func CRJSONToString(_ obj: Any) -> String? { if let data = try? JSONSerialization.data(withJSONObject: obj, options: []) { if let json = String(data: data, encoding: .utf8) { return json } } return nil } }
2.调用
func netWorkRequestLogin(login_type: String = "123", id:"13412345678", password: String = "123456") { let api = WMPostApiManager.login(login_type: login_type, id: id, password: password) Network.request(api, dataType: JSON.self, showErrorMsg: true).do(onSuccess: { data in guard let datas = data.rawValue as? [String: Any] else { return } keyWindow().rootViewController = SSTabBarViewController() }, onDispose: { //响应 }).subscribe().disposed(by: disposeBag) }