234 lines
7.2 KiB
Vue
234 lines
7.2 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* F006 部门管理 - W4.14
|
|
*
|
|
* W11 A8: 套 PageHeader/SearchBar 标准化模板
|
|
*
|
|
* 后端: GET /sys/dept/list (返树结构)
|
|
* 字段: id/pid/name/sort/createDate/parentName
|
|
*
|
|
* 设计规范:v1.6
|
|
*/
|
|
import { onMounted, ref, reactive } from 'vue'
|
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
|
import { httpGet, httpPost, httpPut, httpDelete } from '@/utils/request'
|
|
import DataTable from '@/components/DataTable/DataTable.vue'
|
|
import PageHeader from '@/components/PageHeader/PageHeader.vue'
|
|
import SearchBar from '@/components/SearchBar/SearchBar.vue'
|
|
|
|
interface Dept {
|
|
id: string
|
|
pid: string
|
|
name: string
|
|
sort: number
|
|
createDate: string
|
|
parentName?: string
|
|
children?: Dept[]
|
|
raw: any
|
|
}
|
|
|
|
const list = ref<Dept[]>([])
|
|
const loading = ref(false)
|
|
const error = ref<string | null>(null)
|
|
|
|
const editVisible = ref(false)
|
|
const editMode = ref<'create' | 'update'>('create')
|
|
const editForm = reactive({
|
|
id: null as number | null,
|
|
pid: '0',
|
|
name: '',
|
|
sort: 0
|
|
})
|
|
|
|
const parentOptions = ref<Array<{ value: string; label: string }>>([])
|
|
|
|
function fmtDate(ts?: string | number | null) {
|
|
if (!ts) return '--'
|
|
const n = typeof ts === 'string' ? Date.parse(ts) : ts
|
|
if (!n || isNaN(n)) return String(ts)
|
|
const d = new Date(n)
|
|
if (isNaN(d.getTime())) return String(ts)
|
|
const pad = (x: number) => String(x).padStart(2, '0')
|
|
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
|
|
}
|
|
|
|
function flattenTree(depts: Dept[], level = 0): Dept[] {
|
|
const out: Dept[] = []
|
|
for (const d of depts) {
|
|
out.push({ ...d, raw: d })
|
|
if (d.children?.length) {
|
|
out.push(...flattenTree(d.children, level + 1))
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
async function load() {
|
|
loading.value = true
|
|
error.value = null
|
|
try {
|
|
const data = await httpGet<any>('/sys/dept/list')
|
|
const tree = Array.isArray(data) ? data : []
|
|
list.value = flattenTree(tree)
|
|
parentOptions.value = [
|
|
{ value: '0', label: '顶级部门' },
|
|
...list.value.map(d => ({ value: d.id, label: d.name }))
|
|
]
|
|
} catch (e: any) {
|
|
error.value = e?.msg || e?.message || '后端接口异常'
|
|
list.value = []
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
function createHandle() {
|
|
editMode.value = 'create'
|
|
editForm.id = null
|
|
editForm.pid = '0'
|
|
editForm.name = ''
|
|
editForm.sort = 0
|
|
editVisible.value = true
|
|
}
|
|
|
|
async function editHandle(row: Dept) {
|
|
editMode.value = 'update'
|
|
editForm.id = Number(row.id)
|
|
editForm.pid = row.pid
|
|
editForm.name = row.name
|
|
editForm.sort = row.sort
|
|
editVisible.value = true
|
|
}
|
|
|
|
async function editConfirm() {
|
|
if (!editForm.name.trim()) {
|
|
ElMessage.warning('请输入部门名称')
|
|
return
|
|
}
|
|
const payload = {
|
|
id: editForm.id,
|
|
pid: editForm.pid,
|
|
name: editForm.name.trim(),
|
|
sort: editForm.sort
|
|
}
|
|
try {
|
|
if (editMode.value === 'create') {
|
|
await httpPost('/sys/dept', payload)
|
|
ElMessage.success('新增成功')
|
|
} else {
|
|
await httpPut('/sys/dept', payload)
|
|
ElMessage.success('修改成功')
|
|
}
|
|
editVisible.value = false
|
|
load()
|
|
} catch (e: any) {
|
|
ElMessage.error(e?.msg || e?.message || '操作失败')
|
|
}
|
|
}
|
|
|
|
async function deleteHandle(row: Dept) {
|
|
try {
|
|
await ElMessageBox.confirm(
|
|
`确认删除部门 ${row.name} 吗?`,
|
|
'删除确认',
|
|
{ type: 'warning', confirmButtonText: '删除', cancelButtonText: '取消' }
|
|
)
|
|
} catch { return }
|
|
try {
|
|
await httpDelete(`/sys/dept/${row.id}`)
|
|
ElMessage.success('删除成功')
|
|
load()
|
|
} catch (e: any) {
|
|
ElMessage.error(e?.msg || e?.message || '删除失败')
|
|
}
|
|
}
|
|
|
|
onMounted(load)
|
|
</script>
|
|
|
|
<template>
|
|
<div class="rr-sys-dept">
|
|
<PageHeader title="部门管理" :sub="``">
|
|
<el-button type="primary" class="rr-btn-primary" @click="createHandle">+ 新增部门</el-button>
|
|
</PageHeader>
|
|
|
|
<div class="rr-list-card">
|
|
<DataTable
|
|
:columns="[
|
|
{ prop: 'name', label: '部门名称', minWidth: 240 },
|
|
{ prop: 'parentName', label: '上级部门', minWidth: 200 },
|
|
{ prop: 'sort', label: '排序', width: 100 },
|
|
{ prop: 'createDate', label: '创建时间', width: 170, slot: 'createDate' },
|
|
{ label: '操作', width: 180, slot: 'action' }
|
|
]"
|
|
:data="list"
|
|
:loading="loading"
|
|
:total="list.length"
|
|
:page="1"
|
|
:limit="999"
|
|
:show-pagination="false"
|
|
:page-sizes="[10, 20, 50, 100]"
|
|
:error="error"
|
|
empty-text="暂无部门"
|
|
borderless
|
|
row-key="id"
|
|
>
|
|
<template #createDate="{ row }">
|
|
<span class="rr-time">{{ fmtDate(row.createDate) }}</span>
|
|
</template>
|
|
<template #action="{ row }">
|
|
<el-button text type="primary" size="small" @click="editHandle(row)">编辑</el-button>
|
|
<el-button text type="primary" size="small" @click="deleteHandle(row)">删除</el-button>
|
|
</template>
|
|
</DataTable>
|
|
</div>
|
|
|
|
<el-dialog
|
|
v-model="editVisible"
|
|
:title="editMode === 'create' ? '新增部门' : '编辑部门'"
|
|
width="540"
|
|
:style="{ borderRadius: '12px', overflow: 'hidden' }"
|
|
align-center
|
|
destroy-on-close
|
|
>
|
|
<el-form label-width="100px" label-position="right">
|
|
<el-form-item label="部门名称" required>
|
|
<el-input v-model="editForm.name" placeholder="请输入部门名称" size="default" maxlength="50" />
|
|
</el-form-item>
|
|
<el-form-item label="上级部门">
|
|
<el-select v-model="editForm.pid" placeholder="选择上级部门" style="width: 100%" size="default">
|
|
<el-option
|
|
v-for="opt in parentOptions"
|
|
:key="opt.value"
|
|
:label="opt.label"
|
|
:value="opt.value"
|
|
/>
|
|
</el-select>
|
|
</el-form-item>
|
|
<el-form-item label="排序">
|
|
<el-input-number v-model="editForm.sort" :min="0" controls-position="right" size="default" style="width: 100%" />
|
|
</el-form-item>
|
|
</el-form>
|
|
<template #footer>
|
|
<el-button @click="editVisible = false">取消</el-button>
|
|
<el-button type="primary" @click="editConfirm">
|
|
{{ editMode === 'create' ? '确认新增' : '确认修改' }}
|
|
</el-button>
|
|
</template>
|
|
</el-dialog>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.rr-sys-dept { padding: 16px; }
|
|
.rr-list-card { background: var(--surface); border-radius: 12px; padding: 4px; }
|
|
.rr-time { font-family: 'SF Mono', Menlo, Monaco, Consolas, monospace; font-size: 12px; color: var(--ink-2); }
|
|
.rr-muted { color: var(--ink-3); font-size: 12px; }
|
|
.rr-page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
|
|
.rr-page-titles { display: flex; flex-direction: column; gap: 2px; }
|
|
.rr-page-title { margin: 0; font-size: 20px; font-weight: 700; color: var(--el-text-color-primary); }
|
|
.rr-page-sub { margin: 0; font-size: 12px; color: var(--el-text-color-secondary); }
|
|
.rr-page-actions { display: flex; gap: 8px; }
|
|
.rr-list-card { background: var(--el-bg-color); border-radius: 12px; padding: 4px; }
|
|
.rr-time { font-family: 'SF Mono', Menlo, Monaco, Consolas, monospace; font-size: 12px; color: var(--el-text-color-regular); }
|
|
</style> |