fuguobin
1 year ago
17 changed files with 266 additions and 113 deletions
Before Width: | Height: | Size: 8.2 KiB After Width: | Height: | Size: 14 KiB |
@ -0,0 +1,111 @@ |
|||
/** |
|||
* Author: Fu Guobin |
|||
* Date: 2022/08/22 |
|||
* Last Modified by: Fu Guobin |
|||
* Last Modified time: 2023/08/28 |
|||
* Copyright:Daniel(Fu Guobin) |
|||
* Description:storage方法封装 |
|||
*/ |
|||
|
|||
import { ref, computed } from 'vue'; |
|||
|
|||
type StorageData = { |
|||
key: string; |
|||
value: any; |
|||
}; |
|||
|
|||
type StorageType = 'localStorage' | 'sessionStorage'; |
|||
|
|||
const getItem = (key: string, storageType: StorageType): any => { |
|||
debugger |
|||
try { |
|||
const storage = getStorage(storageType); |
|||
const item = storage.getItem(key); |
|||
if (item) { |
|||
return JSON.parse(item); |
|||
} |
|||
return null; |
|||
} catch (error) { |
|||
console.error(`Error getting item from ${storageType}: ${error}`); |
|||
return null; |
|||
} |
|||
}; |
|||
|
|||
const setItem = (key: string, value: any, storageType: StorageType): void => { |
|||
try { |
|||
const storage = getStorage(storageType); |
|||
const stringValue = JSON.stringify(value); |
|||
storage.setItem(key, stringValue); |
|||
} catch (error) { |
|||
console.error(`Error setting item in ${storageType}: ${error}`); |
|||
} |
|||
}; |
|||
|
|||
const removeItem = (key: string, storageType: StorageType): void => { |
|||
try { |
|||
const storage = getStorage(storageType); |
|||
storage.removeItem(key); |
|||
} catch (error) { |
|||
console.error(`Error removing item from ${storageType}: ${error}`); |
|||
} |
|||
}; |
|||
|
|||
const clearStorage = (storageType: StorageType): void => { |
|||
try { |
|||
const storage = getStorage(storageType); |
|||
storage.clear(); |
|||
} catch (error) { |
|||
console.error(`Error clearing ${storageType}: ${error}`); |
|||
} |
|||
}; |
|||
|
|||
const getStorage = (storageType: StorageType): Storage => { |
|||
return storageType === 'localStorage' ? localStorage : sessionStorage; |
|||
}; |
|||
|
|||
const storageData = ref<Array<StorageData>>([]); |
|||
|
|||
const initializeStorage = (storageType: StorageType): void => { |
|||
const storage = getStorage(storageType); |
|||
const keys = Object.keys(storage); |
|||
|
|||
storageData.value = keys.map((key) => ({ |
|||
key, |
|||
value: getItem(key, storageType) |
|||
})); |
|||
}; |
|||
|
|||
const useStorage = (storageType: StorageType) => { |
|||
const getUseStorage = () => { |
|||
debugger |
|||
return computed(() => storageData.value); |
|||
}; |
|||
|
|||
const setUseStorage = (key: string, value: any) => { |
|||
setItem(key, value, storageType); |
|||
const newData = { key, value }; |
|||
storageData.value.push(newData); |
|||
}; |
|||
|
|||
const removeUseStorage = (key: string) => { |
|||
removeItem(key, storageType); |
|||
const index = storageData.value.findIndex((item) => item.key === key); |
|||
if (index !== -1) { |
|||
storageData.value.splice(index, 1); |
|||
} |
|||
}; |
|||
|
|||
const clearUseStorage = () => { |
|||
clearStorage(storageType); |
|||
storageData.value = []; |
|||
}; |
|||
|
|||
return { |
|||
getUseStorage, |
|||
setUseStorage, |
|||
removeUseStorage, |
|||
clearUseStorage |
|||
}; |
|||
}; |
|||
|
|||
export default useStorage; |
Loading…
Reference in new issue