index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
  4. <el-form-item label="部门名称" prop="name">
  5. <el-input v-model="queryParams.name" placeholder="请输入部门名称" clearable @keyup.enter.native="handleQuery"/>
  6. </el-form-item>
  7. <el-form-item label="状态" prop="status">
  8. <el-select v-model="queryParams.status" placeholder="菜单状态" clearable>
  9. <el-option v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="dict.label" :value="parseInt(dict.value)"/>
  10. </el-select>
  11. </el-form-item>
  12. <el-form-item>
  13. <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
  14. <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
  15. </el-form-item>
  16. </el-form>
  17. <el-row :gutter="10" class="mb8">
  18. <el-col :span="1.5">
  19. <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
  20. v-hasPermi="['system:dept:create']">新增</el-button>
  21. </el-col>
  22. <el-col :span="1.5">
  23. <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
  24. </el-col>
  25. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  26. </el-row>
  27. <el-table v-if="refreshTable" v-loading="loading" :data="deptList" row-key="id" :default-expand-all="isExpandAll"
  28. :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
  29. <el-table-column prop="name" label="部门名称" width="260"></el-table-column>
  30. <el-table-column prop="leader" label="负责人" :formatter="userNicknameFormat" width="120"/>
  31. <el-table-column prop="sort" label="排序" width="200"></el-table-column>
  32. <el-table-column prop="status" label="状态" width="100">
  33. <template v-slot="scope">
  34. <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status"/>
  35. </template>
  36. </el-table-column>
  37. <el-table-column label="创建时间" align="center" prop="createTime" width="200">
  38. <template v-slot="scope">
  39. <span>{{ parseTime(scope.row.createTime) }}</span>
  40. </template>
  41. </el-table-column>
  42. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  43. <template v-slot="scope">
  44. <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
  45. v-hasPermi="['system:dept:update']">修改</el-button>
  46. <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
  47. v-hasPermi="['system:dept:create']">新增</el-button>
  48. <el-button v-if="scope.row.parentId !== 0" size="mini" type="text" icon="el-icon-delete"
  49. @click="handleDelete(scope.row)" v-hasPermi="['system:dept:delete']">删除</el-button>
  50. </template>
  51. </el-table-column>
  52. </el-table>
  53. <!-- 添加或修改部门对话框 -->
  54. <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
  55. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  56. <el-row>
  57. <el-col :span="24">
  58. <el-form-item label="上级部门" prop="parentId">
  59. <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级部门" />
  60. </el-form-item>
  61. </el-col>
  62. <el-col :span="12">
  63. <el-form-item label="部门名称" prop="name">
  64. <el-input v-model="form.name" placeholder="请输入部门名称" />
  65. </el-form-item>
  66. </el-col>
  67. <el-col :span="12">
  68. <el-form-item label="显示排序" prop="sort">
  69. <el-input-number v-model="form.sort" controls-position="right" :min="0" />
  70. </el-form-item>
  71. </el-col>
  72. <el-col :span="12">
  73. <el-form-item label="负责人" prop="leaderUserId">
  74. <el-select v-model="form.leaderUserId" placeholder="请输入负责人" clearable style="width: 100%">
  75. <el-option v-for="item in users" :key="parseInt(item.id)" :label="item.nickname" :value="parseInt(item.id)" />
  76. </el-select>
  77. </el-form-item>
  78. </el-col>
  79. <el-col :span="12">
  80. <el-form-item label="联系电话" prop="phone">
  81. <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11" />
  82. </el-form-item>
  83. </el-col>
  84. <el-col :span="12">
  85. <el-form-item label="邮箱" prop="email">
  86. <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
  87. </el-form-item>
  88. </el-col>
  89. <el-col :span="12">
  90. <el-form-item label="部门状态" prop="status">
  91. <el-radio-group v-model="form.status">
  92. <el-radio v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="parseInt(dict.value)">
  93. {{dict.label}}</el-radio>
  94. </el-radio-group>
  95. </el-form-item>
  96. </el-col>
  97. </el-row>
  98. </el-form>
  99. <div slot="footer" class="dialog-footer">
  100. <el-button type="primary" @click="submitForm">确 定</el-button>
  101. <el-button @click="cancel">取 消</el-button>
  102. </div>
  103. </el-dialog>
  104. </div>
  105. </template>
  106. <script>
  107. import { listDept, getDept, delDept, addDept, updateDept } from "@/api/system/dept";
  108. import Treeselect from "@riophae/vue-treeselect";
  109. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  110. import {CommonStatusEnum} from '@/utils/constants'
  111. import { getDictDatas, DICT_TYPE } from '@/utils/dict'
  112. import {listSimpleUsers} from "@/api/system/user";
  113. export default {
  114. name: "Dept",
  115. components: { Treeselect },
  116. data() {
  117. return {
  118. // 遮罩层
  119. loading: true,
  120. // 显示搜索条件
  121. showSearch: true,
  122. // 表格树数据
  123. deptList: [],
  124. // 部门树选项
  125. deptOptions: [],
  126. // 用户下拉列表
  127. users: [],
  128. // 弹出层标题
  129. title: "",
  130. // 是否显示弹出层
  131. open: false,
  132. // 是否展开,默认全部展开
  133. isExpandAll: true,
  134. // 重新渲染表格状态
  135. refreshTable: true,
  136. // 是否展开
  137. expand: false,
  138. // 状态数据字典
  139. statusOptions: [],
  140. // 查询参数
  141. queryParams: {
  142. name: undefined,
  143. status: undefined
  144. },
  145. // 表单参数
  146. form: {},
  147. // 表单校验
  148. rules: {
  149. name: [
  150. { required: true, message: "部门名称不能为空", trigger: "blur" }
  151. ],
  152. sort: [
  153. { required: true, message: "显示排序不能为空", trigger: "blur" }
  154. ],
  155. email: [
  156. {
  157. type: "email",
  158. message: "'请输入正确的邮箱地址",
  159. trigger: ["blur", "change"]
  160. }
  161. ],
  162. phone: [
  163. {
  164. pattern: /^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/,
  165. message: "请输入正确的手机号码",
  166. trigger: "blur"
  167. }
  168. ],
  169. status: [
  170. { required: true, message: "状态不能为空", trigger: "blur" }
  171. ]
  172. },
  173. // 枚举
  174. CommonStatusEnum: CommonStatusEnum,
  175. // 数据字典
  176. statusDictDatas: getDictDatas(DICT_TYPE.COMMON_STATUS)
  177. };
  178. },
  179. created() {
  180. this.getList();
  181. // 获得用户列表
  182. listSimpleUsers().then(response => {
  183. this.users = response.data;
  184. });
  185. },
  186. methods: {
  187. /** 查询部门列表 */
  188. getList() {
  189. this.loading = true;
  190. listDept(this.queryParams).then(response => {
  191. this.deptList = this.handleTree(response.data, "id");
  192. this.loading = false;
  193. });
  194. },
  195. /** 转换部门数据结构 */
  196. normalizer(node) {
  197. if (node.children && !node.children.length) {
  198. delete node.children;
  199. }
  200. return {
  201. id: node.id,
  202. label: node.name,
  203. children: node.children
  204. };
  205. },
  206. // 用户昵称展示
  207. userNicknameFormat(row, column) {
  208. if (!row.leaderUserId) {
  209. return '未设置';
  210. }
  211. for (const user of this.users) {
  212. if (row.leaderUserId === user.id) {
  213. return user.nickname;
  214. }
  215. }
  216. return '未知【' + row.leaderUserId + '】';
  217. },
  218. // 取消按钮
  219. cancel() {
  220. this.open = false;
  221. this.reset();
  222. },
  223. // 表单重置
  224. reset() {
  225. this.form = {
  226. id: undefined,
  227. parentId: undefined,
  228. name: undefined,
  229. sort: undefined,
  230. leaderUserId: undefined,
  231. phone: undefined,
  232. email: undefined,
  233. status: CommonStatusEnum.ENABLE,
  234. };
  235. this.resetForm("form");
  236. },
  237. /** 搜索按钮操作 */
  238. handleQuery() {
  239. this.getList();
  240. },
  241. /** 重置按钮操作 */
  242. resetQuery() {
  243. this.resetForm("queryForm");
  244. this.handleQuery();
  245. },
  246. /** 新增按钮操作 */
  247. handleAdd(row) {
  248. this.reset();
  249. if (row !== undefined) {
  250. this.form.parentId = row.id;
  251. }
  252. this.open = true;
  253. this.title = "添加部门";
  254. listDept().then(response => {
  255. this.deptOptions = this.handleTree(response.data, "id");
  256. });
  257. },
  258. /** 展开/折叠操作 */
  259. toggleExpandAll() {
  260. this.refreshTable = false;
  261. this.isExpandAll = !this.isExpandAll;
  262. this.$nextTick(() => {
  263. this.refreshTable = true;
  264. });
  265. },
  266. /** 修改按钮操作 */
  267. handleUpdate(row) {
  268. this.reset();
  269. getDept(row.id).then(response => {
  270. this.form = response.data;
  271. if (this.form.parentId === 0) { // 无父部门时,标记为 undefined,避免展示为 Unknown
  272. this.form.parentId = undefined;
  273. }
  274. this.open = true;
  275. this.title = "修改部门";
  276. });
  277. listDept(row.id).then(response => {
  278. this.deptOptions = this.handleTree(response.data, "id");
  279. });
  280. },
  281. /** 提交按钮 */
  282. submitForm: function() {
  283. this.$refs["form"].validate(valid => {
  284. if (valid) {
  285. if (this.form.id !== undefined) {
  286. updateDept(this.form).then(response => {
  287. this.$modal.msgSuccess("修改成功");
  288. this.open = false;
  289. this.getList();
  290. });
  291. } else {
  292. addDept(this.form).then(response => {
  293. this.$modal.msgSuccess("新增成功");
  294. this.open = false;
  295. this.getList();
  296. });
  297. }
  298. }
  299. });
  300. },
  301. /** 删除按钮操作 */
  302. handleDelete(row) {
  303. this.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项?').then(function() {
  304. return delDept(row.id);
  305. }).then(() => {
  306. this.getList();
  307. this.$modal.msgSuccess("删除成功");
  308. }).catch(() => {});
  309. }
  310. }
  311. };
  312. </script>