index.vue 9.19 KB
<template>
  <div class="system-container">
    <div class="user-container">
      <search-form
        class="searchTop border-shadow"
        :formModel="searchForm"
        :formList="roleFormList"
      >
        <slot name="form-slot">
          <el-button
            type="primary"
            :size="btn_size"
            icon="el-icon-search"
            :id="tableParams.btnId"
          >搜索</el-button>
          <el-button
            type="primary"
            :size="btn_size"
            icon="el-icon-refresh"
            @click="refresh"
          >重置</el-button>
        </slot>
      </search-form>
      <div class="table-btn">
        <el-button
          type="primary"
          :size="btn_size"
          icon="el-icon-plus"
          @click="btnClick('create')"
        >新增</el-button>
      </div>
      <basic-table
        :tableParams="tableParams"
        :getListApi="getListApi"
        :searchForm="searchForm"
        :hasNum="false"
      >
        <span
          slot="table-button"
          slot-scope="scope"
        >
          <el-button
            v-for="(j, idx) in tableParams.tableOption.btnList"
            :key="idx"
            :type="j.type"
            size="mini"
            plain
            @click="btnClick(j.pType, scope.scope)"
          >{{j.name}}</el-button>
        </span>
      </basic-table>
      <basic-dialog
        :title="roleDialogTitle"
        :isVisible="roleVisible"
        @confirmDialog="handleConfirm"
        @closeDialog="handleClose"
      >
        <slot>
          <rule-form
            :ruleForm="roleForm"
            :rules="roleRules"
            :ruleData="roleData"
            :labelWidth="'120px'"
            ref='roleForm'
          >
            <div slot="visible-type">
              <el-radio-group
                @change="radioChange($event)"
                v-model="roleForm.visibleType"
              >
                <el-radio
                  v-for="(child,childIndex) in $Constants.ROLE_VISIBLE"
                  :key="childIndex"
                  :label="childIndex"
                >{{child}}</el-radio>
              </el-radio-group>
              <el-select
                v-if="roleForm.visibleType == '2'"
                v-model="roleForm.visibleTenant"
                placeholder="请选择"
              >
                <el-option
                  v-for="i in tenantList"
                  :key="i.tenantId"
                  :label="i.departmentName"
                  :value="i.tenantId"
                >
                </el-option>
              </el-select>
            </div>
            <div slot="menu-list">
              <el-tree
                :data="data"
                show-checkbox
                default-expand-all
                node-key="id"
                ref="tree"
                highlight-current
                @getCheckedKeys="getCheckedKeys"
                @setCheckedNodes="setCheckedNodes"
              >
              </el-tree>
            </div>
          </rule-form>
        </slot>
      </basic-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, roleInfo, getTenantList, addRole, editRole, deleteRole } from 'services/role';

