index.vue 11 KB

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