InfrastructureCrudPage.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <template>
  2. <div class="app-container infra-page">
  3. <!-- 页头 -->
  4. <div class="page-head">
  5. <div class="page-title">
  6. <span class="page-title-bar" />
  7. <h2>{{ title || '设施信息' }}</h2>
  8. </div>
  9. <div class="page-actions">
  10. <el-button v-if="showAdd" type="primary" plain icon="Plus" @click="handleAdd">{{ addText }}</el-button>
  11. <el-button v-if="showDelete" type="danger" plain icon="Delete" :disabled="selection.length === 0" @click="handleDelete()">
  12. 删除
  13. </el-button>
  14. <el-button v-if="showImport" type="info" plain icon="Upload" @click="importVisible = true">导入</el-button>
  15. <el-button v-if="showExport" type="success" plain icon="Download" :loading="exporting" @click="handleExport">导出</el-button>
  16. </div>
  17. </div>
  18. <!-- 查询条件 -->
  19. <el-card shadow="never" class="filter-card">
  20. <el-form :inline="true" class="query-form" @submit.prevent>
  21. <el-form-item v-for="field in queryFields" :key="field.prop" :label="field.label">
  22. <el-select
  23. v-if="field.type === 'dict'"
  24. v-model="query[field.prop]"
  25. clearable
  26. :disabled="field.disabled"
  27. :placeholder="field.placeholder || '请选择' + field.label"
  28. style="width: 170px"
  29. >
  30. <el-option
  31. v-for="option in dictOptions(field.dictType)"
  32. :key="option.value"
  33. :label="option.label"
  34. :value="option.value"
  35. />
  36. </el-select>
  37. <el-input
  38. v-else
  39. v-model="query[field.prop]"
  40. clearable
  41. :placeholder="field.placeholder || '请输入' + field.label"
  42. style="width: 180px"
  43. @keyup.enter="handleQuery"
  44. />
  45. </el-form-item>
  46. <el-form-item>
  47. <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
  48. <el-button icon="Refresh" @click="resetQuery">重置</el-button>
  49. </el-form-item>
  50. </el-form>
  51. </el-card>
  52. <!-- 列表 -->
  53. <el-card shadow="never" class="table-card">
  54. <el-table v-loading="loading" :data="rows" border stripe @selection-change="onSelectionChange">
  55. <el-table-column type="selection" width="48" align="center" />
  56. <el-table-column
  57. v-for="column in columns"
  58. :key="column.prop"
  59. :prop="column.prop"
  60. :label="column.label"
  61. :min-width="column.width || 120"
  62. show-overflow-tooltip
  63. >
  64. <template #default="{ row }">
  65. <el-tag v-if="column.dictType" :type="tagType(row[column.prop], column.dictType)">
  66. {{ labelOf(row[column.prop], column.dictType) }}
  67. </el-tag>
  68. <span v-else>{{ display(row[column.prop]) }}</span>
  69. </template>
  70. </el-table-column>
  71. <slot name="table-columns-extra" />
  72. <el-table-column :label="actionColumnLabel" :width="actionColumnWidth" fixed="right" align="center">
  73. <template #default="{ row }">
  74. <slot name="row-actions" :row="row" :edit="handleEdit" :remove="handleDelete">
  75. <el-button link type="primary" icon="Edit" @click="handleEdit(row)">修改</el-button>
  76. <el-button link type="danger" icon="Delete" @click="handleDelete(row)">删除</el-button>
  77. </slot>
  78. </template>
  79. </el-table-column>
  80. </el-table>
  81. <pagination
  82. v-show="total > 0"
  83. :total="total"
  84. v-model:page="query.pageNum"
  85. v-model:limit="query.pageSize"
  86. @pagination="loadList"
  87. />
  88. </el-card>
  89. <!-- 新增 / 修改 -->
  90. <el-dialog v-model="dialogVisible" :title="dialogTitle" width="780px" append-to-body destroy-on-close>
  91. <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
  92. <el-row :gutter="16">
  93. <template v-for="field in formFields" :key="field.prop">
  94. <!-- 位置描述字段由「地址 + 地图坐标」组件统一承载,放在备注上一栏 -->
  95. <FacilityLocationFields
  96. v-if="field.prop === 'location'"
  97. :model-value="form"
  98. address-prop="location"
  99. @update:model-value="(val) => Object.assign(form, val)"
  100. />
  101. <el-col v-else :span="field.span || 12">
  102. <el-form-item :label="field.label" :prop="field.prop">
  103. <el-select
  104. v-if="field.type === 'dict'"
  105. v-model="form[field.prop]"
  106. clearable
  107. :disabled="field.disabled"
  108. :placeholder="field.placeholder || '请选择' + field.label"
  109. style="width: 100%"
  110. >
  111. <el-option
  112. v-for="option in dictOptions(field.dictType)"
  113. :key="option.value"
  114. :label="option.label"
  115. :value="option.value"
  116. />
  117. </el-select>
  118. <el-input-number
  119. v-else-if="field.type === 'number'"
  120. v-model="form[field.prop]"
  121. :precision="field.precision === undefined ? 2 : field.precision"
  122. controls-position="right"
  123. style="width: 100%"
  124. />
  125. <el-date-picker
  126. v-else-if="field.type === 'date'"
  127. v-model="form[field.prop]"
  128. :type="field.dateType || 'date'"
  129. :value-format="field.valueFormat || (field.dateType === 'datetime' ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD')"
  130. :disabled="field.disabled"
  131. :placeholder="field.placeholder || '请选择' + field.label"
  132. style="width: 100%"
  133. />
  134. <el-input
  135. v-else-if="field.type === 'textarea'"
  136. v-model="form[field.prop]"
  137. type="textarea"
  138. :rows="3"
  139. :placeholder="field.placeholder || '请输入' + field.label"
  140. />
  141. <el-input
  142. v-else
  143. v-model="form[field.prop]"
  144. :placeholder="field.placeholder || '请输入' + field.label"
  145. />
  146. </el-form-item>
  147. </el-col>
  148. </template>
  149. </el-row>
  150. </el-form>
  151. <template #footer>
  152. <el-button type="primary" :loading="submitting" @click="submitForm">确 定</el-button>
  153. <el-button @click="dialogVisible = false">取 消</el-button>
  154. </template>
  155. </el-dialog>
  156. <!-- 批量导入 -->
  157. <FacilityImportDialog
  158. v-if="showImport"
  159. v-model="importVisible"
  160. :title="importTitle || title"
  161. :upload-url="importUrl"
  162. :template-url="templateUrl"
  163. :dict-hint="dictHint"
  164. @imported="onImported"
  165. />
  166. </div>
  167. </template>
  168. <script setup>
  169. import { computed, getCurrentInstance, nextTick, onMounted, reactive, ref, watch } from 'vue'
  170. import { saveAs } from 'file-saver'
  171. import request from '@/utils/request'
  172. import { getDicts } from '@/api/system/dict/data'
  173. import FacilityImportDialog from './FacilityImportDialog.vue'
  174. import FacilityLocationFields from './FacilityLocationFields.vue'
  175. const props = defineProps({
  176. /** 查询条件字段 */
  177. queryFields: { type: Array, default: () => [] },
  178. /** 表格列 */
  179. columns: { type: Array, required: true },
  180. /** 表单字段 */
  181. formFields: { type: Array, default: () => [] },
  182. /** 接口集合:{ page, detail, save, update, remove } */
  183. api: { type: Object, required: true },
  184. /** 主键字段名 */
  185. idField: { type: String, default: 'id' },
  186. /** 固定附加查询条件(如排水闸门固定 facilityType=0) */
  187. extraQuery: { type: Object, default: () => ({}) },
  188. /** 查询初值 */
  189. defaultQuery: { type: Object, default: () => ({}) },
  190. /** 标题文案(删除提示用) */
  191. title: { type: String, default: '' },
  192. /** 新增按钮文案 */
  193. addText: { type: String, default: '新增' },
  194. /** 是否展示新增 */
  195. showAdd: { type: Boolean, default: true },
  196. /** 是否展示批量删除 */
  197. showDelete: { type: Boolean, default: true },
  198. /** 是否展示导入 */
  199. showImport: { type: Boolean, default: false },
  200. /** 是否展示导出 */
  201. showExport: { type: Boolean, default: false },
  202. /** 导入地址,showImport=true 时必填 */
  203. importUrl: { type: String, default: '' },
  204. /** 模板下载地址,showImport=true 时必填 */
  205. templateUrl: { type: String, default: '' },
  206. /** 导出地址(GET,返回 xlsx) */
  207. exportUrl: { type: String, default: '' },
  208. /** 导入弹窗标题 */
  209. importTitle: { type: String, default: '' },
  210. /** 导入模板字典列说明 */
  211. dictHint: { type: String, default: '' },
  212. /** 导出文件名前缀 */
  213. exportFileName: { type: String, default: '' },
  214. /** 作为视图切换使用的查询字段名(变化时 emit switch-change) */
  215. switchField: { type: String, default: '' },
  216. /** 操作列表头/宽度 */
  217. actionColumnLabel: { type: String, default: '操作' },
  218. actionColumnWidth: { type: Number, default: 170 }
  219. })
  220. const emit = defineEmits(['switch-change'])
  221. const { proxy } = getCurrentInstance()
  222. // ==================== 字典 ====================
  223. const dictMap = reactive({})
  224. function loadDict(dictType) {
  225. if (!dictType || dictMap[dictType]) return
  226. getDicts(dictType)
  227. .then((res) => {
  228. dictMap[dictType] = (res.data || []).map((item) => ({
  229. value: item.dictValue,
  230. label: item.dictLabel,
  231. listClass: item.listClass
  232. }))
  233. })
  234. .catch(() => {
  235. dictMap[dictType] = []
  236. })
  237. }
  238. function dictOptions(dictType) {
  239. return dictMap[dictType] || []
  240. }
  241. function labelOf(value, dictType) {
  242. if (value === null || value === undefined || value === '') return '-'
  243. const hit = dictOptions(dictType).find((option) => String(option.value) === String(value))
  244. return hit ? hit.label : value
  245. }
  246. function tagType(value, dictType) {
  247. if (value === null || value === undefined || value === '') return 'info'
  248. const hit = dictOptions(dictType).find((option) => String(option.value) === String(value))
  249. return (hit && hit.listClass) || 'info'
  250. }
  251. function display(value) {
  252. return value === null || value === undefined || value === '' ? '-' : value
  253. }
  254. function collectDictTypes() {
  255. const types = new Set()
  256. const collect = (list) => (list || []).forEach((field) => field.dictType && types.add(field.dictType))
  257. collect(props.queryFields)
  258. collect(props.columns)
  259. collect(props.formFields)
  260. types.forEach(loadDict)
  261. }
  262. onMounted(() => {
  263. collectDictTypes()
  264. loadList()
  265. })
  266. // ==================== 列表 ====================
  267. const rows = ref([])
  268. const total = ref(0)
  269. const loading = ref(false)
  270. const selection = ref([])
  271. const query = reactive({
  272. pageNum: 1,
  273. pageSize: 10,
  274. ...props.defaultQuery
  275. })
  276. if (props.switchField) {
  277. watch(() => query[props.switchField], (value) => {
  278. emit('switch-change', value)
  279. nextTick(handleQuery)
  280. })
  281. }
  282. function buildParams() {
  283. const params = { ...query }
  284. Object.keys(params).forEach((key) => {
  285. if (params[key] === '' || params[key] === undefined || params[key] === null) {
  286. delete params[key]
  287. }
  288. })
  289. return { ...params, ...props.extraQuery }
  290. }
  291. async function loadList() {
  292. loading.value = true
  293. try {
  294. const res = await props.api.page(buildParams())
  295. const payload = res && res.data && res.data.records !== undefined ? res.data : res || {}
  296. rows.value = payload.records || []
  297. total.value = payload.total || 0
  298. } catch (error) {
  299. rows.value = []
  300. total.value = 0
  301. } finally {
  302. loading.value = false
  303. }
  304. }
  305. function handleQuery() {
  306. query.pageNum = 1
  307. loadList()
  308. }
  309. function resetQuery() {
  310. Object.keys(query).forEach((key) => {
  311. if (key !== 'pageNum' && key !== 'pageSize') {
  312. query[key] = ''
  313. }
  314. })
  315. query.pageNum = 1
  316. loadList()
  317. }
  318. function onSelectionChange(list) {
  319. selection.value = list
  320. }
  321. // ==================== 导入 / 导出 ====================
  322. const importVisible = ref(false)
  323. const exporting = ref(false)
  324. function onImported() {
  325. handleQuery()
  326. }
  327. function handleExport() {
  328. if (!props.exportUrl) {
  329. proxy.$modal.msgError('未配置导出地址')
  330. return
  331. }
  332. exporting.value = true
  333. request({
  334. url: props.exportUrl,
  335. method: 'get',
  336. params: buildParams(),
  337. responseType: 'blob'
  338. })
  339. .then((blob) => {
  340. const name = props.exportFileName || props.title || '设施信息'
  341. saveAs(new Blob([blob]), `${name}_${new Date().toISOString().slice(0, 10)}.xlsx`)
  342. })
  343. .catch(() => proxy.$modal.msgError('导出失败,请稍后重试'))
  344. .finally(() => {
  345. exporting.value = false
  346. })
  347. }
  348. // ==================== 表单 ====================
  349. const dialogVisible = ref(false)
  350. const dialogTitle = ref('')
  351. const submitting = ref(false)
  352. const formRef = ref()
  353. const form = reactive({})
  354. const rules = computed(() => {
  355. const result = {}
  356. props.formFields
  357. .filter((field) => field.required)
  358. .forEach((field) => {
  359. result[field.prop] = [{ required: true, message: `请填写${field.label}`, trigger: 'blur' }]
  360. })
  361. return result
  362. })
  363. function emptyForm() {
  364. const initial = { status: '0' }
  365. props.formFields.forEach((field) => {
  366. if (!(field.prop in initial)) {
  367. initial[field.prop] = field.default !== undefined
  368. ? field.default
  369. : (field.type === 'number' ? undefined : '')
  370. }
  371. })
  372. return initial
  373. }
  374. function handleAdd() {
  375. Object.keys(form).forEach((key) => delete form[key])
  376. Object.assign(form, emptyForm())
  377. dialogTitle.value = props.addText
  378. dialogVisible.value = true
  379. }
  380. function handleEdit(row) {
  381. Object.keys(form).forEach((key) => delete form[key])
  382. Object.assign(form, row)
  383. dialogTitle.value = '修改'
  384. dialogVisible.value = true
  385. }
  386. function submitForm() {
  387. formRef.value.validate((valid) => {
  388. if (!valid) return
  389. submitting.value = true
  390. const payload = { ...form }
  391. const idValue = payload[props.idField]
  392. const isEdit = idValue !== undefined && idValue !== null && idValue !== ''
  393. const action = isEdit ? props.api.update(payload) : props.api.save(payload)
  394. action
  395. .then(() => {
  396. proxy.$modal.msgSuccess(isEdit ? '修改成功' : '新增成功')
  397. dialogVisible.value = false
  398. loadList()
  399. })
  400. .catch((error) => {
  401. proxy.$modal.msgError(error?.msg || (isEdit ? '修改失败' : '新增失败'))
  402. })
  403. .finally(() => {
  404. submitting.value = false
  405. })
  406. })
  407. }
  408. function handleDelete(row) {
  409. const ids = row && row[props.idField] ? [row[props.idField]] : selection.value.map((item) => item[props.idField])
  410. if (!ids || ids.length === 0) {
  411. proxy.$modal.msgError('请先选择要删除的数据')
  412. return
  413. }
  414. proxy.$modal
  415. .confirm(`是否确认删除选中的 ${ids.length} 条${props.title || ''}数据?`)
  416. .then(() => props.api.remove(ids))
  417. .then(() => {
  418. proxy.$modal.msgSuccess('删除成功')
  419. loadList()
  420. })
  421. .catch(() => {})
  422. }
  423. </script>
  424. <style scoped lang="scss">
  425. .infra-page {
  426. background: #f5f8fc;
  427. }
  428. .page-head {
  429. display: flex;
  430. align-items: center;
  431. justify-content: space-between;
  432. gap: 12px;
  433. margin-bottom: 16px;
  434. }
  435. .page-title {
  436. display: flex;
  437. align-items: center;
  438. gap: 10px;
  439. }
  440. .page-title-bar {
  441. width: 4px;
  442. height: 20px;
  443. border-radius: 2px;
  444. background: var(--el-color-primary);
  445. }
  446. .page-title h2 {
  447. margin: 0;
  448. font-size: 18px;
  449. font-weight: 600;
  450. color: #1f2937;
  451. }
  452. .page-actions {
  453. display: flex;
  454. align-items: center;
  455. gap: 8px;
  456. flex-wrap: wrap;
  457. }
  458. .filter-card {
  459. margin-bottom: 16px;
  460. border-radius: 12px;
  461. }
  462. .table-card {
  463. border-radius: 12px;
  464. }
  465. .query-form {
  466. margin-bottom: 0;
  467. :deep(.el-form-item) {
  468. margin-bottom: 0;
  469. }
  470. }
  471. @media (max-width: 768px) {
  472. .page-head {
  473. flex-direction: column;
  474. align-items: flex-start;
  475. }
  476. }
  477. </style>