|
| 1 | +import { defineComponent, PropType, ref } from 'vue' |
| 2 | +import { Button, Radio, Select, Popover, addToast } from '@munet/ui' |
| 3 | +import type { SelectOption } from '@munet/ui' |
| 4 | +import { useI18n } from 'vue-i18n' |
| 5 | +import { useStorage } from '@vueuse/core' |
| 6 | +import { apiClient, getMusicList, type MusicListItem } from '@/api' |
| 7 | +import { deleteMusic } from '@/api/customResource' |
| 8 | +import { STEP } from './index' |
| 9 | +import remoteExport from './remoteExport' |
| 10 | + |
| 11 | +export enum OPTIONS { |
| 12 | + None, |
| 13 | + EditProps, |
| 14 | + Delete, |
| 15 | + ExportOpt, |
| 16 | + ExportUgcByName, |
| 17 | + ExportUgcById, |
| 18 | + ExportSusByName, |
| 19 | + ExportSusById, |
| 20 | + ExportJackets, |
| 21 | + ExportMp3, |
| 22 | +} |
| 23 | + |
| 24 | +export enum SUBDIR { |
| 25 | + None, |
| 26 | + Genre, |
| 27 | +} |
| 28 | + |
| 29 | +const SUPPORTS_SUBDIR = (a: OPTIONS) => |
| 30 | + a === OPTIONS.ExportUgcByName || |
| 31 | + a === OPTIONS.ExportUgcById || |
| 32 | + a === OPTIONS.ExportSusByName || |
| 33 | + a === OPTIONS.ExportSusById |
| 34 | + |
| 35 | +const DISABLE_ON_A000 = (a: OPTIONS) => a === OPTIONS.EditProps || a === OPTIONS.Delete |
| 36 | + |
| 37 | +export default defineComponent({ |
| 38 | + props: { |
| 39 | + selectedMusic: { type: Array as PropType<MusicListItem[]>, required: true }, |
| 40 | + continue: { type: Function as PropType<(step: STEP) => void>, required: true }, |
| 41 | + onListUpdated: { type: Function as PropType<(list: MusicListItem[]) => void>, required: true }, |
| 42 | + }, |
| 43 | + setup(props) { |
| 44 | + const { t } = useI18n() |
| 45 | + const selected = ref(OPTIONS.None) |
| 46 | + const subdir = useStorage<SUBDIR>('batchExportSubdir', SUBDIR.None) |
| 47 | + const loading = ref(false) |
| 48 | + const hasA000 = () => props.selectedMusic.some(m => m.assetDir === 'A000') |
| 49 | + |
| 50 | + const subdirOptions = (): SelectOption[] => [ |
| 51 | + { label: t('batch.subdir.none'), value: SUBDIR.None }, |
| 52 | + { label: t('batch.subdir.genre'), value: SUBDIR.Genre }, |
| 53 | + ] |
| 54 | + |
| 55 | + const items: { key: OPTIONS; label: string }[] = [ |
| 56 | + { key: OPTIONS.EditProps, label: t('batch.editProps') }, |
| 57 | + { key: OPTIONS.Delete, label: t('common.delete') }, |
| 58 | + { key: OPTIONS.ExportOpt, label: t('batch.exportOpt') }, |
| 59 | + { key: OPTIONS.ExportUgcByName, label: t('batch.exportUgcByName') }, |
| 60 | + { key: OPTIONS.ExportUgcById, label: t('batch.exportUgcById') }, |
| 61 | + { key: OPTIONS.ExportSusByName, label: t('batch.exportSusByName') }, |
| 62 | + { key: OPTIONS.ExportSusById, label: t('batch.exportSusById') }, |
| 63 | + { key: OPTIONS.ExportJackets, label: t('batch.exportJackets') }, |
| 64 | + { key: OPTIONS.ExportMp3, label: t('batch.exportMp3') }, |
| 65 | + ] |
| 66 | + |
| 67 | + const proceed = async () => { |
| 68 | + switch (selected.value) { |
| 69 | + case OPTIONS.EditProps: |
| 70 | + props.continue(STEP.EditProps) |
| 71 | + return |
| 72 | + case OPTIONS.Delete: { |
| 73 | + loading.value = true |
| 74 | + try { |
| 75 | + for (const m of props.selectedMusic) await deleteMusic(m.id, m.assetDir) |
| 76 | + addToast({ message: t('batch.done'), type: 'success' }) |
| 77 | + props.onListUpdated(await getMusicList()) |
| 78 | + props.continue(STEP.Select) |
| 79 | + } catch (e: any) { |
| 80 | + addToast({ message: String(e?.response?.data || e?.message), type: 'error' }) |
| 81 | + } finally { |
| 82 | + loading.value = false |
| 83 | + } |
| 84 | + return |
| 85 | + } |
| 86 | + case OPTIONS.ExportJackets: |
| 87 | + case OPTIONS.ExportMp3: { |
| 88 | + const endpoint = selected.value === OPTIONS.ExportJackets ? 'BatchExportJackets' : 'BatchExportMp3' |
| 89 | + const filename = selected.value === OPTIONS.ExportJackets ? 'jackets.zip' : 'audio.zip' |
| 90 | + loading.value = true |
| 91 | + try { |
| 92 | + const ids = props.selectedMusic.map(m => ({ id: m.id, assetDir: m.assetDir })) |
| 93 | + const resp = await apiClient.post(`/api/Music/${endpoint}`, { ids }, { responseType: 'blob', timeout: 600000 }) |
| 94 | + const url = URL.createObjectURL(resp.data) |
| 95 | + const a = document.createElement('a') |
| 96 | + a.href = url |
| 97 | + a.download = filename |
| 98 | + a.click() |
| 99 | + URL.revokeObjectURL(url) |
| 100 | + addToast({ message: t('batch.exportSuccess'), type: 'success' }) |
| 101 | + props.continue(STEP.Select) |
| 102 | + } catch (e: any) { |
| 103 | + addToast({ message: String(e?.response?.data || e?.message), type: 'error' }) |
| 104 | + } finally { |
| 105 | + loading.value = false |
| 106 | + } |
| 107 | + return |
| 108 | + } |
| 109 | + case OPTIONS.ExportOpt: |
| 110 | + case OPTIONS.ExportUgcByName: |
| 111 | + case OPTIONS.ExportUgcById: |
| 112 | + case OPTIONS.ExportSusByName: |
| 113 | + case OPTIONS.ExportSusById: |
| 114 | + await remoteExport(props.continue, props.selectedMusic, selected.value, subdir.value, t) |
| 115 | + return |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return () => ( |
| 120 | + <div class="flex flex-col gap-3"> |
| 121 | + <h3 class="text-lg font-bold m-0">{t('batch.chooseAction')}</h3> |
| 122 | + <p class="text-sm op-50 m-0">{t('batch.selected', { count: props.selectedMusic.length })}</p> |
| 123 | + |
| 124 | + <fieldset disabled={loading.value} class="border-none p-0 m-0"> |
| 125 | + <div class="flex flex-col gap-2"> |
| 126 | + {items.map(opt => { |
| 127 | + const disabled = DISABLE_ON_A000(opt.key) && hasA000() |
| 128 | + if (disabled) { |
| 129 | + return ( |
| 130 | + <Popover key={opt.key} trigger="hover"> |
| 131 | + {{ |
| 132 | + trigger: () => ( |
| 133 | + <div class="flex gap-2 items-center op-50"> |
| 134 | + <input type="radio" disabled /> |
| 135 | + <label>{opt.label}</label> |
| 136 | + </div> |
| 137 | + ), |
| 138 | + default: () => t('batch.a000Warning'), |
| 139 | + }} |
| 140 | + </Popover> |
| 141 | + ) |
| 142 | + } |
| 143 | + return ( |
| 144 | + <Radio key={opt.key} k={opt.key} v-model:value={selected.value}> |
| 145 | + {opt.label} |
| 146 | + </Radio> |
| 147 | + ) |
| 148 | + })} |
| 149 | + |
| 150 | + {SUPPORTS_SUBDIR(selected.value) && ( |
| 151 | + <div class="flex items-center gap-2 mt-2 max-w-xs"> |
| 152 | + <span class="text-sm op-60 shrink-0">{t('batch.subdir.label')}</span> |
| 153 | + <Select v-model:value={subdir.value} options={subdirOptions()} /> |
| 154 | + </div> |
| 155 | + )} |
| 156 | + </div> |
| 157 | + </fieldset> |
| 158 | + |
| 159 | + <div class="flex justify-end gap-2 mt-4"> |
| 160 | + <Button onClick={() => props.continue(STEP.Select)} disabled={loading.value}> |
| 161 | + {t('batch.previous')} |
| 162 | + </Button> |
| 163 | + <Button ing={loading.value} disabled={selected.value === OPTIONS.None} onClick={proceed}> |
| 164 | + {t('batch.next')} |
| 165 | + </Button> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + ) |
| 169 | + }, |
| 170 | +}) |
0 commit comments