阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 1 | // search.tsx |
| 2 | import React, { useState, useEffect } from 'react'; |
| 3 | import styles from './search.module.css'; |
| 4 | import { useLocation } from "react-router-dom"; |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 5 | import { getSearch } from '@/api/search' |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 6 | |
| 7 | interface PostItem { |
| 8 | postId: number; |
| 9 | userId: number; |
| 10 | postTitle: string; |
| 11 | postContent: string; |
| 12 | createdAt: number; |
| 13 | postType: string; |
| 14 | viewCount: number; |
| 15 | hotScore: number; |
| 16 | lastCalculated: number; |
| 17 | tags?: number[]; |
| 18 | } |
| 19 | |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 20 | const tagMap: Record<string, Record<number, string>> = { |
| 21 | Game: { |
| 22 | 0: "android", |
| 23 | 1: "mac", |
| 24 | 2: "pc", |
| 25 | 3: "ios", |
| 26 | 4: "other", |
| 27 | 5: "action", |
| 28 | 6: "adventure", |
| 29 | 7: "leisure", |
| 30 | 8: "riddle", |
| 31 | 9: "sport", |
| 32 | 10: "strategy", |
| 33 | 11: "table", |
| 34 | }, |
| 35 | video: { |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 36 | 20: "chinese", |
| 37 | 21: "America", |
| 38 | 22: "Japan", |
| 39 | 23: "Koera", |
| 40 | 24: "Europe", |
| 41 | 25: "other", |
| 42 | 26: "Short", |
| 43 | 27: "plot", |
| 44 | 28: "comedy", |
| 45 | 29: "love", |
| 46 | 30: "action", |
| 47 | 31: "terror", |
| 48 | 32: "science fiction", |
| 49 | 33: "commit a crime", |
| 50 | 34: "Thriller", |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 51 | }, |
| 52 | music: { |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 53 | 40: "chinese", |
| 54 | 41: "America", |
| 55 | 42: "Japan", |
| 56 | 43: "Korea", |
| 57 | 44: "Europe", |
| 58 | 45: "other", |
| 59 | 46: "rap", |
| 60 | 47: "Electric sound", |
| 61 | 48: "Guofeng", |
| 62 | 49: "motion", |
| 63 | 50: "ballad", |
| 64 | 51: "Rock and roll", |
| 65 | 52: "classical", |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 66 | }, |
| 67 | software: { |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 68 | 60: "android", |
| 69 | 61: "mac", |
| 70 | 62: "pc", |
| 71 | 63: "ios", |
| 72 | 64: "other", |
| 73 | 65: "life", |
| 74 | 66: "shopping", |
| 75 | 67: "video", |
| 76 | 68: "music", |
| 77 | 69: "read", |
| 78 | 70: "system", |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 79 | }, |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | const SearchPage: React.FC = () => { |
| 83 | const [posts, setPosts] = useState<PostItem[]>([]); |
| 84 | const [filteredPosts, setFilteredPosts] = useState<PostItem[]>([]); |
| 85 | const [selectedPostType, setSelectedPostType] = useState<string>('all'); |
| 86 | const [selectedRating, setSelectedRating] = useState<number | null>(null); |
| 87 | const [selectedTags, setSelectedTags] = useState<number[]>([]); |
| 88 | |
| 89 | const location = useLocation(); |
| 90 | const params = new URLSearchParams(location.search); |
| 91 | const keyword = params.get("keyword"); |
| 92 | |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 93 | const getTagsForPostType = (postType: string) => { |
| 94 | return tagMap[postType] || {}; |
| 95 | }; |
| 96 | |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 97 | useEffect(() => { |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 98 | if (!keyword) return; |
| 99 | |
| 100 | const requestBody = { |
| 101 | keyword, |
| 102 | tags: [] // 可以为空 |
| 103 | }; |
| 104 | |
| 105 | console.log("请求发送:", getSearch, requestBody); |
| 106 | |
| 107 | |
| 108 | fetch(getSearch, { |
| 109 | method: 'Get', |
| 110 | headers: { |
| 111 | 'Content-Type': 'application/json' |
| 112 | }, |
| 113 | body: JSON.stringify(requestBody) |
| 114 | }) |
| 115 | .then(res => res.json()) |
| 116 | .then((res) => { |
| 117 | const data: PostItem[] = res.data; |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 118 | setPosts(data); |
| 119 | setFilteredPosts(data); |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 120 | }) |
| 121 | .catch((error) => { |
| 122 | console.error('搜索请求失败:', error); |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 123 | }); |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 124 | }, [keyword]); |
| 125 | |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 126 | |
| 127 | const applyFilters = () => { |
| 128 | let filtered = posts; |
| 129 | |
| 130 | if (selectedPostType !== 'all') { |
| 131 | filtered = filtered.filter((post) => post.postType === selectedPostType); |
| 132 | } |
| 133 | |
| 134 | if (selectedRating !== null) { |
| 135 | filtered = filtered.filter((post) => post.hotScore >= selectedRating); |
| 136 | } |
| 137 | |
| 138 | if (selectedTags.length > 0) { |
| 139 | filtered = filtered.filter((post) => |
| 140 | post.tags?.some((tag) => selectedTags.includes(tag)) |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | setFilteredPosts(filtered); |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 145 | |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | return ( |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 149 | <div> |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 150 | <div className={styles.secondaryHeader}> |
| 151 | <div className={styles.leftSection}> |
| 152 | <select |
| 153 | value={selectedPostType} |
| 154 | onChange={(e) => setSelectedPostType(e.target.value)} |
| 155 | className={styles.selectBox} |
| 156 | > |
| 157 | <option value="all">所有分区</option> |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 158 | <option value="video">影视</option> |
| 159 | <option value="music">音乐</option> |
| 160 | <option value="Game">游戏</option> |
| 161 | <option value="software">软件</option> |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 162 | </select> |
| 163 | |
| 164 | <select |
| 165 | value={selectedRating || ''} |
| 166 | onChange={(e) => |
| 167 | setSelectedRating(e.target.value ? Number(e.target.value) : null) |
| 168 | } |
| 169 | className={styles.selectBox} |
| 170 | > |
| 171 | <option value="">所有评分</option> |
| 172 | <option value="1">1星及以上</option> |
| 173 | <option value="2">2星及以上</option> |
| 174 | <option value="3">3星及以上</option> |
| 175 | <option value="4">4星及以上</option> |
| 176 | <option value="5">5星</option> |
| 177 | </select> |
| 178 | </div> |
| 179 | |
| 180 | <div className={styles.centerSection}> |
| 181 | <div className={styles.tagFilters}> |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 182 | {Object.entries(getTagsForPostType(selectedPostType)).map(([tagId, tagName]) => ( |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 183 | <label key={tagId}> |
| 184 | <input |
| 185 | type="checkbox" |
| 186 | value={tagId} |
| 187 | checked={selectedTags.includes(Number(tagId))} |
| 188 | onChange={(e) => { |
| 189 | const value = Number(e.target.value); |
| 190 | setSelectedTags((prev) => |
| 191 | prev.includes(value) |
| 192 | ? prev.filter((t) => t !== value) |
| 193 | : [...prev, value] |
| 194 | ); |
| 195 | }} |
| 196 | /> |
| 197 | {tagName} |
| 198 | </label> |
| 199 | ))} |
| 200 | </div> |
| 201 | </div> |
| 202 | |
| 203 | <div className={styles.rightSection}> |
| 204 | <button className={styles.filterButton} onClick={applyFilters}> |
| 205 | 筛选 |
| 206 | </button> |
| 207 | </div> |
| 208 | </div> |
| 209 | |
阳菜,放晴! | ce4a641 | 2025-06-08 14:35:23 +0800 | [diff] [blame^] | 210 | <div className={styles.results}> |
| 211 | {filteredPosts.length === 0 ? ( |
| 212 | <p>暂无搜索结果</p> |
| 213 | ) : ( |
| 214 | <ul className={styles.resultList}> |
| 215 | {filteredPosts.map((post) => ( |
| 216 | <li key={post.postId} className={styles.resultItem}> |
| 217 | <h3>{post.postTitle || '无标题'}</h3> |
| 218 | <p>{post.postContent || '无内容'}</p> |
| 219 | <p>类型:{post.postType || '未知'} | 热度评分:{post.hotScore}</p> |
| 220 | </li> |
| 221 | ))} |
| 222 | </ul> |
| 223 | )} |
| 224 | </div> |
| 225 | |
| 226 | </div> |
| 227 | |
| 228 | |
| 229 | |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 230 | ); |
| 231 | }; |
| 232 | |
| 233 | export default SearchPage; |