From baeed2e8d3a82f94e80d04ea47c95b122fcf93dd Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期二, 02 二月 2021 16:25:37 +0800
Subject: [PATCH] 修复角色管理-编辑角色-功能权限显示异常

---
 ruoyi-ui/src/views/system/role/index.vue |  238 +++++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 182 insertions(+), 56 deletions(-)

diff --git a/ruoyi-ui/src/views/system/role/index.vue b/ruoyi-ui/src/views/system/role/index.vue
index 7f0feb9..6e96faa 100644
--- a/ruoyi-ui/src/views/system/role/index.vue
+++ b/ruoyi-ui/src/views/system/role/index.vue
@@ -1,7 +1,7 @@
 <template>
   <div class="app-container">
-    <el-form :inline="true">
-      <el-form-item label="瑙掕壊鍚嶇О">
+    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
+      <el-form-item label="瑙掕壊鍚嶇О" prop="roleName">
         <el-input
           v-model="queryParams.roleName"
           placeholder="璇疯緭鍏ヨ鑹插悕绉�"
@@ -11,7 +11,7 @@
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
-      <el-form-item label="鏉冮檺瀛楃">
+      <el-form-item label="鏉冮檺瀛楃" prop="roleKey">
         <el-input
           v-model="queryParams.roleKey"
           placeholder="璇疯緭鍏ユ潈闄愬瓧绗�"
@@ -21,7 +21,7 @@
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
-      <el-form-item label="鐘舵��">
+      <el-form-item label="鐘舵��" prop="status">
         <el-select
           v-model="queryParams.status"
           placeholder="瑙掕壊鐘舵��"
@@ -51,22 +51,63 @@
       </el-form-item>
       <el-form-item>
         <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">鎼滅储</el-button>
+        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">閲嶇疆</el-button>
+      </el-form-item>
+    </el-form>
+
+    <el-row :gutter="10" class="mb8">
+      <el-col :span="1.5">
         <el-button
           type="primary"
+          plain
           icon="el-icon-plus"
           size="mini"
           @click="handleAdd"
           v-hasPermi="['system:role:add']"
         >鏂板</el-button>
-      </el-form-item>
-    </el-form>
+      </el-col>
+      <el-col :span="1.5">
+        <el-button
+          type="success"
+          plain
+          icon="el-icon-edit"
+          size="mini"
+          :disabled="single"
+          @click="handleUpdate"
+          v-hasPermi="['system:role:edit']"
+        >淇敼</el-button>
+      </el-col>
+      <el-col :span="1.5">
+        <el-button
+          type="danger"
+          plain
+          icon="el-icon-delete"
+          size="mini"
+          :disabled="multiple"
+          @click="handleDelete"
+          v-hasPermi="['system:role:remove']"
+        >鍒犻櫎</el-button>
+      </el-col>
+      <el-col :span="1.5">
+        <el-button
+          type="warning"
+          plain
+          icon="el-icon-download"
+          size="mini"
+          @click="handleExport"
+          v-hasPermi="['system:role:export']"
+        >瀵煎嚭</el-button>
+      </el-col>
+      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
+    </el-row>
 
-    <el-table v-loading="loading" :data="roleList">
+    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
+      <el-table-column type="selection" width="55" align="center" />
       <el-table-column label="瑙掕壊缂栧彿" prop="roleId" width="120" />
       <el-table-column label="瑙掕壊鍚嶇О" prop="roleName" :show-overflow-tooltip="true" width="150" />
-      <el-table-column label="鏉冮檺瀛楃" prop="roleKey" :show-overflow-tooltip="true" width="180" />
-      <el-table-column label="鏄剧ず椤哄簭" prop="roleSort" width="120" />
-      <el-table-column label="鐘舵��" align="center" width="120">
+      <el-table-column label="鏉冮檺瀛楃" prop="roleKey" :show-overflow-tooltip="true" width="150" />
+      <el-table-column label="鏄剧ず椤哄簭" prop="roleSort" width="100" />
+      <el-table-column label="鐘舵��" align="center" width="100">
         <template slot-scope="scope">
           <el-switch
             v-model="scope.row.status"
@@ -117,7 +158,7 @@
     />
 
     <!-- 娣诲姞鎴栦慨鏀硅鑹查厤缃璇濇 -->
