Files
tra-app/src/store/storage.js
T
2025-07-03 11:16:51 +08:00

52 lines
1.2 KiB
JavaScript

import { defineStore } from 'pinia';
import { ref } from 'vue';
export const useStorageStore = defineStore('storageStore',{
state:()=>({
imgObjStr: '{}',
videoObjStr: '{}',
audioObjStr: '{}'
}),
getters:{
imgObj(state){
return JSON.parse(state.imgObjStr)
},
videoObj(state){
return JSON.parse(state.videoObjStr)
},
audioObj(state){
return JSON.parse(state.audioObjStr)
}
},
actions:{
setStorage(type, id, url) {
if(type === 'image'){
let _obj = JSON.parse(this.imgObjStr)
_obj[id] = url
this.imgObjStr = JSON.stringify(_obj)
}else if(type === 'video'){
let _obj = JSON.parse(this.videoObjStr)
_obj[id] = url
this.videoObjStr = JSON.stringify(_obj)
}else if(type === 'audio'){
let _obj = JSON.parse(this.audioObjStr)
_obj[id] = url
this.audioObjStr = JSON.stringify(_obj)
}
},
getStorageById(type, id, cb = (()=>{})){
// 两种方式 ,传递回调函数 或 直接取return值
let _obj
if(type === 'image'){
_obj = JSON.parse(this.imgObjStr)
}else if(type === 'video'){
_obj = JSON.parse(this.videoObjStr)
}else if(type === 'audio'){
_obj = JSON.parse(this.audioObjStr)
}
let _cache = _obj[id]||''
cb && cb(_cache)
return _cache
}
}
})