From 6ff29ae81920cc8d3872f276c37bf7e445016371 Mon Sep 17 00:00:00 2001 From: w940853815 <940853815@qq.com> Date: Fri, 20 Mar 2026 17:57:36 +0800 Subject: [PATCH] fix: miss storage.js --- src/utils/storage.js | 166 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 src/utils/storage.js diff --git a/src/utils/storage.js b/src/utils/storage.js new file mode 100644 index 000000000..5cd388618 --- /dev/null +++ b/src/utils/storage.js @@ -0,0 +1,166 @@ +import VueCookie from 'vue-cookie' +import Cookies from 'js-cookie' + +export function getBasePath() { + if (typeof window === 'undefined') { + return '' + } + return window.__BASE_PATH__ || '' +} + +export function getCookiePath() { + return getBasePath() || '/' +} + +function getLocalStoragePrefix() { + const basePath = getBasePath() + return basePath ? `${basePath}:` : '' +} + +function getScopedStorageKey(key) { + const normalizedKey = String(key) + const prefix = getLocalStoragePrefix() + return prefix ? `${prefix}${normalizedKey}` : normalizedKey +} + +function getRawLocalStorage() { + if (typeof window === 'undefined') { + return null + } + return window.localStorage +} + +export const scopedLocalStorage = { + get length() { + const storage = getRawLocalStorage() + if (!storage) { + return 0 + } + const prefix = getLocalStoragePrefix() + if (!prefix) { + return storage.length + } + let count = 0 + for (let i = 0; i < storage.length; i++) { + const key = storage.key(i) + if (key && key.startsWith(prefix)) { + count++ + } + } + return count + }, + key(index) { + const storage = getRawLocalStorage() + if (!storage) { + return null + } + const prefix = getLocalStoragePrefix() + if (!prefix) { + return storage.key(index) + } + let scopedIndex = 0 + for (let i = 0; i < storage.length; i++) { + const key = storage.key(i) + if (!key || !key.startsWith(prefix)) { + continue + } + if (scopedIndex === index) { + return key.slice(prefix.length) + } + scopedIndex++ + } + return null + }, + getItem(key) { + const storage = getRawLocalStorage() + if (!storage) { + return null + } + return storage.getItem(getScopedStorageKey(key)) + }, + setItem(key, value) { + const storage = getRawLocalStorage() + if (!storage) { + return + } + storage.setItem(getScopedStorageKey(key), value) + }, + removeItem(key) { + const storage = getRawLocalStorage() + if (!storage) { + return + } + storage.removeItem(getScopedStorageKey(key)) + }, + clear() { + const storage = getRawLocalStorage() + if (!storage) { + return + } + const prefix = getLocalStoragePrefix() + if (!prefix) { + storage.clear() + return + } + const keys = [] + for (let i = 0; i < storage.length; i++) { + const key = storage.key(i) + if (key && key.startsWith(prefix)) { + keys.push(key) + } + } + keys.forEach(key => storage.removeItem(key)) + } +} + +function normalizeCookieOptions(options) { + const nextOptions = options && typeof options === 'object' && !(options instanceof Date) + ? { ...options } + : {} + + if ( + options !== undefined && + (options === null || typeof options !== 'object' || options instanceof Date) + ) { + nextOptions.expires = options + } + + if (!nextOptions.path) { + nextOptions.path = getCookiePath() + } + return nextOptions +} + +export const vueCookie = VueCookie + +if (!vueCookie.__BASE_PATH_SCOPED__) { + const originalSet = vueCookie.set.bind(vueCookie) + const originalDelete = vueCookie.delete.bind(vueCookie) + + vueCookie.set = (name, value, options) => { + return originalSet(name, value, normalizeCookieOptions(options)) + } + + vueCookie.delete = (name, options) => { + return originalDelete(name, normalizeCookieOptions(options)) + } + + vueCookie.__BASE_PATH_SCOPED__ = true +} + +export const jsCookie = Cookies + +if (!jsCookie.__BASE_PATH_SCOPED__) { + const originalSet = jsCookie.set.bind(jsCookie) + const originalRemove = jsCookie.remove.bind(jsCookie) + + jsCookie.set = (name, value, options) => { + return originalSet(name, value, normalizeCookieOptions(options)) + } + + jsCookie.remove = (name, options) => { + return originalRemove(name, normalizeCookieOptions(options)) + } + + jsCookie.__BASE_PATH_SCOPED__ = true +}