Files
tra-app/src/store/storage.js
T

99 lines
2.3 KiB
JavaScript

import { defineStore } from 'pinia';
import { ref } from 'vue';
export const useStorageStore = defineStore('storageStore',{
state:()=>({
imgObjStr: '{}',
videoObjStr: '{}',
audioObjStr: '{}',
touchBtnZIndex: 12,
showPreviewDetail: false,
fileType: 'iamge',
fileId: '',
fileSrc: '',
fileSrcList: [],
isBase: false,
gettingVoiceId: ''
}),
getters:{
imgObj(state){
return JSON.parse(state.imgObjStr)
},
videoObj(state){
return JSON.parse(state.videoObjStr)
},
audioObj(state){
return JSON.parse(state.audioObjStr)
},
getTouchBtnZIndex(state){
return state.touchBtnZIndex
},
getShowPreviewDetail(state){
return state.showPreviewDetail
},
getFileType(state){
return state.fileType
},
getFileId(state){
return state.fileId
},
getFileSrc(state){
return state.fileSrc
},
getFileSrcList(state){
return state.fileSrcList
},
getGettingVoiceId(state){
return state.gettingVoiceId
},
getIsBase(state){
return state.isBase
}
},
actions:{
setGettingVoiceId(id){
return this.gettingVoiceId = id
},
setShowPreviewDetail({ status = false, fileType = 'image', fileId = '', fileSrc = '', fileSrcList = [], isBase = false }){
this.showPreviewDetail = status
this.fileId = fileId
this.fileType = fileType
this.fileSrc = fileSrc
this.fileSrcList = fileSrcList
this.isBase = isBase
},
setTouchBtnZIndex(index = 12){
this.touchBtnZIndex = index
},
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
}
}
})