42fddc5c85
/cms/photo 原来只能传图片(accept=image/*,前后端各拦一次),甲方要求支持常见 文档格式与音视频,并统一缩略图宽高。 - index:标题改「素材库」,上传放开全类型,新增类型筛选(全部/图片/文档/音频/视频) - image / list:封面统一 4:3(网格 aspect-ratio + object-fit: cover,列表 120x90); 图片改用 thumbnail 地址(原来直接拉原图);文档/音频用类型图标卡,视频用图标卡 加播放角标;点击视频/音频开 MediaPreview 弹窗,文档新窗口打开 - photo-edit:去掉上传表单(上传即入库,弹窗只改元数据),顺带修掉「上传已由后端 落一条记录、点保存又 addFiles 插一条无 path 空记录」的重复入库 bug;分享代码按 类型分流(图片 img 系列 / 视频 video / 音频 audio / 文档只给链接与下载) - utils/media.ts:媒体大类判定、上传校验、大小格式化、类型图标与配色的唯一出口, MEDIA_ACCEPT 是后端 UploadRules 的镜像,两边要一起改
402 lines
11 KiB
Vue
402 lines
11 KiB
Vue
<!-- 素材库 · 列表视图 -->
|
|
<template>
|
|
<div class="page">
|
|
<a-card :bordered="false">
|
|
<!-- 表格 -->
|
|
<ele-pro-table
|
|
ref="tableRef"
|
|
row-key="id"
|
|
:columns="columns"
|
|
:datasource="datasource"
|
|
v-model:selection="selection"
|
|
:scroll="{ x: 860 }"
|
|
cache-key="proCmsPhotoTable"
|
|
>
|
|
<template #toolbar>
|
|
<a-space>
|
|
<a-upload
|
|
:show-upload-list="false"
|
|
:accept="MEDIA_ACCEPT"
|
|
:customRequest="onUpload"
|
|
>
|
|
<a-button type="primary" class="ele-btn-icon">
|
|
<template #icon>
|
|
<UploadOutlined />
|
|
</template>
|
|
<span>上传素材</span>
|
|
</a-button>
|
|
</a-upload>
|
|
<a-button
|
|
danger
|
|
type="primary"
|
|
class="ele-btn-icon"
|
|
v-if="selection.length > 0"
|
|
@click="removeBatch"
|
|
>
|
|
<template #icon>
|
|
<delete-outlined />
|
|
</template>
|
|
<span>删除</span>
|
|
</a-button>
|
|
<a-select
|
|
v-model:value="category"
|
|
style="width: 120px"
|
|
@change="reload()"
|
|
>
|
|
<a-select-option
|
|
v-for="item in MEDIA_CATEGORY_OPTIONS"
|
|
:key="item.value"
|
|
:value="item.value"
|
|
>
|
|
{{ item.label }}
|
|
</a-select-option>
|
|
</a-select>
|
|
<a-input-search
|
|
allow-clear
|
|
v-model:value="searchText"
|
|
placeholder="请输入关键词"
|
|
@search="reload"
|
|
@pressEnter="reload"
|
|
>
|
|
<template #addonBefore>
|
|
<a-select
|
|
v-model:value="type"
|
|
style="width: 100px; margin: -5px -12px"
|
|
>
|
|
<a-select-option value="name">文件名称</a-select-option>
|
|
<a-select-option value="createNickname">
|
|
上传人
|
|
</a-select-option>
|
|
</a-select>
|
|
</template>
|
|
</a-input-search>
|
|
</a-space>
|
|
</template>
|
|
<template #bodyCell="{ column, record }">
|
|
<template v-if="column.key === 'path'">
|
|
<!-- 与网格视图同一套 4:3 缩略图规则 -->
|
|
<div class="media-thumb" @click="openPreview(record)">
|
|
<a-image
|
|
v-if="kindOf(record) === 'image'"
|
|
:src="record.thumbnail || record.url"
|
|
:preview="{ src: record.url }"
|
|
:width="120"
|
|
/>
|
|
<template v-else>
|
|
<component
|
|
:is="iconOf(record)"
|
|
class="media-thumb-icon"
|
|
:style="{ color: colorOf(record) }"
|
|
/>
|
|
<span class="media-thumb-ext">{{ extOf(record) }}</span>
|
|
</template>
|
|
</div>
|
|
</template>
|
|
<template v-if="column.dataIndex === 'name'">
|
|
<span>{{ record.name }}</span>
|
|
<copy-outlined
|
|
style="padding-left: 4px"
|
|
@click="openEdit(record)"
|
|
/>
|
|
</template>
|
|
<template v-if="column.dataIndex === 'comments'">
|
|
<span @click="openEdit(record)">{{ record.comments }}</span>
|
|
</template>
|
|
<template v-if="column.key === 'action'">
|
|
<a-space>
|
|
<a @click="openEdit(record)">编辑</a>
|
|
<a-divider type="vertical" />
|
|
<a-popconfirm
|
|
placement="topRight"
|
|
title="确定要删除此素材吗?"
|
|
@confirm="remove(record)"
|
|
>
|
|
<a class="ele-text-danger">删除</a>
|
|
</a-popconfirm>
|
|
</a-space>
|
|
</template>
|
|
</template>
|
|
</ele-pro-table>
|
|
</a-card>
|
|
<!-- 编辑弹窗 -->
|
|
<PhotoEdit v-model:visible="showEdit" :data="current" @done="reload" />
|
|
<!-- 音视频预览 -->
|
|
<MediaPreview v-model:visible="showPreview" :record="current" />
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { createVNode, ref } from 'vue';
|
|
import { message, Modal } from 'ant-design-vue/es';
|
|
import {
|
|
UploadOutlined,
|
|
DeleteOutlined,
|
|
CopyOutlined,
|
|
ExclamationCircleOutlined
|
|
} from '@ant-design/icons-vue';
|
|
import type { EleProTable } from 'ele-admin-pro/es';
|
|
import type {
|
|
DatasourceFunction,
|
|
ColumnItem
|
|
} from 'ele-admin-pro/es/ele-pro-table/types';
|
|
import { messageLoading, toDateString } from 'ele-admin-pro/es';
|
|
import PhotoEdit from './components/photo-edit.vue';
|
|
import MediaPreview from './components/media-preview.vue';
|
|
import {
|
|
pageFiles,
|
|
removeFile,
|
|
removeFiles,
|
|
uploadFile
|
|
} from '@/api/system/file/index';
|
|
import type {
|
|
FileRecord,
|
|
FileRecordParam
|
|
} from '@/api/system/file/model/index';
|
|
import {
|
|
formatFileSize,
|
|
mediaColor,
|
|
mediaExtLabel,
|
|
mediaIcon,
|
|
mediaKindOfRecord,
|
|
MEDIA_ACCEPT,
|
|
MEDIA_CATEGORY_OPTIONS,
|
|
MEDIA_KIND_LABEL,
|
|
validateMediaFile,
|
|
type MediaKind
|
|
} from '@/utils/media';
|
|
|
|
// 表格实例
|
|
const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
|
|
// 表格选中数据
|
|
const selection = ref<FileRecord[]>([]);
|
|
// 当前操作的数据(编辑 / 预览共用)
|
|
const current = ref<FileRecord | null>(null);
|
|
// 是否显示编辑弹窗
|
|
const showEdit = ref(false);
|
|
// 是否显示音视频预览
|
|
const showPreview = ref(false);
|
|
const type = ref('name');
|
|
const searchText = ref('');
|
|
// 媒体大类筛选,空串表示全部
|
|
const category = ref('');
|
|
// 表格列配置
|
|
const columns = ref<ColumnItem[]>([
|
|
{
|
|
key: 'index',
|
|
width: 48,
|
|
align: 'center',
|
|
fixed: 'left',
|
|
hideInSetting: true,
|
|
customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
|
|
},
|
|
{
|
|
title: '缩略图',
|
|
dataIndex: 'path',
|
|
width: 140,
|
|
key: 'path'
|
|
},
|
|
{
|
|
title: '类型',
|
|
dataIndex: 'contentType',
|
|
width: 100,
|
|
customRender: ({ record }) => MEDIA_KIND_LABEL[kindOf(record)]
|
|
},
|
|
{
|
|
title: '文件名称',
|
|
dataIndex: 'name'
|
|
},
|
|
{
|
|
title: '描述',
|
|
dataIndex: 'comments',
|
|
ellipsis: true
|
|
},
|
|
{
|
|
title: '文件大小',
|
|
dataIndex: 'length',
|
|
sorter: true,
|
|
showSorterTooltip: false,
|
|
ellipsis: true,
|
|
customRender: ({ text }) => formatFileSize(text),
|
|
width: 120
|
|
},
|
|
{
|
|
title: '上传人',
|
|
width: 120,
|
|
dataIndex: 'createNickname'
|
|
},
|
|
{
|
|
title: '上传时间',
|
|
dataIndex: 'createTime',
|
|
sorter: true,
|
|
width: 180,
|
|
showSorterTooltip: false,
|
|
ellipsis: true,
|
|
customRender: ({ text }) => toDateString(text)
|
|
},
|
|
{
|
|
title: '操作',
|
|
key: 'action',
|
|
width: 200,
|
|
align: 'center'
|
|
}
|
|
]);
|
|
|
|
const kindOf = (record: FileRecord): MediaKind => mediaKindOfRecord(record);
|
|
const iconOf = (record: FileRecord) =>
|
|
mediaIcon(kindOf(record), record.path || record.name);
|
|
const colorOf = (record: FileRecord) =>
|
|
mediaColor(kindOf(record), record.path || record.name);
|
|
const extOf = (record: FileRecord) => mediaExtLabel(record);
|
|
|
|
// 表格数据源
|
|
const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
|
|
where = {};
|
|
if (type.value == 'name') {
|
|
where.name = searchText.value;
|
|
}
|
|
if (type.value == 'createNickname') {
|
|
where.createNickname = searchText.value;
|
|
}
|
|
// 空串会被当成一个真实的筛选值,必须置空
|
|
where.mediaCategory = category.value || undefined;
|
|
return pageFiles({ ...where, ...orders, page, limit });
|
|
};
|
|
|
|
/* 搜索 */
|
|
const reload = (where?: FileRecordParam) => {
|
|
selection.value = [];
|
|
tableRef?.value?.reload({ page: 1, where });
|
|
};
|
|
|
|
/* 打开编辑弹窗 */
|
|
const openEdit = (row?: FileRecord) => {
|
|
current.value = row ?? null;
|
|
showEdit.value = true;
|
|
};
|
|
|
|
/* 点击缩略图:音视频开播放器,文档新窗口打开 */
|
|
const openPreview = (row: FileRecord) => {
|
|
const kind = kindOf(row);
|
|
if (kind === 'image') {
|
|
return;
|
|
}
|
|
if (kind === 'doc') {
|
|
window.open(row.url, '_blank');
|
|
return;
|
|
}
|
|
current.value = row;
|
|
showPreview.value = true;
|
|
};
|
|
|
|
/* 删除单个 */
|
|
const remove = (row: FileRecord) => {
|
|
const hide = messageLoading('请求中..', 0);
|
|
removeFile(row.id)
|
|
.then((msg) => {
|
|
hide();
|
|
message.success(msg);
|
|
reload();
|
|
})
|
|
.catch((e) => {
|
|
hide();
|
|
message.error(e.message);
|
|
});
|
|
};
|
|
|
|
/* 批量删除 */
|
|
const removeBatch = () => {
|
|
if (!selection.value.length) {
|
|
message.error('请至少选择一条数据');
|
|
return;
|
|
}
|
|
Modal.confirm({
|
|
title: '提示',
|
|
content: '确定要删除选中的记录吗?',
|
|
icon: createVNode(ExclamationCircleOutlined),
|
|
maskClosable: true,
|
|
onOk: () => {
|
|
const hide = messageLoading('请求中..', 0);
|
|
removeFiles(selection.value.map((d) => d.id))
|
|
.then((msg) => {
|
|
hide();
|
|
message.success(msg);
|
|
reload();
|
|
})
|
|
.catch((e) => {
|
|
hide();
|
|
message.error(e.message);
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
// 上传文件
|
|
const onUpload = (item) => {
|
|
const { file } = item;
|
|
const error = validateMediaFile(file);
|
|
if (error) {
|
|
message.error(error);
|
|
return;
|
|
}
|
|
const hide = messageLoading({
|
|
content: '上传中..',
|
|
duration: 0,
|
|
mask: true
|
|
});
|
|
uploadFile(file)
|
|
.then(() => {
|
|
hide();
|
|
message.success('上传成功');
|
|
reload();
|
|
})
|
|
.catch((e) => {
|
|
message.error(e.message);
|
|
hide();
|
|
});
|
|
};
|
|
</script>
|
|
|
|
<script lang="ts">
|
|
export default {
|
|
name: 'PhotoList'
|
|
};
|
|
</script>
|
|
|
|
<style lang="less" scoped>
|
|
.media-thumb {
|
|
position: relative;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 120px;
|
|
height: 90px;
|
|
overflow: hidden;
|
|
background: #f5f6f8;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
|
|
:deep(.ant-image),
|
|
:deep(.ant-image-img) {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
}
|
|
|
|
.media-thumb-icon {
|
|
font-size: 28px;
|
|
}
|
|
|
|
.media-thumb-ext {
|
|
position: absolute;
|
|
right: 4px;
|
|
bottom: 4px;
|
|
padding: 0 4px;
|
|
font-size: 11px;
|
|
line-height: 16px;
|
|
color: #718096;
|
|
background: rgba(255, 255, 255, 0.9);
|
|
border-radius: 3px;
|
|
}
|
|
</style>
|