<template> <!-- 专题导航 --> <div class="thematic"> <div class="searchBoxs"> <n-space> <n-select v-model:value="searchVal1" placeholder="请选择板块位置" style="width: 200px" :options="bkwzOptions" clearable> </n-select> <n-button type="primary" @click="handleClick('search')"> <template #icon> <n-icon><Search /></n-icon> </template> 搜索 </n-button> <n-button type="primary" @click="handleClick('add')"> <template #icon> <n-icon><Add /></n-icon> </template> 新增专题 </n-button> </n-space> </div> <!-- 表格 --> <div class="tableBox"> <n-data-table :bordered="false" :max-height="700" striped :columns="columns" :data="tableData" :loading="tableLoading" :remote="true" :pagination="pagination" > </n-data-table> </div> <!-- 新增修改弹窗 --> <n-modal :title="modalTitle" :mask-closable="false" preset="dialog" :show-icon="false" :style="{ width: '600px' }" v-model:show="modalShow" > <n-form ref="formRef" :label-width="100" :rules="formInfo.rules" :model="formInfo.data" label-placement="left"> <n-form-item label="板块位置:" path="position"> <n-select v-model:value="formInfo.data.position" :options="bkwzOptions" placeholder="请选择板块位置" /> </n-form-item> <n-form-item label="链接方式:" path="type"> <n-select v-model:value="formInfo.data.type" :options="linkTypes" placeholder="请选择链接方式" /> </n-form-item> <n-form-item label="跳转链接:" path="linkUrl"> <n-input v-model:value="formInfo.data.linkUrl" placeholder="请输入跳转链接" /> </n-form-item> <n-form-item label="封面图:" class="uploadWarns" path="fileNo"> <n-upload v-model:file-list="uploadList" accept=".jpg,.png,.jpeg,.svg,.gif" :max="1" list-type="image-card" @change="changeFile"> </n-upload> <span style="margin-top: 10px">提示:格式为.jpg,.png,.jpeg,.svg,.gif,大小小于2M</span> </n-form-item> </n-form> <template #action> <n-space> <n-button @click="() => (modalShow = false)">取消</n-button> <n-button type="primary" @click="handleClick('submit')">确定 </n-button> </n-space> </template> </n-modal> </div> </template> <script> import { toRefs, onMounted, reactive, h, ref } from 'vue'; import { Search, Add } from '@vicons/ionicons5'; import { NButton, NImage } from 'naive-ui'; import { fileUpload, fileDelete, ztdhSearch, ztdhSave, ztdhUpdate, ztdhDelete } from '@/services'; import { resetForm } from '@/utils/util'; export default { name: 'thematic', components: { Search, Add, NButton }, setup() { const allData = reactive({ searchVal1: null, modalTitle: '新增', modalShow: false, uploadList: [], formInfo: { data: { linkUrl: '', type: '1', fileNo: '', position: '', }, rules: { linkUrl: { required: true, trigger: ['blur'], message: '请输入跳转链接', }, fileNo: { required: true, trigger: ['blur'], message: '请上传图片', }, }, }, linkTypes: [ { value: 1, label: '内链' }, { value: 2, label: '外链' }, ], tableLoading: true, bkwzOptions: [ { value: 1, label: 'banner' }, { value: 2, label: '轮播图' }, ], tableData: [], columns: [ { title: '序号', align: 'center', width: '80', render(row, index) { return (paginationReactive.page - 1) * paginationReactive.pageSize + index + 1; }, }, { title: '封面图', align: 'center', render(row) { let lists = row.fileList == null ? [] : row.fileList; let value = []; lists.map((item, index) => { let imgs = h(NImage, { width: 50, height: 50, style: 'margin-right:5px;', src: item.fileCloudStorageKey, }); value.push(imgs); }); return value; }, }, { title: '跳转链接', align: 'center', key: 'linkUrl' }, { title: '板块位置', align: 'center', key: 'position', render(row) { return row.position == 1 ? 'banner' : '轮播图'; }, }, { title: '创建时间', align: 'center', key: 'createTime' }, { title: '操作', key: 'actions', width: '220', align: 'center', render(row) { const btn = allData.actionColumn.map((item, index) => { return h( NButton, { text: true, size: item.size, style: { margin: '10px', }, type: item.btnType, onClick: () => handleClick(item.type, row), }, { default: () => item.default } ); }); return btn; }, }, ], actionColumn: [ { size: 'small', btnType: 'primary', type: 'edit', default: '修改', }, { size: 'small', btnType: 'error', type: 'delete', default: '删除', }, ], }); //分页 const paginationReactive = reactive({ page: 1, pageSize: 10, showSizePicker: true, pageSizes: [10, 20, 50], showQuickJumper: true, pageCount: 0, itemCount: 0, prefix: () => { return '共 ' + paginationReactive.itemCount + ' 项'; }, onChange: (page) => { paginationReactive.page = page; getTableData(); }, onPageSizeChange: (pageSize) => { paginationReactive.pageSize = pageSize; paginationReactive.page = 1; getTableData(); }, }); const getTableData = async () => { allData.tableLoading = true; let pramas = { current: paginationReactive.page, size: paginationReactive.pageSize, position: allData.searchVal1, }; let res = await ztdhSearch(pramas); if (res && res.code == 200) { allData.tableData = res.data.records; paginationReactive.pageCount = res.data.pages; paginationReactive.itemCount = res.data.total; } allData.tableLoading = false; }; const formRef = ref(null); // 点击事件 const handleClick = async (type, row) => { switch (type) { case 'search': paginationReactive.page = 1; getTableData(); break; case 'add': allData.modalTitle = '新增'; resetForm(allData.formInfo.data); allData.modalShow = true; allData.formInfo.data.type = 1; allData.uploadList = []; break; case 'edit': allData.modalTitle = '修改'; allData.formInfo.data = { ...row }; allData.modalShow = true; // 图片文件默认填充 allData.uploadList = []; if (row.fileList != null && row.fileList.length > 0) { allData.formInfo.data.fileNo = row.fileList[0].fileNo; row.fileList.map((item) => { let param = { id: item.id, name: item.tableName, status: 'finished', url: item.fileCloudStorageKey, }; allData.uploadList.push(param); }); } break; case 'submit': formRef.value.validate((errors) => { if (!errors) { submitData(); } else { $message.error('验证失败,请检查必填项'); } }); break; case 'delete': $dialog.info({ title: '提示', content: `确定删除该数据吗?`, positiveText: '确定', negativeText: '取消', onPositiveClick: () => { let ids = [row.id]; dataDel(ids); }, }); break; } }; // 删除数据 async function dataDel(ids) { let res = await ztdhDelete({ ids: ids }); if (res && res.code === 200) { $message.success('删除成功'); getTableData(); } } // 提交数据 async function submitData() { let params = { ...allData.formInfo.data }; if (allData.modalTitle == '新增') { let res = await ztdhSave(params); if (res && res.code == 200) { $message.success('操作成功'); getTableData(); allData.modalShow = false; } } else { let res = await ztdhUpdate(params); if (res && res.code == 200) { $message.success('操作成功'); getTableData(); allData.modalShow = false; } } } // 文件上传和删除 const changeFile = async (file) => { console.log(file, '文件上传'); $loadingBar.start(); if (file.event) { // 文件上传大小判断 let size = file.file.file.size / 1024 / 1024; if (size > 2) { $message.error('图片大小大于2M,请删除后重新上传'); setTimeout(() => { allData.uploadList = [ { id: file.file.id, name: file.file.name, status: 'error', }, ]; }); return false; } // 文件上传 let formdata = new FormData(); formdata.append('files', file.file.file); let config = { headers: { 'Content-Type': 'multipart/form-data' }, }; let res = await fileUpload(formdata, config); if (res && res.code === 200) { if (res.data.length > 0) { allData.formInfo.data.fileNo = res.data[0].fileNo; } } else { allData.uploadList = []; let param = { id: file.file.id, name: file.file.name, status: 'error', }; allData.uploadList.push(param); } $loadingBar.finish(); } else { // 文件删除 // let fileNos = []; // fileNos.push(allData.formInfo.data.fileNo); // let res = await fileDelete(fileNos); // if (res && res.code === 200) { allData.formInfo.data.fileNo = ''; // } } }; onMounted(() => { getTableData(); }); return { ...toRefs(allData), pagination: paginationReactive, handleClick, getTableData, changeFile, formRef, }; }, }; </script> <style lang="less" scoped> .thematic { width: 100%; .searchBoxs { margin: 10px; } } </style>