longz
2026-04-28 bfa7d3b23e526809b04299d9c2719c3449aba3be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<template>
  <div>
    <div class="toolbar">
      <el-button v-permission="'sys:depts:create'" type="primary" @click="openAddRoot">新增一级部门</el-button>
    </div>
 
    <el-table
      :data="treeData"
      row-key="id"
      :tree-props="{ children: 'children' }"
      default-expand-all
      v-loading="loading"
      border
      stripe
    >
      <el-table-column prop="deptName" label="部门名称" min-width="280">
        <template #default="{ row }">
          <span>{{ row.deptName }}</span>
          <el-tag v-if="row.status === 0" type="info" size="small" class="st-tag">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="260" fixed="right">
        <template #default="{ row }">
          <div class="table-ops">
            <el-button
              v-permission="'sys:depts:create'"
              link
              type="primary"
              size="small"
              :disabled="row.level >= 5"
              @click="openAddChild(row)"
            >
              添加下级
            </el-button>
            <el-button v-permission="'sys:depts:update'" link type="primary" size="small" @click="openEdit(row)">编辑</el-button>
            <el-button v-permission="'sys:depts:delete'" link type="danger" size="small" @click="confirmDelete(row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
 
    <el-dialog v-model="dlg" :title="dlgTitle" width="480px" destroy-on-close @closed="resetDlg">
      <el-form label-width="100px">
        <el-form-item v-if="showParentReadonly" label="上级部门">
          <el-input :model-value="parentDeptName" disabled />
        </el-form-item>
        <el-form-item v-if="showParentSelect" label="上级部门" required>
          <el-tree-select
            v-model="form.parentId"
            :data="parentTreeSelectData"
            check-strictly
            :render-after-expand="false"
            placeholder="请选择上级部门"
            clearable
            class="tree-sel"
          />
        </el-form-item>
        <el-form-item label="部门名称" required>
          <el-input v-model="form.deptName" maxlength="128" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item label="部门状态" required>
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dlg = false">取消</el-button>
        <el-button
          v-permission="mode === 'edit' ? 'sys:depts:update' : 'sys:depts:create'"
          type="primary"
          :loading="saving"
          @click="submitDlg"
        >
          保存
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup lang="ts">
import { computed, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import http from "../api/http";
 
type DeptNode = {
  id: number;
  uuid: string;
  parentId?: number;
  deptName: string;
  sortOrder?: number;
  status: number;
  level: number;
  children?: DeptNode[];
};
 
type TreeSelNode = {
  value: number;
  label: string;
  disabled?: boolean;
  children?: TreeSelNode[];
};
 
const loading = ref(false);
const saving = ref(false);
const treeData = ref<DeptNode[]>([]);
const dlg = ref(false);
type DlgMode = "root" | "child" | "edit";
const mode = ref<DlgMode>("root");
const editingId = ref<number | null>(null);
const editingIsRoot = ref(false);
const parentDeptName = ref("");
const childParentId = ref<number | null>(null);
 
const form = ref({
  uuid: "",
  deptName: "",
  status: 1,
  parentId: undefined as number | undefined,
});
 
const dlgTitle = computed(() => {
  if (mode.value === "root") return "新增一级部门";
  if (mode.value === "child") return "添加下级部门";
  return "编辑部门";
});
 
const showParentReadonly = computed(() => mode.value === "child");
const showParentSelect = computed(() => mode.value === "edit" && !editingIsRoot.value);
 
const parentTreeSelectData = ref<TreeSelNode[]>([]);
 
function collectDescendantIds(n: DeptNode): number[] {
  const r: number[] = [n.id];
  for (const c of n.children || []) {
    r.push(...collectDescendantIds(c));
  }
  return r;
}
 
function toTreeSelectData(nodes: DeptNode[], disabledIds: Set<number>): TreeSelNode[] {
  return nodes.map((n) => ({
    value: n.id,
    label: n.deptName + (n.status === 0 ? "(禁用)" : ""),
    disabled: disabledIds.has(n.id),
    children: n.children?.length ? toTreeSelectData(n.children, disabledIds) : undefined,
  }));
}
 
async function loadTree() {
  loading.value = true;
  try {
    const { data } = await http.get("/sys/dept-manage/tree");
    treeData.value = (data.result || []) as DeptNode[];
  } catch (e: unknown) {
    ElMessage.error(e instanceof Error ? e.message : "加载失败");
  } finally {
    loading.value = false;
  }
}
 
function openAddRoot() {
  mode.value = "root";
  editingId.value = null;
  editingIsRoot.value = true;
  parentDeptName.value = "";
  childParentId.value = null;
  form.value = { uuid: "", deptName: "", status: 1, parentId: undefined };
  dlg.value = true;
}
 
function openAddChild(row: DeptNode) {
  if (row.level >= 5) return;
  mode.value = "child";
  editingId.value = null;
  editingIsRoot.value = false;
  parentDeptName.value = row.deptName;
  childParentId.value = row.id;
  form.value = { uuid: "", deptName: "", status: 1, parentId: row.id };
  dlg.value = true;
}
 
function openEdit(row: DeptNode) {
  mode.value = "edit";
  editingId.value = row.id;
  const root = !row.parentId || row.parentId === 0;
  editingIsRoot.value = root;
  parentDeptName.value = "";
  childParentId.value = null;
  form.value = {
    uuid: row.uuid,
    deptName: row.deptName,
    status: row.status,
    parentId: root ? undefined : row.parentId,
  };
  if (!root) {
    const disabled = new Set(collectDescendantIds(row));
    parentTreeSelectData.value = toTreeSelectData(treeData.value, disabled);
  } else {
    parentTreeSelectData.value = [];
  }
  dlg.value = true;
}
 
function resetDlg() {
  editingId.value = null;
  parentTreeSelectData.value = [];
}
 
async function submitDlg() {
  if (!form.value.deptName.trim()) {
    ElMessage.warning("请填写部门名称");
    return;
  }
  saving.value = true;
  try {
    if (mode.value === "root") {
      await http.post("/sys/dept-manage", {
        parentId: 0,
        deptName: form.value.deptName.trim(),
        status: form.value.status,
      });
      ElMessage.success("已创建");
    } else if (mode.value === "child") {
      if (childParentId.value == null) {
        ElMessage.warning("缺少上级部门");
        return;
      }
      await http.post("/sys/dept-manage", {
        parentId: childParentId.value,
        deptName: form.value.deptName.trim(),
        status: form.value.status,
      });
      ElMessage.success("已创建");
    } else if (editingId.value != null) {
      const body: Record<string, unknown> = {
        uuid: form.value.uuid,
        deptName: form.value.deptName.trim(),
        status: form.value.status,
      };
      if (!editingIsRoot.value) {
        if (form.value.parentId == null) {
          ElMessage.warning("请选择上级部门");
          return;
        }
        body.parentId = form.value.parentId;
      }
      await http.put(`/sys/dept-manage/${editingId.value}`, body);
      ElMessage.success("已保存");
    }
    dlg.value = false;
    loadTree();
  } catch (e: unknown) {
    ElMessage.error(e instanceof Error ? e.message : "保存失败");
  } finally {
    saving.value = false;
  }
}
 
function confirmDelete(row: DeptNode) {
  ElMessageBox.confirm("删除为逻辑删除,是否继续?", "确认删除", {
    type: "warning",
    confirmButtonText: "删除",
    cancelButtonText: "取消",
  })
    .then(async () => {
      try {
        await http.delete(`/sys/dept-manage/${row.id}`, { params: { uuid: row.uuid } });
        ElMessage.success("已删除");
        loadTree();
      } catch (e: unknown) {
        ElMessage.error(e instanceof Error ? e.message : "删除失败");
      }
    })
    .catch(() => {});
}
 
loadTree();
</script>
 
<style scoped>
.toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 12px;
}
.st-tag {
  margin-left: 8px;
  vertical-align: middle;
}
.tree-sel {
  width: 100%;
}
</style>