11import axios from '@/axios/axios' ;
22import { get as getCacheKv , set as setCacheKv } from '@/services/cachekv' ;
3+ import { localuser } from '@/services/localAccount' ;
34
45const SEARCH_HISTORY_KEY = 'search_history' ;
56const 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+
159168export 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
177190export 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