fix
This commit is contained in:
parent
5ff82b10bb
commit
0ecb1dbb6a
@ -27,6 +27,12 @@ struct API {
|
|||||||
case error(Int32, String)
|
case error(Int32, String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 加载模式
|
||||||
|
enum LoadMode: String {
|
||||||
|
case prev = "prev"
|
||||||
|
case next = "next"
|
||||||
|
}
|
||||||
|
|
||||||
static let url = "http://localhost:18085/api/dimensionhub/"
|
static let url = "http://localhost:18085/api/dimensionhub/"
|
||||||
|
|
||||||
// 服务器地址
|
// 服务器地址
|
||||||
@ -38,8 +44,11 @@ struct API {
|
|||||||
return await doRequest(request: request, as: T.self)
|
return await doRequest(request: request, as: T.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
static func loadMoreUpdateDramas<T: Codable>(as: T.Type) async -> APIResponse<T> {
|
// 前后刷新获取数据
|
||||||
let request = URLRequest(url: URL(string: url + "load_more_dramas")!)
|
static func loadMoreUpdateDramas<T: Codable>(userId: Int, mode: LoadMode, id: Int, as: T.Type) async -> APIResponse<T> {
|
||||||
|
// TODO
|
||||||
|
let request = URLRequest(url: URL(string: baseUrl + "/api/load_more_dramas?user_id=\(userId)&mode=\(mode.rawValue)&id=8030")!)
|
||||||
|
print(request)
|
||||||
|
|
||||||
return await doRequest(request: request, as: T.self)
|
return await doRequest(request: request, as: T.self)
|
||||||
}
|
}
|
||||||
@ -65,18 +74,17 @@ struct API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let x = String(data: data, encoding: .utf8)!
|
let x = String(data: data, encoding: .utf8)!
|
||||||
|
|
||||||
print("url: \(request.url?.path()), data is: \(x)")
|
print("url: \(request.url?.path()), data is: \(x)")
|
||||||
do {
|
do {
|
||||||
let result = try JSONDecoder().decode(APISuccessResponse<T>.self, from: data)
|
let result = try JSONDecoder().decode(APISuccessResponse<T>.self, from: data)
|
||||||
return .result(result.result)
|
return .result(result.result)
|
||||||
} catch let err {
|
} catch let err {
|
||||||
print(err)
|
print("http request: \(request.url!.path()), get error: \(err)")
|
||||||
let apiError = try JSONDecoder().decode(APIErrorResponse.self, from: data)
|
let apiError = try JSONDecoder().decode(APIErrorResponse.self, from: data)
|
||||||
return .error(apiError.error.code, apiError.error.message)
|
return .error(apiError.error.code, apiError.error.message)
|
||||||
}
|
}
|
||||||
} catch let err {
|
} catch let err {
|
||||||
print(err)
|
print("http error: \(err)")
|
||||||
return .error(-1, err.localizedDescription)
|
return .error(-1, err.localizedDescription)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +0,0 @@
|
|||||||
//
|
|
||||||
// DataModels.swift
|
|
||||||
// dimensionhub
|
|
||||||
// 定义json处理的model信息
|
|
||||||
// Created by 安礼成 on 2025/2/20.
|
|
||||||
//
|
|
||||||
|
|
||||||
// 首页
|
|
||||||
|
|
||||||
|
|
||||||
// 日期弹出层
|
|
||||||
|
|
||||||
21
dimensionhub/Core/UserModel.swift
Normal file
21
dimensionhub/Core/UserModel.swift
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// UserModel.swift
|
||||||
|
// dimensionhub
|
||||||
|
//
|
||||||
|
// Created by 安礼成 on 2025/2/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
|
@Model
|
||||||
|
final class UserModel {
|
||||||
|
var userId: Int
|
||||||
|
var username: String
|
||||||
|
|
||||||
|
init(userId: Int, username: String) {
|
||||||
|
self.userId = userId
|
||||||
|
self.username = username
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -87,8 +87,8 @@ final class IndexModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
func loadMoreUpdateDramas() async {
|
func loadMoreUpdateDramas(userId: Int, mode: API.LoadMode, id: Int) async {
|
||||||
let response = await API.loadMoreUpdateDramas(as: [UpdateDramaGroup].self)
|
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: id, as: [UpdateDramaGroup].self)
|
||||||
if case let .result(groups) = response {
|
if case let .result(groups) = response {
|
||||||
let showItems = groupUpdateDramas(updateDramaGroups: groups)
|
let showItems = groupUpdateDramas(updateDramaGroups: groups)
|
||||||
self.showUpdateDramas.append(contentsOf: showItems)
|
self.showUpdateDramas.append(contentsOf: showItems)
|
||||||
@ -111,6 +111,7 @@ final class IndexModel {
|
|||||||
struct IndexView: View {
|
struct IndexView: View {
|
||||||
@Environment(\.modelContext) private var modelContext
|
@Environment(\.modelContext) private var modelContext
|
||||||
@Query private var items: [Item]
|
@Query private var items: [Item]
|
||||||
|
@Query private var userModel: [UserModel]
|
||||||
|
|
||||||
@State var indexModel = IndexModel()
|
@State var indexModel = IndexModel()
|
||||||
@State var isLoading: Bool = false
|
@State var isLoading: Bool = false
|
||||||
@ -207,7 +208,7 @@ struct IndexView: View {
|
|||||||
if screenBounds.height - frame.minY > 50 && !isLoading {
|
if screenBounds.height - frame.minY > 50 && !isLoading {
|
||||||
Task {
|
Task {
|
||||||
self.isLoading = true
|
self.isLoading = true
|
||||||
await self.indexModel.loadMoreUpdateDramas()
|
await self.indexModel.loadMoreUpdateDramas(userId: 1, mode: .next, id: 1234)
|
||||||
self.isLoading = false
|
self.isLoading = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -227,7 +228,7 @@ struct IndexView: View {
|
|||||||
.frame(width: 370)
|
.frame(width: 370)
|
||||||
.ignoresSafeArea(edges: .bottom)
|
.ignoresSafeArea(edges: .bottom)
|
||||||
.task {
|
.task {
|
||||||
await self.indexModel.loadData()
|
//await self.indexModel.loadData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,6 +13,7 @@ struct dimensionhubApp: App {
|
|||||||
var sharedModelContainer: ModelContainer = {
|
var sharedModelContainer: ModelContainer = {
|
||||||
let schema = Schema([
|
let schema = Schema([
|
||||||
Item.self,
|
Item.self,
|
||||||
|
UserModel.self
|
||||||
])
|
])
|
||||||
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
|
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user