Files
shanxia2.0/web-admin/src/views/system/site/index.vue
T
2026-06-29 11:15:39 +08:00

712 lines
23 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!-- 站点管理 -->
<!-- eslint-disable vue/no-v-model-argument -->
<template>
<div class="page-container">
<section class="panel">
<div class="panel-top">
<h2>站点列表</h2>
</div>
<div class="filter-block">
<el-form :model="searchForm" class="filter-form" @submit.prevent>
<div class="filter-grid">
<el-form-item label="关键词">
<el-input v-model="searchForm.keyword" placeholder="站点名称/编码" clearable />
</el-form-item>
</div>
<div class="filter-actions-row">
<span class="filter-hint">按站点名称、编码模糊查询。</span>
<div class="filter-actions-end">
<el-button class="btn-ghost" @click="handleReset"
><el-icon><RefreshLeft /></el-icon>重置</el-button
>
<el-button class="btn-main" type="primary" @click="handleSearch"
><el-icon><Search /></el-icon>搜索</el-button
>
</div>
</div>
</el-form>
</div>
<div class="list-toolbar">
<div class="list-toolbar-row">
<div class="list-toolbar-selected">
已选 <span class="sel-num">{{ selectedRows.length }}</span> 项
</div>
<el-button class="btn-main" type="primary" @click="handleAdd">
<el-icon><Plus /></el-icon>
新增站点
</el-button>
<el-button
class="btn-ghost"
type="danger"
:disabled="selectedRows.length === 0 || !allRowsDeletableByCreator(selectedRows)"
:title="allRowsDeletableByCreator(selectedRows) ? '' : CREATOR_DELETE_TIP"
@click="handleBatchDelete"
>
批量删除
</el-button>
</div>
</div>
<div class="list-body list-wrap">
<el-table
class="list-table"
:data="tableData"
v-loading="loading"
stripe
row-key="id"
@selection-change="handleSelectionChange"
@row-dblclick="handleView"
>
<el-table-column type="selection" min-width="55" align="center" header-align="center" />
<el-table-column
type="index"
label="序号"
min-width="68"
align="center"
header-align="center"
:index="(index) => (pagination.page - 1) * pagination.pageSize + index + 1"
/>
<el-table-column
prop="siteName"
label="站点名称"
min-width="140"
align="center"
header-align="center"
/>
<el-table-column
prop="siteCode"
label="站点编码"
min-width="120"
align="center"
header-align="center"
/>
<el-table-column
prop="siteType"
label="站点类型"
min-width="100"
align="center"
header-align="center"
>
<template #default="{ row }">
{{ getSiteTypeLabel(row.siteType) }}
</template>
</el-table-column>
<el-table-column
prop="status"
label="状态"
min-width="80"
align="center"
header-align="center"
>
<template #default="{ row }">
<el-tag :type="dictStore.getTagType('sys_status', row.status)" size="small">
{{ dictStore.getLabel('sys_status', row.status) }}
</el-tag>
</template>
</el-table-column>
<el-table-column
prop="address"
label="地址"
min-width="160"
align="center"
header-align="center"
show-overflow-tooltip
/>
<el-table-column
prop="contactName"
label="联系人"
min-width="100"
align="center"
header-align="center"
/>
<el-table-column
prop="contactPhone"
label="联系电话"
min-width="140"
align="center"
header-align="center"
/>
<el-table-column
prop="createTime"
label="创建时间"
min-width="180"
align="center"
header-align="center"
/>
<el-table-column
label="操作"
min-width="120"
fixed="right"
align="center"
header-align="center"
>
<template #default="{ row }">
<div style="display: flex; justify-content: center; gap: 8px">
<el-button class="btn-op-item" @click="handleView(row)">
<el-icon><View /></el-icon>查看
</el-button>
<el-dropdown trigger="hover" @command="(cmd: string) => handleDropdownCommand(cmd, row)">
<el-button class="btn-op-item">
<el-icon><More /></el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="edit">
<el-icon><Edit /></el-icon>编辑
</el-dropdown-item>
<el-dropdown-item command="delete" divided :disabled="!canDeleteByCreator(row)">
<el-icon><Delete /></el-icon>删除
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</template>
</el-table-column>
</el-table>
<div class="pagination list-pager-bar">
<el-pagination
:current-page="pagination.page"
:page-size="pagination.pageSize"
:total="pagination.total"
:page-sizes="[10, 20, 50]"
layout="total, sizes, prev, pager, next"
@size-change="(val) => ((pagination.page = 1), (pagination.pageSize = val), loadData())"
@current-change="(val) => ((pagination.page = val), loadData())"
/>
</div>
</div>
<el-dialog
v-model="dialogVisible"
:title="dialogTitle"
width="720px"
class="dialog-form-layout"
align-center
destroy-on-close
:close-on-click-modal="false"
@close="handleDialogClose"
>
<el-form
ref="formRef"
:model="formData"
:rules="formRules"
label-position="top"
class="site-dialog-form"
scroll-to-error
:scroll-into-view-options="{ behavior: 'smooth', block: 'center' }"
>
<div class="form-grid-2">
<el-form-item label="站点名称" prop="siteName">
<el-input v-model="formData.siteName" placeholder="请输入站点名称" />
</el-form-item>
<el-form-item label="站点编码" prop="siteCode">
<el-input v-model="formData.siteCode" placeholder="请输入站点编码" />
</el-form-item>
<el-form-item label="站点类型">
<el-select v-model="formData.siteType" placeholder="请选择站点类型" clearable>
<el-option
v-for="opt in siteTypeOptions"
:key="opt.value"
:label="opt.label"
:value="opt.value"
/>
</el-select>
</el-form-item>
<el-form-item label="状态">
<el-select v-model="formData.status" placeholder="请选择状态">
<el-option
v-for="opt in statusOptions"
:key="opt.value"
:label="opt.label"
:value="opt.value"
/>
</el-select>
</el-form-item>
<el-form-item label="地址" style="grid-column: 1 / -1">
<el-input v-model="formData.address" placeholder="请输入地址" />
</el-form-item>
<el-form-item label="联系人">
<el-select
v-model="contactUserId"
class="w-full"
filterable
remote
clearable
reserve-keyword
:remote-method="remoteSearchContactUsers"
:loading="contactUserLoading"
placeholder="输入关键词检索用户"
@change="onContactUserChange"
>
<el-option
v-for="u in contactUserOptions"
:key="u.id"
:label="formatContactUserLabel(u)"
:value="u.id"
/>
</el-select>
</el-form-item>
<el-form-item label="联系电话">
<el-input
v-model="formData.contactPhone"
placeholder="可选,选择联系人后自动填入,可修改"
/>
</el-form-item>
<el-form-item label="经营范围" style="grid-column: 1 / -1">
<el-select
v-model="formData.businessScopeList"
multiple
placeholder="请选择经营范围"
clearable
>
<el-option
v-for="opt in businessScopeOptions"
:key="opt.value"
:label="opt.label"
:value="opt.value"
/>
</el-select>
</el-form-item>
<el-form-item label="备注" style="grid-column: 1 / -1">
<el-input v-model="formData.remark" type="textarea" :rows="2" placeholder="可选" />
</el-form-item>
</div>
</el-form>
<template #footer>
<el-button class="btn-ghost" @click="dialogVisible = false">
<el-icon><Close /></el-icon>
取消
</el-button>
<el-button class="btn-main" type="primary" @click="handleSubmit" :loading="submitLoading">
<el-icon><Check /></el-icon>
确定
</el-button>
</template>
</el-dialog>
<SystemViewDialog
v-model:visible="viewVisible"
:title="viewRow?.siteName || '站点详情'"
:icon="Location"
:meta="`站点编码:${viewRow?.siteCode || ''}${viewRow?.createTime ? ' · ' + viewRow.createTime : ''}`"
:sections="viewSections"
/>
</section>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, computed, onMounted, nextTick } from 'vue'
import { canDeleteByCreator, filterDeletableByCreator, allRowsDeletableByCreator, CREATOR_DELETE_TIP } from '@/utils/creator-delete'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Close, Check, View, Edit, Delete, More, Location, RefreshLeft, Search } from '@element-plus/icons-vue'
import { withFullscreenLoading, fullscreenLoadingText } from '@/utils/fullscreen-loading'
import { useDictStore } from '@/store/modules/dict'
import SystemViewDialog from '@/components/business/system-view-dialog/index.vue'
import {
fetchSiteList,
fetchCreateSite,
fetchUpdateSite,
fetchDeleteSite,
fetchDictDataOptions,
fetchUserList
} from '@/api/admin'
defineOptions({ name: 'SystemSite' })
const dictStore = useDictStore()
// ---- 查看弹窗 ----
const viewVisible = ref(false)
const viewRow = ref<Api.Site.SiteItem | null>(null)
const viewSections = computed(() => {
const row = viewRow.value
if (!row) return []
return [
{
title: '基本信息',
fields: [
{ label: '站点名称', value: row.siteName },
{ label: '站点编码', value: row.siteCode },
{ label: '站点类型', value: getSiteTypeLabel(row.siteType), tagType: 'primary' },
{
label: '状态',
value: dictStore.getLabel('sys_status', row.status),
tagType: dictStore.getTagType('sys_status', row.status)
},
{ label: '地址', value: row.address, span: true }
]
},
{
title: '联系信息',
fields: [
{ label: '联系人', value: row.contactName },
{ label: '联系电话', value: row.contactPhone },
{
label: '经营范围',
value: row.businessScope
? row.businessScope
.split(',')
.map((code) => {
const opt = businessScopeOptions.value.find((o) => o.value === code)
return opt ? opt.label : code
})
.join('、')
: '无',
span: true
},
{ label: '备注', value: row.remark, span: true },
{ label: '创建时间', value: row.createTime }
]
}
]
})
function handleView(row: Api.Site.SiteItem) {
viewRow.value = row
viewVisible.value = true
}
const searchForm = reactive<{ keyword: string }>({ keyword: '' })
const tableData = ref<Api.Site.SiteItem[]>([])
const selectedRows = ref<Api.Site.SiteItem[]>([])
const loading = ref(false)
const pagination = reactive({ page: 1, pageSize: 10, total: 0 })
const dialogVisible = ref(false)
const dialogTitle = ref('新增站点')
const isEdit = ref(false)
const submitLoading = ref(false)
const formRef = ref()
const editingId = ref<string | null>(null)
const formData = reactive({
siteName: '',
siteCode: '',
siteType: '',
status: '1',
address: '',
contactName: '',
contactPhone: '',
businessScopeList: [] as string[],
orderNum: 0,
remark: ''
})
const formRules = {
siteName: [{ required: true, message: '请输入站点名称', trigger: 'blur' }],
siteCode: [{ required: true, message: '请输入站点编码', trigger: 'blur' }]
}
const siteTypeOptions = ref<Api.Dict.DictDataOption[]>([])
const statusOptions = ref<Api.Dict.DictDataOption[]>([])
const businessScopeOptions = ref<Api.Dict.DictDataOption[]>([])
/** 联系人:关联 sys_user,提交仍写 contactName / contactPhone 字符串 */
const contactUserId = ref<string | null>(null)
const contactUserOptions = ref<Api.User.UserItem[]>([])
const contactSelectedSnapshot = ref<Api.User.UserItem | null>(null)
const contactUserLoading = ref(false)
function formatContactUserLabel(u: Api.User.UserItem) {
const name = (u.nickName || '').trim() || '-'
const no = (u.userNo || '').trim()
const phone = (u.phoneNo || '').trim()
const parts = [name]
if (no) parts.push(no)
if (phone) parts.push(phone)
return parts.join(' · ')
}
async function remoteSearchContactUsers(query: string) {
contactUserLoading.value = true
try {
const q = (query || '').trim()
const res = await fetchUserList({
page: 1,
pageSize: 50,
keyword: q || undefined,
status: '1'
})
let list = [...(res.list || [])]
const curId = contactUserId.value
if (
curId &&
contactSelectedSnapshot.value?.id === curId &&
!list.some((x) => x.id === curId)
) {
list = [contactSelectedSnapshot.value, ...list]
}
contactUserOptions.value = list
} catch (e) {
console.error('检索用户失败:', e)
contactUserOptions.value = []
} finally {
contactUserLoading.value = false
}
}
function onContactUserChange(val: string | null | undefined) {
if (!val) {
contactUserId.value = null
contactSelectedSnapshot.value = null
contactUserOptions.value = []
formData.contactName = ''
formData.contactPhone = ''
return
}
const u =
contactUserOptions.value.find((x) => x.id === val) ||
(contactSelectedSnapshot.value?.id === val ? contactSelectedSnapshot.value : undefined)
if (!u) return
contactSelectedSnapshot.value = { ...u }
formData.contactName = (u.nickName || '').trim()
formData.contactPhone = (u.phoneNo || '').trim()
}
async function resolveContactUserForEdit() {
contactUserId.value = null
contactSelectedSnapshot.value = null
contactUserOptions.value = []
const name = (formData.contactName || '').trim()
const phone = (formData.contactPhone || '').trim()
if (!name && !phone) return
contactUserLoading.value = true
try {
const res = await fetchUserList({
page: 1,
pageSize: 50,
keyword: name || phone || undefined,
status: '1'
})
const list = res.list || []
const match =
list.find(
(u) =>
(u.nickName || '').trim() === name && (!phone || (u.phoneNo || '').trim() === phone)
) ||
(name ? list.find((u) => (u.nickName || '').trim() === name) : undefined) ||
(name ? list.find((u) => (u.userNo || '').trim() === name) : undefined)
if (match) {
contactUserOptions.value = list
contactUserId.value = match.id
contactSelectedSnapshot.value = { ...match }
formData.contactName = (match.nickName || '').trim()
if ((match.phoneNo || '').trim()) {
formData.contactPhone = (match.phoneNo || '').trim()
}
}
} catch (e) {
console.error('匹配联系人用户失败:', e)
} finally {
contactUserLoading.value = false
}
}
const loadDictOptions = async () => {
try {
const [zdlx, bmzt, zdywfw] = await Promise.all([
fetchDictDataOptions('zdlx'),
fetchDictDataOptions('bmzt'),
fetchDictDataOptions('zdywfw')
])
siteTypeOptions.value = zdlx || []
statusOptions.value = bmzt || []
businessScopeOptions.value = zdywfw || []
} catch (error) {
console.error('加载字典选项失败:', error)
}
}
const getSiteTypeLabel = (code: string) => {
if (!code) return '-'
const opt = siteTypeOptions.value.find((o) => o.value === code)
return opt ? opt.label : code
}
const handleSelectionChange = (rows: Api.Site.SiteItem[]) => {
selectedRows.value = rows
}
async function loadData() {
loading.value = true
try {
const res = await fetchSiteList({
page: pagination.page,
pageSize: pagination.pageSize,
keyword: searchForm.keyword || undefined
})
tableData.value = res.list || []
pagination.total = res.total ?? 0
} finally {
loading.value = false
}
}
function handleSearch() {
pagination.page = 1
loadData()
}
function handleReset() {
searchForm.keyword = ''
pagination.page = 1
loadData()
}
function resetForm() {
formData.siteName = ''
formData.siteCode = ''
formData.siteType = ''
formData.status = '1'
formData.address = ''
formData.contactName = ''
formData.contactPhone = ''
formData.businessScopeList = []
formData.orderNum = 0
formData.remark = ''
editingId.value = null
contactUserId.value = null
contactUserOptions.value = []
contactSelectedSnapshot.value = null
}
function handleDialogClose() {
resetForm()
formRef.value?.clearValidate()
}
function handleAdd() {
isEdit.value = false
dialogTitle.value = '新增站点'
resetForm()
dialogVisible.value = true
formRef.value?.clearValidate?.()
}
function handleEdit(row: Api.Site.SiteItem) {
isEdit.value = true
dialogTitle.value = '编辑站点'
editingId.value = row.id
formData.siteName = row.siteName || ''
formData.siteCode = row.siteCode || ''
formData.siteType = row.siteType || ''
formData.status = row.status || '1'
formData.address = row.address || ''
formData.contactName = row.contactName || ''
formData.contactPhone = row.contactPhone || ''
formData.businessScopeList = row.businessScope ? row.businessScope.split(',') : []
formData.orderNum = row.orderNum ?? 0
formData.remark = row.remark || ''
dialogVisible.value = true
formRef.value?.clearValidate?.()
nextTick(() => {
resolveContactUserForEdit()
})
}
async function handleSubmit() {
await formRef.value?.validate?.()
submitLoading.value = true
try {
const payload = {
siteName: formData.siteName.trim(),
siteCode: formData.siteCode.trim(),
siteType: formData.siteType || undefined,
status: formData.status,
address: formData.address?.trim() || undefined,
contactName: formData.contactName?.trim() || undefined,
contactPhone: formData.contactPhone?.trim() || undefined,
businessScope: formData.businessScopeList.length
? formData.businessScopeList.join(',')
: undefined,
orderNum: formData.orderNum,
remark: formData.remark?.trim() || undefined
}
if (isEdit.value && editingId.value) {
await fetchUpdateSite(editingId.value, payload)
} else {
await fetchCreateSite(payload)
}
ElMessage.success(isEdit.value ? '更新成功' : '创建成功')
dialogVisible.value = false
await loadData()
} catch {
/* 校验或接口错误已由全局处理 */
} finally {
submitLoading.value = false
}
}
function handleDropdownCommand(command: string, row: any) {
switch (command) {
case 'edit':
handleEdit(row)
break
case 'delete':
handleDelete(row)
break
}
}
async function handleDelete(row: Api.Site.SiteItem)
if (!canDeleteByCreator(row)) {
ElMessage.warning(CREATOR_DELETE_TIP)
return
}
{
await ElMessageBox.confirm(`确定删除站点「${row.siteName}」吗?`, '提示', { type: 'warning' })
await fetchDeleteSite(row.id)
ElMessage.success('删除成功')
await loadData()
}
async function handleBatchDelete() {
const deletable = filterDeletableByCreator(selectedRows.value)
if (!deletable.length) {
ElMessage.warning(CREATOR_DELETE_TIP)
return
}
if (deletable.length < selectedRows.value.length) {
ElMessage.warning(CREATOR_DELETE_TIP)
return
}
const ids = deletable.map((r) => r.id)
if (!ids.length) return
await ElMessageBox.confirm(`确定删除选中的 ${ids.length} 个站点吗?`, '提示', {
type: 'warning'
})
await withFullscreenLoading(async () => {
for (const id of ids) {
await fetchDeleteSite(id)
}
ElMessage.success('批量删除成功')
selectedRows.value = []
await loadData()
}, fullscreenLoadingText.batchDelete)
}
// 输入检索条件时自动搜索(防抖 300ms)
watchDebounced(
searchForm,
() => {
pagination.page = 1
loadData()
},
{ debounce: 300, deep: true }
)
onMounted(() => {
loadData()
loadDictOptions()
})
</script>
<style scoped lang="scss">
.site-dialog-form {
:deep(.el-form-item) {
margin-bottom: 0;
}
}
</style>