54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
interface PreviewItem {
|
|
fileName: string
|
|
name: string
|
|
resourceUrl: string
|
|
type: 'image' | 'video'
|
|
}
|
|
|
|
// 选择资源库图片
|
|
export const useImportFile = (fileList: Ref<PreviewItem[]>) => {
|
|
const showFileExplorer = ref(false)
|
|
const fileType = {
|
|
mainImageUrl: '主图',
|
|
videoUrl: '视频',
|
|
subImageUrl: '副图'
|
|
}
|
|
const curFileType = ref<'mainImageUrl' | 'videoUrl' | 'subImageUrl'>('mainImageUrl')
|
|
const currentPathArray = ref<{ name: string; id: number }[]>([{ name: '根目录', id: 0 }])
|
|
|
|
// 处理文件选择
|
|
const handleChooseResourceFileCallback = (files: PreviewItem[] = []) => {
|
|
handleChooseFiles(curFileType.value, files, fileList.value)
|
|
}
|
|
|
|
const onClickChooseResourceBtn = (type: 'mainImageUrl' | 'videoUrl' | 'subImageUrl') => {
|
|
curFileType.value = type
|
|
showFileExplorer.value = true
|
|
}
|
|
return {
|
|
showFileExplorer,
|
|
currentPathArray,
|
|
handleChooseResourceFileCallback,
|
|
onClickChooseResourceBtn
|
|
}
|
|
}
|
|
|
|
export const handleChooseFiles = (
|
|
fileType: 'mainImageUrl' | 'videoUrl' | 'subImageUrl',
|
|
files: any = [],
|
|
fileList: PreviewItem[]
|
|
) => {
|
|
const fileNameMap = {
|
|
mainImageUrl: '主图',
|
|
videoUrl: '视频',
|
|
subImageUrl: '副图'
|
|
}
|
|
if (['mainImageUrl', 'videoUrl'].includes(fileType)) {
|
|
const index = fileList.findIndex((item: any) => item.fileType === fileType)
|
|
if (index !== -1) {
|
|
fileList.splice(index, 1)
|
|
}
|
|
}
|
|
fileList.push(...files.map((item: any) => ({ ...item, fileType, name: fileNameMap[fileType] })))
|
|
}
|