feat: 新增通用组件(ProTable/StatusTag/OrgTreeSelect)与机构管理增删改查
parent
499873c157
commit
314067b91e
|
|
@ -0,0 +1,38 @@
|
|||
// 机构数据以扁平列表存储,前端/mock均通过 parentId 自行组装树
|
||||
// org001 为一级机构(内置根机构),不可删除;admin 用户归属于此
|
||||
export const orgs = [
|
||||
{ id: 'org001', name: '总行', parentId: null, level: 1, remark: '内置根机构,不可删除', builtin: true, createdAt: '2026-01-01 00:00:00' },
|
||||
{ id: 'org002', name: '华东分行', parentId: 'org001', level: 2, remark: '', builtin: false, createdAt: '2026-01-02 00:00:00' },
|
||||
{ id: 'org003', name: '华南分行', parentId: 'org001', level: 2, remark: '', builtin: false, createdAt: '2026-01-02 00:00:00' },
|
||||
{ id: 'org004', name: '上海分部', parentId: 'org002', level: 3, remark: '', builtin: false, createdAt: '2026-01-03 00:00:00' }
|
||||
]
|
||||
|
||||
let seq = orgs.length
|
||||
|
||||
export function nextOrgId() {
|
||||
seq += 1
|
||||
return `org${String(seq).padStart(3, '0')}`
|
||||
}
|
||||
|
||||
export function buildOrgTree(list) {
|
||||
const map = new Map(list.map((o) => [o.id, { ...o, children: [] }]))
|
||||
const roots = []
|
||||
map.forEach((node) => {
|
||||
if (node.parentId && map.has(node.parentId)) {
|
||||
map.get(node.parentId).children.push(node)
|
||||
} else {
|
||||
roots.push(node)
|
||||
}
|
||||
})
|
||||
return roots
|
||||
}
|
||||
|
||||
export function collectDescendantIds(list, id) {
|
||||
const result = []
|
||||
const children = list.filter((o) => o.parentId === id)
|
||||
children.forEach((c) => {
|
||||
result.push(c.id)
|
||||
result.push(...collectDescendantIds(list, c.id))
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ import express from 'express'
|
|||
import cors from 'cors'
|
||||
import cookieParser from 'cookie-parser'
|
||||
import authRoutes from './routes/auth.js'
|
||||
import systemRoutes from './routes/system.js'
|
||||
|
||||
const app = express()
|
||||
app.use(cors({ origin: 'http://localhost:5173', credentials: true }))
|
||||
|
|
@ -9,6 +10,7 @@ app.use(express.json({ limit: '10mb' }))
|
|||
app.use(cookieParser())
|
||||
|
||||
app.use('/auth', authRoutes)
|
||||
app.use('/system', systemRoutes)
|
||||
|
||||
const PORT = 8888
|
||||
app.listen(PORT, () => {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,99 @@
|
|||
import express from 'express'
|
||||
import { orgs, nextOrgId, buildOrgTree, collectDescendantIds } from '../data/orgs.js'
|
||||
import { users } from '../data/users.js'
|
||||
import { requireAuth } from '../middleware/requireAuth.js'
|
||||
|
||||
const router = express.Router()
|
||||
router.use(requireAuth)
|
||||
|
||||
// ---------------- 机构管理 ----------------
|
||||
|
||||
router.get('/org/list', (req, res) => {
|
||||
const { name, id } = req.query || {}
|
||||
let list = orgs
|
||||
if (name || id) {
|
||||
const matched = orgs.filter(
|
||||
(o) => (name && o.name.includes(name)) || (id && o.id.includes(id))
|
||||
)
|
||||
const idSet = new Set()
|
||||
matched.forEach((m) => {
|
||||
let cur = m
|
||||
while (cur) {
|
||||
idSet.add(cur.id)
|
||||
cur = orgs.find((o) => o.id === cur.parentId)
|
||||
}
|
||||
})
|
||||
list = orgs.filter((o) => idSet.has(o.id))
|
||||
}
|
||||
res.json({ code: 200, msg: 'ok', data: buildOrgTree(list) })
|
||||
})
|
||||
|
||||
router.post('/org', (req, res) => {
|
||||
const { name, parentId, remark } = req.body || {}
|
||||
if (!name) return res.json({ code: 40004, msg: '机构名称不能为空', data: null })
|
||||
if (!parentId) return res.json({ code: 40004, msg: '请选择上级机构', data: null })
|
||||
const parent = orgs.find((o) => o.id === parentId)
|
||||
if (!parent) return res.json({ code: 40004, msg: '上级机构不存在', data: null })
|
||||
const duplicate = orgs.some((o) => o.parentId === parentId && o.name === name)
|
||||
if (duplicate) return res.json({ code: 40005, msg: '同一上级机构下机构名称不可重复', data: null })
|
||||
|
||||
const org = {
|
||||
id: nextOrgId(),
|
||||
name,
|
||||
parentId,
|
||||
level: parent.level + 1,
|
||||
remark: remark || '',
|
||||
builtin: false,
|
||||
createdAt: new Date().toISOString().slice(0, 19).replace('T', ' ')
|
||||
}
|
||||
orgs.push(org)
|
||||
res.json({ code: 200, msg: 'ok', data: org })
|
||||
})
|
||||
|
||||
router.put('/org/:id', (req, res) => {
|
||||
const { id } = req.params
|
||||
const { name, parentId, remark } = req.body || {}
|
||||
const org = orgs.find((o) => o.id === id)
|
||||
if (!org) return res.json({ code: 40404, msg: '机构不存在', data: null })
|
||||
if (!name) return res.json({ code: 40004, msg: '机构名称不能为空', data: null })
|
||||
|
||||
let nextParentId = org.parentId
|
||||
let level = org.level
|
||||
if (org.parentId !== null) {
|
||||
if (!parentId) return res.json({ code: 40004, msg: '请选择上级机构', data: null })
|
||||
if (parentId === id) return res.json({ code: 40006, msg: '上级机构不可选择自身', data: null })
|
||||
const descendantIds = collectDescendantIds(orgs, id)
|
||||
if (descendantIds.includes(parentId)) {
|
||||
return res.json({ code: 40006, msg: '上级机构不可选择自身下级', data: null })
|
||||
}
|
||||
const parent = orgs.find((o) => o.id === parentId)
|
||||
if (!parent) return res.json({ code: 40004, msg: '上级机构不存在', data: null })
|
||||
nextParentId = parentId
|
||||
level = parent.level + 1
|
||||
}
|
||||
|
||||
const duplicate = orgs.some((o) => o.id !== id && o.parentId === nextParentId && o.name === name)
|
||||
if (duplicate) return res.json({ code: 40005, msg: '同一上级机构下机构名称不可重复', data: null })
|
||||
|
||||
org.name = name
|
||||
org.parentId = nextParentId
|
||||
org.level = level
|
||||
org.remark = remark || ''
|
||||
res.json({ code: 200, msg: 'ok', data: org })
|
||||
})
|
||||
|
||||
router.delete('/org/:id', (req, res) => {
|
||||
const { id } = req.params
|
||||
const index = orgs.findIndex((o) => o.id === id)
|
||||
if (index === -1) return res.json({ code: 40404, msg: '机构不存在', data: null })
|
||||
const org = orgs[index]
|
||||
if (org.builtin) return res.json({ code: 40007, msg: '内置一级机构不可删除', data: null })
|
||||
const hasChildren = orgs.some((o) => o.parentId === id)
|
||||
if (hasChildren) return res.json({ code: 40008, msg: '该机构存在下级机构,不可删除', data: null })
|
||||
const hasUsers = users.some((u) => u.orgId === id)
|
||||
if (hasUsers) return res.json({ code: 40009, msg: '该机构已关联用户,不可删除', data: null })
|
||||
orgs.splice(index, 1)
|
||||
res.json({ code: 200, msg: 'ok', data: null })
|
||||
})
|
||||
|
||||
export default router
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import request from './request'
|
||||
|
||||
// ---------------- 机构管理 ----------------
|
||||
export const fetchOrgListApi = (params) => request.get('/system/org/list', { params })
|
||||
export const createOrgApi = (data) => request.post('/system/org', data)
|
||||
export const updateOrgApi = (id, data) => request.put(`/system/org/${id}`, data)
|
||||
export const deleteOrgApi = (id) => request.delete(`/system/org/${id}`)
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
<template>
|
||||
<a-tree-select
|
||||
:value="modelValue"
|
||||
:tree-data="treeData"
|
||||
:field-names="{ label: 'title', value: 'value', children: 'children' }"
|
||||
show-search
|
||||
tree-node-filter-prop="title"
|
||||
placeholder="请选择上级机构"
|
||||
allow-clear
|
||||
:loading="loading"
|
||||
@update:value="$emit('update:modelValue', $event)"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted, watch } from 'vue'
|
||||
import { fetchOrgListApi } from '@/api/system'
|
||||
|
||||
const props = defineProps({
|
||||
modelValue: { type: String, default: undefined },
|
||||
excludeId: { type: String, default: '' }
|
||||
})
|
||||
defineEmits(['update:modelValue'])
|
||||
|
||||
const treeData = ref([])
|
||||
const loading = ref(false)
|
||||
|
||||
function toTreeData(nodes, excludeId) {
|
||||
return nodes
|
||||
.filter((node) => node.id !== excludeId)
|
||||
.map((node) => ({
|
||||
title: node.name,
|
||||
value: node.id,
|
||||
children: node.children?.length ? toTreeData(node.children, excludeId) : []
|
||||
}))
|
||||
}
|
||||
|
||||
async function loadOrgTree() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await fetchOrgListApi()
|
||||
if (res.data.code === 200) {
|
||||
treeData.value = toTreeData(res.data.data, props.excludeId)
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(loadOrgTree)
|
||||
watch(() => props.excludeId, loadOrgTree)
|
||||
|
||||
defineExpose({ reload: loadOrgTree })
|
||||
</script>
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
<template>
|
||||
<div class="pro-table">
|
||||
<a-form layout="inline" class="search-form">
|
||||
<slot name="search" :form="searchForm" />
|
||||
<a-form-item>
|
||||
<a-space>
|
||||
<a-button type="primary" :loading="loading" @click="handleSearch">查询</a-button>
|
||||
<a-button @click="handleReset">重置</a-button>
|
||||
</a-space>
|
||||
</a-form-item>
|
||||
</a-form>
|
||||
|
||||
<div class="toolbar">
|
||||
<slot name="actions" />
|
||||
</div>
|
||||
|
||||
<a-table
|
||||
:columns="columns"
|
||||
:data-source="list"
|
||||
:loading="loading"
|
||||
:row-key="rowKey"
|
||||
:pagination="pagination"
|
||||
@change="handleTableChange"
|
||||
>
|
||||
<template v-for="name in forwardSlotNames" #[name]="slotProps" :key="name">
|
||||
<slot :name="name" v-bind="slotProps" />
|
||||
</template>
|
||||
</a-table>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { reactive, ref, computed, onMounted, useSlots } from 'vue'
|
||||
|
||||
const props = defineProps({
|
||||
columns: { type: Array, required: true },
|
||||
fetchData: { type: Function, required: true },
|
||||
rowKey: { type: String, default: 'id' },
|
||||
initialSearch: { type: Object, default: () => ({}) },
|
||||
pageSize: { type: Number, default: 10 }
|
||||
})
|
||||
|
||||
const slots = useSlots()
|
||||
const forwardSlotNames = computed(() =>
|
||||
Object.keys(slots).filter((name) => name !== 'search' && name !== 'actions')
|
||||
)
|
||||
|
||||
const searchForm = reactive({ ...props.initialSearch })
|
||||
const list = ref([])
|
||||
const loading = ref(false)
|
||||
const pagination = reactive({
|
||||
current: 1,
|
||||
pageSize: props.pageSize,
|
||||
total: 0,
|
||||
showSizeChanger: true,
|
||||
showTotal: (total) => `共 ${total} 条`
|
||||
})
|
||||
|
||||
async function loadData() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await props.fetchData({
|
||||
...searchForm,
|
||||
page: pagination.current,
|
||||
pageSize: pagination.pageSize
|
||||
})
|
||||
list.value = res?.list || []
|
||||
pagination.total = res?.total || 0
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function handleSearch() {
|
||||
pagination.current = 1
|
||||
loadData()
|
||||
}
|
||||
|
||||
function handleReset() {
|
||||
Object.keys(searchForm).forEach((key) => {
|
||||
searchForm[key] = props.initialSearch[key] ?? undefined
|
||||
})
|
||||
pagination.current = 1
|
||||
loadData()
|
||||
}
|
||||
|
||||
function handleTableChange(pag) {
|
||||
pagination.current = pag.current
|
||||
pagination.pageSize = pag.pageSize
|
||||
loadData()
|
||||
}
|
||||
|
||||
onMounted(loadData)
|
||||
|
||||
defineExpose({ reload: loadData, searchForm })
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.search-form {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.toolbar {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
</style>
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
<template>
|
||||
<a-tag :color="meta.color">{{ meta.text }}</a-tag>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
|
||||
const props = defineProps({
|
||||
value: { type: [String, Number], default: '' },
|
||||
map: { type: Object, default: () => ({}) }
|
||||
})
|
||||
|
||||
const DEFAULT_MAP = {
|
||||
enabled: { text: '正常', color: 'green' },
|
||||
locked: { text: '已锁定', color: 'red' },
|
||||
disabled: { text: '已停用', color: 'default' }
|
||||
}
|
||||
|
||||
const meta = computed(() => {
|
||||
const merged = { ...DEFAULT_MAP, ...props.map }
|
||||
return merged[props.value] || { text: props.value, color: 'default' }
|
||||
})
|
||||
</script>
|
||||
|
|
@ -0,0 +1,189 @@
|
|||
<template>
|
||||
<div class="org-list-page">
|
||||
<a-form layout="inline" class="search-form">
|
||||
<a-form-item label="机构名称">
|
||||
<a-input v-model:value="searchForm.name" placeholder="请输入机构名称" allow-clear />
|
||||
</a-form-item>
|
||||
<a-form-item label="机构ID">
|
||||
<a-input v-model:value="searchForm.id" placeholder="请输入机构ID" allow-clear />
|
||||
</a-form-item>
|
||||
<a-form-item>
|
||||
<a-space>
|
||||
<a-button type="primary" :loading="loading" @click="handleSearch">查询</a-button>
|
||||
<a-button @click="handleReset">重置</a-button>
|
||||
</a-space>
|
||||
</a-form-item>
|
||||
</a-form>
|
||||
|
||||
<div class="toolbar">
|
||||
<a-button v-permission="'org:add'" type="primary" @click="openCreateModal(null)">
|
||||
<PlusOutlined /> 新增机构
|
||||
</a-button>
|
||||
</div>
|
||||
|
||||
<a-table
|
||||
:columns="columns"
|
||||
:data-source="tableData"
|
||||
:loading="loading"
|
||||
row-key="id"
|
||||
:pagination="false"
|
||||
default-expand-all-rows
|
||||
>
|
||||
<template #bodyCell="{ column, record }">
|
||||
<template v-if="column.dataIndex === 'action'">
|
||||
<a-space>
|
||||
<a v-permission="'org:edit'" @click="openEditModal(record)">修改</a>
|
||||
<a-popconfirm title="确定删除该机构吗?" @confirm="handleDelete(record)">
|
||||
<a v-permission="'org:delete'" class="danger-link">删除</a>
|
||||
</a-popconfirm>
|
||||
</a-space>
|
||||
</template>
|
||||
</template>
|
||||
</a-table>
|
||||
|
||||
<a-modal
|
||||
v-model:open="modalOpen"
|
||||
:title="modalMode === 'create' ? '新增机构' : '修改机构'"
|
||||
:confirm-loading="submitting"
|
||||
@ok="handleSubmit"
|
||||
>
|
||||
<a-form ref="formRef" layout="vertical" :model="form" :rules="rules">
|
||||
<a-form-item label="机构名称" name="name">
|
||||
<a-input v-model:value="form.name" placeholder="请输入机构名称" />
|
||||
</a-form-item>
|
||||
<a-form-item v-if="!isRootRecord" label="上级机构" name="parentId">
|
||||
<OrgTreeSelect v-model="form.parentId" :exclude-id="form.id" />
|
||||
</a-form-item>
|
||||
<a-form-item label="备注" name="remark">
|
||||
<a-textarea v-model:value="form.remark" placeholder="请输入备注" :rows="3" />
|
||||
</a-form-item>
|
||||
</a-form>
|
||||
</a-modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, reactive, computed, onMounted } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||||
import OrgTreeSelect from '@/components/OrgTreeSelect.vue'
|
||||
import { fetchOrgListApi, createOrgApi, updateOrgApi, deleteOrgApi } from '@/api/system'
|
||||
|
||||
const columns = [
|
||||
{ title: '机构ID', dataIndex: 'id', width: 140 },
|
||||
{ title: '机构名称', dataIndex: 'name' },
|
||||
{ title: '机构级别', dataIndex: 'level', width: 100 },
|
||||
{ title: '上级机构', dataIndex: 'parentName', width: 160 },
|
||||
{ title: '备注', dataIndex: 'remark' },
|
||||
{ title: '操作', dataIndex: 'action', width: 140 }
|
||||
]
|
||||
|
||||
const searchForm = reactive({ name: '', id: '' })
|
||||
const loading = ref(false)
|
||||
const orgTree = ref([])
|
||||
|
||||
function annotateParentName(nodes, parentName) {
|
||||
return nodes.map((node) => ({
|
||||
...node,
|
||||
parentName: parentName || '—',
|
||||
children: node.children?.length ? annotateParentName(node.children, node.name) : []
|
||||
}))
|
||||
}
|
||||
|
||||
const tableData = computed(() => annotateParentName(orgTree.value, ''))
|
||||
|
||||
async function loadData() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await fetchOrgListApi({ name: searchForm.name || undefined, id: searchForm.id || undefined })
|
||||
if (res.data.code === 200) orgTree.value = res.data.data
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function handleSearch() {
|
||||
loadData()
|
||||
}
|
||||
function handleReset() {
|
||||
searchForm.name = ''
|
||||
searchForm.id = ''
|
||||
loadData()
|
||||
}
|
||||
|
||||
onMounted(loadData)
|
||||
|
||||
// ---------------- 新增/修改弹窗 ----------------
|
||||
const modalOpen = ref(false)
|
||||
const modalMode = ref('create')
|
||||
const submitting = ref(false)
|
||||
const formRef = ref(null)
|
||||
const form = reactive({ id: '', name: '', parentId: undefined, remark: '' })
|
||||
const isRootRecord = computed(() => modalMode.value === 'edit' && form.id === 'org001')
|
||||
|
||||
const rules = {
|
||||
name: [{ required: true, message: '请输入机构名称' }],
|
||||
parentId: [{ required: true, message: '请选择上级机构' }]
|
||||
}
|
||||
|
||||
function openCreateModal(parentRecord) {
|
||||
modalMode.value = 'create'
|
||||
form.id = ''
|
||||
form.name = ''
|
||||
form.parentId = parentRecord?.id
|
||||
form.remark = ''
|
||||
modalOpen.value = true
|
||||
}
|
||||
|
||||
function openEditModal(record) {
|
||||
modalMode.value = 'edit'
|
||||
form.id = record.id
|
||||
form.name = record.name
|
||||
form.parentId = record.parentId || undefined
|
||||
form.remark = record.remark
|
||||
modalOpen.value = true
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value.validate()
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
submitting.value = true
|
||||
try {
|
||||
const payload = { name: form.name, parentId: form.parentId, remark: form.remark }
|
||||
const res =
|
||||
modalMode.value === 'create'
|
||||
? await createOrgApi(payload)
|
||||
: await updateOrgApi(form.id, payload)
|
||||
if (res.data.code === 200) {
|
||||
message.success(modalMode.value === 'create' ? '新增成功' : '修改成功')
|
||||
modalOpen.value = false
|
||||
loadData()
|
||||
}
|
||||
} finally {
|
||||
submitting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDelete(record) {
|
||||
const res = await deleteOrgApi(record.id)
|
||||
if (res.data.code === 200) {
|
||||
message.success('删除成功')
|
||||
loadData()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.search-form {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.toolbar {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.danger-link {
|
||||
color: #ff4d4f;
|
||||
}
|
||||
</style>
|
||||
Loading…
Reference in New Issue