department.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <template>
  2. <div class="mainBox">
  3. <!--搜索功能 start------------------------------------------------------------>
  4. <div class="layerBoxNoBg">
  5. <div>
  6. <el-button type="primary" @click="openWindow()">添加职能部门</el-button>
  7. </div>
  8. </div>
  9. <!--搜索功能 end------------------------------------------------------------>
  10. <!--表格内容 start------------------------------------------------------------>
  11. <div class="layerBox">
  12. <tableTitle :name="tableDivTitle"/>
  13. <el-row>
  14. <template>
  15. <el-table :data="tableData" style="width: 100%">
  16. <el-table-column fixed prop="id" label="编号"></el-table-column>
  17. <el-table-column prop="name" label="职能部门"></el-table-column>
  18. <el-table-column fixed="right" label="操作" width="200" header-align="center">
  19. <template slot-scope="scope">
  20. <div class="listBtnBox">
  21. <div class="listDeleteBtn" @click="deleteRow(scope.row.id)"><i class="el-icon-edit-outline"></i>删除</div>
  22. <div class="listEditBtn" @click="editRow(scope.row.id)"><i class="el-icon-edit-outline"></i>编辑</div>
  23. </div>
  24. </template>
  25. </el-table-column>
  26. </el-table>
  27. </template>
  28. </el-row>
  29. </div>
  30. <!--分页 start------------------------------------------------------------>
  31. <div class="alignBox">
  32. <el-row>
  33. <el-col :span="24">
  34. <el-pagination :current-page="getApiData.page" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-size="10" layout="total, prev, pager, next, jumper" :total="allCount"></el-pagination>
  35. </el-col>
  36. </el-row>
  37. </div>
  38. <!--分页 end------------------------------------------------------------>
  39. <!--表格内容 end------------------------------------------------------------>
  40. <!--弹出框 start------------------------------------------------------------>
  41. <el-dialog :title="editId ? '编辑职能部门' : '添加职能部门'" :visible.sync="windowStatus" :close-on-click-modal="false">
  42. <el-form :model="form" ref="form" :rules="formRules" autocomplete="off" label-position="left">
  43. <div class="formDiv">
  44. <el-form-item label="选择上级部门" :label-width="formLabelWidth" prop="pid" class="custom-align-right">
  45. <el-cascader :key="searchDepartmentKey" v-model="form.pid" placeholder="请选择上级部门" :props="searchDepartmentData" filterable clearable></el-cascader>
  46. </el-form-item>
  47. </div>
  48. <div class="formDiv">
  49. <el-form-item label="部门名称:" :label-width="formLabelWidth" prop="name" class="custom-align-right">
  50. <el-input v-model="form.name" autocomplete="off" placeholder="请输入职能部门名称"></el-input>
  51. </el-form-item>
  52. </div>
  53. </el-form>
  54. <div slot="footer" class="dialog-footer">
  55. <div>
  56. <el-button @click="closeWindow">取 消</el-button>
  57. <el-button type="primary" @click="addData" v-if="editId==0">提交</el-button>
  58. <el-button type="primary" @click="editData" v-else>修改</el-button>
  59. </div>
  60. </div>
  61. </el-dialog>
  62. <!--弹出框 end------------------------------------------------------------>
  63. </div>
  64. </template>
  65. <script>
  66. //引入公用样式
  67. import '@/styles/global.less';
  68. //表格标题
  69. import tableTitle from './components/tableTitle';
  70. export default {
  71. components: {
  72. tableTitle,//表格标题
  73. },
  74. data() {
  75. //0.全局操作 start ------------------------------------------------------------>
  76. //表单验证
  77. const validateEmpty = (rule,value,callback) => {
  78. if (!value || value.trim() === "") {
  79. callback(new Error('该项不能为空!'));
  80. } else {
  81. callback();
  82. }
  83. }
  84. const validateArray = (rule,value,callback) => {
  85. if (value.length == 0) {
  86. callback(new Error('该项不能为空!'))
  87. } else {
  88. callback()
  89. }
  90. }
  91. let self = this;
  92. //0.全局操作 end ------------------------------------------------------------>
  93. return {
  94. //1.列表和分页相关 start ------------------------------------------------------------>
  95. tableDivTitle:"职能部门管理",
  96. tableData: [],//列表
  97. allCount:0,//总条数
  98. editId:0,//要修改的网站id
  99. getApiData:{
  100. search:"",//职能部门名称
  101. page:1,//当前是第几页
  102. pageSize:10,//一共多少条
  103. },
  104. //行政职能部门
  105. searchDepartmentKey:0,
  106. searchDepartmentData: {
  107. checkStrictly: true,
  108. lazy: true,
  109. async lazyLoad (node, resolve) {
  110. const { level, data } = node;
  111. if (data && data.children && data.children.length !== 0) {
  112. return resolve(node)
  113. }
  114. console.log(level)
  115. let parentId = level == 0 ? 0 : data.value
  116. let parames = {
  117. 'pid':parentId
  118. }
  119. self.$store.dispatch('public/getAllDepartment',parames).then(res=> {
  120. if (res.data) {
  121. const nodes = res.data.map(item => ({
  122. value: item.id,
  123. label: item.name,
  124. leaf: level >= 3,
  125. children: []
  126. }))
  127. resolve(nodes)
  128. }
  129. })
  130. }
  131. },
  132. //分页相关 end ------------------------------------------------------------>
  133. //2.弹出框设置 start ------------------------------------------------------------>
  134. //是否显示弹出窗口
  135. windowStatus:false,
  136. formLabelWidth: '120px',
  137. //弹出框设置 end ------------------------------------------------------------>
  138. //3.搜索相关 start ------------------------------------------------------------>
  139. //搜索相关 end ------------------------------------------------------------>
  140. //3.弹出框中的表单设置 start ------------------------------------------------------------>
  141. //3.1 表单收集的数据
  142. form: {
  143. pid:"",
  144. name:""
  145. },
  146. //3.2表单验证规则
  147. formRules: {
  148. //角色名称不能为空
  149. pid:[{required:true,trigger:'blur',validator:validateArray}],
  150. //职能描述不能为空
  151. name:[{required:true,trigger:'blur',validator:validateEmpty}],
  152. },
  153. //弹出框中的表单设置 end ------------------------------------------------------------>
  154. }
  155. },
  156. methods: {
  157. //1.列表和分页相关 start ------------------------------------------------------------>
  158. //1.1 获取内容
  159. getData(){
  160. this.$store.dispatch('public/getDepartmentList',this.getApiData).then(res=> {
  161. console.log(res)
  162. this.tableData = res.data.rows;
  163. this.allCount = res.data.count;
  164. }).catch(() => {
  165. this.$message({
  166. type: 'warning',
  167. message: '网络错误,请重试!'
  168. });
  169. })
  170. },
  171. //1.2 列表内容分页
  172. //直接跳转
  173. handleSizeChange(val) {
  174. this.getApiData.page = val;
  175. this.getData();
  176. },
  177. //1.3 点击分页
  178. handleCurrentChange(val) {
  179. this.getApiData.page = val;
  180. this.getData();
  181. },
  182. //1.4 删除职能部门
  183. deleteRow(id){
  184. this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
  185. confirmButtonText: '确定',
  186. cancelButtonText: '取消',
  187. type: 'warning'
  188. }).then(() => {
  189. console.log("当前删除:" + id)
  190. this.$store.dispatch('public/delDepartment',{id:id}).then(res=> {
  191. this.getData();
  192. this.$message({
  193. type: 'success',
  194. message: '删除成功!'
  195. });
  196. }).catch(() => {
  197. this.$message({
  198. type: 'warning',
  199. message: '网络错误,请重试!'
  200. });
  201. })
  202. }).catch(() => {
  203. this.$message({
  204. type: 'warning',
  205. message: '已取消删除'
  206. });
  207. });
  208. },
  209. //列表和分页相关 end ------------------------------------------------------------>
  210. //2.弹出框设置 start ------------------------------------------------------------>
  211. //2.1 打开弹出框
  212. openWindow() {
  213. this.clearToServe();
  214. this.windowStatus = true;
  215. //this.$refs.form.clearValidate();
  216. },
  217. //2.2 关闭弹出框
  218. closeWindow(){
  219. this.windowStatus = false;
  220. this.clearToServe();
  221. this.$refs.form.clearValidate();
  222. },
  223. //2.3 清空提交窗口
  224. clearToServe(){
  225. this.editId = 0;
  226. this.form.pid = [];
  227. this.form.name = "";
  228. },
  229. //弹出框设置 end ------------------------------------------------------------>
  230. //4.添加 start ------------------------------------------------------------>
  231. addData(){
  232. if(this.form.pid.length!=0){
  233. const lastPicValue = this.form.pid[this.form.pid.length - 1];
  234. this.form.pid = String(lastPicValue);
  235. }
  236. this.$refs.form.validate(valid => {
  237. if (valid) {
  238. this.$store.dispatch('public/addDepartment',this.form).then(res=> {
  239. if(res.code == 200){
  240. this.$message({
  241. type: 'success',
  242. message: '已成功添加职能部门!'
  243. });
  244. console.log(res)
  245. this.closeWindow();
  246. this.getData();
  247. }else{
  248. this.$message({
  249. type: 'warning',
  250. message: res.message
  251. });
  252. console.log(res)
  253. this.closeWindow();
  254. this.getData();
  255. }
  256. }).catch(() => {
  257. this.$message({
  258. type: 'info',
  259. message: '网络错误,请重试!'
  260. });
  261. })
  262. }
  263. })
  264. },
  265. //添加 end ------------------------------------------------------------>
  266. //4.编辑 start ------------------------------------------------------------>
  267. //回显数据
  268. editRow(id){
  269. this.openWindow();
  270. //设置要编辑的id
  271. this.editId = id;
  272. this.$store.dispatch('public/getDepartmentMain',{id:id}).then(res=> {
  273. console.log(res)
  274. this.form.name = res.data.name;
  275. this.form.pid = res.data.pid;
  276. //回显父级导航
  277. // 这里的接口有问题,应该返回数组,但是返回的只有上一级的pid
  278. // 我这里没有办法判断他的层级关系
  279. // 检查 res.data.pid 的类型
  280. if (typeof res.data.pid === 'number') {
  281. // 如果是数字,将其转换为数组
  282. this.form.pid = [res.data.pid];
  283. } else {
  284. // 如果已经是数组,直接赋值
  285. this.form.pid = JSON.parse(res.data.pid); // 如果是字符串,解析为数组
  286. }
  287. this.searchDepartmentKey += 1;
  288. this.loadCascaderPath(this.form.pid);
  289. console.log(this.form.pid)
  290. console.log(this.searchDepartmentKey)
  291. }).catch(() => {
  292. this.$message({
  293. type: 'warning',
  294. message: '网络错误,请重试!'
  295. });
  296. })
  297. },
  298. //回显职能部门
  299. async loadCascaderPath(path,type) {
  300. for (let i = 0; i < path.length; i++) {
  301. const parentId = path[i - 1] || 0; // 获取当前层级的父级ID
  302. const level = i; // 当前层级的索引
  303. await this.$store.dispatch('public/getDepartmentMain', { pid: parentId })
  304. .then((res) => {
  305. const nodes = res.data.map(item => ({
  306. value: item.id,
  307. label: item.name,
  308. leaf: level >= 3, // 这里假设4层结构,设置叶子节点标记
  309. }));
  310. // 将数据传递到 resolve,通知 cascader 这一层的数据加载完毕
  311. if (level === path.length - 1) {
  312. this.form.pid = path;
  313. }
  314. });
  315. }
  316. },
  317. //提交编辑
  318. editData(){
  319. this.form.id = this.editId;
  320. if(this.form.pid.length!=0){
  321. const lastPicValue = this.form.pid[this.form.pid.length - 1];
  322. this.form.pid = String(lastPicValue);
  323. }
  324. //获取选中的节点
  325. this.$refs.form.validate(valid => {
  326. if (valid) {
  327. this.$store.dispatch('public/upDepartment',this.form).then(res=> {
  328. console.log(res)
  329. this.closeWindow();
  330. this.$message({
  331. type: 'success',
  332. message: '修改成功!'
  333. });
  334. this.getData();
  335. }).catch(() => {
  336. this.$message({
  337. type: 'warning',
  338. message: '网络错误,请重试!'
  339. });
  340. })
  341. }
  342. })
  343. }
  344. //编辑 end ------------------------------------------------------------>
  345. },
  346. mounted(){
  347. //获取页面列表
  348. this.getData();
  349. //获取菜单树
  350. //this.getAllMenuList();
  351. }
  352. }
  353. </script>
  354. <style scoped lang="less">
  355. .layerBoxNoBg {
  356. padding: 30px 0 0 0;
  357. }
  358. //表单微调 start------------------------------------------------------------>*/
  359. ::v-deep .custom-form-item > .el-form-item__label {
  360. line-height: 140px !important;
  361. }
  362. ::v-deep .custom-textarea .el-textarea__inner {
  363. resize: none; /* 禁止用拖拽调整大小 */
  364. }
  365. ::v-deep .custom-align-right .el-form-item__label {
  366. text-align: right; /* 设置标签文字右对齐 */
  367. }
  368. ::v-deep .el-select-group__title {
  369. color: #909399;
  370. }
  371. ::v-deep .el-select {
  372. width: 100% !important;
  373. }
  374. </style>