qingqiu chegngong
This commit is contained in:
134
yinmeng-ios/Base/Request/YMNetworkHelper.swift
Normal file
134
yinmeng-ios/Base/Request/YMNetworkHelper.swift
Normal file
@@ -0,0 +1,134 @@
|
||||
//
|
||||
// YMNetworkAPI.swift
|
||||
// yinmeng-ios
|
||||
//
|
||||
// Created by MaiMang 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"] = "yinmeng"
|
||||
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: "http://beta.api.ymlive.fun/", 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"])
|
||||
.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 maiUrlSss = response1.request?.url?.absoluteString ?? "unkown"
|
||||
switch response1.result {
|
||||
case .success:
|
||||
let maiResponNk :Dictionary = response1.value as? Dictionary<String, Any> ?? Dictionary.init()
|
||||
let maiResultMo = maiResponNk
|
||||
if maiResultMo.keys.contains("code") {
|
||||
let maicodeNum :Int = maiResultMo["code"] as? Int ?? 0
|
||||
if maicodeNum == 200 {
|
||||
if maiResultMo.keys.contains("data") {
|
||||
let maiDDD = maiResultMo["data"] as Any
|
||||
succeed2?(maiDDD)
|
||||
}else{
|
||||
succeed2?(Dictionary<String, Any>.init())
|
||||
}
|
||||
}else{
|
||||
if maicodeNum == 401 && maiUrlSss.contains("auth-center/sso/logout") == false {
|
||||
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MAISessionTickValid"), object: nil)
|
||||
sessionNetMana.cancelAllRequests()
|
||||
}
|
||||
var messageIn = response1.error.debugDescription
|
||||
if maiResultMo.keys.contains("message") { messageIn = maiResultMo["message"] as? String ?? "" }
|
||||
fail3?(maicodeNum,messageIn)
|
||||
}
|
||||
} else if maiResultMo.keys.contains("errno") {
|
||||
let maiCodeNum :Int = maiResultMo["errno"] as? Int ?? 0
|
||||
if maiCodeNum == 0 {
|
||||
if maiResultMo.keys.contains("data") {
|
||||
let dataSc = maiResultMo["data"] as Any
|
||||
succeed2?(dataSc)
|
||||
}else{
|
||||
succeed2?(Dictionary<String, Any>.init())
|
||||
}
|
||||
}else{
|
||||
var majmessageStr = response1.error.debugDescription
|
||||
if maiResultMo.keys.contains("errmsg") { majmessageStr = maiResultMo["errmsg"] as? String ?? ""}
|
||||
fail3?(maiCodeNum,majmessageStr)
|
||||
}
|
||||
} else {
|
||||
fail3?(10000,"请求失败")
|
||||
}
|
||||
case let .failure(error):
|
||||
var maiErrorMssg = response1.error?.errorDescription ?? ""
|
||||
var maicodeNum = response1.error?.responseCode ?? 0
|
||||
fail3?(maicodeNum,maiErrorMssg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct ResponseModel: HandyJSON {
|
||||
var code:Int? = 0
|
||||
var message:String? = ""
|
||||
var data:Any?
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user