按钮级别的新版本,多级别的组织架构
loulijun2021
2023-08-23 6e940f67eabe8ae168124dafac6b7430d17c2432
src/views/basicSettings/roleList.vue
@@ -1,709 +1,446 @@
<template>
  <div>
    <div class="body" :style="{height:mainHeight+'px'}">
      <div class="bodyTopButtonGroup" style="justify-content: space-between">
        <el-button v-waves type="primary" icon="el-icon-circle-plus-outline" @click="add('add')">新增</el-button>
      </div>
      <div
        class="elTableDiv"
      >
        <div style="width: 300px;background:#fff">
          <div style="margin: 20px 10px 0 10px;display: flex;justify-content: space-between;">
            <div style="display: flex;">
              <div
                style="width: 5px;height: 100%;border-radius: 5px;"
                :style="{background:$store.state.settings.theme}"
              />
              <div style="margin-left: 8px;">角色名称</div>
            </div>
            <div style="margin-right:10px">
              <i class="el-icon-plus" style="cursor: pointer;color: #999" @click="rolePlusClick" />
            </div>
          </div>
          <el-tree
            ref="treeLeftRef"
            style="padding: 10px;overflow: auto"
            :style="{height:(tableHeight-20)+'px'}"
            :data="treeLeft"
            node-key="code"
            highlight-current
            :props="defaultPropsLeft"
            :default-expand-all="true"
            :expand-on-click-node="false"
            draggable
            :allow-drop="treeLeftAllowDrop"
            @node-click="treeLeftNodeClick"
          >
            <span slot-scope="{ node, data }" class="custom-tree-node">
              <span v-if="!data.isEdit">{{ node.label }}</span>
              <input
                v-if="data.isEdit"
                :ref="data.code"
                v-model="data.name"
                type="text"
                @click.stop
                @blur="roleSave(data)"
                @keyup.enter="roleSave(data)"
              >
              <span v-if="!data.isEdit&&data.identifying!=='0'&&data.identifying!=='1'">
                <i
                  v-if="data.code!=='000'"
                  class="el-icon-edit"
                  style="margin-right:10px;color: #999"
                  @click.stop="val=>roleEditClick(val,node,data)"
                />
                <i
                  v-if="data.code!=='000'"
                  class="el-icon-delete"
                  style="margin-right: 4px;color: #999"
                  @click.stop="val=>roleDeleteClick(val,node,data)"
                />
              </span>
            </span>
          </el-tree>
        </div>
        <div
          style="margin-left: 10px;width:100%;position: relative"
      <div class="bodyTopFormGroup">
        <el-form
          ref="form"
          :model="form"
          label-width="100px"
          inline
          style="display: flex;"
        >
          <el-button
            style="position: absolute;right: 15px;z-index:1;top:3px"
            type="primary"
            @click="saveClick"
          >保 存
          </el-button>
          <el-tabs v-model="activeName" type="border-card">
            <el-tab-pane label="PC端" name="PC">
              <div style="display: flex;">
                <div
                  ref="scrollLeft"
                  style="border-right: 1px solid rgba(0,0,0,0.2);width: 200px;
                  overflow-y: auto;overflow-x:hidden;"
                  :style="{height:(tableHeight-40)+'px'}"
                  @scroll="scrollLeftScroll()"
                  @mouseover="isScroll='left'"
                >
                  <div style=" ">
                    <i class="el-icon-s-operation" :style="{color:$store.state.settings.theme}" /> 菜单权限
                  </div>
                  <el-tree
                    ref="treeCenterRef"
                    style="padding-top: 10px;width: 199px;"
                    :data="treeCenter"
                    highlight-current
                    :props="defaultPropsCenter"
                    show-checkbox
                    node-key="menucode"
                    :default-expand-all="true"
                    @check-change="handleTreeCenterCheckChange"
                    @check="handleTreeCenterCheck"
                    @node-click="getLeftTreeNode"
                  />
                </div>
                <div
                  ref="scrollCenter"
                  style="border-right: 1px solid rgba(0,0,0,0.2);width: 660px;padding-left: 10px;overflow-y: auto"
                  :style="{height:(tableHeight-40)+'px'}"
                  @scroll="scrollCenterScroll()"
                  @mouseover="isScroll='center'"
                >
                  <div style=" ">
                    <i class="el-icon-s-operation" :style="{color:$store.state.settings.theme}" /> 按钮权限
                  </div>
                  <el-checkbox
                    v-model="buttonIsChecked"
                    style="height: 26px;display:flex;align-items: center;margin-top:10px;width:200px"
                    @change="val=>handleCheckAllChange(val,'button')"
                  >全选
                  </el-checkbox>
                  <div v-if="treeCenter.length>0">
                    <div v-for="(item,index) in treeCenter[0].children" :key="item.menucode">
                      <div
                        v-if="!item.buttoncodelist"
                        style="margin-left: 40px;height:26px;display: flex;"
                      />
                      <el-checkbox-group
                        v-for="it in item.children"
                        v-if="item.expanded"
                        :key="it.menucode"
                        v-model="it.buttonChecked"
                        style="margin-left: 40px;height:26px;display: flex;"
                        @change="val=>handleCheckedButtonChange(val,index,it.menucode,it.buttonChecked)"
                      >
                        <el-checkbox
                          v-for="i in it.buttoncodelist"
                          :key="i"
                          :label="i"
                          style="display:flex;align-items: center;"
                        />
                      </el-checkbox-group>
                    </div>
                  </div>
                </div>
                <div
                  ref="scrollRight"
                  style="width: 360px;padding-left: 10px;overflow-y: auto"
                  :style="{height:(tableHeight-40)+'px',}"
                  @scroll="scrollRightScroll()"
                  @mouseover="isScroll='right'"
                >
                  <div style=" ">
                    <i class="el-icon-s-operation" :style="{color:$store.state.settings.theme}" /> 数据权限
                  </div>
                  <el-checkbox
                    v-model="dataIsChecked"
                    style="height: 26px;display:flex;align-items: center;margin-top:10px;width:200px"
                    @change="val=>handleCheckAllChange(val,'data')"
                  >全选
                  </el-checkbox>
                  <div v-if="treeCenter.length>0">
                    <div v-for="item in treeCenter[0].children" :key="item.menucode">
                      <div
                        v-if="!item.datacodelist"
                        style="margin-left: 40px;height:26px;display: flex;"
                      />
                      <el-radio-group
                        v-for="it in item.children"
                        v-if="item.expanded"
                        :key="it.menucode"
                        v-model="it.dataChecked"
                        style="margin-left: 40px;height:26px;display: flex"
                        @change="val=>handleCheckedDataChange(val,it.menucode)"
                      >
                        <el-radio
                          v-for="i in it.datacodelist"
                          :key="i"
                          style="display:flex;align-items: center"
                          :label="i"
                        />
                      </el-radio-group>
                    </div>
                  </div>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="APP端" name="APP">大大</el-tab-pane>
            <el-tab-pane label="工控端" name="工控端">工控端</el-tab-pane>
          </el-tabs>
        </div>
          <div class="elForm">
            <el-form-item label="角色编码" style=" display: flex;">
              <el-input v-model="form.storg_code" placeholder="请输入" style="width: 200px" />
            </el-form-item>
            <el-form-item label="角色名称" style=" display: flex;">
              <el-input v-model="form.storg_name" placeholder="请输入" style="width: 200px" />
            </el-form-item>
          </div>
          <div
            class="bodySearchReset"
            :style="{marginLeft:$store.state.app.sidebar.opened? $store.state.settings.menuIsHorizontal?'15%':'3%':'10%'}"
          >
            <el-button v-waves type="primary" icon="el-icon-search" @click="getRoleData">查询</el-button>
            <el-button v-waves type="info" icon="el-icon-refresh" @click="reset">重置</el-button>
          </div>
        </el-form>
      </div>
      <div class="elTableDiv">
        <el-table
          ref="tableDataRef"
          class="tableFixed"
          :data="tableData"
          :height="tableHeight+'px'"
          border
          row-class-name="custom-row"
          :style="{width: 100+'%',height:tableHeight+'px',}"
          highlight-current-row
          :header-cell-style="this.$headerCellStyle"
          :cell-style="this.$cellStyle"
          @sort-change="sortChange"
        >
          <el-table-column
            prop="rowNum"
            width="120"
            fixed
            label="序号"
          />
          <el-table-column
            prop="rolecode"
            label="角色编码"
            sortable="custom"
          />
          <el-table-column
            prop="rolename"
            label="角色名称"
            sortable="custom"
          />
          <el-table-column
            prop="status"
            label="状态"
            sortable="custom"
          >
            <template slot-scope="{row}">
              <el-tag v-if="row.status==='Y'" size="small" type="success">正常</el-tag>
              <el-tag v-if="row.status==='N'" size="small" type="danger">停用</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="dataname"
            label="权限范围"
            sortable="custom"
          />
          <el-table-column
            prop="description"
            label="备注"
            sortable="custom"
          />
          <el-table-column
            prop="lm_user"
            label="创建人员"
            sortable="custom"
          />
          <el-table-column
            prop="lm_date"
            label="创建时间"
            width="160"
            sortable="custom"
          />
          <el-table-column
            label="操作"
            width="160"
            fixed="right"
          >
            <template slot-scope="{row}">
              <div class="operationClass">
                <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                  <i
                    class="el-icon-edit-outline"
                    :style="{color:$store.state.settings.theme}"
                    @click="edit('edit',row)"
                  />
                </el-tooltip>
                <el-tooltip v-del-tab-index class="item" effect="dark" content="删除" placement="top">
                  <i
                    class="el-icon-delete"
                    :style="{color:$store.state.settings.theme}"
                    @click="del(row)"
                  />
                </el-tooltip>
                <el-dropdown placement="bottom" @command="command=>handleCommand(command,row)">
                  <div
                    class="el-dropdown-link"
                    style="display: flex;align-content: center;
                    justify-content: center;
                    cursor: pointer;"
                    :style="{color:$store.state.settings.theme}"
                  >
                    <div>更多</div>
                    <div>
                      <i class="el-icon-arrow-down el-icon--right" />
                    </div>
                  </div>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item icon="el-icon-circle-check" command="menuPower">菜单权限</el-dropdown-item>
                    <el-dropdown-item icon="el-icon-user" command="divideUser">分配用户</el-dropdown-item>
                    <el-dropdown-item icon="el-icon-download" command="downloadMenu">导出菜单</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <pagination
        :total="total"
        :page.sync="form.page"
        :limit.sync="form.rows"
        align="right"
        layout="total,prev, pager, next,sizes,jumper"
        popper-class="select_bottom"
        @pagination="getRoleData"
      />
    </div>
    <el-dialog
      v-el-drag-dialog
      :title="operation==='add'?'新增':'编辑'"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
      top="15vh"
      @closed="handleClose"
      @close="handleClose"
    >
      <el-form ref="dialogForm" inline :rules="dialogFormRules" :model="dialogForm" label-width="80px">
        <!--        <el-form-item label="角色编码" prop="rolecode">-->
        <!--          <el-input v-model="dialogForm.rolecode" :disabled="operation!=='add'" style="width: 200px" />-->
        <!--        </el-form-item>-->
        <el-form-item label="角色名称" prop="rolename">
          <el-input v-model="dialogForm.rolename" style="width: 200px" />
        </el-form-item>
        <el-form-item required label="数据范围">
          <el-select
            v-model="dialogForm.datacode"
            style="width:200px"
            placeholder="请选择"
            :popper-append-to-body="false"
          >
            <el-option
              v-for="item in datacodeArr"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item required label="状态">
          <el-radio-group v-model="dialogForm.status" style="width: 200px">
            <el-radio label="Y">正常</el-radio>
            <el-radio label="N">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="dialogForm.datacode==='CUSTOM'" label="数据权限" style="margin: 0">
          <el-checkbox-group v-model="checkboxGroupSelected" @change="checkboxGroupChange">
            <el-checkbox v-for="item in checkboxGroup" :key="item" :label="item">
              {{ item }}
              <el-tooltip class="item" effect="dark" content="勾选父节点是否同时选中子节点" placement="top">
                <i v-if="item==='父子联动'" class="el-icon-question" />
              </el-tooltip>
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-card
          v-if="dialogForm.datacode==='CUSTOM'"
          class="box-card"
          shadow="never"
          style="margin-bottom: 30px;width: 78%;margin-left: 12%;"
        >
          <el-tree
            ref="tree"
            :key="checkboxGroupSelected.toString()"
            :data="treeData"
            show-checkbox
            :check-strictly="!checkboxGroupSelected.includes('父子联动')"
            :default-expand-all="checkboxGroupSelected.includes('展开/折叠')"
            node-key="torg_code"
            :props="defaultProps"
          />
        </el-card>
        <el-form-item label="备注">
          <el-input v-model="dialogForm.description" type="textarea" style="width:500px" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footerButton">
          <el-button v-waves @click="dialogVisibleCancel">取 消</el-button>
          <el-button v-waves type="primary" @click="dialogVisibleConfirm">确 定</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import {
  DataPermissions, RoleAddUpdateSava, RoleDeleteSava,
  RolePermissionSava,
  RolePermissionSearchRole,
  RolePermissionSearchRoleMenu,
  RolePermissionSearchRoleMenuButton
  AddUpdateGroup,
  DeleteGroup, RoleAddUpdateSava,
  RoleData, RoleDeleteSava
} from '@/api/basicSettings'
import { validateCode } from '@/utils/global'
import elDragDialog from '@/directive/el-drag-dialog'
import waves from '@/directive/waves'
import { PrentOrganization } from '@/api/GeneralBasicData'
import arrayToTree from 'array-to-tree'
import { throttle } from '@/utils/debounceAndThrottle'
export default {
  name: 'RoleList',
  directives: { waves },
  name: 'Zzjg',
  components: {
    Pagination
  },
  directives: { elDragDialog, waves },
  data() {
    return {
      mainHeight: 0,
      tableHeight: 0,
      treeLeft: [], // 左侧树
      treeCenter: [], // 中间树
      defaultPropsLeft: {
        children: 'roles',
        label: 'name'
      form: {
        role_code: '',
        role_name: '',
        prop: 'lm_date', // 排序字段
        order: 'desc', // 排序字段
        page: 1, // 第几页
        rows: 20 // 每页多少条
      },
      defaultPropsCenter: {
      total: 10,
      tableData: [],
      dialogVisible: false,
      dialogForm: {
        rolecode: '', //
        rolename: '', //
        datacode: 'ALL',
        description: '',
        datapermissions: '', // 数据权限
        status: 'Y'// 状态
      },
      datacodeArr: [
        { code: 'ALL', name: '全部' },
        { code: 'CUSTOM', name: '自定义' },
        { code: 'LEVEL', name: '本级' },
        { code: 'PERSON', name: '本人' }
      ],
      checkboxGroupSelected: ['展开/折叠', '父子联动'],
      checkboxGroup: ['展开/折叠', '全选/全不选', '父子联动'],
      operation: '',
      dialogFormRules: {
        rolecode: [
          { required: true, validator: validateCode, trigger: ['blur', 'change'] }
        ],
        rolename: [
          { required: true, message: '请输入组织名称', trigger: ['blur', 'change'] }
        ]
      },
      defaultProps: {
        children: 'children',
        label: 'menuname'
        label: 'torg_name'
      },
      activeName: 'PC',
      buttonIsChecked: false,
      dataIsChecked: false,
      menuCheckedCodeArr: [], // 菜单权限数组选中的
      allButtonData: [], // 所有菜单对应的按钮权限加数据权限
      isScroll: 'left',
      dataKeyValue: []// 数据权限键值对
      treeData: []
    }
  },
  watch: {
    treeLeft: {
      handler(newValue, oldValue) {
        newValue[0].roles.forEach(i => {
          if (i.isEdit) {
            this.$nextTick(() => {
              this.$refs[i.code].focus()
            })
          }
        })
      },
      deep: true
    }
  },
  created() {
    this.getRoleData()
  },
  mounted() {
    window.addEventListener('resize', this.getHeight)
    this.getHeight()
    this.getRolePermissionSearchRole()
    this.getPrentOrganization()
  },
  methods: {
    // 获取角色权限查询角色
    async getRolePermissionSearchRole() {
      const { data: res } = await RolePermissionSearchRole()
      res[0].roles.forEach(i => {
        i.isEdit = false
      })
      this.treeLeft = res
      if (res && res.length > 0) {
        this.$nextTick(() => { // 默认选中第一个
          this.$refs.treeLeftRef.setCurrentKey(res[0].roles[0].code) // 默认选中节点第一个
        })
        await this.getDataPermissions()// 获取数据权限键值对
        await this.getRolePermissionSearchRoleMenuButton(res[0].roles[0].code)// 用于数据回显
        await this.getRolePermissionSearchRoleMenu(res[0].roles[0].code)// 用于渲染页面
      } else {
        this.$notify.error('请先建相对应的角色!')
      }
    async getRoleData() {
      const res = await RoleData(this.form)
      this.tableData = res.data
      this.total = res.count
    },
    async getDataPermissions() {
      const { data: res } = await DataPermissions()
      this.dataKeyValue = res
    },
    // 获取菜单权限对应的按钮
    async getRolePermissionSearchRoleMenuButton(rolecode) {
      const { data: res } = await RolePermissionSearchRoleMenuButton({ rolecode, type: this.activeName })
      this.allButtonData = res.length > 0 ? JSON.parse(res[0].jsondate) : []
    },
    // 获取菜单权限
    async getRolePermissionSearchRoleMenu(rolecode) {
      const { data: res } = await RolePermissionSearchRoleMenu({ rolecode, type: this.activeName })
      // 拿到数组list转换成树形tree
      const result = arrayToTree(res, {
    // 组织架构级联选择器
    async getPrentOrganization() {
      const { data: res } = await PrentOrganization()
      this.treeData = arrayToTree(res, {
        parentProperty: 'parent_id',
        customID: 'menucode',
        customID: 'torg_code',
        childrenProperty: 'children'
      })
      this.treeCenter = [
        { menucode: '-1', menuname: '全部', children: result }
      ]
      this.treeCenter[0].children.forEach(item => {
        if (item.children && item.children.length) {
          item.children.forEach(it => {
            it.buttoncodelist = it.buttoncodelist !== '' && it.buttoncodelist !== null ? it.buttoncodelist.split(',') : []
            if (it.buttoncodelist && it.buttoncodelist.length > 0) {
              it.buttoncodelist = this.$ButtonData.filter(j =>
                it.buttoncodelist.includes(j.buttoncode)
              )
              it.buttoncodelist = it.buttoncodelist.map(j => j.buttonname)
            }
            it.datacodelist = it.datacodelist ? it.datacodelist.split(',') : []
            if (it.datacodelist && it.datacodelist.length > 0) {
              it.datacodelist = this.dataKeyValue.filter(j =>
                it.datacodelist.includes(j.datacode)
              ).map(j => j.dataname)
            }
          })
          item.expanded = true
        }
      })
      this.handleData()// 处理数据回显
    },
    // 处理组合数据
    handleData() {
      this.menuCheckedCodeArr = this.allButtonData.map(i => i.menucode)
    // 排序改变时
    sortChange({ column, prop, order }) {
      if (order === 'descending') {
        order = 'desc'
      } else if (order === 'ascending') {
        order = 'asc'
      } else {
        order = 'desc'
      }
      this.form.order = order
      this.form.prop = prop
      this.getRoleData()
    },
    // 重置
    reset() {
      this.form.storg_code = ''
      this.form.storg_name = ''
      this.getRoleData()
    },
    // 新增按钮
    add(operation) {
      this.operation = operation
      this.dialogVisible = true
      this.dialogForm.OperType = 'Add'
    },
    // 修改按钮
    async edit(operation, row) {
      this.operation = operation
      this.dialogVisible = true
      this.dialogForm.OperType = 'Update'
      this.$nextTick(() => {
        this.$refs.treeCenterRef.setCheckedKeys(this.menuCheckedCodeArr)
      })
      this.treeCenter[0].children.forEach(item => {
        if (item.children && item.children.length) {
          item.children.forEach(it => {
            let temp = this.allButtonData.filter(j => j.menucode === it.menucode).length > 0 ? this.allButtonData.filter(j => j.menucode === it.menucode)[0].buttoncode : []
            temp = temp.length > 0 ? temp.split(',') : []
            it.buttonChecked = [] // 数据回显
            this.$ButtonData.forEach(j => {
              if (temp.includes(j.buttoncode)) {
                it.buttonChecked.push(j.buttonname)
              }
            })
            const datacode = this.allButtonData.filter(j => j.menucode === it.menucode).length > 0 ? this.allButtonData.filter(j => j.menucode === it.menucode)[0].datacode : ''
            it.dataChecked = datacode ? this.dataKeyValue.find(j => j.datacode === datacode).dataname : '' // 数据回显
        this.dialogForm.rolecode = row.rolecode
        this.dialogForm.rolename = row.rolename
        this.dialogForm.datacode = row.datacode
        this.dialogForm.status = row.status
        this.dialogForm.description = row.description
        if (this.dialogForm.datacode === 'CUSTOM') {
          setTimeout(() => {
            this.$refs.tree.setCheckedKeys(row.datapermissionscode.split(','))
          })
        }
      })
      this.handleCheckedButtonChange('', '', '', '', true)
      this.handleCheckedDataChange('', '', true)
      // this.$forceUpdate()
    },
    // 左边滚动条滑动时
    scrollLeftScroll() {
      if (this.isScroll === 'left') {
        this.$refs.scrollCenter.scrollTop = this.$refs.scrollLeft.scrollTop
        this.$refs.scrollRight.scrollTop = this.$refs.scrollLeft.scrollTop
      }
    },
    // 中间边滚动条滑动时
    scrollCenterScroll() {
      if (this.isScroll === 'center') {
        this.$refs.scrollLeft.scrollTop = this.$refs.scrollCenter.scrollTop
        this.$refs.scrollRight.scrollTop = this.$refs.scrollCenter.scrollTop
      }
    },
    // 右侧滚动条滑动时
    scrollRightScroll() {
      if (this.isScroll === 'right') {
        this.$refs.scrollLeft.scrollTop = this.$refs.scrollRight.scrollTop
        this.$refs.scrollCenter.scrollTop = this.$refs.scrollRight.scrollTop
      }
    },
    // 全选改变
    handleCheckAllChange(val, belong) {
      const checkedKeys = this.$refs.treeCenterRef.getCheckedKeys()
      if (val) {
        this.treeCenter[0].children.forEach(item => {
          if (item.children && item.children.length) {
            item.children.forEach(it => {
              if (checkedKeys.includes(it.menucode)) {
                if (belong === 'button') {
                  it.buttonChecked = it.buttoncodelist
                }
                if (belong === 'data') {
                  it.dataChecked = '全部'
                }
              }
            })
          }
        })
      } else {
        this.treeCenter[0].children.forEach(item => {
          if (item.children && item.children.length) {
            item.children.forEach(it => {
              if (checkedKeys.includes(it.menucode)) {
                if (belong === 'button') {
                  it.buttonChecked = []
                }
                if (belong === 'data') {
                  it.dataChecked = ''
                }
              }
            })
          }
        })
      }
    },
    // 单个按钮改变
    handleCheckedButtonChange(oldValue, index, code, newValue, flag) {
      // console.log(oldValue, index, code, newValue, 2)
      // 判断是否包含在内
      if (!flag) {
        if (!this.menuCheckedCodeArr.includes(code) && oldValue.length === 0 && newValue.length === 1) { // 否
          this.menuCheckedCodeArr.push(code)
        } else if (this.menuCheckedCodeArr.includes(code) && oldValue.length === 1 && newValue.length === 0) { // 是
          // 当数据权限未勾选时则过滤掉相对应的菜单
          if (this.treeCenter[0].children[index].children.find(i => i.menucode === code).dataChecked.toString().length < 1) {
            this.menuCheckedCodeArr = this.menuCheckedCodeArr.filter(i => i !== code)
          }
        }
        this.$refs.treeCenterRef.setCheckedKeys(this.menuCheckedCodeArr)
      }
      // 这里是判断全选checkbox 是否选中
      let count = 0
      let childrenLength = 0
      this.treeCenter[0].children.forEach(item => {
        if (item.children && item.children.length) {
          item.children.forEach(it => {
            if (it.buttonChecked && it.buttonChecked.length === it.buttoncodelist.length && it.buttoncodelist.length > 0) {
              count++
            }
            if (it.buttoncodelist.length) {
              childrenLength++
            }
          })
        }
      })
      this.buttonIsChecked = count === childrenLength
      this.$forceUpdate()
    },
    // 获取树形节点node
    getLeftTreeNode(data, Node, VueComponent) {
      this.treeCenter[0].children.forEach(item => {
        if (item.menucode === data.menucode) {
          item.expanded = Node.expanded
        }
      })
      if (data.menucode === '-1') {
        Node.expanded = true // 暂时先写成这个
      }
      this.$forceUpdate()
    },
    // 处理中间树形勾选
    handleTreeCenterCheck(obj, { checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys }) {
      // console.log(obj, checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys, '加多宝')
      if (checkedKeys.length === 0) { // 树形菜单全不选的情况下
        this.buttonIsChecked = false
        this.dataIsChecked = false
        this.menuCheckedCodeArr = []
      } else {
        if (checkedKeys.includes(obj.menucode)) { // 判断是否包含在内   //是
          this.menuCheckedCodeArr.push(obj.menucode)
        } else { // 否
          this.menuCheckedCodeArr = this.menuCheckedCodeArr.filter(i => i !== obj.menucode)
        }
      }
    },
    // 处理中间树形选中值改变
    handleTreeCenterCheckChange(obj, selfChecked, selfChildrenTreeCheck) {
      // console.log(obj, selfChecked, selfChildrenTreeCheck, '王老吉')
      if (!selfChildrenTreeCheck) {
        if (!selfChecked) { // 从选中到不选中
          obj.buttonChecked = []
          obj.dataChecked = ''
          this.menuCheckedCodeArr = this.menuCheckedCodeArr.filter(i => i !== obj.menucode)
        }
        if (selfChecked) { // 从不选中到选中
          obj.buttonCheckedCode = []
          this.treeCenter[0].children.forEach(item => {
            if (item.children && item.children.length) {
              item.children.forEach(it => {
                if (it.menucode === obj.menucode) {
                  this.menuCheckedCodeArr.push(obj.menucode)
                }
              })
            }
          })
        }
        this.$forceUpdate()
      }
    },
    // 单选框按钮改变
    handleCheckedDataChange(value, code, flag) {
      // console.log(value, code)
      if (!flag) {
        this.menuCheckedCodeArr.push(code)
        this.menuCheckedCodeArr = [...new Set(this.menuCheckedCodeArr)]
        this.$refs.treeCenterRef.setCheckedKeys(this.menuCheckedCodeArr)
      }
      // 这里是判断全选radio 是否选中
      let count = 0
      let childrenLength = 0
      this.treeCenter[0].children.forEach(item => {
        if (item.children && item.children.length) {
          item.children.forEach(it => {
            if (it.dataChecked.length > 0 && it.datacodelist.length > 0) {
              count++
            }
            // console.log(it.dataChecked, 234)
            if (it.datacodelist.length > 0) {
              childrenLength++
            }
          })
        }
      })
      // console.log(count, childrenLength, 123)
      this.dataIsChecked = count === childrenLength
      this.$forceUpdate()
    },
    // 角色切换
    async treeLeftNodeClick(obj, Node, VueComponent) {
      if (obj.code !== '000') {
        await this.getRolePermissionSearchRoleMenuButton(obj.code)
        // await this.getRolePermissionSearchRoleMenu(obj.code)
        this.handleData()
      }
    },
    // 角色名称新增
    rolePlusClick() {
      this.treeLeft[0].roles.push({ code: '', name: '', isEdit: true })
    },
    // 角色名称修改
    roleEditClick(self, node, data) {
      // console.log(self, node, data)
      this.treeLeft[0].roles.forEach(i => {
        i.isEdit = false
      })
      data.isEdit = !data.isEdit
    },
    // 角色名称删除
    roleDeleteClick(self, node, data) {
      // console.log(self, node, data)
    // 删除按钮
    async del(row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const D = {
          rolecode: data.code,
          rolename: data.name,
          type: this.activeName
        }
        RoleDeleteSava(D).then(res => {
        RoleDeleteSava({ rolecode: row.rolecode }).then(res => {
          if (res.code === '200') {
            this.$notify.success('角色删除成功!')
            this.$notify.success('删除成功!')
            if (this.form.page > 1 && this.tableData.length === 1) {
              this.form.page--
            }
            this.getRoleData()
          }
          this.getRolePermissionSearchRole()
        })
      }).catch(() => {
        this.$notify.info('已取消删除')
      })
    },
    // 角色名称保存   采用防抖形式
    roleSave: throttle(function(val) {
      val.isEdit = false
      if (val.name.trim().length === 0) {
        this.getRolePermissionSearchRole()
        return this.$notify.info('无效保存!')
    checkboxGroupChange(val) {
      if (val.includes('全选/全不选')) {
        this.$refs.tree.setCheckedKeys([this.treeData[0].torg_code])
      }
      const data = {
        rolecode: val.code,
        rolename: val.name,
        type: val.code === '' ? 'Add' : 'Update'
      }
      console.log(data, '执行了!')
      RoleAddUpdateSava(data).then(res => {
        if (res.code === '200') {
          this.$notify.success(val.code === '' ? '角色新增成功!' : '角色修改成功!')
        }
      })
      this.getRolePermissionSearchRole()
    }, 1000, true),
    // 页面勾选好保存事件
    async saveClick() {
      const menuKeyArr = this.$refs.treeCenterRef.getCheckedKeys()
      const arr = []
      this.treeCenter[0].children.forEach(item => {
        if (item.children && item.children.length) {
          item.children.forEach(it => {
            if (menuKeyArr.includes(it.menucode)) {
              if (it.buttoncodelist.length > 0 && it.buttoncodelist && it.dataChecked && it.dataChecked.toString().length > 0) { // 数据权限已勾选
                it.dataCheckedCode = this.dataKeyValue.find(i => i.dataname === it.dataChecked).datacode
              }
              if (it.buttonChecked && it.buttonChecked.length > 0) { // 已选菜单
                it.buttonCheckedCode = this.$ButtonData.filter(i =>
                  it.buttonChecked.includes(i.buttonname)
                ).map(i => i.buttoncode)
              }
              arr.push({
                menucode: it.menucode,
                datacode: it.dataCheckedCode !== undefined ? it.dataCheckedCode : '',
                buttoncode: it.buttonCheckedCode ? it.buttonCheckedCode.join(',') : ''
              })
    },
    // 对话框关闭事件
    handleClose() {
      this.dialogForm.rolecode = ''
      this.dialogForm.rolename = ''
      this.dialogForm.description = ''
      this.dialogForm.datapermissions = ''
      this.dialogForm.datacode = 'ALL'
      this.dialogForm.status = 'Y'
      this.$refs.dialogForm.clearValidate()
    },
    // 对话框取消
    dialogVisibleCancel() {
      this.dialogVisible = false
    },
    // 对话框确认
    dialogVisibleConfirm() {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          if (this.$refs.tree) {
            this.dialogForm.datapermissions = this.$refs.tree.getCheckedKeys().join(',')
          }
          RoleAddUpdateSava(this.dialogForm).then(res => {
            if (res.code === '200') {
              this.$notify.success(this.operation === 'add' ? '添加成功!' : '修改成功!')
              this.dialogVisible = false
              this.getRoleData()
            } else {
              this.$notify.error(this.operation === 'add' ? '添加失败!' : '修改失败!')
            }
          })
        }
      })
      const rolecode = this.$refs.treeLeftRef.getCurrentKey()
      const res = await RolePermissionSava(JSON.stringify(arr), rolecode, this.activeName)
      if (res.code === '200') {
        const rolename = this.treeLeft[0].roles.find(i => i.code === rolecode).name
        this.$notify.success(rolename + '的角色权限提交成功!')
        await this.getRolePermissionSearchRoleMenuButton(rolecode)
    },
    handleCommand(command, row) {
      console.log(command, row)
      if (command === 'menuPower') {
        this.$router.push('./roleList?powerId=' + row.id)// demo
      }
    },
    // 获取页面高度
    getHeight() {
      this.$nextTick(() => {
        this.mainHeight = window.innerHeight - 85
        this.tableHeight = this.mainHeight - 50
        this.tableHeight = this.mainHeight - 200
        this.$refs.tableDataRef.doLayout()
      })
    },
    // 左侧树形拖动处理
    treeLeftAllowDrop(draggingNode, dropNode, type) {
      // console.log(draggingNode, dropNode, type)
      // 注掉的是同级拖拽
      if (draggingNode.level === dropNode.level) {
        return type === 'prev' || type === 'next'
      } else {
        // 不同级进行处理
        return false
      }
    }
  }
}
</script>
<style scoped lang="scss">
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
::v-deep .el-checkbox__label {
  width: 55px;
}
.el-icon-plus:hover, .el-icon-edit:hover, .el-icon-delete:hover {
  color: #000 !important;
}
</style>