feat: 音乐管理功能优化
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
<script setup lang='ts'>
|
||||
import type { IFileData } from '@/components/ImageUpload/type'
|
||||
import type { IFileData } from '@/components/FileUpload/type'
|
||||
import localforage from 'localforage'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import CustomDialog from '@/components/Dialog/index.vue'
|
||||
import ImageUpload from '@/components/ImageUpload/index.vue'
|
||||
import FileUpload from '@/components/FileUpload/index.vue'
|
||||
import useStore from '@/store'
|
||||
|
||||
const { t } = useI18n()
|
||||
@@ -103,7 +103,7 @@ watch(visible, (newVal) => {
|
||||
>
|
||||
<template #content>
|
||||
<div class="flex flex-col items-center gap-6 w-full px-12">
|
||||
<ImageUpload v-if="visible" :limit-type="limitType" @upload-file="uploadFile" />
|
||||
<FileUpload v-if="visible" :limit-type="limitType" @upload-file="uploadFile" />
|
||||
<input v-model="fileName" :disabled="imageData === null" type="text" placeholder="图片名称" class="input w-full">
|
||||
</div>
|
||||
</template>
|
||||
|
||||
128
src/views/Config/Global/MusicConfig/components/UploadDialog.vue
Normal file
128
src/views/Config/Global/MusicConfig/components/UploadDialog.vue
Normal file
@@ -0,0 +1,128 @@
|
||||
<script setup lang='ts'>
|
||||
import type { IFileData } from '@/components/FileUpload/type'
|
||||
import localforage from 'localforage'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useToast } from 'vue-toast-notification'
|
||||
import CustomDialog from '@/components/Dialog/index.vue'
|
||||
import FileUpload from '@/components/FileUpload/index.vue'
|
||||
import useStore from '@/store'
|
||||
|
||||
const toast = useToast()
|
||||
const { t } = useI18n()
|
||||
const limitType = ref('audio/*')
|
||||
const imgUploadToast = ref(0) // 0是不显示,1是成功,2是失败,3是不是图片
|
||||
const visible = defineModel('visible', {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
})
|
||||
const globalConfig = useStore().globalConfig
|
||||
const audioDbStore = localforage.createInstance({
|
||||
name: 'audioStore',
|
||||
})
|
||||
const audioData = ref<IFileData | null>(null)
|
||||
|
||||
const fileName = computed({
|
||||
get() {
|
||||
return audioData.value?.fileName || null
|
||||
},
|
||||
set(value) {
|
||||
if (audioData.value && value) {
|
||||
audioData.value.fileName = value
|
||||
}
|
||||
},
|
||||
})
|
||||
const uploadDialogRef = ref()
|
||||
|
||||
async function uploadFile(fileData: IFileData | null) {
|
||||
if (!fileData) {
|
||||
audioData.value = null
|
||||
return
|
||||
}
|
||||
const isAudio = /audio*/.test(fileData?.type || '')
|
||||
if (!isAudio) {
|
||||
toast.open({
|
||||
message: '不是音频文件',
|
||||
type: 'error',
|
||||
position: 'top-right',
|
||||
})
|
||||
return
|
||||
}
|
||||
audioData.value = fileData
|
||||
}
|
||||
async function getAudioDbStore() {
|
||||
const keys = await audioDbStore.keys()
|
||||
if (keys.length > 0) {
|
||||
audioDbStore.iterate((value: { fileName: string, dataUrl: string }, key: string) => {
|
||||
globalConfig.addMusic({
|
||||
id: key,
|
||||
name: value.fileName,
|
||||
url: 'Storage',
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
function submitUpload() {
|
||||
if (audioData.value) {
|
||||
const { dataUrl, fileName } = audioData.value
|
||||
const uniqueId = uuidv4()
|
||||
audioDbStore.setItem(uniqueId, {
|
||||
dataUrl,
|
||||
fileName,
|
||||
})
|
||||
.then(() => {
|
||||
toast.open({
|
||||
message: '上传成功',
|
||||
type: 'success',
|
||||
position: 'top-right',
|
||||
})
|
||||
getAudioDbStore()
|
||||
})
|
||||
.catch(() => {
|
||||
toast.open({
|
||||
message: '上传失败',
|
||||
type: 'error',
|
||||
position: 'top-right',
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
watch(visible, (newVal) => {
|
||||
if (newVal) {
|
||||
uploadDialogRef.value.showDialog()
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="toast toast-top toast-end">
|
||||
<div v-if="imgUploadToast === 2" class="alert alert-error">
|
||||
<span>{{ t('error.uploadFail') }}</span>
|
||||
</div>
|
||||
<div v-if="imgUploadToast === 1" class="alert alert-success">
|
||||
<span>{{ t('error.uploadSuccess') }}</span>
|
||||
</div>
|
||||
<div v-if="imgUploadToast === 3" class="alert alert-error">
|
||||
<span>{{ t('error.notImage') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<CustomDialog
|
||||
ref="uploadDialogRef"
|
||||
v-model:visible="visible"
|
||||
title="音乐上传"
|
||||
:submit-func="submitUpload"
|
||||
class=""
|
||||
>
|
||||
<template #content>
|
||||
<div class="flex flex-col items-center gap-6 w-full px-12">
|
||||
<FileUpload v-if="visible" :limit-type="limitType" @upload-file="uploadFile" />
|
||||
<input v-model="fileName" :disabled="audioData === null" type="text" placeholder="图片名称" class="input w-full">
|
||||
</div>
|
||||
</template>
|
||||
</CustomDialog>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
||||
@@ -2,22 +2,21 @@
|
||||
import type { IMusic } from '@/types/storeType'
|
||||
import localforage from 'localforage'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import PageHeader from '@/components/PageHeader/index.vue'
|
||||
import useStore from '@/store'
|
||||
import { readFileData } from '@/utils/file'
|
||||
import UploadDialog from './components/UploadDialog.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
const audioUploadToast = ref(0) // 0是不显示,1是成功,2是失败,3是不是图片
|
||||
const audioDbStore = localforage.createInstance({
|
||||
name: 'audioStore',
|
||||
})
|
||||
const globalConfig = useStore().globalConfig
|
||||
|
||||
const { getMusicList: localMusicList } = storeToRefs(globalConfig)
|
||||
const limitType = ref('audio/*')
|
||||
const localMusicListValue = ref(localMusicList)
|
||||
const uploadVisible = ref(false)
|
||||
async function play(item: IMusic) {
|
||||
globalConfig.setCurrentMusic(item, false)
|
||||
}
|
||||
@@ -37,42 +36,10 @@ function deleteAll() {
|
||||
globalConfig.clearMusicList()
|
||||
audioDbStore.clear()
|
||||
}
|
||||
async function getMusicDbStore() {
|
||||
const keys = await audioDbStore.keys()
|
||||
if (keys.length > 0) {
|
||||
audioDbStore.iterate((value: string, key: string) => {
|
||||
globalConfig.addMusic({
|
||||
id: key + new Date().getTime().toString(),
|
||||
name: key,
|
||||
url: 'Storage',
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
async function handleFileChange(e: Event) {
|
||||
const isAudio = /audio*/.test(((e.target as HTMLInputElement).files as FileList)[0].type)
|
||||
if (!isAudio) {
|
||||
audioUploadToast.value = 3
|
||||
|
||||
return
|
||||
}
|
||||
const { dataUrl, fileName } = await readFileData(((e.target as HTMLInputElement).files as FileList)[0])
|
||||
audioDbStore.setItem(`${new Date().getTime().toString()}+${fileName}`, dataUrl)
|
||||
.then(() => {
|
||||
audioUploadToast.value = 1
|
||||
getMusicDbStore()
|
||||
})
|
||||
.catch(() => {
|
||||
audioUploadToast.value = 2
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
getMusicDbStore()
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<UploadDialog v-model:visible="uploadVisible" />
|
||||
<div>
|
||||
<PageHeader title="音乐管理">
|
||||
<template #buttons>
|
||||
@@ -81,11 +48,7 @@ onMounted(() => {
|
||||
{{ t('button.reset') }}
|
||||
</button>
|
||||
<label for="explore">
|
||||
<input
|
||||
id="explore" type="file" class="" style="display: none" :accept="limitType"
|
||||
@change="handleFileChange"
|
||||
>
|
||||
<span class="btn btn-primary btn-sm">{{ t('button.upload') }}</span>
|
||||
<span class="btn btn-primary btn-sm" @click="uploadVisible = true">{{ t('button.upload') }}</span>
|
||||
</label>
|
||||
<button class="btn btn-error btn-sm" @click="deleteAll">
|
||||
{{ t('button.allDelete') }}
|
||||
|
||||
Reference in New Issue
Block a user