export default {
  data() {
    var checkMenu = (rule, value, callBack) => {
      if (value && value.length == 0) {
        return callBack(new Error('请选择菜单列表'))
      } else {
        return callBack()
      }
    }
    return {
      searchForm: {},
      roleFormList: [
        {
          type: 'input',
          inputType: 'text',
          prop: 'roleName',
          placeholder: '请输入角色名称'
        }
      ],
      tableParams: {
        mutiSelectName: 'role',
        mutiSelectUniqueKey: 'id',
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'list',
        tableColumns: [
          {
            label: '角色名称',
            prop: 'roleName'
          }, {
            label: '创建时间',
            prop: 'createDate'
          }, {
            label: '备注',
            prop: 'remark'
          }
        ],
        tableOption: {
          width: 300,
          btnList: [
            {
              type: 'primary',
              name: '新增',
              pType: 'create'
            },
            {
              type: 'primary',
              name: '编辑',
              pType: 'edit'
            },
            {
              type: 'danger',
              name: '删除',
              pType: 'delete'
            }
          ]
        }
      },
      getListApi: getRoleList,
      // 弹窗数据
      roleDialogTitle: '新增角色',
      roleVisible: false,
      roleForm: {
        enableStatus: true,
        menuIdList: []
      },
      roleData: [
        { label: '角色名称:', prop: 'roleName', type: 'input' },
        { label: '备注:', prop: 'remark', type: 'input' },
        { label: '状态:', prop: 'enableStatus', type: 'switch' },
        { label: '租户可见度:', prop: 'visibleType', type: 'customize', slotName: 'visible-type' },
        // { label: '租户可见度:', prop: 'visibleType', type: 'radio', hasSelect: true, children: this.$Constants.ROLE_VISIBLE },
        { label: '数据权限:', prop: 'permissionType', type: 'radio', children: this.$Constants.ROLE_AUTH },
        // [{ id: 1, label: '本人' }, { id: 2, label: '本人及下属' }, { id: 3, label: '本部门' }, { id: 4, label: '本部门&下属' }, { id: 5, label: '全部' }]
        { label: '菜单权限:', prop: 'menuIdList', type: 'customize', slotName: 'menu-list' }
      ],
      roleRules: {
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        enableStatus: [{ required: true }],
        visibleType: [{ required: true, message: '请选择租户可见度' }],
        permissionType: [{ required: true, message: '请选择数据权限' }],
        menuIdList: [{ required: true, validator: checkMenu, trigger: 'blur' }]
      },
      data: [{
        id: 1,
        label: '一级 1',
        children: [{
          id: 4,
          label: '二级 1-1',
          children: [{
            id: 9,
            label: '三级 1-1-1'
          }, {
            id: 10,
            label: '三级 1-1-2'
          }]
        }]
      }, {
        id: 2,
        label: '一级 2',
        children: [{
          id: 5,
          label: '二级 2-1'
        }, {
          id: 6,
          label: '二级 2-2'
        }]
      }, {
        id: 3,
        label: '一级 3',
        children: [{
          id: 7,
          label: '二级 3-1'
        }, {
          id: 8,
          label: '二级 3-2'
        }]
      }],
      tenantList: []
    }
  },
  mounted() {
    this.getTenantList()
  },
  methods: {
    // 重置搜索条件
    refresh() {
      this.searchForm = {}
    },
    // 确定
    handleConfirm() {
      this.getCheckedKeys()
      console.log(this.roleForm)
      this.$refs.roleForm.$children[0].validate((valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.roleForm))
          params.enableStatus = this.roleForm.enableStatus ? 1 : 2
          let api = addRole
          if (params.id) api = editRole
          api(params).then(res => {
            this.handleClose()
          })
        } else {
          console.log('error')
        }
      })
    },
    // 取消
    handleClose() {
      if (!this.roleVisible && this.$refs.roleForm) {
        this.$refs.roleForm.$children[0].resetFields()
      }
      this.roleVisible = !this.roleVisible
    },
    // 按钮事件
    btnClick(pType, scope) {
      console.log(pType, scope)
      switch (pType) {
        case 'create':
          this.roleDialogTitle = '新增角色'
          this.roleForm = {
            enableStatus: true,
            menuIdList: []
          }
          this.setCheckedNodes()
          this.handleClose()
          break
        case 'edit':
          this.roleDialogTitle = '编辑角色'
          this.editEvent(scope)
          break
        case 'delete':
          this.deleteEvent(scope.id)
          break
      }
    },
    // 编辑触发事件
    editEvent(row) {
      roleInfo({ id: row.id }).then(res => {
        this.roleForm = JSON.parse(JSON.stringify(res.data))
        this.roleForm.enableStatus = res.data.enableStatus == 1 ? true : false

        this.handleClose(res.data)
      })
    },
    // 删除触发事件
    deleteEvent(id) {
      const that = this
      const tipData = {
        that: that,
        tipText: '是否删除当前记录',
        tipTitle: '提示',
        type: 'error',
        callBack: () => {
          deleteRole({ id: id }).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        }
      }
      this.$Common.confirmDialog(tipData)
    },
    // 获取选中菜单数组
    getCheckedKeys() {
      this.roleForm.menuIdList = this.$refs.tree.getCheckedKeys()
      console.log(this.$refs.tree.getCheckedKeys())
    },
    setCheckedNodes() {
      this.$refs.tree.setCheckedNodes([])
    },
    // 获取租户列表
    getTenantList() {
      getTenantList().then(res => {
        this.tenantList = res.data
      })
    },
    radioChange(e) {
      console.log(e)
    }
  }
}
</script>