blob: aaccc99aa6d3696635902d23cd605e5d556cbd10 [file] [log] [blame]
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +08001// search.tsx
阳菜,放晴!33a0d332025-06-08 22:39:42 +08002import React, { useState, useEffect, } from 'react';
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +08003import styles from './search.module.css';
阳菜,放晴!33a0d332025-06-08 22:39:42 +08004import { useLocation, useSearchParams } from "react-router-dom";
阳菜,放晴!2f987042025-06-08 14:54:50 +08005import { getSearch } from '@/api/search'
阳菜,放晴!33a0d332025-06-08 22:39:42 +08006import request from '@/utils/request'; // 路径按你项目调整
7import { useApi } from '@/hooks/request';
8import { debounce } from 'lodash';
9// import { getTagsByMainTag } from '@/utils/common';
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080010
11interface PostItem {
12 postId: number;
13 userId: number;
14 postTitle: string;
15 postContent: string;
16 createdAt: number;
17 postType: string;
18 viewCount: number;
19 hotScore: number;
20 lastCalculated: number;
21 tags?: number[];
22}
23
阳菜,放晴!77743f42025-06-06 23:04:08 +080024const tagMap: Record<string, Record<number, string>> = {
25 Game: {
阳菜,放晴!33a0d332025-06-08 22:39:42 +080026 1: "android",
27 2: "mac",
28 3: "pc",
29 4: "ios",
30 5: "other",
31 6: "action",
32 7: "adventure",
33 8: "leisure",
34 9: "riddle",
35 10: "sport",
36 11: "strategy",
37 12: "table",
阳菜,放晴!77743f42025-06-06 23:04:08 +080038 },
39 video: {
阳菜,放晴!33a0d332025-06-08 22:39:42 +080040 21: "chinese",
41 22: "America",
42 23: "Japan",
43 24: "Koera",
44 25: "Europe",
45 26: "other",
46 27: "Short",
47 28: "plot",
48 29: "comedy",
49 30: "love",
50 31: "action",
51 32: "terror",
52 33: "science fiction",
53 34: "commit a crime",
54 35: "Thriller",
阳菜,放晴!77743f42025-06-06 23:04:08 +080055 },
56 music: {
阳菜,放晴!33a0d332025-06-08 22:39:42 +080057 41: "chinese",
58 42: "America",
59 43: "Japan",
60 44: "Korea",
61 45: "Europe",
62 46: "other",
63 47: "rap",
64 48: "Electric sound",
65 49: "Guofeng",
66 50: "motion",
67 51: "ballad",
68 52: "Rock and roll",
69 53: "classical",
阳菜,放晴!77743f42025-06-06 23:04:08 +080070 },
71 software: {
阳菜,放晴!33a0d332025-06-08 22:39:42 +080072 61: "android",
73 62: "mac",
74 63: "pc",
75 64: "ios",
76 65: "other",
77 66: "life",
78 67: "shopping",
79 68: "video",
80 69: "music",
81 70: "read",
82 71: "system",
阳菜,放晴!77743f42025-06-06 23:04:08 +080083 },
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080084};
85
阳菜,放晴!33a0d332025-06-08 22:39:42 +080086const getTagsByMainTag = (mainTag: string): Record<number, string> => {
87 if (mainTag === "all") return {};
88 return tagMap[mainTag] || {};
89};
90
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080091const SearchPage: React.FC = () => {
阳菜,放晴!33a0d332025-06-08 22:39:42 +080092 const [searchParams] = useSearchParams()
93
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080094 const [selectedPostType, setSelectedPostType] = useState<string>('all');
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080095 const [selectedTags, setSelectedTags] = useState<number[]>([]);
阳菜,放晴!33a0d332025-06-08 22:39:42 +080096 const [postsRes, setPosts] = useState<PostItem[]>([]);
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +080097
阳菜,放晴!33a0d332025-06-08 22:39:42 +080098 const keyword = searchParams.get('keyword') || '';
99
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800100 useEffect(() => {
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800101 if (keyword) {
102 handleSearch({
103 keyword,
104 author: '',
105 tags: []
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800106 });
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800107 }
阳菜,放晴!2f987042025-06-08 14:54:50 +0800108 }, [keyword]);
109
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800110
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800111 const handleSearch = async (params:{
112 keyword: string;
113 author: string;
114 tags: number[];
115 }) => {
116 try {
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800117
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800118 let queryParams: string = '';
119 if(params.keyword.trim() !== '') {
120 // queryParams.append('keyword',params.keyword.trim());
121 queryParams += "keyword";
122 queryParams += "=";
123 queryParams += params.keyword.trim();
124 queryParams += "&";
125 // queryParams.concat("keyword");
126 // queryParams.concat(params.keyword.trim());
127 // queryParams.concat("&");
128 }
129 queryParams += "author";
130 queryParams += "&";
131 if (params.tags.length > 0 || params.author !== '') {
132 // queryParams.append('tags', params.tags.join(','));
133 // queryParams.concat("tags");
134 // queryParams.concat(params.tags.join(','));
135 // queryParams.concat("&");
136 queryParams += "tags";
137 queryParams += "=";
138 if(params.tags.length >0){
139 queryParams += params.author;
140 queryParams += ",";
141 queryParams += params.tags.join(',');
142 }else{
143 queryParams += params.author;
144 }
145
146 }else{
147 // queryParams.append('tags', '');
148 // queryParams.concat("tags");
149 queryParams += "tags";
150 }
151
152 console.log("queryParams",queryParams);
153 const url = `${getSearch}?${queryParams}`;
154 const res = await request.get(url);
155 console.log("url:",url);
156 console.log("res:",res);
157 setPosts(res);
158 } catch (error) {
159 console.error('获取帖子搜索错误', error);
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800160 }
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800161 };
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800162
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800163
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800164 const handleFilterClick = () => {
165 // 分区处理:如果选择"all",则author为空字符串
166 const author = selectedPostType === 'all' ? '' : selectedPostType;
167
168 handleSearch({
169 keyword,
170 author,
171 tags: selectedTags
172 });
173 };
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800174
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800175 const handlePostTypeChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
176 const value = e.target.value;
177 setSelectedPostType(value);
178 // 切换分区时清空已选标签
179 setSelectedTags([]);
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800180 };
181
182 return (
阳菜,放晴!2f987042025-06-08 14:54:50 +0800183 <div>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800184 <div className={styles.secondaryHeader}>
185 <div className={styles.leftSection}>
186 <select
187 value={selectedPostType}
188 onChange={(e) => setSelectedPostType(e.target.value)}
189 className={styles.selectBox}
190 >
191 <option value="all">所有分区</option>
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800192 <option value="20">影视</option>
193 <option value="40">音乐</option>
194 <option value="0">游戏</option>
195 <option value="60">软件</option>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800196 </select>
197
198 <select
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800199 className={styles.selectBox}
200 >
201 <option value="">所有评分</option>
202 <option value="1">1星及以上</option>
203 <option value="2">2星及以上</option>
204 <option value="3">3星及以上</option>
205 <option value="4">4星及以上</option>
206 <option value="5">5星</option>
207 </select>
208 </div>
209
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800210 <div className={styles.centerSection}>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800211 <div className={styles.tagFilters}>
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800212 {selectedPostType !== "all" &&
213 Object.entries(getTagsByMainTag(selectedPostType)).map(([tagId, tagName]) => {
214 const tagIdNum = Number(tagId);
215 return (
216 <label key={tagId}>
217 <input
218 type="checkbox"
219 value={tagId}
220 checked={selectedTags.includes(tagIdNum)}
221 onChange={(e) => {
222 const value = Number(e.target.value);
223 setSelectedTags((prev) =>
224 prev.includes(value)
225 ? prev.filter((t) => t !== value)
226 : [...prev, value]
227 );
228 }}
229 />
230 {tagName}
231 </label>
232 );
233 })
234 }
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800235 </div>
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800236 </div>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800237
238 <div className={styles.rightSection}>
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800239 <button className={styles.filterButton} onClick={handleFilterClick}>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800240 筛选
241 </button>
242 </div>
243 </div>
244
阳菜,放晴!2f987042025-06-08 14:54:50 +0800245 <div className={styles.results}>
阳菜,放晴!33a0d332025-06-08 22:39:42 +0800246 {postsRes?.length === 0 ? (
247 <p>暂无搜索结果</p>
248 ) : (
249 <ul className={styles.resultList}>
250 {postsRes?.map((post) => (
251 <li key={post.postId} className={styles.resultItem}>
252 <h3>{post.postTitle || '无标题'}</h3>
253 <p>{post.postContent || '无内容'}</p>
254 <p>类型:{post.postType || '未知'} | 观看次数:{post.viewCount}</p>
255 </li>
256 ))}
257 </ul>
258 )}
阳菜,放晴!2f987042025-06-08 14:54:50 +0800259 </div>
阳菜,放晴!2f987042025-06-08 14:54:50 +0800260 </div>
阳菜,放晴!7e1e3a52025-06-05 23:00:51 +0800261 );
262};
263
264export default SearchPage;