index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <template>
  2. <div class="app-container">
  3. <el-form
  4. :model="queryParams"
  5. ref="queryForm"
  6. size="small"
  7. :inline="true"
  8. v-show="showSearch"
  9. >
  10. <el-form-item label="部门名称" prop="deptName">
  11. <el-input
  12. v-model="queryParams.deptName"
  13. placeholder="请输入部门名称"
  14. clearable
  15. @keyup.enter.native="handleQuery"
  16. />
  17. </el-form-item>
  18. <el-form-item label="状态" prop="status">
  19. <el-select
  20. v-model="queryParams.status"
  21. placeholder="部门状态"
  22. clearable
  23. >
  24. <el-option
  25. v-for="dict in dict.type.sys_normal_disable"
  26. :key="dict.value"
  27. :label="dict.label"
  28. :value="dict.value"
  29. />
  30. </el-select>
  31. </el-form-item>
  32. <el-form-item>
  33. <el-button
  34. type="primary"
  35. icon="el-icon-search"
  36. size="mini"
  37. @click="handleQuery"
  38. >搜索</el-button
  39. >
  40. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
  41. >重置</el-button
  42. >
  43. </el-form-item>
  44. </el-form>
  45. <el-row :gutter="10" class="mb8">
  46. <el-col :span="1.5">
  47. <el-button
  48. type="primary"
  49. plain
  50. icon="el-icon-plus"
  51. size="mini"
  52. @click="handleAdd"
  53. v-hasPermi="['system:dept:add']"
  54. >新增</el-button
  55. >
  56. </el-col>
  57. <el-col :span="1.5">
  58. <el-button
  59. type="info"
  60. plain
  61. icon="el-icon-sort"
  62. size="mini"
  63. @click="toggleExpandAll"
  64. >展开/折叠</el-button
  65. >
  66. </el-col>
  67. <el-col :span="1.5">
  68. <el-button
  69. type="primary"
  70. plain
  71. icon="el-icon-refresh"
  72. size="mini"
  73. @click="handleSync"
  74. v-hasPermi="['system:dept:add']"
  75. >同步</el-button
  76. >
  77. </el-col>
  78. <right-toolbar
  79. :showSearch.sync="showSearch"
  80. @queryTable="getList"
  81. ></right-toolbar>
  82. </el-row>
  83. <el-table
  84. v-if="refreshTable"
  85. v-loading="loading"
  86. :data="deptList"
  87. row-key="deptId"
  88. :default-expand-all="isExpandAll"
  89. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
  90. >
  91. <el-table-column
  92. prop="deptName"
  93. label="部门名称"
  94. width="260"
  95. ></el-table-column>
  96. <el-table-column
  97. prop="orderNum"
  98. label="排序"
  99. width="200"
  100. ></el-table-column>
  101. <el-table-column prop="status" label="状态" width="100">
  102. <template slot-scope="scope">
  103. <dict-tag
  104. :options="dict.type.sys_normal_disable"
  105. :value="scope.row.status"
  106. />
  107. </template>
  108. </el-table-column>
  109. <el-table-column
  110. label="创建时间"
  111. align="center"
  112. prop="createTime"
  113. width="200"
  114. >
  115. <template slot-scope="scope">
  116. <span>{{ parseTime(scope.row.createTime) }}</span>
  117. </template>
  118. </el-table-column>
  119. <el-table-column
  120. label="操作"
  121. align="center"
  122. class-name="small-padding fixed-width"
  123. >
  124. <template slot-scope="scope">
  125. <el-button
  126. size="mini"
  127. type="text"
  128. icon="el-icon-edit"
  129. @click="handleUpdate(scope.row)"
  130. v-hasPermi="['system:dept:edit']"
  131. >修改</el-button
  132. >
  133. <el-button
  134. size="mini"
  135. type="text"
  136. icon="el-icon-plus"
  137. @click="handleAdd(scope.row)"
  138. v-hasPermi="['system:dept:add']"
  139. >新增</el-button
  140. >
  141. <el-button
  142. v-if="scope.row.parentId != 0"
  143. size="mini"
  144. type="text"
  145. icon="el-icon-delete"
  146. @click="handleDelete(scope.row)"
  147. v-hasPermi="['system:dept:remove']"
  148. >删除</el-button
  149. >
  150. </template>
  151. </el-table-column>
  152. </el-table>
  153. <!-- 添加或修改部门对话框 -->
  154. <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
  155. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  156. <el-row>
  157. <el-col :span="24" v-if="form.parentId !== 0">
  158. <el-form-item label="上级部门" prop="parentId">
  159. <treeselect
  160. v-model="form.parentId"
  161. :options="deptOptions"
  162. :normalizer="normalizer"
  163. placeholder="选择上级部门"
  164. />
  165. </el-form-item>
  166. </el-col>
  167. </el-row>
  168. <el-row>
  169. <el-col :span="12">
  170. <el-form-item label="部门名称" prop="deptName">
  171. <el-input v-model="form.deptName" placeholder="请输入部门名称" />
  172. </el-form-item>
  173. </el-col>
  174. <el-col :span="12">
  175. <el-form-item label="显示排序" prop="orderNum">
  176. <el-input-number
  177. v-model="form.orderNum"
  178. controls-position="right"
  179. :min="0"
  180. />
  181. </el-form-item>
  182. </el-col>
  183. </el-row>
  184. <el-row>
  185. <el-col :span="12">
  186. <el-form-item label="负责人" prop="leader">
  187. <el-input
  188. v-model="form.leader"
  189. placeholder="请输入负责人"
  190. maxlength="20"
  191. />
  192. </el-form-item>
  193. </el-col>
  194. <el-col :span="12">
  195. <el-form-item label="联系电话" prop="phone">
  196. <el-input
  197. v-model="form.phone"
  198. placeholder="请输入联系电话"
  199. maxlength="11"
  200. />
  201. </el-form-item>
  202. </el-col>
  203. </el-row>
  204. <el-row>
  205. <el-col :span="12">
  206. <el-form-item label="邮箱" prop="email">
  207. <el-input
  208. v-model="form.email"
  209. placeholder="请输入邮箱"
  210. maxlength="50"
  211. />
  212. </el-form-item>
  213. </el-col>
  214. <el-col :span="12">
  215. <el-form-item label="部门状态">
  216. <el-radio-group v-model="form.status">
  217. <el-radio
  218. v-for="dict in dict.type.sys_normal_disable"
  219. :key="dict.value"
  220. :label="dict.value"
  221. >{{ dict.label }}</el-radio
  222. >
  223. </el-radio-group>
  224. </el-form-item>
  225. </el-col>
  226. </el-row>
  227. <el-row>
  228. <el-col :span="12">
  229. <el-form-item label="行政区划" prop="xzqdm">
  230. <el-cascader
  231. size="small"
  232. :options="xzoptions"
  233. :props="{
  234. checkStrictly: true,
  235. emitPath: false,
  236. }"
  237. v-model="form.xzqdm"
  238. clearable
  239. ref="cascader"
  240. :filterable="true"
  241. @change="setXzqdm"
  242. :show-all-levels="false"
  243. style="width: 100%"
  244. ></el-cascader>
  245. </el-form-item>
  246. </el-col>
  247. </el-row>
  248. <el-row>
  249. <el-col :span="24">
  250. <el-form-item label="资源目录">
  251. <treeselect
  252. v-model="resourceList"
  253. :options="resourceOptions"
  254. :normalizer="resourcenormalizer"
  255. :multiple="true"
  256. placeholder="选择资源目录"
  257. :flat="true"
  258. />
  259. </el-form-item>
  260. </el-col>
  261. </el-row>
  262. </el-form>
  263. <div slot="footer" class="dialog-footer">
  264. <el-button type="primary" @click="submitForm">确 定</el-button>
  265. <el-button @click="cancel">取 消</el-button>
  266. </div>
  267. </el-dialog>
  268. </div>
  269. </template>
  270. <script>
  271. import {
  272. listDept,
  273. getDept,
  274. delDept,
  275. addDept,
  276. updateDept,
  277. listDeptExcludeChild,
  278. queryResources,
  279. syncDept,
  280. } from "@/api/system/dept";
  281. import Treeselect from "@riophae/vue-treeselect";
  282. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  283. import { getXzq } from "@/api/system/user";
  284. export default {
  285. name: "Dept",
  286. dicts: ["sys_normal_disable"],
  287. components: { Treeselect },
  288. data() {
  289. return {
  290. resourceList: [],
  291. // 遮罩层
  292. loading: true,
  293. // 显示搜索条件
  294. showSearch: true,
  295. // 表格树数据
  296. deptList: [],
  297. // 部门树选项
  298. deptOptions: [],
  299. //资源目录
  300. resourceOptions: [],
  301. // 弹出层标题
  302. title: "",
  303. // 是否显示弹出层
  304. open: false,
  305. // 是否展开,默认全部展开
  306. isExpandAll: true,
  307. // 重新渲染表格状态
  308. refreshTable: true,
  309. // 查询参数
  310. queryParams: {
  311. deptName: undefined,
  312. status: undefined,
  313. },
  314. // 表单参数
  315. form: {},
  316. // 行政区选项
  317. xzoptions: [],
  318. defaultProps: {
  319. children: "children",
  320. label: "label",
  321. },
  322. // 表单校验
  323. rules: {
  324. parentId: [
  325. { required: true, message: "上级部门不能为空", trigger: "blur" },
  326. ],
  327. deptName: [
  328. { required: true, message: "部门名称不能为空", trigger: "blur" },
  329. ],
  330. orderNum: [
  331. { required: true, message: "显示排序不能为空", trigger: "blur" },
  332. ],
  333. email: [
  334. {
  335. type: "email",
  336. message: "请输入正确的邮箱地址",
  337. trigger: ["blur", "change"],
  338. },
  339. ],
  340. phone: [
  341. {
  342. pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
  343. message: "请输入正确的手机号码",
  344. trigger: "blur",
  345. },
  346. ],
  347. },
  348. };
  349. },
  350. created() {
  351. this.getXzq();
  352. this.getList();
  353. },
  354. methods: {
  355. /** 同步按钮操作 */
  356. handleSync(row) {
  357. syncDept().then((response) => {
  358. if (response.code == 200) {
  359. listDept().then((response) => {
  360. this.deptOptions = this.handleTree(response.data, "deptId");
  361. this.$modal.msgSuccess("同步成功");
  362. });
  363. } else {
  364. this.$modal.msgSuccess("同步失败");
  365. }
  366. });
  367. },
  368. /** 查询部门列表 */
  369. getList() {
  370. this.loading = true;
  371. listDept(this.queryParams).then((response) => {
  372. this.deptList = this.handleTree(response.data, "deptId");
  373. this.loading = false;
  374. });
  375. },
  376. /** 转换部门数据结构 */
  377. normalizer(node) {
  378. if (node.children && !node.children.length) {
  379. delete node.children;
  380. }
  381. return {
  382. id: node.deptId,
  383. label: node.deptName,
  384. children: node.children,
  385. };
  386. },
  387. /** 转换资源目录数据结构 */
  388. resourcenormalizer(node) {
  389. console.log(node);
  390. if (node.children && !node.children.length) {
  391. delete node.children;
  392. }
  393. return {
  394. id: node.bsm,
  395. label: node.name,
  396. children: node.children,
  397. isDisabled: node.parent == 1,
  398. };
  399. },
  400. // 取消按钮
  401. cancel() {
  402. this.open = false;
  403. this.reset();
  404. },
  405. // 表单重置
  406. reset() {
  407. this.form = {
  408. deptId: undefined,
  409. parentId: undefined,
  410. deptName: undefined,
  411. orderNum: undefined,
  412. leader: undefined,
  413. phone: undefined,
  414. email: undefined,
  415. status: "0",
  416. xzqdm: undefined,
  417. resources: undefined,
  418. };
  419. this.resourceList = [];
  420. this.resetForm("form");
  421. },
  422. /** 搜索按钮操作 */
  423. handleQuery() {
  424. this.getList();
  425. },
  426. /** 重置按钮操作 */
  427. resetQuery() {
  428. this.resetForm("queryForm");
  429. this.handleQuery();
  430. },
  431. /** 新增按钮操作 */
  432. handleAdd(row) {
  433. this.reset();
  434. if (row != undefined) {
  435. this.form.parentId = row.deptId;
  436. }
  437. this.open = true;
  438. this.title = "添加部门";
  439. listDept().then((response) => {
  440. this.deptOptions = this.handleTree(response.data, "deptId");
  441. });
  442. queryResources().then((response) => {
  443. this.resourceOptions = this.handleTree(response.data, "bsm", "pbsm");
  444. });
  445. },
  446. /** 展开/折叠操作 */
  447. toggleExpandAll() {
  448. this.refreshTable = false;
  449. this.isExpandAll = !this.isExpandAll;
  450. this.$nextTick(() => {
  451. this.refreshTable = true;
  452. });
  453. },
  454. /** 修改按钮操作 */
  455. handleUpdate(row) {
  456. this.reset();
  457. getDept(row.deptId).then((response) => {
  458. this.form = response.data;
  459. this.form.xzqdm = response.data.district;
  460. if (this.form.resources) {
  461. this.resourceList = this.form.resources.split(",");
  462. }
  463. this.open = true;
  464. this.title = "修改部门";
  465. });
  466. queryResources().then((response) => {
  467. this.resourceOptions = this.handleTree(response.data, "bsm", "pbsm");
  468. });
  469. listDeptExcludeChild(row.deptId).then((response) => {
  470. this.deptOptions = this.handleTree(response.data, "deptId");
  471. });
  472. },
  473. /** 提交按钮 */
  474. submitForm: function () {
  475. if (this.resourceList.length > 0) {
  476. this.form.resources = this.resourceList.join(",");
  477. } else {
  478. this.form.resources = "";
  479. }
  480. console.log(this.form.resources);
  481. // return;
  482. this.$refs["form"].validate((valid) => {
  483. if (valid) {
  484. let params = Object.assign(this.form, {
  485. district: this.form.xzqdm,
  486. });
  487. if (this.form.deptId != undefined) {
  488. updateDept(params).then((response) => {
  489. this.$modal.msgSuccess("修改成功");
  490. this.open = false;
  491. this.getList();
  492. });
  493. } else {
  494. addDept(params).then((response) => {
  495. this.$modal.msgSuccess("新增成功");
  496. this.open = false;
  497. this.getList();
  498. });
  499. }
  500. }
  501. });
  502. },
  503. /** 删除按钮操作 */
  504. handleDelete(row) {
  505. this.$modal
  506. .confirm('是否确认删除名称为"' + row.deptName + '"的数据项?')
  507. .then(function () {
  508. return delDept(row.deptId);
  509. })
  510. .then(() => {
  511. this.getList();
  512. this.$modal.msgSuccess("删除成功");
  513. })
  514. .catch(() => {});
  515. },
  516. // 构建行政区tree
  517. resetTree(data) {
  518. data.forEach((item) => {
  519. if (!item.isparent && item.children.length == 0) {
  520. delete item.children;
  521. }
  522. if (item.isparent && item.children.length > 0) {
  523. this.resetTree(item.children);
  524. }
  525. });
  526. return data;
  527. },
  528. // 获取行政区
  529. getXzq() {
  530. getXzq({ city: true }).then((res) => {
  531. if (res.success) {
  532. this.xzoptions = this.resetTree(res.data);
  533. }
  534. });
  535. },
  536. // 获取行政区数据
  537. setXzqdm(bsm) {
  538. if (typeof this.$refs.cascader.getCheckedNodes()[0] !== "undefined") {
  539. this.form.xzqdm = this.$refs.cascader.getCheckedNodes()[0].value;
  540. // this.form.xzqName = bsm ? this.$refs.cascader.getCheckedNodes()[0].label : "";
  541. }
  542. },
  543. },
  544. };
  545. </script>