From ebb6cf7b3e89cdce93225bf343d8236dfeff9a9a Mon Sep 17 00:00:00 2001 From: wangfukang <15630117759@163.com> Date: Fri, 23 Oct 2026 09:36:36 +0800 Subject: [PATCH] 1 --- utils/searchHistory.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 utils/searchHistory.js diff --git a/utils/searchHistory.js b/utils/searchHistory.js new file mode 100644 index 0000000..453e7ee --- /dev/null +++ b/utils/searchHistory.js @@ -0,0 +1,22 @@ +const SEARCH_HISTORY_LIMIT = 5 + +export function getSearchHistory(storageKey) { + if (!storageKey) return [] + const cached = uni.getStorageSync(storageKey) + if (!Array.isArray(cached)) return [] + return cached + .map(item => String(item || '').trim()) + .filter(Boolean) + .slice(0, SEARCH_HISTORY_LIMIT) +} + +export function saveSearchHistory(storageKey, keyword) { + const word = String(keyword || '').trim() + const current = getSearchHistory(storageKey) + if (!storageKey || !word) return current + const next = [word] + .concat(current.filter(item => item !== word)) + .slice(0, SEARCH_HISTORY_LIMIT) + uni.setStorageSync(storageKey, next) + return next +}