// // target.swift // copinism // // Created by 黄仕杰 on 2024/6/4. // import SwiftUI import Foundation import Alamofire import Kingfisher struct Target: Decodable { let id: Int let introduce: String let image: URL let name: String let content: String let duration: Int let rugged: Int let remark: String let learn_count: Int let is_like: Bool let create_time: String let update_time: String let hot: Int } struct TargetData: Decodable{ let total: Int let list: [Target] } //{ // "id": 155, // "introduce": "深入理解社会主义发展历程与未来挑战", // "image": "https://tool.flyaha.top/view/subject/s/public/s8855840.jpg?addree=img1.doubanio.com", // "title": "社会主义发展史纲", // "name": "社会主义发展史纲", // "join_count": 1, // "uid": 3, // "content": "深入理解社会主义发展历程与未来挑战", // "remark": "社会主义发展史纲:的备注信息", // "progress": 100, // "create_time": "2024-04-15", // "update_time": "2024-05-07", // "start_time": "2024-04-15", // "end_time": "2024-04-19", // "learn_count": 1, // "status": 2, // "status_str": "进行中", // "check_in_status": 3, // "check_in_day": 5, // "learning": true, // "hall": false, // "hour": 0, // "minute": 0, // "people_limit": 1 // } struct TargetMe: Decodable { let id: Int let introduce: String let image: URL let title: String let name: String let join_count: Int let uid: Int let content: String let remark: String let progress: Double let create_time: String let update_time: String let start_time: String let end_time: String let learn_count: Int let status: Int let status_str: String let check_in_status: Int let check_in_day: Int let learning: Bool let hall: Bool let hour: Int let minute: Int let people_limit: Int } struct TargetMeData: Decodable{ let total: Int let list: [TargetMe] } struct TargetUIView: View { @State private var image = URL(string: "https://s3.hcpp.top/avatar/ORTPJkyqeULR/%E9%BB%84%E5%AF%8C%E8%B4%B5/1709263869_avatar.jpg?e=1717147962&token=DhKKj58tWve-b3U6BMyFGVUdZ78NXqs4mUhnPaZq:OXHPrsKyvippj5kg-Z65yr4JziI=") var body: some View { Button(action: { let req = TargetReq(page: 1, page_size: 10, tag_id: 0) // Example usage: GetTargets(req: req){ (result: Result, AFError>) in switch result { case .success(let body): if body.code == 0 { image = body.data.list[0].image } else { } case .failure(let error): debugPrint("Error: \(error)") } } }) { KFImage(image) .resizable() .placeholder { ProgressView() } .frame(width: 50, height: 50) .clipShape(Circle()) } } } func GetTargets(req: TargetReq, completion: @escaping (Result, AFError>) -> Void) { makeAlamofireRequest(api: apiprefixV1 + targetApi, method: .post, param: req,completion: completion) } func GetMeTargets(req: TargetMeReq, completion: @escaping (Result, AFError>) -> Void) { makeAlamofireRequest(api: apiprefixV1 + targeMetApi, method: .post, param: req,completion: completion) } #Preview { TargetUIView() }