增删改查

299 阅读1分钟
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                v-if="checkPermBtn('add-roles')"
                size="small"
                type="primary"
                @click="addRoleFn"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border :data="tableData">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success" @click="assignPermFn(row.id)">分配权限</el-button>
                  <el-button v-if="checkPermBtn('edit-roles')" size="small" type="primary" @click="editFn(row.id)">编辑</el-button>
                  <el-button v-if="checkPermBtn('del-roles')" size="small" type="danger" @click="delFn(row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev, pager, next"
                :page-size="pagesize"
                :total="100"
                @current-change="currentChangeFn"
              />
            </el-row>
            <!-- 新增角色 -->
            <!-- 1、model属性
              2、rules
              3、prop属性
              4、表单双向绑定 -->
            <el-dialog title="新增角色" :visible="showDialog" width="70%" @click="closeFn">
              <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="formData.name" />
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                  <el-input v-model="formData.description" />
                </el-form-item>
              </el-form>
              <span slot="footer">
                <el-button @click="closeFn">取消</el-button>
                <el-button type="primary" @click="confirmFn">确定</el-button>
              </span>

            </el-dialog>
            <!-- 公司页 -->
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="company.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="company.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="company.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="company.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible="visibleDialog">
      <!--
          show-checkbox: 显示复选框
          check-strictly: 取消父子节点的关联
          node-key:将来获取选中的节点内容时必须设置的一个属性
         -->
      <el-tree
        ref="tree"
        :data="permList"
        :props="{label: 'name'}"
        default-expand-all
        show-checkbox
        node-key="id"
        :check-strictly="true"
      />
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button type="primary" size="small" @click="confirmAssignPermissionFn">确定</el-button>
          <el-button size="small" @click="closePermFn">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, addRoleList, deleteRole, getRoleDetailById, updateRole, getCompany } from '@/api/setting'
import { permission } from '@/api/permission'
import { listToTreeData } from '@/utils/index'
import { assignperm } from '@/api/setting'
export default {
  data() {
    return {
      checkPermList: [],
      visibleDialog: false,
      tableData: [],
      total: 0,
      page: 1,
      pagesize: 10,
      showDialog: false,
      company: {},
      permList: [],
      curId: '',
      formData: {
        name: '', // 角色名称
        description: '' // 角色描述

      },
      rules: {
        name: [{
          required: true, message: '角色名称不能为空!', trigger: 'blur'
        }, {
          min: 2, max: 10, message: '角色名称长度2到50', trigger: 'blur'
        }],
        description: [{
          required: true, message: '角色描述不能为空!', trigger: 'blur'
        }, {
          min: 2, max: 10, message: '角色描述长度2到50', trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    this.getRoleList()
    // 获取公司信息
    const id = this.$store.state.user.userInfo.companyId
    this.company = await getCompany(id)
    // 获取权限列表
    const res = await permission()
    // console.log(151, res)
    this.permList = listToTreeData(res, '0')
  },

  methods: {
    // 确定  分配权限
    async confirmAssignPermissionFn() {
      // 获取选中的权限数据
      this.checkPermList = this.$refs.tree.getCheckedKeys()
      // console.log(171, this.checkPermList)
      // 传入数据 调用接口
      await assignperm({
        id: this.curId,
        permIds: this.checkPermList
      })
      // 关闭弹窗
      this.closePermFn()
    },
    closePermFn() {
      this.visibleDialog = false
      this.$refs.tree.getCheckedKeys([])
    },

    async assignPermFn(id) {
      this.curId = id
      this.visibleDialog = true
      // 回显
      // 获取角色详情
      const res = await getRoleDetailById(id)
      // 实现回显
      this.checkPermList = res.permIds
      // console.log(170, this.checkPermList)
      this.$refs.tree.setCheckedKeys(this.checkPermList)
    },
    // 新增角色
    addRoleFn() {
    // 控制弹窗的显示
      this.showDialog = true
    },
    // 关闭
    closeFn() {
    // 清空数据
      this.formData = {
        name: '',
        description: ''
      }
      // 清空数据和校验规则
      this.$refs.form.resetFields()
      // 关闭弹窗
      this.showDialog = false
    },
    // 确定 新增
    async confirmFn() {
    // 表单验证
      await this.$refs.form.validate()
      // 调用接口函数
      if (this.formData.id) {
      // console.log(1)
        await updateRole(this.formData)

      // console.log(this.formData)
      } else {
      // console.log(2)
        await addRoleList(this.formData)
      }
      // 刷新列表
      this.getRoleList()
      // 关闭弹窗
      this.closeFn()
    },
    // 角色数据
    async getRoleList() {
      const res = await getRoleList({
        page: this.page,
        pagesize: this.pagesize
      })
      // console.log('1', res)
      this.tableData = res.rows
      this.total = res.total
    },
    // 页码发生改变时触发
    currentChangeFn(page) {
    // console.log(83, page)
      this.page = page
      this.getRoleList()
    },
    // 删除角色
    delFn(id) {
      console.log(id)
      // 询问框
      this.$confirm('是否确定删除?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
      // console.log('确定删除')
      // 调用接口删除
        await deleteRole(id)
        // 更新列表数据
        this.getRoleList()
      }).catch(() => {
        console.log('取消删除')
      })
    },

    // 编辑角色
    async editFn(id) {
    // console.log(id)
      this.showDialog = true
      this.formData = await getRoleDetailById(id)
    // this.getRoleList()
    }
  // 根据id查询企业
  }
}
</script>

<style>

</style>