Skip to content

Commit 6beffca

Browse files
committed
feat: 添加访问令牌检查以优化搜索历史记录管理
1 parent 79e9f1b commit 6beffca

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/services/searchService.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import axios from '@/axios/axios';
22
import { get as getCacheKv, set as setCacheKv } from '@/services/cachekv';
3+
import { localuser } from '@/services/localAccount';
34

45
const SEARCH_HISTORY_KEY = 'search_history';
56
const MAX_HISTORY_ITEMS = 10;
@@ -156,7 +157,19 @@ const normalizeSearchHistory = (value) => {
156157
return normalized;
157158
};
158159

160+
const hasAccessToken = () => {
161+
try {
162+
return !!localuser?.isLogin?.value;
163+
} catch {
164+
return false;
165+
}
166+
};
167+
159168
export const loadSearchHistory = async () => {
169+
if (!hasAccessToken()) {
170+
return [];
171+
}
172+
160173
try {
161174
const history = await getCacheKv(SEARCH_HISTORY_KEY);
162175
if (history === undefined || history === null) return [];
@@ -175,6 +188,10 @@ export const loadSearchHistory = async () => {
175188
};
176189

177190
export const addToSearchHistory = async (term, currentHistory = []) => {
191+
if (!hasAccessToken()) {
192+
return [];
193+
}
194+
178195
try {
179196
const normalizedTerm = String(term || '').trim();
180197
if (!normalizedTerm) return normalizeSearchHistory(currentHistory);
@@ -188,11 +205,12 @@ export const addToSearchHistory = async (term, currentHistory = []) => {
188205
if (history.length > MAX_HISTORY_ITEMS) {
189206
history.pop();
190207
}
208+
191209
await setCacheKv(SEARCH_HISTORY_KEY, history);
192210
return history;
193211
} catch (error) {
194212
console.error('Failed to save search history:', error);
195-
return normalizeSearchHistory(currentHistory);
213+
return [];
196214
}
197215
};
198216

0 commit comments

Comments
 (0)