index.vue 10.7 KB
<template>
  <div class="system-container">
    <div class="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
        ref="roleTable"
        :tableParams="tableParams"
        :getListApi="getListApi"
        :searchForm="searchForm"
        :hasNum="false"
      >
        <span
          slot="status"
          slot-scope="scope"
        >
          <span
            class="dot"
            :class="scope.scope.enableStatus ? USERSTATUS[scope.scope.enableStatus].split(',')[1] : ''"
          ></span>
          <span class="statusText">{{ scope.scope.enableStatus ? USERSTATUS[scope.scope.enableStatus].split(',')[0] : '--'}}</span>
        </span>
        <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
        class="role"
        :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(roleForm.roleVisibility)"
                v-model="roleForm.roleVisibility"
              >
                <el-radio
                  v-for="(child,childIndex) in visibilityList"
                  :key="childIndex"
                  :label="child.code"
                >{{child.value}}</el-radio>
              </el-radio-group>
              <el-select
                v-if="roleForm.roleVisibility == 2"
                v-model="roleForm.roleTenants"
                placeholder="请选择"
                multiple
                filterable
              >
                <el-option
                  v-for="i in tenantList"
                  :key="i.tenantId"
                  :label="i.departmentName"
                  :value="i.tenantId"
                >
                </el-option>
              </el-select>
            </div>
            <el-tree
              slot="menu-list"
              :data="treedata"
              show-checkbox
              default-expand-all
              node-key="id"
              ref="tree"
              highlight-current
            >
            </el-tree>
          </rule-form>
        </slot>
      </basic-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, roleInfo, getTenantList, addRole, editRole, deleteRole, getMenuTree, getVisibility } from 'services/role';
export default {
  data() {
    var checkMenu = (rule, value, callBack) => {
      if (value && value.length == 0) {
        return callBack(new Error('请选择菜单列表'))
      } else {
        return callBack()
      }
    }
    return {
      USERSTATUS: this.$Constants.USER_STATUS,
      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: 'remark'
          }, {
            label: '状态',
            prop: 'enableStatus',
            addHtml: 'status'
          }, {
            label: '创建时间',
            prop: 'createdDate'
          }
        ],
        tableOption: {
          width: 300,
          btnList: [
            {
              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', maxlength: 50 },
        { label: '备注:', prop: 'remark', type: 'input', inputText: 'textarea', maxlength: 200, showWordLimit: true },
        { label: '状态:', prop: 'enableStatus', type: 'switch' },
        { label: '租户可见度:', prop: 'roleVisibility', type: 'customize', slotName: 'visible-type' },
        { label: '数据权限:', prop: 'rolePermissionType', type: 'radio', children: this.$Constants.ROLE_AUTH },
        { label: '菜单权限:', prop: 'menuIdList', type: 'customize', slotName: 'menu-list' }
      ],
      roleRules: {
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' },
        { validator: this.$Common.checkName, trigger: 'blur' },
        { min: 5, max: 50, message: '长度在 5 到 50 个字符', trigger: 'blur' }],
        enableStatus: [{ required: true }],
        roleVisibility: [{ required: true, message: '请选择租户可见度' }],
        rolePermissionType: [{ required: true, message: '请选择数据权限' }],
        menuIdList: [{ required: true, validator: checkMenu, trigger: 'blur' }]
      },
      treedata: [],
      tenantList: [],
      visibilityList: []
    }
  },
  mounted() {
    this.getTenantList()
    this.getVisibility()
  },
  methods: {
    // 重置搜索条件
    refresh() {
      this.searchForm = {}
    },
    // 确定
    handleConfirm() {
      this.getCheckedKeys()
      console.log(this.roleForm)
      this.$refs.roleForm.$children[0].validate((valid) => {
        if (valid) {
          let api = addRole
          let params = {
            remark: this.roleForm.remark,
            roleName: this.roleForm.roleName,
            menuIdList: this.roleForm.menuIdList
          }
          params.enableStatus = this.roleForm.enableStatus ? 1 : 2
          params.roleVisibility = Number(this.roleForm.roleVisibility)
          params.rolePermissionType = Number(this.roleForm.rolePermissionType)
          if (params.roleVisibility == 2) params.roleTenants = this.roleForm.roleTenants.toString()
          if (this.roleForm.id) {
            api = editRole
            params.roleId = this.roleForm.id
          }
          api(params).then(res => {
            if (res.code == '200') {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.handleClose()
              this.$refs.roleTable.refreshTable()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      })
    },
    // 取消
    handleClose() {
      if (!this.roleVisible && this.$refs.roleForm) {
        this.$refs.roleForm.$children[0].resetFields()
      }
      this.roleVisible = !this.roleVisible
      if (!this.roleVisible) this.$refs.tree.setCheckedKeys([])
    },
    // 按钮事件
    btnClick(pType, scope) {
      console.log(pType, scope)
      switch (pType) {
        case 'create':
          this.roleDialogTitle = '新增角色'
          this.roleForm = {
            enableStatus: true,
            menuIdList: []
          }
          this.getMenuTree()
          this.handleClose()
          break
        case 'edit':
          this.roleDialogTitle = '编辑角色'
          this.editEvent(scope)
          break
        case 'delete':
          this.deleteEvent(scope.id)
          break
      }
    },
    // 编辑触发事件
    editEvent(row) {
      const that = this
      roleInfo({ id: row.id }).then(res => {
        this.roleForm = JSON.parse(JSON.stringify(res.data))
        this.roleForm.enableStatus = res.data.enableStatus == 1 ? true : false
        this.roleForm.roleVisibility = res.data.roleVisibility + ''
        this.roleForm.rolePermissionType = res.data.rolePermissionType + ''
        if (res.data.roleTenants) this.roleForm.roleTenants = res.data.roleTenants.split(',').map(Number)
        this.getMenuTree({ roleId: row.id })
        this.handleClose(res.data)
      })
    },
    // 删除触发事件
    deleteEvent(id) {
      const that = this
      const tipData = {
        that: that,
        tipText: '是否删除当前记录',
        tipTitle: '提示',
        type: 'error',
        callBack: () => {
          deleteRole({ id: id }).then(res => {
            if (res.code == '200') {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.$refs.roleTable.refreshTable()
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      }
      this.$Common.confirmDialog(tipData)
    },
    // 获取菜单权限列表
    getMenuTree(params) {
      getMenuTree(params).then(res => {
        this.treedata = res.data.menuTree
        setTimeout(() => {
          this.$refs.tree.setCheckedKeys(res.data.checkedIds)
        }, 1000 / 60)
      })
    },
    // 获取选中菜单数组
    getCheckedKeys() {
      this.roleForm.menuIdList = this.$refs.tree.getCheckedKeys()
    },
    // 获取角色可见度可选项
    getVisibility() {
      getVisibility().then(res => {
        console.log(res.data)
        this.visibilityList = res.data
      })
    },
    // 获取租户列表
    getTenantList() {
      getTenantList().then(res => {
        this.tenantList = res.data
      })
    },
    radioChange(e) {
      this.$set(this.roleForm, 'roleVisibility', e)
    }
  }
}
</script>
<style lang="scss">
.role .el-tree {
  margin-top: 7px;
}
</style>