fix api
This commit is contained in:
parent
24f31805da
commit
9479eb2f22
@ -39,58 +39,33 @@ struct API {
|
|||||||
|
|
||||||
// 发送设备的token到服务
|
// 发送设备的token到服务
|
||||||
static func appInit<T: Codable>(userId: String, as: T.Type) async -> APIResponse<T> {
|
static func appInit<T: Codable>(userId: String, as: T.Type) async -> APIResponse<T> {
|
||||||
// Create the request
|
|
||||||
var request = URLRequest(url: URL(string: baseUrl + "/api/appInit")!)
|
|
||||||
request.httpMethod = "POST"
|
|
||||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
||||||
|
|
||||||
// Convert parameters to JSON data
|
|
||||||
|
|
||||||
let device = await UIDevice.current
|
let device = await UIDevice.current
|
||||||
let system = "\(await device.systemName):\(await device.systemVersion)"
|
let system = "\(await device.systemName):\(await device.systemVersion)"
|
||||||
|
|
||||||
let parameters: [String:Any] = [
|
let parameters: [String:Any] = [
|
||||||
"user_id": userId,
|
"user_id": userId,
|
||||||
"system": system,
|
"system": system,
|
||||||
"lang": NSLocale.preferredLanguages.first ?? "",
|
"lang": NSLocale.preferredLanguages.first ?? "",
|
||||||
"vendorId": await UIDevice.current.identifierForVendor?.uuidString ?? ""
|
"vendorId": await UIDevice.current.identifierForVendor?.uuidString ?? ""
|
||||||
]
|
]
|
||||||
let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: [])
|
let request = postRequest(url: "/api/appInit", params: parameters)
|
||||||
request.httpBody = jsonData
|
|
||||||
|
|
||||||
let str = String(data: jsonData, encoding: .utf8)!
|
|
||||||
print("json is: \(str)")
|
|
||||||
|
|
||||||
return await doRequest(request: request, as: T.self)
|
return await doRequest(request: request, as: T.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
static func userEvent<T: Codable>(userId: String, event: String, as: T.Type) async -> APIResponse<T> {
|
static func userEvent<T: Codable>(userId: String, event: String, as: T.Type) async -> APIResponse<T> {
|
||||||
// Create the request
|
|
||||||
var request = URLRequest(url: URL(string: baseUrl + "/api/appInit")!)
|
|
||||||
request.httpMethod = "POST"
|
|
||||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
||||||
|
|
||||||
// Convert parameters to JSON data
|
// Convert parameters to JSON data
|
||||||
|
|
||||||
let parameters: [String:Any] = [
|
let parameters: [String:Any] = [
|
||||||
"user_id": userId,
|
"user_id": userId,
|
||||||
"event": event
|
"event": event
|
||||||
]
|
]
|
||||||
let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: [])
|
let request = postRequest(url: "/api/event", params: parameters)
|
||||||
request.httpBody = jsonData
|
|
||||||
|
|
||||||
let str = String(data: jsonData, encoding: .utf8)!
|
|
||||||
print("json is: \(str)")
|
|
||||||
|
|
||||||
return await doRequest(request: request, as: T.self)
|
return await doRequest(request: request, as: T.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送设备的token到服务
|
// 发送设备的token到服务
|
||||||
static func sendDeviceTokenToServer<T: Codable>(userId: String, token: String, as: T.Type) async -> APIResponse<T> {
|
static func sendDeviceTokenToServer<T: Codable>(userId: String, token: String, as: T.Type) async -> APIResponse<T> {
|
||||||
// Create the request
|
|
||||||
var request = URLRequest(url: URL(string: baseUrl + "/api/device_token")!)
|
|
||||||
request.httpMethod = "POST"
|
|
||||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
||||||
|
|
||||||
// Convert parameters to JSON data
|
// Convert parameters to JSON data
|
||||||
let parameters: [String:Any] = [
|
let parameters: [String:Any] = [
|
||||||
"user_id": userId,
|
"user_id": userId,
|
||||||
@ -101,8 +76,7 @@ struct API {
|
|||||||
"vendorId": await UIDevice.current.identifierForVendor?.uuidString ?? ""
|
"vendorId": await UIDevice.current.identifierForVendor?.uuidString ?? ""
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: [])
|
let request = postRequest(url: "/api/device_token", params: parameters)
|
||||||
request.httpBody = jsonData
|
|
||||||
|
|
||||||
return await doRequest(request: request, as: T.self)
|
return await doRequest(request: request, as: T.self)
|
||||||
}
|
}
|
||||||
@ -181,17 +155,22 @@ struct API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static func resetUserUnreadNum<T: Codable>(userId: String, dramaIds: [Int], as: T.Type) async -> APIResponse<T> {
|
static func resetUserUnreadNum<T: Codable>(userId: String, dramaIds: [Int], as: T.Type) async -> APIResponse<T> {
|
||||||
let jsonData = try! JSONSerialization.data(withJSONObject: ["drama_ids": dramaIds], options: [])
|
let request = postRequest(url: "/api/reset_unread_num?user_id=\(userId)", params: ["drama_ids": dramaIds])
|
||||||
|
|
||||||
let url = URL(string: baseUrl + "/api/reset_unread_num?user_id=\(userId)")!
|
|
||||||
var request = URLRequest(url: url)
|
|
||||||
request.httpMethod = "POST"
|
|
||||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
||||||
request.httpBody = jsonData
|
|
||||||
|
|
||||||
return await doRequest(request: request, as: T.self)
|
return await doRequest(request: request, as: T.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static func postRequest(url: String, params: [String:Any]) -> URLRequest {
|
||||||
|
var request = URLRequest(url: URL(string: baseUrl + url)!)
|
||||||
|
request.httpMethod = "POST"
|
||||||
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||||
|
|
||||||
|
let jsonData = try! JSONSerialization.data(withJSONObject: params, options: [])
|
||||||
|
request.httpBody = jsonData
|
||||||
|
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
|
||||||
// 执行http请求
|
// 执行http请求
|
||||||
private static func doRequest<T: Codable>(request: URLRequest, as: T.Type) async -> APIResponse<T> {
|
private static func doRequest<T: Codable>(request: URLRequest, as: T.Type) async -> APIResponse<T> {
|
||||||
do {
|
do {
|
||||||
|
|||||||
@ -114,9 +114,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
|
|||||||
let response = await API.appInit(userId: userId, as: String.self)
|
let response = await API.appInit(userId: userId, as: String.self)
|
||||||
switch response {
|
switch response {
|
||||||
case .result(let result):
|
case .result(let result):
|
||||||
NSLog("reset user unread num result is: \(result)")
|
NSLog("appInit result is: \(result)")
|
||||||
case .error(let errorCode, let message):
|
case .error(let errorCode, let message):
|
||||||
NSLog("reset user unread error_code: \(errorCode), message: \(message)")
|
NSLog("appInit error_code: \(errorCode), message: \(message)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user