LaoeGaoci | 85307e6 | 2025-05-30 23:28:42 +0800 | [diff] [blame] | 1 | 'use client'; |
LaoeGaoci | 6825385 | 2025-06-09 22:42:18 +0800 | [diff] [blame] | 2 | import React, { useEffect, useState, useRef } from "react"; |
| 3 | import { Card } from 'primereact/card'; |
| 4 | import { Image } from 'primereact/image'; |
LaoeGaoci | 85307e6 | 2025-05-30 23:28:42 +0800 | [diff] [blame] | 5 | |
LaoeGaoci | 6825385 | 2025-06-09 22:42:18 +0800 | [diff] [blame] | 6 | import { Dialog } from 'primereact/dialog'; |
| 7 | // 页面跳转 |
| 8 | import { useRouter } from 'next/navigation'; |
| 9 | // 消息提醒 |
| 10 | import { Toast } from 'primereact/toast'; |
| 11 | // 分页 |
| 12 | import { Paginator, type PaginatorPageChangeEvent } from 'primereact/paginator'; |
| 13 | |
| 14 | import { Button } from 'primereact/button'; |
| 15 | // 接口传输 |
| 16 | import axios from 'axios'; |
| 17 | // 标签 |
| 18 | import { Tag } from 'primereact/tag'; |
| 19 | |
| 20 | |
| 21 | import { useLocalStorage } from '../../hook/useLocalStorage'; |
| 22 | |
| 23 | import "./purchased-resource.scss" |
| 24 | interface User { |
| 25 | Id: number; |
| 26 | } |
| 27 | // 热门资源数据 |
| 28 | interface Torrent { |
| 29 | torrentRecordId: number; |
| 30 | torrentUrl: string; |
| 31 | infoHash: string; |
| 32 | uploadTime: string; |
| 33 | uploaderUserId: number; |
| 34 | } |
| 35 | |
| 36 | interface ResourceVersion { |
| 37 | resourceVersionId: number; |
| 38 | resourceVersionName: string; |
| 39 | compatibleVersions: string[]; |
| 40 | torrentList: Torrent[]; |
| 41 | seeds: number; |
| 42 | } |
| 43 | |
| 44 | interface Resource { |
| 45 | resourceId: number; |
| 46 | resourceName: string; |
| 47 | resourcePicture: string; |
| 48 | resourceSummary: string; |
| 49 | lastUpdateTime: string; |
| 50 | hot: number; |
| 51 | gameplayList: string[]; |
| 52 | resourceVersionList: ResourceVersion[]; |
| 53 | } |
| 54 | interface ResourceList { |
| 55 | records: Resource[]; |
| 56 | total: number; |
| 57 | } |
| 58 | |
| 59 | export default function PurchasedResource() { |
| 60 | const user = useLocalStorage<User>('user'); |
| 61 | const userId: number = user?.Id ?? -1; |
| 62 | // 热门资源列表 |
| 63 | const [resources, setResources] = useState<Resource[]>([]); |
| 64 | const [totalResource, setTotalResource] = useState(0); |
| 65 | const [visible, setVisible] = useState<boolean>(false); |
| 66 | const [selectedResource, setSelectedResource] = useState<Resource | null>(null); |
| 67 | // 消息提醒 |
| 68 | const toast = useRef<Toast>(null); |
| 69 | const router = useRouter(); |
| 70 | |
| 71 | // 分页 |
| 72 | const [first, setFirst] = useState(0); |
| 73 | const [rows, setRows] = useState(5); |
| 74 | const onPageChange = (event: PaginatorPageChangeEvent) => { |
| 75 | setFirst(event.first); |
| 76 | setRows(event.rows); |
| 77 | }; |
| 78 | // 获取帖子列表 |
| 79 | useEffect(() => { |
| 80 | handleSearch(); |
| 81 | }, [first, rows]); |
| 82 | |
| 83 | const handleSearch = async () => { |
| 84 | |
| 85 | try { |
| 86 | const pageNumber = first / rows + 1; |
| 87 | const response = await axios.get<ResourceList>(process.env.PUBLIC_URL + `/user/purchase`, { |
| 88 | params: { |
| 89 | userId, |
| 90 | pageNumber, |
| 91 | rows |
| 92 | } |
| 93 | }); |
| 94 | console.log(response.data.records); |
| 95 | setResources(response.data.records); |
| 96 | setTotalResource(response.data.total); |
| 97 | } catch (err) { |
| 98 | console.error('搜索资源失败', err); |
| 99 | toast.current?.show({ severity: 'error', summary: 'error', detail: '搜索资源失败' }); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | |
LaoeGaoci | 85307e6 | 2025-05-30 23:28:42 +0800 | [diff] [blame] | 104 | return ( |
LaoeGaoci | 6825385 | 2025-06-09 22:42:18 +0800 | [diff] [blame] | 105 | <div className="PurchasedResource"> |
| 106 | <div className="header"> |
| 107 | <h1>我的资源库</h1> |
| 108 | </div> |
| 109 | {/* 全部社区 */} |
| 110 | <div className="all-resources-list"> |
| 111 | {resources.map((resource) => ( |
| 112 | <Card key={resource.resourceId} className="all-resources-card"> |
| 113 | <Image alt="avatar" src={process.env.NEXT_PUBLIC_NGINX_URL + "Resource/" + resource.resourcePicture} className="resource-avatar" width="250" height="140" onClick={() => router.push(`/resource/resource-detail/${resource.resourceId}`)} /> |
| 114 | <div className="resource-header"> |
| 115 | <div className="resource-content"> |
| 116 | <h3>{resource.resourceName}</h3> |
| 117 | <div className="tags"> |
| 118 | {resource.gameplayList.map((tag, index) => ( |
| 119 | <Tag key={index} value={tag} /> |
| 120 | ))} |
| 121 | </div> |
| 122 | </div> |
| 123 | <div className="resources-states"> |
| 124 | <Button |
| 125 | label="下载" |
| 126 | className="classificationButton" |
| 127 | onClick={() => { |
| 128 | setSelectedResource(resource); |
| 129 | setVisible(true); |
| 130 | }} |
| 131 | /> |
| 132 | </div> |
| 133 | </div> |
| 134 | </Card> |
| 135 | ))} |
| 136 | </div> |
| 137 | {totalResource > 5 && (<Paginator className="Paginator" first={first} rows={rows} totalRecords={totalResource} rowsPerPageOptions={[5, 10]} onPageChange={onPageChange} />)} |
| 138 | <Dialog |
| 139 | header="选择版本并下载" |
| 140 | visible={visible} |
| 141 | style={{ width: '50vw' }} |
| 142 | onHide={() => { |
| 143 | setVisible(false); |
| 144 | setSelectedResource(null); |
| 145 | }} |
| 146 | > |
| 147 | {selectedResource?.resourceVersionList.map((version) => ( |
| 148 | <div key={version.resourceVersionId} style={{ marginBottom: '1.5rem' }}> |
| 149 | <h4>{version.resourceVersionName}</h4> |
| 150 | <p>兼容版本: {version.compatibleVersions.join(', ')}</p> |
| 151 | <div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}> |
| 152 | {version.torrentList.map((torrent) => ( |
| 153 | <Button |
| 154 | key={torrent.torrentRecordId} |
| 155 | label={`种子:${torrent.torrentUrl}`} |
| 156 | onClick={() => { |
| 157 | window.open(torrent.torrentUrl, '_blank'); |
| 158 | }} |
| 159 | className="p-button-sm" |
| 160 | /> |
| 161 | ))} |
| 162 | </div> |
| 163 | </div> |
| 164 | ))} |
| 165 | </Dialog> |
LaoeGaoci | 85307e6 | 2025-05-30 23:28:42 +0800 | [diff] [blame] | 166 | </div> |
| 167 | ); |
| 168 | }; |
| 169 | |