<template> <!-- 政策法规 --> <div class="policyLaw"> <div class="searchBoxs"> <n-space> <n-input v-model:value="searchVal1" placeholder="请输入法规标题" style="width: 200px" clearable></n-input> <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: '800px' }" 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="title"> <n-input v-model:value="formInfo.data.title" 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</span> </n-form-item> <n-form-item label="法规简介:" path="degist"> <n-input v-model:value="formInfo.data.degist" placeholder="请输入法规简介" /> </n-form-item> <n-form-item label="法规内容:" path="content"> <Editor @editorContent="editorContent" ref="richEditor" /> </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, nextTick } from 'vue'; import { Search, Add } from '@vicons/ionicons5'; import { NButton, NImage } from 'naive-ui'; import { fileUpload, fileDelete, zcfgSearch, zcfgSave, zcfgUpdate, articalDelete } from '@/services'; import { resetForm, formatDate } from '@/utils/util'; import Editor from './editor.vue'; export default { name: 'policyLaw', components: { Search, Add, NButton, Editor }, setup() { const allData = reactive({ searchVal1: null, modalTitle: '新增', modalShow: false, uploadList: [], formInfo: { data: { title: null, degist: '', fileNo: [], content: '', }, rules: { title: { required: true, trigger: ['blur'], message: '请输入法规标题', }, degist: { required: true, trigger: ['blur'], message: '请输入法规简介', }, content: { required: true, trigger: ['blur'], message: '请输入法规内容', }, fileNo: { type: 'array', required: true, trigger: ['blur'], message: '请上传图片', }, }, }, tableLoading: true, 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: 'title' }, { title: '法规简介', align: 'center', key: 'degist' }, { title: '创建时间', align: 'center', key: 'createTime' }, { title: '修改时间', align: 'center', key: 'updateTime' }, { 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, title: allData.searchVal1, }; let res = await zcfgSearch(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 richEditor = 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.uploadList = []; allData.formInfo.data.fileNo = []; break; case 'edit': allData.modalTitle = '修改'; allData.formInfo.data = { ...row }; allData.modalShow = true; allData.formInfo.data.fileNo = []; allData.formInfo.data.fileNo.push(row.fileList.length > 0 ? row.fileList[0].fileNo : ''); // 富文本默认值 nextTick(() => { richEditor.value.content = row.content; }); // 图片文件默认填充 allData.uploadList = []; if (row.fileList != null && row.fileList.length > 0) { 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 articalDelete({ ids: ids }); if (res && res.code === 200) { $message.success('删除成功'); getTableData(); } } // 提交数据 async function submitData() { let params = { ...allData.formInfo.data }; if (allData.modalTitle == '新增') { let res = await zcfgSave(params); if (res && res.code == 200) { $message.success('操作成功'); getTableData(); allData.modalShow = false; } } else { let res = await zcfgUpdate(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.push(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 { // 文件删除 // console.log(allData.formInfo.data.fileNo); // let res = await fileDelete(allData.formInfo.data.fileNo); // if (res && res.code === 200) { allData.formInfo.data.fileNo = []; // } } }; // 获取从富文本传过来的值 const editorContent = (obj) => { allData.formInfo.data.content = obj; }; onMounted(() => { getTableData(); }); return { ...toRefs(allData), pagination: paginationReactive, handleClick, getTableData, changeFile, formRef, editorContent, richEditor, }; }, }; </script> <style lang="less" scoped> .policyLaw { width: 100%; .searchBoxs { margin: 10px; } } </style>