<script lang="ts" setup name="ListSource"> import { reactive, ref } from 'vue' import type { Ref } from 'vue' import { ElLoading, ElMessage } from 'element-plus' import { Delete } from '@element-plus/icons-vue' import type { IlistQuery, IlistType } from './list_interface' import ListSourceAdd from './listAdd.vue' import { getSoucreList, getSoucreListDelete } from '@/api/system/source' import { uploadApi } from '@/api/system/notice' import { exportExcel } from '@/utils/exportXlsx' // 查询条件 const listQuery: Ref<IlistQuery> = ref({ supplierNo: '', // 业务内容 supplierName: '', // 溯源供方名称 businessContent: '', // 溯源供方编号 offset: 1, limit: 10, }) // 控制是否显示新增页面 const show = ref(true) // 表格数据 const list = ref([]) // 总数 const total = ref(20) // 表头 const columns = ref([ { text: '溯源供方编号', value: 'supplierNo', width: '160', }, { text: '溯源供方名称', value: 'supplierName', width: '120', }, { text: '业务内容', value: 'businessContent', }, { text: '业务资质', value: 'briefName', }, { text: '负责人', value: 'director', }, { text: '联系方式', value: 'mobile', }, { text: '地址', value: 'companyCity', }, { text: '创建时间', value: 'createTime', width: '200', }, { text: '备注', value: 'remark', }, ]) // 表格loding const listLoading = ref(false) const fetchData = (isNowPage: boolean) => { listLoading.value = true if (!isNowPage) { // 是否显示当前页,否则跳转第一页 listQuery.value.offset = 1 } getSoucreList(listQuery.value).then((response) => { list.value = response.data.rows total.value = parseInt(response.data.total) listLoading.value = false }) } fetchData(true) // 选中的内容 const checkoutList = ref([]) // 文件上传input const fileRef = ref() // 删除id const deleteId = ref('') // 详情id const infoId = ref('0') const isPopconfirmShow = ref(false) // 点击按钮 const buttonType = ref('') // 多选发生改变时 const handleSelectionChange = (e: any) => { checkoutList.value = e } // 点击编辑/详情 const handleEdit = (index: string, row: object, value: string) => { buttonType.value = value infoId.value = row.id show.value = false } // 点击删除 const handleDelete = (index: string, row: object) => { isPopconfirmShow.value = true deleteId.value = row.id } // 分页 const handleSizeChange = (val: number) => { listQuery.value.limit = val fetchData(true) } // 分页 const handleCurrentChange = (val: number) => { listQuery.value.offset = val fetchData(true) } // 点击搜索 const searchList = () => { fetchData(true) } const confirmEvent = () => { getSoucreListDelete({ id: deleteId.value }).then((res) => { if (res.code === 200) { isPopconfirmShow.value = false fetchData(true) } }) } const cancelEvent = () => { console.log('cancel!') } // 点击重置 const clearList = () => { listQuery.value = { supplierNo: '', // 业务内容 supplierName: '', // 溯源供方名称 businessContent: '', // 溯源供方编号 offset: 1, limit: 20, } } // 关闭编辑 const close = () => { show.value = true fetchData(true) } // 导出 const exportExcelBtn = () => { const loading = ElLoading.service({ lock: true, text: 'Loading', background: 'rgba(255, 255, 255, 0.8)', }) if (checkoutList.value.length <= 0) { exportExcel({ json: list.value.map((item: IlistType, index: number) => ({ index: index + 1, supplierNo: item.supplierNo, supplierName: item.supplierName, businessContent: item.businessContent, briefName: item.briefName, director: item.director, mobile: item.mobile, companyCity: item.companyCity, createTime: item.createTime, remark: item.remark })), name: '溯源供方', titleArr: ['序号', '溯源供方编号', '溯源供方名称', '业务内容', '业务资质', '负责人', '联系方式', '地址', '创建时间', '备注'], sheetName: 'sheet1', }) } else { exportExcel({ json: checkoutList.value.map((item: IlistType, index: number) => ({ index: index + 1, supplierNo: item.supplierNo, supplierName: item.supplierName, businessContent: item.businessContent, briefName: item.briefName, director: item.director, mobile: item.mobile, companyCity: item.companyCity, createTime: item.createTime, remark: item.remark })), name: '溯源供方', titleArr: ['序号', '溯源供方编号', '溯源供方名称', '业务内容', '业务资质', '负责人', '联系方式', '地址', '创建时间', '备注'], sheetName: 'sheet1', }) } loading.close() } // 上传文件/批量导入 const onFileChange = (event: any) => { if (event.target.files[0].type === 'application/pdf') { if (event.target.files?.length !== 0) { // 创建formdata对象 const fd = new FormData() fd.append('multipartFile', event.target.files[0]) uploadApi(fd).then((res) => { if (res.code === 200) { ElMessage.success('上传成功') } else { ElMessage.error(res.message) } }) } } else { ElMessage.error('请上传pdf格式') } } const upload = () => { fileRef.value.click() } const uploadAll = () => { upload() } const add = () => { buttonType.value = '' show.value = false } const exportAll = () => { exportExcelBtn() } // 打印 const printObj = ref({ id: 'print', // 需要打印元素的id popTitle: '溯源供方编号模板', // 打印配置页上方的标题 extraHead: '', // 最上方的头部文字,附加在head标签上的额外标签,使用逗号分割 preview: false, // 是否启动预览模式,默认是false previewBeforeOpenCallback() { console.log('正在加载预览窗口!') }, // 预览窗口打开之前的callback previewOpenCallback() { console.log('已经加载完预览窗口,预览打开了!') }, // 预览窗口打开时的callback beforeOpenCallback() { console.log('开始打印之前!') }, // 开始打印之前的callback openCallback() { console.log('执行打印了!') }, // 调用打印时的callback closeCallback() { console.log('关闭了打印工具!') }, // 关闭打印的callback(无法区分确认or取消) clickMounted() { console.log('点击v-print绑定的按钮了!') }, // url: 'http://localhost:8080/', // 打印指定的URL,确保同源策略相同 // asyncUrl (reslove) { // setTimeout(() => { // reslove('http://localhost:8080/') // }, 2000) // }, standard: '', extarCss: '', }) </script> <template> <div v-if="show"> <app-container> <search-area @search="searchList" > <search-item> <el-input v-model.trim="listQuery.supplierNo" placeholder="溯源供方编号" clearable /> </search-item> <search-item> <el-input v-model.trim="listQuery.supplierName" placeholder="溯源供方名称" clearable /> </search-item> <search-item> <el-input v-model.trim="listQuery.businessContent" placeholder="业务内容" clearable /> </search-item> <template #btns> <el-button class="filter-item" type="info" :icon="Delete" :style="{ marginTop: '-10px' }" @click="clearList"> 重置 </el-button> </template> </search-area> <table-container> <template #btns-right> <el-button type="primary" @click="uploadAll"> 批量导入 </el-button> <el-button type="primary" @click="add"> 模板下载 </el-button> <el-button type="primary" @click="add"> 新建 </el-button> <el-button type="primary" @click="exportAll"> 导出 </el-button> <el-button v-print="printObj" type="primary"> 打印 </el-button> </template> <input v-show="false" ref="fileRef" type="file" accept="pdf/*" @change="onFileChange"> <el-table id="print" :data="list" style="width: 100%;" @selection-change="handleSelectionChange" > <el-table-column type="selection" width="30" /> <el-table-column align="center" label="#" width="30" type="index" /> <el-table-column v-for="(column, index) in columns" :key="index" :label="column.text" :prop="column.value" :width="column.width" align="center" /> <el-table-column align="center" label="操作" width="200"> <template #default="scope"> <el-button size="small" type="primary" link @click="handleEdit(scope.$index, scope.row, '编辑')" > 编辑 </el-button> <el-button size="small" link type="primary" @click="handleEdit(scope.$index, scope.row, '详情')" > 详情 </el-button> <el-button size="small" link type="danger" @click="handleDelete(scope.$index, scope.row)" > 删除 </el-button> </template> </el-table-column> </el-table> </table-container> </app-container> <div class="pagination"> <el-pagination v-model:current-page="listQuery.offset" v-model:page-size="listQuery.limit" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" /> </div> <el-dialog v-model="isPopconfirmShow" title="确认框" width="30%" > <span>确定删除吗?</span> <template #footer> <span class="dialog-footer"> <el-button @click="isPopconfirmShow = false">取消</el-button> <el-button type="primary" @click="confirmEvent"> 确定 </el-button> </span> </template> </el-dialog> </div> <list-source-add v-else :info-id="infoId" :button-type="buttonType" @close="close" /> </template> <style lang="scss" scoped> .pagination { position: fixed; bottom: 0; } </style>