-    <el-dialog :title="title" :visible.sync="open" width="500px">
+    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
       <el-form ref="form" :model="form" :rules="rules" label-width="80px">
         <el-form-item label="瑙掕壊鍚嶇О" prop="roleName">
           <el-input v-model="form.roleName" placeholder="璇疯緭鍏ヨ鑹插悕绉�" />
@@ -138,11 +179,16 @@
           </el-radio-group>
         </el-form-item>
         <el-form-item label="鑿滃崟鏉冮檺">
+          <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">灞曞紑/鎶樺彔</el-checkbox>
+          <el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">鍏ㄩ��/鍏ㄤ笉閫�</el-checkbox>
+          <el-checkbox v-model="form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">鐖跺瓙鑱斿姩</el-checkbox>
           <el-tree
+            class="tree-border"
             :data="menuOptions"
             show-checkbox
             ref="menu"
             node-key="id"
+            :check-strictly="!form.menuCheckStrictly"
             empty-text="鍔犺浇涓紝璇风◢鍚�"
             :props="defaultProps"
           ></el-tree>
@@ -158,7 +204,7 @@
     </el-dialog>
 
     <!-- 鍒嗛厤瑙掕壊鏁版嵁鏉冮檺瀵硅瘽妗� -->
-    <el-dialog :title="title" :visible.sync="openDataScope" width="500px">
+    <el-dialog :title="title" :visible.sync="openDataScope" width="500px" append-to-body>
       <el-form :model="form" label-width="80px">
         <el-form-item label="瑙掕壊鍚嶇О">
           <el-input v-model="form.roleName" :disabled="true" />
@@ -177,12 +223,17 @@
           </el-select>
         </el-form-item>
         <el-form-item label="鏁版嵁鏉冮檺" v-show="form.dataScope == 2">
+          <el-checkbox v-model="deptExpand" @change="handleCheckedTreeExpand($event, 'dept')">灞曞紑/鎶樺彔</el-checkbox>
+          <el-checkbox v-model="deptNodeAll" @change="handleCheckedTreeNodeAll($event, 'dept')">鍏ㄩ��/鍏ㄤ笉閫�</el-checkbox>
+          <el-checkbox v-model="form.deptCheckStrictly" @change="handleCheckedTreeConnect($event, 'dept')">鐖跺瓙鑱斿姩</el-checkbox>
           <el-tree
+            class="tree-border"
             :data="deptOptions"
             show-checkbox
             default-expand-all
             ref="dept"
             node-key="id"
+            :check-strictly="!form.deptCheckStrictly"
             empty-text="鍔犺浇涓紝璇风◢鍚�"
             :props="defaultProps"
           ></el-tree>
@@ -197,15 +248,24 @@
 </template>
 
 <script>
