LaoeGaoci | 85307e6 | 2025-05-30 23:28:42 +0800 | [diff] [blame] | 1 | 'use client'; |
LaoeGaoci | 85307e6 | 2025-05-30 23:28:42 +0800 | [diff] [blame] | 2 | |
LaoeGaoci | 76b9ccf | 2025-06-08 22:46:05 +0800 | [diff] [blame] | 3 | import React, { useRef, useState } from 'react'; |
| 4 | import { InputText } from 'primereact/inputtext'; |
| 5 | import { Button } from 'primereact/button'; |
| 6 | import { Card } from 'primereact/card'; |
| 7 | import { Tag } from 'primereact/tag'; |
| 8 | import { Image } from 'primereact/image'; |
| 9 | import { Paginator, PaginatorPageChangeEvent } from 'primereact/paginator'; |
| 10 | // 页面跳转 |
| 11 | import { useRouter } from 'next/navigation'; |
| 12 | // 评分图标 |
| 13 | import { Fire } from '@icon-park/react'; |
| 14 | import { RobotOne } from '@icon-park/react'; |
| 15 | import axios from 'axios'; |
| 16 | |
| 17 | import './search-page.scss'; |
| 18 | |
| 19 | interface Resource { |
| 20 | resourceId: number; |
| 21 | resourceName: string; |
| 22 | resourcePicture: string; |
| 23 | resourceSummary: string; |
| 24 | lastUpdateTime: string; |
| 25 | hot: number; |
| 26 | gamePlayList: { gameplayName: string }[]; |
| 27 | } |
| 28 | |
| 29 | export default function SmartSearchPage() { |
| 30 | const [input, setInput] = useState(''); |
| 31 | const [isSearched, setIsSearched] = useState(false); |
| 32 | |
| 33 | const [resources, setResources] = useState<Resource[]>([]); |
| 34 | const [page, setPage] = useState(0); |
| 35 | const [rows, setRows] = useState(5); |
| 36 | const [totalRecords, setTotalRecords] = useState(0); |
| 37 | const scrollRef = useRef<HTMLDivElement>(null); |
| 38 | const router = useRouter(); |
| 39 | const fetchSearch = async (query: string, page: number, size: number) => { |
| 40 | console.log(page, size,) |
| 41 | const res = await axios.get(process.env.PUBLIC_URL + '/ai', { |
| 42 | params: { input: query, pageNumber: page + 1, rows: size } |
| 43 | }); |
| 44 | console.log(res.data.records) |
| 45 | setResources(res.data.records); |
| 46 | setTotalRecords(res.data.total); |
| 47 | scrollRef.current?.scrollTo({ top: 0, behavior: 'smooth' }); |
| 48 | }; |
| 49 | |
| 50 | const handleSearch = () => { |
| 51 | if (input.trim()) { |
| 52 | setIsSearched(true); // ⬅️ 设置为 true,触发样式移动 |
| 53 | setPage(0); // 重置到第一页 |
| 54 | fetchSearch(input, 0, rows); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | const onPageChange = (e: PaginatorPageChangeEvent) => { |
| 59 | setPage(e.page); |
| 60 | setRows(e.rows); |
| 61 | fetchSearch(input, e.page, e.rows); |
| 62 | }; |
| 63 | |
LaoeGaoci | 85307e6 | 2025-05-30 23:28:42 +0800 | [diff] [blame] | 64 | return ( |
LaoeGaoci | 76b9ccf | 2025-06-08 22:46:05 +0800 | [diff] [blame] | 65 | <div className={`smart-search-page ${!isSearched ? 'centered' : ''}`}> |
| 66 | <div className="search-header">AI 智能搜索助手</div> |
| 67 | |
| 68 | <div className="search-input"> |
| 69 | <InputText |
| 70 | value={input} |
| 71 | onChange={(e) => setInput(e.target.value)} |
| 72 | onKeyDown={(e) => e.key === 'Enter' && handleSearch()} |
| 73 | placeholder="请输入你的需求" |
| 74 | className="input-text" |
| 75 | /> |
| 76 | <Button icon={<RobotOne theme="outline" size="24" fill="#ffffff" />} onClick={handleSearch} className="search-btn" /> |
| 77 | </div> |
| 78 | |
| 79 | <div className="all-resources-list" ref={scrollRef}> |
| 80 | {resources.map((item) => ( |
| 81 | <Card key={item.resourceId} className="all-resources-card" onClick={() => router.push(`/resource/resource-detail/${item.resourceId}`)}> |
| 82 | {/* 左侧图片 */} |
| 83 | <Image |
| 84 | src={process.env.NEXT_PUBLIC_NGINX_URL + item.resourcePicture} |
| 85 | alt={item.resourceName} |
| 86 | width="250" height="140" |
| 87 | preview |
| 88 | /> |
| 89 | |
| 90 | {/* 中间内容 */} |
| 91 | <div className="resource-header"> |
| 92 | <div className="resource-content"> |
| 93 | <h3>{item.resourceName}</h3> |
| 94 | <div className="tags"> |
| 95 | {item.gamePlayList.map((tag, i) => ( |
| 96 | <Tag key={i} value={tag.gameplayName} /> |
| 97 | ))} |
| 98 | </div> |
| 99 | </div> |
| 100 | |
| 101 | {/* 右侧状态栏 */} |
| 102 | <div className="resources-states"> |
| 103 | <div className="state-item"> |
| 104 | <Fire theme="outline" size="16" fill="#FF8D1A" /> |
| 105 | <span>热度:{item.hot}</span> |
| 106 | </div> |
| 107 | <div className="state-item"> |
| 108 | <span>最新更新时间:{item.lastUpdateTime}</span> |
| 109 | </div> |
| 110 | </div> |
| 111 | |
| 112 | </div> |
| 113 | </Card> |
| 114 | ))} |
| 115 | </div> |
| 116 | |
| 117 | {totalRecords > 5 && (<Paginator className="Paginator" first={page * rows} rows={rows} totalRecords={totalRecords} rowsPerPageOptions={[5, 10]} onPageChange={onPageChange} />)} |
LaoeGaoci | 85307e6 | 2025-05-30 23:28:42 +0800 | [diff] [blame] | 118 | </div> |
| 119 | ); |
LaoeGaoci | 76b9ccf | 2025-06-08 22:46:05 +0800 | [diff] [blame] | 120 | } |