| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- <template>
- <div class="app-container infra-page">
- <!-- 页头 -->
- <div class="page-head">
- <div class="page-title">
- <span class="page-title-bar" />
- <h2>{{ title || '设施信息' }}</h2>
- </div>
- <div class="page-actions">
- <el-button v-if="showAdd" type="primary" plain icon="Plus" @click="handleAdd">{{ addText }}</el-button>
- <el-button v-if="showDelete" type="danger" plain icon="Delete" :disabled="selection.length === 0" @click="handleDelete()">
- 删除
- </el-button>
- <el-button v-if="showImport" type="info" plain icon="Upload" @click="importVisible = true">导入</el-button>
- <el-button v-if="showExport" type="success" plain icon="Download" :loading="exporting" @click="handleExport">导出</el-button>
- </div>
- </div>
- <!-- 查询条件 -->
- <el-card shadow="never" class="filter-card">
- <el-form :inline="true" class="query-form" @submit.prevent>
- <el-form-item v-for="field in queryFields" :key="field.prop" :label="field.label">
- <el-select
- v-if="field.type === 'dict'"
- v-model="query[field.prop]"
- clearable
- :disabled="field.disabled"
- :placeholder="field.placeholder || '请选择' + field.label"
- style="width: 170px"
- >
- <el-option
- v-for="option in dictOptions(field.dictType)"
- :key="option.value"
- :label="option.label"
- :value="option.value"
- />
- </el-select>
- <el-input
- v-else
- v-model="query[field.prop]"
- clearable
- :placeholder="field.placeholder || '请输入' + field.label"
- style="width: 180px"
- @keyup.enter="handleQuery"
- />
- </el-form-item>
- <el-form-item>
- <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
- <el-button icon="Refresh" @click="resetQuery">重置</el-button>
- </el-form-item>
- </el-form>
- </el-card>
- <!-- 列表 -->
- <el-card shadow="never" class="table-card">
- <el-table v-loading="loading" :data="rows" border stripe @selection-change="onSelectionChange">
- <el-table-column type="selection" width="48" align="center" />
- <el-table-column
- v-for="column in columns"
- :key="column.prop"
- :prop="column.prop"
- :label="column.label"
- :min-width="column.width || 120"
- show-overflow-tooltip
- >
- <template #default="{ row }">
- <el-tag v-if="column.dictType" :type="tagType(row[column.prop], column.dictType)">
- {{ labelOf(row[column.prop], column.dictType) }}
- </el-tag>
- <span v-else>{{ display(row[column.prop]) }}</span>
- </template>
- </el-table-column>
- <slot name="table-columns-extra" />
- <el-table-column :label="actionColumnLabel" :width="actionColumnWidth" fixed="right" align="center">
- <template #default="{ row }">
- <slot name="row-actions" :row="row" :edit="handleEdit" :remove="handleDelete">
- <el-button link type="primary" icon="Edit" @click="handleEdit(row)">修改</el-button>
- <el-button link type="danger" icon="Delete" @click="handleDelete(row)">删除</el-button>
- </slot>
- </template>
- </el-table-column>
- </el-table>
- <pagination
- v-show="total > 0"
- :total="total"
- v-model:page="query.pageNum"
- v-model:limit="query.pageSize"
- @pagination="loadList"
- />
- </el-card>
- <!-- 新增 / 修改 -->
- <el-dialog v-model="dialogVisible" :title="dialogTitle" width="780px" append-to-body destroy-on-close>
- <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
- <el-row :gutter="16">
- <template v-for="field in formFields" :key="field.prop">
- <!-- 位置描述字段由「地址 + 地图坐标」组件统一承载,放在备注上一栏 -->
- <FacilityLocationFields
- v-if="field.prop === 'location'"
- :model-value="form"
- address-prop="location"
- @update:model-value="(val) => Object.assign(form, val)"
- />
- <el-col v-else :span="field.span || 12">
- <el-form-item :label="field.label" :prop="field.prop">
- <el-select
- v-if="field.type === 'dict'"
- v-model="form[field.prop]"
- clearable
- :disabled="field.disabled"
- :placeholder="field.placeholder || '请选择' + field.label"
- style="width: 100%"
- >
- <el-option
- v-for="option in dictOptions(field.dictType)"
- :key="option.value"
- :label="option.label"
- :value="option.value"
- />
- </el-select>
- <el-input-number
- v-else-if="field.type === 'number'"
- v-model="form[field.prop]"
- :precision="field.precision === undefined ? 2 : field.precision"
- controls-position="right"
- style="width: 100%"
- />
- <el-date-picker
- v-else-if="field.type === 'date'"
- v-model="form[field.prop]"
- :type="field.dateType || 'date'"
- :value-format="field.valueFormat || (field.dateType === 'datetime' ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD')"
- :disabled="field.disabled"
- :placeholder="field.placeholder || '请选择' + field.label"
- style="width: 100%"
- />
- <el-input
- v-else-if="field.type === 'textarea'"
- v-model="form[field.prop]"
- type="textarea"
- :rows="3"
- :placeholder="field.placeholder || '请输入' + field.label"
- />
- <el-input
- v-else
- v-model="form[field.prop]"
- :placeholder="field.placeholder || '请输入' + field.label"
- />
- </el-form-item>
- </el-col>
- </template>
- </el-row>
- </el-form>
- <template #footer>
- <el-button type="primary" :loading="submitting" @click="submitForm">确 定</el-button>
- <el-button @click="dialogVisible = false">取 消</el-button>
- </template>
- </el-dialog>
- <!-- 批量导入 -->
- <FacilityImportDialog
- v-if="showImport"
- v-model="importVisible"
- :title="importTitle || title"
- :upload-url="importUrl"
- :template-url="templateUrl"
- :dict-hint="dictHint"
- @imported="onImported"
- />
- </div>
- </template>
- <script setup>
- import { computed, getCurrentInstance, nextTick, onMounted, reactive, ref, watch } from 'vue'
- import { saveAs } from 'file-saver'
- import request from '@/utils/request'
- import { getDicts } from '@/api/system/dict/data'
- import FacilityImportDialog from './FacilityImportDialog.vue'
- import FacilityLocationFields from './FacilityLocationFields.vue'
- const props = defineProps({
- /** 查询条件字段 */
- queryFields: { type: Array, default: () => [] },
- /** 表格列 */
- columns: { type: Array, required: true },
- /** 表单字段 */
- formFields: { type: Array, default: () => [] },
- /** 接口集合:{ page, detail, save, update, remove } */
- api: { type: Object, required: true },
- /** 主键字段名 */
- idField: { type: String, default: 'id' },
- /** 固定附加查询条件(如排水闸门固定 facilityType=0) */
- extraQuery: { type: Object, default: () => ({}) },
- /** 查询初值 */
- defaultQuery: { type: Object, default: () => ({}) },
- /** 标题文案(删除提示用) */
- title: { type: String, default: '' },
- /** 新增按钮文案 */
- addText: { type: String, default: '新增' },
- /** 是否展示新增 */
- showAdd: { type: Boolean, default: true },
- /** 是否展示批量删除 */
- showDelete: { type: Boolean, default: true },
- /** 是否展示导入 */
- showImport: { type: Boolean, default: false },
- /** 是否展示导出 */
- showExport: { type: Boolean, default: false },
- /** 导入地址,showImport=true 时必填 */
- importUrl: { type: String, default: '' },
- /** 模板下载地址,showImport=true 时必填 */
- templateUrl: { type: String, default: '' },
- /** 导出地址(GET,返回 xlsx) */
- exportUrl: { type: String, default: '' },
- /** 导入弹窗标题 */
- importTitle: { type: String, default: '' },
- /** 导入模板字典列说明 */
- dictHint: { type: String, default: '' },
- /** 导出文件名前缀 */
- exportFileName: { type: String, default: '' },
- /** 作为视图切换使用的查询字段名(变化时 emit switch-change) */
- switchField: { type: String, default: '' },
- /** 操作列表头/宽度 */
- actionColumnLabel: { type: String, default: '操作' },
- actionColumnWidth: { type: Number, default: 170 }
- })
- const emit = defineEmits(['switch-change'])
- const { proxy } = getCurrentInstance()
- // ==================== 字典 ====================
- const dictMap = reactive({})
- function loadDict(dictType) {
- if (!dictType || dictMap[dictType]) return
- getDicts(dictType)
- .then((res) => {
- dictMap[dictType] = (res.data || []).map((item) => ({
- value: item.dictValue,
- label: item.dictLabel,
- listClass: item.listClass
- }))
- })
- .catch(() => {
- dictMap[dictType] = []
- })
- }
- function dictOptions(dictType) {
- return dictMap[dictType] || []
- }
- function labelOf(value, dictType) {
- if (value === null || value === undefined || value === '') return '-'
- const hit = dictOptions(dictType).find((option) => String(option.value) === String(value))
- return hit ? hit.label : value
- }
- function tagType(value, dictType) {
- if (value === null || value === undefined || value === '') return 'info'
- const hit = dictOptions(dictType).find((option) => String(option.value) === String(value))
- return (hit && hit.listClass) || 'info'
- }
- function display(value) {
- return value === null || value === undefined || value === '' ? '-' : value
- }
- function collectDictTypes() {
- const types = new Set()
- const collect = (list) => (list || []).forEach((field) => field.dictType && types.add(field.dictType))
- collect(props.queryFields)
- collect(props.columns)
- collect(props.formFields)
- types.forEach(loadDict)
- }
- onMounted(() => {
- collectDictTypes()
- loadList()
- })
- // ==================== 列表 ====================
- const rows = ref([])
- const total = ref(0)
- const loading = ref(false)
- const selection = ref([])
- const query = reactive({
- pageNum: 1,
- pageSize: 10,
- ...props.defaultQuery
- })
- if (props.switchField) {
- watch(() => query[props.switchField], (value) => {
- emit('switch-change', value)
- nextTick(handleQuery)
- })
- }
- function buildParams() {
- const params = { ...query }
- Object.keys(params).forEach((key) => {
- if (params[key] === '' || params[key] === undefined || params[key] === null) {
- delete params[key]
- }
- })
- return { ...params, ...props.extraQuery }
- }
- async function loadList() {
- loading.value = true
- try {
- const res = await props.api.page(buildParams())
- const payload = res && res.data && res.data.records !== undefined ? res.data : res || {}
- rows.value = payload.records || []
- total.value = payload.total || 0
- } catch (error) {
- rows.value = []
- total.value = 0
- } finally {
- loading.value = false
- }
- }
- function handleQuery() {
- query.pageNum = 1
- loadList()
- }
- function resetQuery() {
- Object.keys(query).forEach((key) => {
- if (key !== 'pageNum' && key !== 'pageSize') {
- query[key] = ''
- }
- })
- query.pageNum = 1
- loadList()
- }
- function onSelectionChange(list) {
- selection.value = list
- }
- // ==================== 导入 / 导出 ====================
- const importVisible = ref(false)
- const exporting = ref(false)
- function onImported() {
- handleQuery()
- }
- function handleExport() {
- if (!props.exportUrl) {
- proxy.$modal.msgError('未配置导出地址')
- return
- }
- exporting.value = true
- request({
- url: props.exportUrl,
- method: 'get',
- params: buildParams(),
- responseType: 'blob'
- })
- .then((blob) => {
- const name = props.exportFileName || props.title || '设施信息'
- saveAs(new Blob([blob]), `${name}_${new Date().toISOString().slice(0, 10)}.xlsx`)
- })
- .catch(() => proxy.$modal.msgError('导出失败,请稍后重试'))
- .finally(() => {
- exporting.value = false
- })
- }
- // ==================== 表单 ====================
- const dialogVisible = ref(false)
- const dialogTitle = ref('')
- const submitting = ref(false)
- const formRef = ref()
- const form = reactive({})
- const rules = computed(() => {
- const result = {}
- props.formFields
- .filter((field) => field.required)
- .forEach((field) => {
- result[field.prop] = [{ required: true, message: `请填写${field.label}`, trigger: 'blur' }]
- })
- return result
- })
- function emptyForm() {
- const initial = { status: '0' }
- props.formFields.forEach((field) => {
- if (!(field.prop in initial)) {
- initial[field.prop] = field.default !== undefined
- ? field.default
- : (field.type === 'number' ? undefined : '')
- }
- })
- return initial
- }
- function handleAdd() {
- Object.keys(form).forEach((key) => delete form[key])
- Object.assign(form, emptyForm())
- dialogTitle.value = props.addText
- dialogVisible.value = true
- }
- function handleEdit(row) {
- Object.keys(form).forEach((key) => delete form[key])
- Object.assign(form, row)
- dialogTitle.value = '修改'
- dialogVisible.value = true
- }
- function submitForm() {
- formRef.value.validate((valid) => {
- if (!valid) return
- submitting.value = true
- const payload = { ...form }
- const idValue = payload[props.idField]
- const isEdit = idValue !== undefined && idValue !== null && idValue !== ''
- const action = isEdit ? props.api.update(payload) : props.api.save(payload)
- action
- .then(() => {
- proxy.$modal.msgSuccess(isEdit ? '修改成功' : '新增成功')
- dialogVisible.value = false
- loadList()
- })
- .catch((error) => {
- proxy.$modal.msgError(error?.msg || (isEdit ? '修改失败' : '新增失败'))
- })
- .finally(() => {
- submitting.value = false
- })
- })
- }
- function handleDelete(row) {
- const ids = row && row[props.idField] ? [row[props.idField]] : selection.value.map((item) => item[props.idField])
- if (!ids || ids.length === 0) {
- proxy.$modal.msgError('请先选择要删除的数据')
- return
- }
- proxy.$modal
- .confirm(`是否确认删除选中的 ${ids.length} 条${props.title || ''}数据?`)
- .then(() => props.api.remove(ids))
- .then(() => {
- proxy.$modal.msgSuccess('删除成功')
- loadList()
- })
- .catch(() => {})
- }
- </script>
- <style scoped lang="scss">
- .infra-page {
- background: #f5f8fc;
- }
- .page-head {
- display: flex;
- align-items: center;
- justify-content: space-between;
- gap: 12px;
- margin-bottom: 16px;
- }
- .page-title {
- display: flex;
- align-items: center;
- gap: 10px;
- }
- .page-title-bar {
- width: 4px;
- height: 20px;
- border-radius: 2px;
- background: var(--el-color-primary);
- }
- .page-title h2 {
- margin: 0;
- font-size: 18px;
- font-weight: 600;
- color: #1f2937;
- }
- .page-actions {
- display: flex;
- align-items: center;
- gap: 8px;
- flex-wrap: wrap;
- }
- .filter-card {
- margin-bottom: 16px;
- border-radius: 12px;
- }
- .table-card {
- border-radius: 12px;
- }
- .query-form {
- margin-bottom: 0;
- :deep(.el-form-item) {
- margin-bottom: 0;
- }
- }
- @media (max-width: 768px) {
- .page-head {
- flex-direction: column;
- align-items: flex-start;
- }
- }
- </style>
|