index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <template>
  2. <div class="p-2">
  3. <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
  4. <div class="mb-[10px]" v-show="showSearch">
  5. <el-card shadow="hover">
  6. <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
  7. <el-form-item label="部门名称" prop="deptName">
  8. <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable @keyup.enter="handleQuery" />
  9. </el-form-item>
  10. <el-form-item label="状态" prop="status">
  11. <el-select v-model="queryParams.status" placeholder="部门状态" clearable>
  12. <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
  13. </el-select>
  14. </el-form-item>
  15. <el-form-item>
  16. <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
  17. <el-button icon="Refresh" @click="resetQuery">重置</el-button>
  18. </el-form-item>
  19. </el-form>
  20. </el-card>
  21. </div>
  22. </transition>
  23. <el-card shadow="hover">
  24. <template #header>
  25. <el-row :gutter="10">
  26. <el-col :span="1.5">
  27. <el-button type="primary" plain icon="Plus" @click="handleAdd()" v-hasPermi="['system:dept:add']">新增 </el-button>
  28. </el-col>
  29. <el-col :span="1.5">
  30. <el-button type="info" plain icon="Sort" @click="handleToggleExpandAll">展开/折叠</el-button>
  31. </el-col>
  32. <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
  33. </el-row>
  34. </template>
  35. <el-table
  36. v-loading="loading"
  37. :data="deptList"
  38. row-key="deptId"
  39. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
  40. ref="deptTableRef"
  41. :default-expand-all="isExpandAll"
  42. >
  43. <el-table-column prop="deptName" label="部门名称" width="260"></el-table-column>
  44. <el-table-column prop="orderNum" align="center" label="排序" width="200"></el-table-column>
  45. <el-table-column prop="status" align="center" label="状态" width="100">
  46. <template #default="scope">
  47. <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
  48. </template>
  49. </el-table-column>
  50. <el-table-column label="创建时间" align="center" prop="createTime" width="200">
  51. <template #default="scope">
  52. <span>{{ parseTime(scope.row.createTime) }}</span>
  53. </template>
  54. </el-table-column>
  55. <el-table-column fixed="right" align="center" label="操作">
  56. <template #default="scope">
  57. <el-tooltip content="修改" placement="top">
  58. <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dept:edit']" />
  59. </el-tooltip>
  60. <el-tooltip content="新增" placement="top">
  61. <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:dept:add']" />
  62. </el-tooltip>
  63. <el-tooltip content="删除" placement="top">
  64. <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dept:remove']" />
  65. </el-tooltip>
  66. </template>
  67. </el-table-column>
  68. </el-table>
  69. </el-card>
  70. <el-dialog :title="dialog.title" v-model="dialog.visible" destroy-on-close append-to-bod width="600px">
  71. <el-form ref="deptFormRef" :model="form" :rules="rules" label-width="80px">
  72. <el-row>
  73. <el-col :span="24" v-if="form.parentId !== 0">
  74. <el-form-item label="上级部门" prop="parentId">
  75. <el-tree-select
  76. v-model="form.parentId"
  77. :data="deptOptions"
  78. :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
  79. value-key="deptId"
  80. placeholder="选择上级部门"
  81. check-strictly
  82. />
  83. </el-form-item>
  84. </el-col>
  85. <el-col :span="12">
  86. <el-form-item label="部门名称" prop="deptName">
  87. <el-input v-model="form.deptName" placeholder="请输入部门名称" />
  88. </el-form-item>
  89. </el-col>
  90. <el-col :span="12">
  91. <el-form-item label="显示排序" prop="orderNum">
  92. <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
  93. </el-form-item>
  94. </el-col>
  95. <el-col :span="12">
  96. <el-form-item label="负责人" prop="leader">
  97. <el-select v-model="form.leader" placeholder="请选择负责人">
  98. <el-option v-for="item in deptUserList" :key="item.userId" :label="item.userName" :value="item.userId" />
  99. </el-select>
  100. </el-form-item>
  101. </el-col>
  102. <el-col :span="12">
  103. <el-form-item label="联系电话" prop="phone">
  104. <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11" />
  105. </el-form-item>
  106. </el-col>
  107. <el-col :span="12">
  108. <el-form-item label="邮箱" prop="email">
  109. <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
  110. </el-form-item>
  111. </el-col>
  112. <el-col :span="12">
  113. <el-form-item label="部门状态">
  114. <el-radio-group v-model="form.status">
  115. <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label
  116. }}</el-radio>
  117. </el-radio-group>
  118. </el-form-item>
  119. </el-col>
  120. </el-row>
  121. </el-form>
  122. <template #footer>
  123. <div class="dialog-footer">
  124. <el-button type="primary" @click="submitForm">确 定</el-button>
  125. <el-button @click="cancel">取 消</el-button>
  126. </div>
  127. </template>
  128. </el-dialog>
  129. </div>
  130. </template>
  131. <script setup name="Dept" lang="ts">
  132. import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept"
  133. import { DeptForm, DeptQuery, DeptVO } from "@/api/system/dept/types";
  134. import {UserVO} from "@/api/system/user/types";
  135. import {listUserByDeptId} from "@/api/system/user";
  136. interface DeptOptionsType {
  137. deptId: number | string;
  138. deptName: string;
  139. children: DeptOptionsType[];
  140. }
  141. const { proxy } = getCurrentInstance() as ComponentInternalInstance
  142. const { sys_normal_disable } = toRefs<any>(proxy?.useDict("sys_normal_disable"));
  143. const deptList = ref<DeptVO[]>([])
  144. const loading = ref(true)
  145. const showSearch = ref(true)
  146. const deptOptions = ref<DeptOptionsType[]>([])
  147. const isExpandAll = ref(true)
  148. const deptUserList = ref<UserVO[]>([]);
  149. const dialog = reactive<DialogOption>({
  150. visible: false,
  151. title: ''
  152. });
  153. const deptTableRef = ref<ElTableInstance>();
  154. const queryFormRef = ref<ElFormInstance>();
  155. const deptFormRef = ref<ElFormInstance>();
  156. const initFormData: DeptForm = {
  157. deptId: undefined,
  158. parentId: undefined,
  159. deptName: undefined,
  160. orderNum: 0,
  161. leader: undefined,
  162. phone: undefined,
  163. email: undefined,
  164. status: "0"
  165. }
  166. const data = reactive<PageData<DeptForm, DeptQuery>>({
  167. form: { ...initFormData },
  168. queryParams: {
  169. pageNum: 1,
  170. pageSize: 10,
  171. deptName: undefined,
  172. status: undefined
  173. },
  174. rules: {
  175. parentId: [{ required: true, message: "上级部门不能为空", trigger: "blur" }],
  176. deptName: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
  177. orderNum: [{ required: true, message: "显示排序不能为空", trigger: "blur" }],
  178. email: [{ type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
  179. phone: [{ pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }]
  180. },
  181. })
  182. const { queryParams, form, rules } = toRefs<PageData<DeptForm, DeptQuery>>(data)
  183. /** 查询菜单列表 */
  184. const getList = async () => {
  185. loading.value = true;
  186. const res = await listDept(queryParams.value);
  187. const data = proxy?.handleTree<DeptVO>(res.data, "deptId")
  188. if (data) {
  189. deptList.value = data
  190. }
  191. loading.value = false
  192. }
  193. /** 查询当前部门的所有用户 */
  194. async function getDeptAllUser(deptId: any) {
  195. if (deptId !== null && deptId !== "" && deptId !== undefined) {
  196. const res = await listUserByDeptId(deptId);
  197. deptUserList.value = res.data;
  198. }
  199. }
  200. /** 取消按钮 */
  201. const cancel = () => {
  202. reset()
  203. dialog.visible = false
  204. }
  205. /** 表单重置 */
  206. const reset = () => {
  207. form.value = { ...initFormData };
  208. deptFormRef.value?.resetFields();
  209. }
  210. /** 搜索按钮操作 */
  211. const handleQuery = () => {
  212. getList();
  213. }
  214. /** 重置按钮操作 */
  215. const resetQuery = () => {
  216. queryFormRef.value?.resetFields();
  217. handleQuery()
  218. }
  219. /** 展开/折叠操作 */
  220. const handleToggleExpandAll = () => {
  221. isExpandAll.value = !isExpandAll.value;
  222. toggleExpandAll(deptList.value, isExpandAll.value)
  223. }
  224. /** 展开/折叠所有 */
  225. const toggleExpandAll = (data: DeptVO[], status: boolean) => {
  226. data.forEach((item) => {
  227. deptTableRef.value?.toggleRowExpansion(item, status)
  228. if (item.children && item.children.length > 0) toggleExpandAll(item.children, status)
  229. })
  230. }
  231. /** 新增按钮操作 */
  232. const handleAdd = async (row?: DeptVO) => {
  233. reset();
  234. const res = await listDept();
  235. const data = proxy?.handleTree<DeptOptionsType>(res.data, "deptId");
  236. if (data) {
  237. deptOptions.value = data
  238. if (row && row.deptId) {
  239. form.value.parentId = row?.deptId;
  240. }
  241. dialog.visible = true;
  242. dialog.title = "添加部门";
  243. }
  244. }
  245. /** 修改按钮操作 */
  246. const handleUpdate = async (row: DeptVO) => {
  247. reset();
  248. //查询当前部门所有用户
  249. getDeptAllUser(row.deptId);
  250. const res = await getDept(row.deptId);
  251. form.value = res.data
  252. const response = await listDeptExcludeChild(row.deptId);
  253. const data = proxy?.handleTree<DeptOptionsType>(response.data, "deptId")
  254. if (data) {
  255. deptOptions.value = data;
  256. if (data.length === 0) {
  257. const noResultsOptions: DeptOptionsType = {
  258. deptId: res.data.parentId,
  259. deptName: res.data.parentName,
  260. children: []
  261. };
  262. deptOptions.value.push(noResultsOptions);
  263. }
  264. }
  265. dialog.visible = true;
  266. dialog.title = "修改部门";
  267. }
  268. /** 提交按钮 */
  269. const submitForm = () => {
  270. deptFormRef.value?.validate(async (valid: boolean) => {
  271. if (valid) {
  272. form.value.deptId ? await updateDept(form.value) : await addDept(form.value);
  273. proxy?.$modal.msgSuccess("操作成功");
  274. dialog.visible = false;
  275. await getList();
  276. }
  277. })
  278. }
  279. /** 删除按钮操作 */
  280. const handleDelete = async (row: DeptVO) => {
  281. await proxy?.$modal.confirm('是否确认删除名称为"' + row.deptName + '"的数据项?');
  282. await delDept(row.deptId);
  283. await getList();
  284. proxy?.$modal.msgSuccess("删除成功");
  285. }
  286. onMounted(() => {
  287. getList();
  288. });
  289. </script>