Initial commit
This commit is contained in:
179
src/components/SelectFile/components/file-record-edit.vue
Normal file
179
src/components/SelectFile/components/file-record-edit.vue
Normal file
@@ -0,0 +1,179 @@
|
||||
<!-- 角色编辑弹窗 -->
|
||||
<template>
|
||||
<ele-modal
|
||||
:width="600"
|
||||
:visible="visible"
|
||||
:confirm-loading="loading"
|
||||
:title="isUpdate ? '编辑' : '上传文件'"
|
||||
:body-style="{ paddingBottom: '8px' }"
|
||||
okText="保存"
|
||||
@update:visible="updateVisible"
|
||||
@ok="save"
|
||||
>
|
||||
<a-form
|
||||
ref="formRef"
|
||||
:model="form"
|
||||
:rules="rules"
|
||||
:label-col="styleResponsive ? { md: 5, sm: 5, xs: 24 } : { flex: '90px' }"
|
||||
:wrapper-col="
|
||||
styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
|
||||
"
|
||||
>
|
||||
<a-form-item label="文件名称" name="name">
|
||||
<a-input
|
||||
allow-clear
|
||||
:maxlength="20"
|
||||
placeholder="请输入文件名称"
|
||||
v-model:value="form.name"
|
||||
@pressEnter="save"
|
||||
/>
|
||||
</a-form-item>
|
||||
<a-form-item label="设置分组" name="name">
|
||||
<SelectDict
|
||||
dict-code="groupId"
|
||||
:placeholder="`选择分组`"
|
||||
v-model:value="form.groupName"
|
||||
@done="chooseGroupId"
|
||||
/>
|
||||
</a-form-item>
|
||||
<a-form-item label="描述" name="comments">
|
||||
<a-textarea
|
||||
:rows="4"
|
||||
:maxlength="200"
|
||||
placeholder="图片描述"
|
||||
v-model:value="form.comments"
|
||||
/>
|
||||
</a-form-item>
|
||||
</a-form>
|
||||
</ele-modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, reactive, watch } from 'vue';
|
||||
import { message } from 'ant-design-vue/es';
|
||||
import type { FormInstance, Rule } from 'ant-design-vue/es/form';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { useThemeStore } from '@/store/modules/theme';
|
||||
import useFormData from '@/utils/use-form-data';
|
||||
import type { FileRecord } from '@/api/system/file/model';
|
||||
import { addFiles, updateFiles } from '@/api/system/file';
|
||||
import { RuleObject } from 'ant-design-vue/es/form';
|
||||
import { DictData } from '@/api/system/dict-data/model';
|
||||
import { listDictData } from '@/api/system/dict-data';
|
||||
|
||||
// 是否开启响应式布局
|
||||
const themeStore = useThemeStore();
|
||||
const { styleResponsive } = storeToRefs(themeStore);
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'done'): void;
|
||||
(e: 'update:visible', visible: boolean): void;
|
||||
}>();
|
||||
|
||||
const props = defineProps<{
|
||||
// 弹窗是否打开
|
||||
visible: boolean;
|
||||
// 修改回显的数据
|
||||
data?: FileRecord | null;
|
||||
}>();
|
||||
|
||||
//
|
||||
const formRef = ref<FormInstance | null>(null);
|
||||
const fileName = ref('');
|
||||
// 是否是修改
|
||||
const isUpdate = ref(false);
|
||||
// 提交状态
|
||||
const loading = ref(false);
|
||||
|
||||
// 表单数据
|
||||
const { form, resetFields, assignFields } = useFormData<FileRecord>({
|
||||
id: 0,
|
||||
name: '',
|
||||
comments: '',
|
||||
groupId: undefined,
|
||||
groupName: ''
|
||||
});
|
||||
|
||||
// 表单验证规则
|
||||
const rules = reactive<Record<string, Rule[]>>({
|
||||
fileName: [
|
||||
{
|
||||
required: true,
|
||||
message: '请上传文件',
|
||||
type: 'string',
|
||||
trigger: 'blur',
|
||||
validator: async (_rule: RuleObject) => {
|
||||
if (!isUpdate.value && fileName.value.length == 0) {
|
||||
return Promise.reject('请上传文件');
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
],
|
||||
name: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入文件名称',
|
||||
type: 'string',
|
||||
trigger: 'blur'
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const chooseGroupId = (item: DictData) => {
|
||||
form.groupId = item.dictDataId;
|
||||
form.groupName = item.dictDataName;
|
||||
};
|
||||
|
||||
/* 保存编辑 */
|
||||
const save = () => {
|
||||
if (!formRef.value) {
|
||||
return;
|
||||
}
|
||||
formRef.value
|
||||
.validate()
|
||||
.then(() => {
|
||||
loading.value = true;
|
||||
const saveOrUpdate = isUpdate.value ? updateFiles : addFiles;
|
||||
saveOrUpdate(form)
|
||||
.then((msg) => {
|
||||
loading.value = false;
|
||||
message.success(msg);
|
||||
updateVisible(false);
|
||||
emit('done');
|
||||
})
|
||||
.catch((e) => {
|
||||
loading.value = false;
|
||||
message.error(e.message);
|
||||
});
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
|
||||
/* 更新visible */
|
||||
const updateVisible = (value: boolean) => {
|
||||
emit('update:visible', value);
|
||||
};
|
||||
|
||||
watch(
|
||||
() => props.visible,
|
||||
(visible) => {
|
||||
if (visible) {
|
||||
if (props.data) {
|
||||
assignFields(props.data);
|
||||
listDictData({ dictDataId: props.data.groupId }).then((data) => {
|
||||
if (data.length > 0) {
|
||||
form.groupName = data[0].dictDataName;
|
||||
}
|
||||
});
|
||||
isUpdate.value = true;
|
||||
} else {
|
||||
isUpdate.value = false;
|
||||
}
|
||||
} else {
|
||||
resetFields();
|
||||
formRef.value?.clearValidate();
|
||||
}
|
||||
}
|
||||
);
|
||||
</script>
|
||||
339
src/components/SelectFile/components/select-data.vue
Normal file
339
src/components/SelectFile/components/select-data.vue
Normal file
@@ -0,0 +1,339 @@
|
||||
<template>
|
||||
<ele-modal
|
||||
width="75%"
|
||||
:visible="visible"
|
||||
:maskClosable="false"
|
||||
:title="title"
|
||||
:footer="null"
|
||||
:body-style="{ paddingBottom: '28px' }"
|
||||
@update:visible="updateVisible"
|
||||
>
|
||||
<ele-pro-table
|
||||
ref="tableRef"
|
||||
row-key="id"
|
||||
:datasource="datasource"
|
||||
:columns="columns"
|
||||
:customRow="customRow"
|
||||
:pagination="false"
|
||||
>
|
||||
<template #toolbar>
|
||||
<div class="ele-cell">
|
||||
<div class="ele-cell-content">
|
||||
<a-space>
|
||||
<a-upload
|
||||
v-if="type == 'video'"
|
||||
:show-upload-list="false"
|
||||
:customRequest="onUpload"
|
||||
>
|
||||
<a-button type="primary" class="ele-btn-icon">
|
||||
<template #icon>
|
||||
<UploadOutlined />
|
||||
</template>
|
||||
<span>上传视频</span>
|
||||
</a-button>
|
||||
</a-upload>
|
||||
<a-upload
|
||||
v-else
|
||||
:show-upload-list="false"
|
||||
:accept="'image/*,application/*'"
|
||||
:customRequest="onUpload"
|
||||
>
|
||||
<a-button type="primary" class="ele-btn-icon">
|
||||
<template #icon>
|
||||
<UploadOutlined />
|
||||
</template>
|
||||
<span>上传图片</span>
|
||||
</a-button>
|
||||
</a-upload>
|
||||
<a-select
|
||||
show-search
|
||||
allow-clear
|
||||
v-model:value="dictDataId"
|
||||
optionFilterProp="label"
|
||||
:options="groupList"
|
||||
style="margin-left: 20px; width: 200px"
|
||||
placeholder="请选择分组"
|
||||
@select="onGroupId"
|
||||
/>
|
||||
<a-input-search
|
||||
allow-clear
|
||||
v-model:value="searchText"
|
||||
placeholder="请输入搜索关键词"
|
||||
style="width: 240px"
|
||||
@search="reload"
|
||||
@pressEnter="reload"
|
||||
/>
|
||||
</a-space>
|
||||
</div>
|
||||
<a-button
|
||||
style="margin-right: 20px"
|
||||
@click="openUrl('/cms/photo/dict')"
|
||||
>管理分组</a-button
|
||||
>
|
||||
</div>
|
||||
</template>
|
||||
<template #bodyCell="{ column, record }">
|
||||
<template v-if="column.key === 'path'">
|
||||
<!-- 文件类型 -->
|
||||
<template v-if="!isImage(record.path)">
|
||||
<span class="ele-text-secondary">[文件]</span>
|
||||
</template>
|
||||
<!-- 含http -->
|
||||
<template v-else-if="record.path.indexOf('http') == 0">
|
||||
<a-image
|
||||
:src="`${record.path}`"
|
||||
:preview="{
|
||||
src: `${record.downloadUrl}`
|
||||
}"
|
||||
:width="100"
|
||||
/>
|
||||
</template>
|
||||
<!-- path -->
|
||||
<template v-else>
|
||||
<a-image
|
||||
:src="`https://oss.wsdns.cn/${record.path}`"
|
||||
:width="120"
|
||||
/>
|
||||
</template>
|
||||
</template>
|
||||
<template v-if="column.dataIndex === 'name'">
|
||||
<a-space class="ele-cell" style="display: flex">
|
||||
<span>{{ record.name }}</span>
|
||||
<EditOutlined title="编辑" @click="openEdit(record)" />
|
||||
</a-space>
|
||||
</template>
|
||||
<template v-if="column.key === 'action'">
|
||||
<template v-if="pageId == record.pageId">
|
||||
<a-radio v-model:checked="checked" @click="onRadio(record)" />
|
||||
</template>
|
||||
<template v-else>
|
||||
<a-space>
|
||||
<lebal>
|
||||
<a-radio @click="onRadio(record)" />
|
||||
<a class="ele-text-success">选择</a>
|
||||
</lebal>
|
||||
<a-divider type="vertical" />
|
||||
<a class="ele-text-placeholder">编辑</a>
|
||||
<a-divider type="vertical" />
|
||||
<a class="ele-text-placeholder">删除</a>
|
||||
</a-space>
|
||||
</template>
|
||||
</template>
|
||||
</template>
|
||||
</ele-pro-table>
|
||||
</ele-modal>
|
||||
<!-- 编辑弹窗 -->
|
||||
<FileRecordEdit v-model:visible="showEdit" :data="current" @done="reload" />
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, watch } from 'vue';
|
||||
import {
|
||||
ColumnItem,
|
||||
DatasourceFunction
|
||||
} from 'ele-admin-pro/es/ele-pro-table/types';
|
||||
import { pageFiles, uploadOss, uploadOssByGroupId } from '@/api/system/file';
|
||||
import { EleProTable, messageLoading } from 'ele-admin-pro';
|
||||
import { FileRecord, FileRecordParam } from '@/api/system/file/model';
|
||||
import { EditOutlined, UploadOutlined } from '@ant-design/icons-vue';
|
||||
import { DictData } from '@/api/system/dict-data/model';
|
||||
import { pageDictData } from '@/api/system/dict-data';
|
||||
import {isImage, openNew, openUrl} from '@/utils/common';
|
||||
import { message } from 'ant-design-vue/es';
|
||||
import FileRecordEdit from './file-record-edit.vue';
|
||||
|
||||
const props = defineProps<{
|
||||
// 弹窗是否打开
|
||||
visible: boolean;
|
||||
// 标题
|
||||
title?: string;
|
||||
// 文件类型
|
||||
type?: string;
|
||||
// 修改回显的数据
|
||||
data?: FileRecord | null;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'done', data: FileRecord): void;
|
||||
(e: 'update:visible', visible: boolean): void;
|
||||
}>();
|
||||
|
||||
/* 更新visible */
|
||||
const updateVisible = (value: boolean) => {
|
||||
emit('update:visible', value);
|
||||
};
|
||||
|
||||
// 搜索内容
|
||||
const searchText = ref(null);
|
||||
const pageId = ref<number>(0);
|
||||
const checked = ref<boolean>(true);
|
||||
const groupList = ref<DictData[]>();
|
||||
const showEdit = ref<boolean>(false);
|
||||
const current = ref<FileRecord | null>();
|
||||
const dictDataId = ref<any>(undefined);
|
||||
|
||||
// 表格实例
|
||||
const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
|
||||
|
||||
// 表格配置
|
||||
const columns = ref<ColumnItem[]>([
|
||||
{
|
||||
title: 'ID',
|
||||
dataIndex: 'id'
|
||||
},
|
||||
{
|
||||
title: '图片',
|
||||
dataIndex: 'path',
|
||||
key: 'path'
|
||||
},
|
||||
{
|
||||
title: '名称',
|
||||
dataIndex: 'name',
|
||||
key: 'name'
|
||||
},
|
||||
{
|
||||
title: '大小',
|
||||
dataIndex: 'length',
|
||||
key: 'length',
|
||||
customRender: ({ text }) => {
|
||||
if (text < 1024) {
|
||||
return text + 'B';
|
||||
} else if (text < 1024 * 1024) {
|
||||
return (text / 1024).toFixed(1) + 'KB';
|
||||
} else if (text < 1024 * 1024 * 1024) {
|
||||
return (text / 1024 / 1024).toFixed(1) + 'M';
|
||||
} else {
|
||||
return (text / 1024 / 1024 / 1024).toFixed(1) + 'G';
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
align: 'center'
|
||||
}
|
||||
]);
|
||||
|
||||
// 表格数据源
|
||||
const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
|
||||
where = {};
|
||||
// 搜索条件
|
||||
if (searchText.value) {
|
||||
where.name = searchText.value;
|
||||
}
|
||||
if (dictDataId.value) {
|
||||
where.groupId = dictDataId.value;
|
||||
}
|
||||
return pageFiles({
|
||||
...where,
|
||||
...orders,
|
||||
page,
|
||||
limit
|
||||
});
|
||||
};
|
||||
|
||||
/* 搜索 */
|
||||
const reload = (where?: FileRecordParam) => {
|
||||
tableRef?.value?.reload({ page: 1, where });
|
||||
};
|
||||
|
||||
const onRadio = (record: FileRecord) => {
|
||||
pageId.value = Number(record.id);
|
||||
updateVisible(false);
|
||||
emit('done', record);
|
||||
};
|
||||
|
||||
const getGroupList = () => {
|
||||
pageDictData({ dictCode: 'groupId' }).then((res) => {
|
||||
groupList.value = res?.list.map((d) => {
|
||||
return {
|
||||
label: d.dictDataName,
|
||||
value: d.dictDataId
|
||||
};
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const onGroupId = (index: number) => {
|
||||
dictDataId.value = index;
|
||||
reload();
|
||||
};
|
||||
|
||||
// 上传文件
|
||||
const onUpload = (item) => {
|
||||
const { file } = item;
|
||||
if (!file.type.startsWith('image') && props.type != 'video') {
|
||||
message.error('只能选择图片');
|
||||
return;
|
||||
}
|
||||
if (props.type == 'video') {
|
||||
if (file.size / 1024 / 1024 > 100) {
|
||||
message.error('大小不能超过 100MB');
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (file.size / 1024 / 1024 > 10) {
|
||||
message.error('大小不能超过 10MB');
|
||||
return;
|
||||
}
|
||||
}
|
||||
const hide = messageLoading({
|
||||
content: '上传中..',
|
||||
duration: 0,
|
||||
mask: true
|
||||
});
|
||||
if (dictDataId.value > 0) {
|
||||
uploadOssByGroupId(file, dictDataId.value)
|
||||
.then(() => {
|
||||
hide();
|
||||
message.success('上传成功');
|
||||
reload();
|
||||
})
|
||||
.catch((e) => {
|
||||
message.error(e.message);
|
||||
hide();
|
||||
});
|
||||
} else {
|
||||
uploadOss(file)
|
||||
.then(() => {
|
||||
hide();
|
||||
message.success('上传成功');
|
||||
reload();
|
||||
})
|
||||
.catch((e) => {
|
||||
message.error(e.message);
|
||||
hide();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const openEdit = (row?: FileRecord) => {
|
||||
current.value = row ?? null;
|
||||
showEdit.value = true;
|
||||
};
|
||||
|
||||
/* 自定义行属性 */
|
||||
const customRow = (record: FileRecord) => {
|
||||
return {
|
||||
// 行点击事件
|
||||
// onClick: () => {
|
||||
// updateVisible(false);
|
||||
// emit('done', record);
|
||||
// },
|
||||
// 行双击事件
|
||||
onDblclick: () => {
|
||||
updateVisible(false);
|
||||
emit('done', record);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
watch(
|
||||
() => props.visible,
|
||||
(visible) => {
|
||||
if (visible) {
|
||||
getGroupList();
|
||||
}
|
||||
}
|
||||
);
|
||||
</script>
|
||||
131
src/components/SelectFile/index.vue
Normal file
131
src/components/SelectFile/index.vue
Normal file
@@ -0,0 +1,131 @@
|
||||
<template>
|
||||
<a-image-preview-group>
|
||||
<a-space>
|
||||
<template v-for="(item, index) in data" :key="index">
|
||||
<div class="image-upload-item" v-if="type == 'video'">
|
||||
{{ item.url }}
|
||||
<a class="image-upload-close" @click="onDeleteItem(index)">
|
||||
<CloseOutlined />
|
||||
</a>
|
||||
</div>
|
||||
<div class="image-upload-item" v-else>
|
||||
<a-image
|
||||
:width="width"
|
||||
:height="width"
|
||||
style="border: 1px dashed var(--grey-7)"
|
||||
:src="item.url"
|
||||
/>
|
||||
<a class="image-upload-close" @click="onDeleteItem(index)">
|
||||
<CloseOutlined />
|
||||
</a>
|
||||
</div>
|
||||
</template>
|
||||
<a-button
|
||||
@click="openEdit"
|
||||
v-if="data?.length < limit"
|
||||
class="select-picture-btn ele-text-placeholder"
|
||||
>
|
||||
<PlusOutlined />
|
||||
</a-button>
|
||||
</a-space>
|
||||
</a-image-preview-group>
|
||||
|
||||
<!-- 选择弹窗 -->
|
||||
<SelectData
|
||||
v-model:visible="showEdit"
|
||||
:data="current"
|
||||
:title="placeholder"
|
||||
:type="type"
|
||||
@done="onChange"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { PlusOutlined, CloseOutlined } from '@ant-design/icons-vue';
|
||||
import { ref } from 'vue';
|
||||
import SelectData from './components/select-data.vue';
|
||||
import { FileRecord } from '@/api/system/file/model';
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
value?: any;
|
||||
data?: any[];
|
||||
width?: number;
|
||||
type?: string;
|
||||
limit?: number;
|
||||
placeholder?: string;
|
||||
index?: number;
|
||||
}>(),
|
||||
{
|
||||
placeholder: '请选择数据',
|
||||
width: 80,
|
||||
limit: 1
|
||||
}
|
||||
);
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'done', data: FileRecord): void;
|
||||
(e: 'del', index: number): void;
|
||||
(e: 'clear'): void;
|
||||
}>();
|
||||
|
||||
// 是否显示编辑弹窗
|
||||
const showEdit = ref(false);
|
||||
// 当前编辑数据
|
||||
const current = ref<FileRecord | null>(null);
|
||||
|
||||
/* 打开编辑弹窗 */
|
||||
const openEdit = (row?: FileRecord) => {
|
||||
current.value = row ?? null;
|
||||
showEdit.value = true;
|
||||
};
|
||||
|
||||
const onChange = (row) => {
|
||||
row.index = props.index;
|
||||
emit('done', row);
|
||||
};
|
||||
|
||||
const onDeleteItem = (index: number) => {
|
||||
emit('del', index);
|
||||
};
|
||||
</script>
|
||||
<style lang="less" scoped>
|
||||
.select-picture-btn {
|
||||
background-color: var(--grey-9);
|
||||
border: 1px dashed var(--border-color-base);
|
||||
width: 80px;
|
||||
height: 80px;
|
||||
font-size: 16px;
|
||||
}
|
||||
//.ant-image-img {
|
||||
// width: 100px !important;
|
||||
// height: 100px !important;
|
||||
//}
|
||||
.image-upload-item {
|
||||
position: relative;
|
||||
}
|
||||
.image-upload-close {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
color: rgb(255, 255, 255);
|
||||
font-size: 10px;
|
||||
border-bottom-left-radius: 18px;
|
||||
border-top-right-radius: 2px;
|
||||
background: rgba(0, 0, 0, 0.6);
|
||||
position: absolute;
|
||||
top: 1px;
|
||||
right: 1px;
|
||||
line-height: 1;
|
||||
box-sizing: border-box;
|
||||
padding: 2px 0 0 5px;
|
||||
transition: background-color 0.2s ease-in-out 0s;
|
||||
cursor: pointer;
|
||||
z-index: 2;
|
||||
//display: flex;
|
||||
//justify-content: center;
|
||||
//align-items: center;
|
||||
}
|
||||
.image-upload-close:hover {
|
||||
background-color: var(--red-6);
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user