diff --git a/dimensionhub/Core/API.swift b/dimensionhub/Core/API.swift index 869b7e6..6402536 100644 --- a/dimensionhub/Core/API.swift +++ b/dimensionhub/Core/API.swift @@ -27,6 +27,12 @@ struct API { case error(Int32, String) } + // 加载模式 + enum LoadMode: String { + case prev = "prev" + case next = "next" + } + static let url = "http://localhost:18085/api/dimensionhub/" // 服务器地址 @@ -38,8 +44,11 @@ struct API { return await doRequest(request: request, as: T.self) } - static func loadMoreUpdateDramas(as: T.Type) async -> APIResponse { - let request = URLRequest(url: URL(string: url + "load_more_dramas")!) + // 前后刷新获取数据 + static func loadMoreUpdateDramas(userId: Int, mode: LoadMode, id: Int, as: T.Type) async -> APIResponse { + // 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) } @@ -65,18 +74,17 @@ struct API { } let x = String(data: data, encoding: .utf8)! - print("url: \(request.url?.path()), data is: \(x)") do { let result = try JSONDecoder().decode(APISuccessResponse.self, from: data) return .result(result.result) } catch let err { - print(err) + print("http request: \(request.url!.path()), get error: \(err)") let apiError = try JSONDecoder().decode(APIErrorResponse.self, from: data) return .error(apiError.error.code, apiError.error.message) } } catch let err { - print(err) + print("http error: \(err)") return .error(-1, err.localizedDescription) } } diff --git a/dimensionhub/Core/DataModels.swift b/dimensionhub/Core/DataModels.swift deleted file mode 100644 index 7fe7f82..0000000 --- a/dimensionhub/Core/DataModels.swift +++ /dev/null @@ -1,12 +0,0 @@ -// -// DataModels.swift -// dimensionhub -// 定义json处理的model信息 -// Created by 安礼成 on 2025/2/20. -// - -// 首页 - - -// 日期弹出层 - diff --git a/dimensionhub/Core/UserModel.swift b/dimensionhub/Core/UserModel.swift new file mode 100644 index 0000000..0e26ccb --- /dev/null +++ b/dimensionhub/Core/UserModel.swift @@ -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 + } + +} diff --git a/dimensionhub/Views/IndexView.swift b/dimensionhub/Views/IndexView.swift index f7f7ab6..761b542 100644 --- a/dimensionhub/Views/IndexView.swift +++ b/dimensionhub/Views/IndexView.swift @@ -87,8 +87,8 @@ final class IndexModel { } @MainActor - func loadMoreUpdateDramas() async { - let response = await API.loadMoreUpdateDramas(as: [UpdateDramaGroup].self) + func loadMoreUpdateDramas(userId: Int, mode: API.LoadMode, id: Int) async { + let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: id, as: [UpdateDramaGroup].self) if case let .result(groups) = response { let showItems = groupUpdateDramas(updateDramaGroups: groups) self.showUpdateDramas.append(contentsOf: showItems) @@ -111,6 +111,7 @@ final class IndexModel { struct IndexView: View { @Environment(\.modelContext) private var modelContext @Query private var items: [Item] + @Query private var userModel: [UserModel] @State var indexModel = IndexModel() @State var isLoading: Bool = false @@ -207,7 +208,7 @@ struct IndexView: View { if screenBounds.height - frame.minY > 50 && !isLoading { Task { self.isLoading = true - await self.indexModel.loadMoreUpdateDramas() + await self.indexModel.loadMoreUpdateDramas(userId: 1, mode: .next, id: 1234) self.isLoading = false } } @@ -227,7 +228,7 @@ struct IndexView: View { .frame(width: 370) .ignoresSafeArea(edges: .bottom) .task { - await self.indexModel.loadData() + //await self.indexModel.loadData() } } diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index 623e47e..1bab17f 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -13,6 +13,7 @@ struct dimensionhubApp: App { var sharedModelContainer: ModelContainer = { let schema = Schema([ Item.self, + UserModel.self ]) let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)