feat: select media by type
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
|
import { mediaService } from '@/api/mediaService';
|
||||||
import { useToast } from 'primevue/usetoast';
|
import { useToast } from 'primevue/usetoast';
|
||||||
import { reactive, ref } from 'vue';
|
import { computed, reactive, ref } from 'vue';
|
||||||
import { useRouter } from 'vue-router';
|
import { useRouter } from 'vue-router';
|
||||||
|
|
||||||
// PrimeVue components
|
// PrimeVue components
|
||||||
@@ -12,6 +13,7 @@ import Dialog from 'primevue/dialog';
|
|||||||
import InputNumber from 'primevue/inputnumber';
|
import InputNumber from 'primevue/inputnumber';
|
||||||
import InputText from 'primevue/inputtext';
|
import InputText from 'primevue/inputtext';
|
||||||
import ProgressSpinner from 'primevue/progressspinner';
|
import ProgressSpinner from 'primevue/progressspinner';
|
||||||
|
import RadioButton from 'primevue/radiobutton';
|
||||||
import Textarea from 'primevue/textarea';
|
import Textarea from 'primevue/textarea';
|
||||||
import Toast from 'primevue/toast';
|
import Toast from 'primevue/toast';
|
||||||
|
|
||||||
@@ -23,7 +25,8 @@ const post = reactive({
|
|||||||
title: '',
|
title: '',
|
||||||
price: 0,
|
price: 0,
|
||||||
introduction: '',
|
introduction: '',
|
||||||
selectedMedia: []
|
selectedMedia: [],
|
||||||
|
status: 'draft' // Add status field with default value
|
||||||
});
|
});
|
||||||
|
|
||||||
// Validation state
|
// Validation state
|
||||||
@@ -38,68 +41,42 @@ const mediaDialogVisible = ref(false);
|
|||||||
const selectedMediaItems = ref([]);
|
const selectedMediaItems = ref([]);
|
||||||
const mediaLoading = ref(false);
|
const mediaLoading = ref(false);
|
||||||
const mediaGlobalFilter = ref('');
|
const mediaGlobalFilter = ref('');
|
||||||
|
const mediaItems = ref([]);
|
||||||
|
|
||||||
// Sample media data - in a real app, this would come from an API
|
// Add pagination state for media dialog
|
||||||
const mediaItems = ref([
|
const mediaFirst = ref(0);
|
||||||
{
|
const mediaRows = ref(10);
|
||||||
id: 1,
|
const mediaTotalRecords = ref(0);
|
||||||
fileName: 'sunset-beach.jpg',
|
const mediaCurrentPage = ref(1);
|
||||||
fileType: 'Image',
|
|
||||||
thumbnailUrl: 'https://via.placeholder.com/300x225',
|
const mediaTotalPages = computed(() => {
|
||||||
fileSize: '2.4 MB',
|
return Math.ceil(mediaTotalRecords.value / mediaRows.value);
|
||||||
uploadTime: 'Today, 10:30 AM'
|
});
|
||||||
},
|
|
||||||
{
|
// Status options
|
||||||
id: 2,
|
const statusOptions = [
|
||||||
fileName: 'presentation.pdf',
|
{ label: '发布', value: 'published' },
|
||||||
fileType: 'PDF',
|
{ label: '草稿', value: 'draft' }
|
||||||
thumbnailUrl: null,
|
];
|
||||||
fileSize: '4.8 MB',
|
|
||||||
uploadTime: 'Yesterday, 3:45 PM'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 3,
|
|
||||||
fileName: 'promo_video.mp4',
|
|
||||||
fileType: 'Video',
|
|
||||||
thumbnailUrl: null,
|
|
||||||
fileSize: '24.8 MB',
|
|
||||||
uploadTime: 'Aug 28, 2023'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 4,
|
|
||||||
fileName: 'report_q3.docx',
|
|
||||||
fileType: 'Document',
|
|
||||||
thumbnailUrl: null,
|
|
||||||
fileSize: '1.2 MB',
|
|
||||||
uploadTime: 'Aug 25, 2023'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 5,
|
|
||||||
fileName: 'podcast_interview.mp3',
|
|
||||||
fileType: 'Audio',
|
|
||||||
thumbnailUrl: null,
|
|
||||||
fileSize: '18.5 MB',
|
|
||||||
uploadTime: 'Aug 20, 2023'
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Open media selection dialog
|
// Open media selection dialog
|
||||||
const openMediaDialog = () => {
|
const openMediaDialog = () => {
|
||||||
mediaDialogVisible.value = true;
|
mediaDialogVisible.value = true;
|
||||||
|
mediaCurrentPage.value = 1;
|
||||||
|
mediaFirst.value = 0;
|
||||||
loadMediaItems();
|
loadMediaItems();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Load media items
|
// Load media items with pagination
|
||||||
const loadMediaItems = async () => {
|
const loadMediaItems = async () => {
|
||||||
mediaLoading.value = true;
|
mediaLoading.value = true;
|
||||||
try {
|
try {
|
||||||
// In a real app, this would be an API call
|
const response = await mediaService.getMedias({
|
||||||
// const response = await mediaApi.getMediaFiles();
|
page: mediaCurrentPage.value,
|
||||||
// mediaItems.value = response.data;
|
limit: mediaRows.value
|
||||||
|
});
|
||||||
// Simulate API delay
|
mediaItems.value = response.items;
|
||||||
await new Promise(resolve => setTimeout(resolve, 500));
|
mediaTotalRecords.value = response.total;
|
||||||
// Using sample data already defined above
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.add({ severity: 'error', summary: '错误', detail: '加载媒体文件失败', life: 3000 });
|
toast.add({ severity: 'error', summary: '错误', detail: '加载媒体文件失败', life: 3000 });
|
||||||
} finally {
|
} finally {
|
||||||
@@ -107,6 +84,14 @@ const loadMediaItems = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Handle media pagination
|
||||||
|
const onMediaPage = (event) => {
|
||||||
|
mediaFirst.value = event.first;
|
||||||
|
mediaRows.value = event.rows;
|
||||||
|
mediaCurrentPage.value = Math.floor(event.first / event.rows) + 1;
|
||||||
|
loadMediaItems();
|
||||||
|
};
|
||||||
|
|
||||||
// Confirm media selection
|
// Confirm media selection
|
||||||
const confirmMediaSelection = () => {
|
const confirmMediaSelection = () => {
|
||||||
if (selectedMediaItems.value.length) {
|
if (selectedMediaItems.value.length) {
|
||||||
@@ -201,6 +186,21 @@ const getFileIcon = (file) => {
|
|||||||
};
|
};
|
||||||
return `pi ${map[file.fileType] || 'pi-file'}`;
|
return `pi ${map[file.fileType] || 'pi-file'}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Format file size helper
|
||||||
|
const formatFileSize = (bytes) => {
|
||||||
|
if (bytes === 0) return '0 B';
|
||||||
|
const k = BigInt(1024);
|
||||||
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
|
const bytesValue = BigInt(bytes);
|
||||||
|
let i = 0;
|
||||||
|
let size = bytesValue;
|
||||||
|
while (size >= k && i < sizes.length - 1) {
|
||||||
|
size = size / k;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return `${Number(size).toLocaleString('en-US')} ${sizes[i]}`;
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -224,8 +224,20 @@ const getFileIcon = (file) => {
|
|||||||
<small v-if="errors.title" class="p-error">{{ errors.title }}</small>
|
<small v-if="errors.title" class="p-error">{{ errors.title }}</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Status -->
|
||||||
|
<div class="col-span-2">
|
||||||
|
<label class="block text-sm font-medium text-gray-700 mb-1">状态</label>
|
||||||
|
<div class="flex gap-4">
|
||||||
|
<div v-for="option in statusOptions" :key="option.value" class="flex items-center">
|
||||||
|
<RadioButton :value="option.value" v-model="post.status"
|
||||||
|
:inputId="'status_' + option.value" />
|
||||||
|
<label :for="'status_' + option.value" class="ml-2">{{ option.label }}</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Price -->
|
<!-- Price -->
|
||||||
<div class="col-span-1">
|
<div class="col-span-2">
|
||||||
<label for="price" class="block text-sm font-medium text-gray-700 mb-1">价格 (¥)</label>
|
<label for="price" class="block text-sm font-medium text-gray-700 mb-1">价格 (¥)</label>
|
||||||
<InputNumber id="price" v-model="post.price" mode="currency" currency="CNY" :minFractionDigits="2"
|
<InputNumber id="price" v-model="post.price" mode="currency" currency="CNY" :minFractionDigits="2"
|
||||||
class="w-full" />
|
class="w-full" />
|
||||||
@@ -290,9 +302,22 @@ const getFileIcon = (file) => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<DataTable v-model:selection="selectedMediaItems" :value="mediaItems" :loading="mediaLoading" dataKey="id"
|
<DataTable v-model:selection="selectedMediaItems" :value="mediaItems" :loading="mediaLoading" dataKey="id"
|
||||||
selectionMode="multiple" :globalFilterFields="['fileName', 'fileType']"
|
:paginator="true" v-model:first="mediaFirst" v-model:rows="mediaRows" :totalRecords="mediaTotalRecords"
|
||||||
:filters="{ global: { value: mediaGlobalFilter, matchMode: 'contains' } }" stripedRows
|
@page="onMediaPage" selectionMode="multiple"
|
||||||
responsiveLayout="scroll">
|
paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport"
|
||||||
|
:rows-per-page-options="[10, 25, 50]" currentPageReportTemplate="第 {first} 到 {last} 条,共 {totalRecords} 条"
|
||||||
|
:lazy="true" :showCurrentPageReport="true">
|
||||||
|
|
||||||
|
<template #paginatorLeft>
|
||||||
|
<div class="flex items-center">
|
||||||
|
每页: {{ mediaRows }}
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<template #paginatorRight>
|
||||||
|
<div class="flex items-center">
|
||||||
|
第 {{ mediaCurrentPage }} 页,共 {{ mediaTotalPages }} 页
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
<template #empty>
|
<template #empty>
|
||||||
<div class="text-center p-4">没有可用的媒体文件</div>
|
<div class="text-center p-4">没有可用的媒体文件</div>
|
||||||
@@ -328,7 +353,12 @@ const getFileIcon = (file) => {
|
|||||||
</template>
|
</template>
|
||||||
</Column>
|
</Column>
|
||||||
|
|
||||||
<Column field="fileSize" header="文件大小"></Column>
|
<Column field="file_size" header="文件大小">
|
||||||
|
<template #body="{ data }">
|
||||||
|
{{ formatFileSize(data.file_size) }}
|
||||||
|
</template>
|
||||||
|
</Column>
|
||||||
|
|
||||||
<Column field="uploadTime" header="上传时间"></Column>
|
<Column field="uploadTime" header="上传时间"></Column>
|
||||||
</DataTable>
|
</DataTable>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user