136 lines
4.2 KiB
Swift
136 lines
4.2 KiB
Swift
//
|
|
// YMNetworkAPI.swift
|
|
// yinmeng-ios
|
|
//
|
|
// Created by Mang on 2024/2/24.
|
|
//
|
|
|
|
import Foundation
|
|
import Alamofire
|
|
import HandyJSON
|
|
import DeviceKit
|
|
typealias SessionCallSucceed = (Any) -> Void
|
|
typealias SessionCallFail = (Int,String) -> Void
|
|
|
|
class YMNetworkHelper: NSObject {
|
|
|
|
var baseParameters: [String: Any] {
|
|
var parameters: [String: Any] = Dictionary()
|
|
parameters["os"] = "iOS"
|
|
parameters["osVersion"] = Device.current.systemVersion
|
|
parameters["model"] = "iPhone14,7"
|
|
parameters["ispType"] = "1"
|
|
parameters["channel"] = APPUtils.currentChannle
|
|
parameters["netType"] = "2"
|
|
parameters["app"] = "mew"
|
|
parameters["appVersion"] = APPUtils.appVersion
|
|
parameters["deviceId"] = APPUtils.currentDeveiceId
|
|
return parameters
|
|
}
|
|
|
|
var sessionNetMana :Session!
|
|
public static let share = YMNetworkHelper.init()
|
|
var headersSet: HTTPHeaders = [
|
|
"Content-Type": "application/json",
|
|
"Authorization": ""
|
|
]
|
|
|
|
func requestSend(type:HTTPMethod,path:String,params:Dictionary<String, Any>, succeed:SessionCallSucceed?,fail:SessionCallFail?) -> Void {
|
|
getHttpRequestHeaders()
|
|
requestSend(type: type,host: AppKeys.api, path: path, params: params, encoding: URLEncoding.queryString, header: headersSet, succeed: succeed, fail: fail)
|
|
}
|
|
|
|
func requestSend(type:HTTPMethod,
|
|
host:String,
|
|
path:String,
|
|
params:[String: Any],
|
|
encoding:ParameterEncoding,
|
|
header:HTTPHeaders,
|
|
succeed:SessionCallSucceed?,
|
|
fail:SessionCallFail?) -> Void {
|
|
let encrypteChonParma = baseParameters.merging(params) {$1}
|
|
sessionNetMana.request(host+path, method: type, parameters: encrypteChonParma,encoding: encoding,headers: header)
|
|
.validate(contentType: ["application/json","text/plain"])
|
|
.responseJSON { [weak self] (response) in
|
|
self?.analyzeThe(response1: response, succeed2: succeed, fail3: fail,uuid4:"")
|
|
}
|
|
}
|
|
|
|
override init() {
|
|
super.init()
|
|
let configCo = AF.sessionConfiguration
|
|
configCo.httpShouldSetCookies = false
|
|
configCo.timeoutIntervalForRequest = 30
|
|
sessionNetMana = Session(configuration: configCo)
|
|
}
|
|
|
|
private func getHttpRequestHeaders() {
|
|
headersSet["pub_uid"] = "\(AuthManager.userUid)"
|
|
headersSet["pub_ticket"] = AuthManager.ticket
|
|
|
|
}
|
|
|
|
|
|
func analyzeThe(response1:AFDataResponse<Any>,
|
|
succeed2:SessionCallSucceed?,
|
|
fail3: SessionCallFail?,uuid4:String) -> Void {
|
|
let UrlSss = response1.request?.url?.absoluteString ?? "unkown"
|
|
switch response1.result {
|
|
case .success:
|
|
let ResponNk :Dictionary = response1.value as? Dictionary<String, Any> ?? Dictionary.init()
|
|
let ResultMo = ResponNk
|
|
if ResultMo.keys.contains("code") {
|
|
let codeNum :Int = ResultMo["code"] as? Int ?? 0
|
|
if codeNum == 200 {
|
|
if ResultMo.keys.contains("data") {
|
|
let DDD = ResultMo["data"] as Any
|
|
succeed2?(DDD)
|
|
}else{
|
|
succeed2?(Dictionary<String, Any>.init())
|
|
}
|
|
}else{
|
|
if codeNum == 401 && UrlSss.contains("auth-center/sso/logout") == false {
|
|
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "SessionTickValid"), object: nil)
|
|
sessionNetMana.cancelAllRequests()
|
|
keyWindow.rootViewController = BaseNavigationViewController(rootViewController:AuthLaunchVC())
|
|
}
|
|
var messageIn = response1.error.debugDescription
|
|
if ResultMo.keys.contains("message") { messageIn = ResultMo["message"] as? String ?? "" }
|
|
fail3?(codeNum,messageIn)
|
|
}
|
|
} else if ResultMo.keys.contains("errno") {
|
|
let CodeNum :Int = ResultMo["errno"] as? Int ?? 0
|
|
if CodeNum == 0 {
|
|
if ResultMo.keys.contains("data") {
|
|
let dataSc = ResultMo["data"] as Any
|
|
succeed2?(dataSc)
|
|
}else{
|
|
succeed2?(Dictionary<String, Any>.init())
|
|
}
|
|
}else{
|
|
var majmessageStr = response1.error.debugDescription
|
|
if ResultMo.keys.contains("errmsg") { majmessageStr = ResultMo["errmsg"] as? String ?? ""}
|
|
fail3?(CodeNum,majmessageStr)
|
|
}
|
|
} else {
|
|
fail3?(10000,"请求失败")
|
|
}
|
|
case let .failure(error):
|
|
var ErrorMssg = response1.error?.errorDescription ?? ""
|
|
var codeNum = response1.error?.responseCode ?? 0
|
|
fail3?(codeNum,ErrorMssg)
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
struct ResponseModel: HandyJSON {
|
|
var code:Int? = 0
|
|
var message:String? = ""
|
|
var data:Any?
|
|
}
|
|
|
|
|