-import { listRole, getRole, delRole, addRole, updateRole, dataScope, changeRoleStatus } from "@/api/system/role";
+import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/system/role";
 import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
 import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";
 
 export default {
+  name: "Role",
   data() {
     return {
       // 閬僵灞�
       loading: true,
+      // 閫変腑鏁扮粍
+      ids: [],
+      // 闈炲崟涓鐢�
+      single: true,
+      // 闈炲涓鐢�
+      multiple: true,
+      // 鏄剧ず鎼滅储鏉′欢
+      showSearch: true,
       // 鎬绘潯鏁�
       total: 0,
       // 瑙掕壊琛ㄦ牸鏁版嵁
@@ -216,6 +276,10 @@
       open: false,
       // 鏄惁鏄剧ず寮瑰嚭灞傦紙鏁版嵁鏉冮檺锛�
       openDataScope: false,
+      menuExpand: false,
+      menuNodeAll: false,
+      deptExpand: true,
+      deptNodeAll: false,
       // 鏃ユ湡鑼冨洿
       dateRange: [],
       // 鐘舵�佹暟鎹瓧鍏�
@@ -308,33 +372,33 @@
     // 鎵�鏈夎彍鍗曡妭鐐规暟鎹�
     getMenuAllCheckedKeys() {
       // 鐩墠琚�変腑鐨勮彍鍗曡妭鐐�
-      let checkedKeys = this.$refs.menu.getHalfCheckedKeys();
+      let checkedKeys = this.$refs.menu.getCheckedKeys();
       // 鍗婇�変腑鐨勮彍鍗曡妭鐐�
-      let halfCheckedKeys = this.$refs.menu.getCheckedKeys();
+      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
       checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
       return checkedKeys;
     },
     // 鎵�鏈夐儴闂ㄨ妭鐐规暟鎹�
     getDeptAllCheckedKeys() {
       // 鐩墠琚�変腑鐨勯儴闂ㄨ妭鐐�
-      let checkedKeys = this.$refs.dept.getHalfCheckedKeys();
+      let checkedKeys = this.$refs.dept.getCheckedKeys();
       // 鍗婇�変腑鐨勯儴闂ㄨ妭鐐�
-      let halfCheckedKeys = this.$refs.dept.getCheckedKeys();
+      let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys();
       checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
       return checkedKeys;
     },
     /** 鏍规嵁瑙掕壊ID鏌ヨ鑿滃崟鏍戠粨鏋� */
     getRoleMenuTreeselect(roleId) {
-      roleMenuTreeselect(roleId).then(response => {
-        this.getMenuTreeselect();
-        this.$refs.menu.setCheckedKeys(response.data);
+      return roleMenuTreeselect(roleId).then(response => {
+        this.menuOptions = response.menus;
+        return response;
       });
     },
     /** 鏍规嵁瑙掕壊ID鏌ヨ閮ㄩ棬鏍戠粨鏋� */
     getRoleDeptTreeselect(roleId) {
-      roleDeptTreeselect(roleId).then(response => {
-        this.getDeptTreeselect();
-        this.$refs.dept.setCheckedKeys(response.data);
+      return roleDeptTreeselect(roleId).then(response => {
+        this.deptOptions = response.depts;
+        return response;
       });
     },
     // 瑙掕壊鐘舵�佷慨鏀�
@@ -364,9 +428,13 @@
     },
     // 琛ㄥ崟閲嶇疆
     reset() {
-      if (this.$refs.tree != undefined) {
-        this.$refs.tree.setCheckedKeys([]);
+      if (this.$refs.menu != undefined) {
+        this.$refs.menu.setCheckedKeys([]);
       }
+      this.menuExpand = false,
+      this.menuNodeAll = false,
+      this.deptExpand = true,
+      this.deptNodeAll = false,
       this.form = {
         roleId: undefined,
         roleName: undefined,
@@ -375,6 +443,8 @@
         status: "0",
         menuIds: [],
         deptIds: [],
+        menuCheckStrictly: true,
+        deptCheckStrictly: true,
         remark: undefined
       };
       this.resetForm("form");
@@ -383,6 +453,48 @@
     handleQuery() {
       this.queryParams.pageNum = 1;
       this.getList();
+    },
+    /** 閲嶇疆鎸夐挳鎿嶄綔 */
+    resetQuery() {
+      this.dateRange = [];
+      this.resetForm("queryForm");
+      this.handleQuery();
+    },
+    // 澶氶�夋閫変腑鏁版嵁
+    handleSelectionChange(selection) {
+      this.ids = selection.map(item => item.roleId)
+      this.single = selection.length!=1
+      this.multiple = !selection.length
+    },
+    // 鏍戞潈闄愶紙灞曞紑/鎶樺彔锛�
+    handleCheckedTreeExpand(value, type) {
+      if (type == 'menu') {
+        let treeList = this.menuOptions;
+        for (let i = 0; i < treeList.length; i++) {
+          this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
+        }
+      } else if (type == 'dept') {
+        let treeList = this.deptOptions;
+        for (let i = 0; i < treeList.length; i++) {
+          this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value;
+        }
+      }
+    },
+    // 鏍戞潈闄愶紙鍏ㄩ��/鍏ㄤ笉閫夛級
+    handleCheckedTreeNodeAll(value, type) {
+      if (type == 'menu') {
+        this.$refs.menu.setCheckedNodes(value ? this.menuOptions: []);
+      } else if (type == 'dept') {
+        this.$refs.dept.setCheckedNodes(value ? this.deptOptions: []);
+      }
+    },
+    // 鏍戞潈闄愶紙鐖跺瓙鑱斿姩锛�
+    handleCheckedTreeConnect(value, type) {
+      if (type == 'menu') {
+        this.form.menuCheckStrictly = value ? true: false;
+      } else if (type == 'dept') {
+        this.form.deptCheckStrictly = value ? true: false;
+      }
     },
     /** 鏂板鎸夐挳鎿嶄綔 */
     handleAdd() {
@@ -394,24 +506,36 @@
     /** 淇敼鎸夐挳鎿嶄綔 */
     handleUpdate(row) {
       this.reset();
-      this.$nextTick(() => {
-        this.getRoleMenuTreeselect(row.roleId);
-      });
-      getRole(row.roleId).then(response => {
+      const roleId = row.roleId || this.ids
+      const roleMenu = this.getRoleMenuTreeselect(roleId);
+      getRole(roleId).then(response => {
         this.form = response.data;
         this.open = true;
+        this.$nextTick(() => {
+          roleMenu.then(res => {
+            let checkedKeys = res.checkedKeys
+            checkedKeys.forEach((v) => {
+                this.$nextTick(()=>{
+                    this.$refs.menu.setChecked(v, true ,false);
+                })
+            })
+          });
+        });
         this.title = "淇敼瑙掕壊";
       });
     },
     /** 鍒嗛厤鏁版嵁鏉冮檺鎿嶄綔 */
     handleDataScope(row) {
       this.reset();
-      this.$nextTick(() => {
-        this.getRoleDeptTreeselect(row.roleId);
-      });
+      const roleDeptTreeselect = this.getRoleDeptTreeselect(row.roleId);
       getRole(row.roleId).then(response => {
         this.form = response.data;
         this.openDataScope = true;
+        this.$nextTick(() => {
+          roleDeptTreeselect.then(res => {
+            this.$refs.dept.setCheckedKeys(res.checkedKeys);
+          });
+        });
         this.title = "鍒嗛厤鏁版嵁鏉冮檺";
       });
     },
@@ -422,24 +546,16 @@
           if (this.form.roleId != undefined) {
             this.form.menuIds = this.getMenuAllCheckedKeys();
             updateRole(this.form).then(response => {
-              if (response.code === 200) {
-                this.msgSuccess("淇敼鎴愬姛");
-                this.open = false;
-                this.getList();
-              } else {
-                this.msgError(response.msg);
-              }
+              this.msgSuccess("淇敼鎴愬姛");
+              this.open = false;
+              this.getList();
             });
           } else {
             this.form.menuIds = this.getMenuAllCheckedKeys();
             addRole(this.form).then(response => {
-              if (response.code === 200) {
-                this.msgSuccess("鏂板鎴愬姛");
-                this.open = false;
-                this.getList();
-              } else {
-                this.msgError(response.msg);
-              }
+              this.msgSuccess("鏂板鎴愬姛");
+              this.open = false;
+              this.getList();
             });
           }
         }
@@ -450,28 +566,38 @@
       if (this.form.roleId != undefined) {
         this.form.deptIds = this.getDeptAllCheckedKeys();
         dataScope(this.form).then(response => {
-          if (response.code === 200) {
-            this.msgSuccess("淇敼鎴愬姛");
-            this.openDataScope = false;
-            this.getList();
-          } else {
-            this.msgError(response.msg);
-          }
+          this.msgSuccess("淇敼鎴愬姛");
+          this.openDataScope = false;
+          this.getList();
         });
       }
     },
     /** 鍒犻櫎鎸夐挳鎿嶄綔 */
     handleDelete(row) {
-      this.$confirm('鏄惁纭鍒犻櫎鍚嶇О涓�"' + row.roleName + '"鐨勬暟鎹」?', "璀﹀憡", {
+      const roleIds = row.roleId || this.ids;
+      this.$confirm('鏄惁纭鍒犻櫎瑙掕壊缂栧彿涓�"' + roleIds + '"鐨勬暟鎹」?', "璀﹀憡", {
           confirmButtonText: "纭畾",
           cancelButtonText: "鍙栨秷",
           type: "warning"
         }).then(function() {
-          return delRole(row.roleId);
+          return delRole(roleIds);
         }).then(() => {
           this.getList();
           this.msgSuccess("鍒犻櫎鎴愬姛");
-        }).catch(function() {});
+        })
+    },
+    /** 瀵煎嚭鎸夐挳鎿嶄綔 */
+    handleExport() {
+      const queryParams = this.queryParams;
+      this.$confirm('鏄惁纭瀵煎嚭鎵�鏈夎鑹叉暟鎹」?', "璀﹀憡", {
+          confirmButtonText: "纭畾",
+          cancelButtonText: "鍙栨秷",
+          type: "warning"
+        }).then(function() {
+          return exportRole(queryParams);
+        }).then(response => {
+          this.download(response.msg);
+        })
     }
   }
 };

--
Gitblit v1.9.3