角色列表

添加model

<template>
  <div>
    <!-- 麪包屑導航區域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首頁</el-breadcrumb-item>
      <el-breadcrumb-item>權限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片視圖 -->
    <el-card>
      <!-- 添加角色按鈕區域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rolesList: []
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    // 獲取所有角色的列表
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')

      if (res.meta.status !== 200) {
        return this.$message.error('獲取角色列表失敗!')
      }

      this.rolelist = res.data

      console.log(this.rolelist)
    }
  }
}
</script>
<style lang="less" scoped>
</style>

sss添加ui

最外層是:  面報跳轉導航breadcrumb   卡片card

card中有:添加按鈕行row     顯示列表table    對話框dialog   

table中   有5列column

第一列column有1行row

一行有兩列,表頭是通過model層的值控制的

第一行第一列會監聽彈出刪除dialog

 

第一行第二列又根據model層數據劃分多個行row

第一行第2列每一行裏面分爲兩個列

第一行第2列裏面的第一行裏面的第二列裏面有多個按鈕tag

最終代碼是:

<template>
  <div>
    <!-- 麪包屑導航區域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首頁</el-breadcrumb-item>
      <el-breadcrumb-item>權限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片視圖 -->
    <el-card>
      <!-- 添加角色按鈕區域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 角色列表區域 -->
      <el-table :data="rolelist" border stripe>
        <!-- 展開列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'vcenter']"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <!-- 渲染一級權限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row, item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 渲染二級和三級權限 -->
              <el-col :span="19">
                <!-- 通過 for 循環 嵌套渲染二級權限 -->
                <el-row
                  :class="[i2 === 0 ? '' : 'bdtop', 'vcenter']"
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row, item2.id)"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- v-for="(item3, i3) in item2.children" -->
                    <el-tag
                      type="warning"
                      v-for="(item3) in item2.children"
                      :key="item3.id"
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- <pre>
              {{scope.row}}
            </pre>-->
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名稱" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">編輯</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">刪除</el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showSetRightDialog(scope.row)"
            >分配權限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配權限的對話框 -->
    <el-dialog
      title="分配權限"
      :visible.sync="setRightDialogVisible"
      width="50%"
      @close="setRightDialogClosed"
    >
      <!-- 樹形控件 -->
      <el-tree
        :data="rightslist"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defKeys"
        ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">確 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 所有角色列表數據
      rolelist: [],
      // 控制分配權限對話框的顯示與隱藏
      setRightDialogVisible: false,
      // 所有權限的數據
      rightslist: [],
      // 樹形控件的屬性綁定對象
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      // 默認選中的節點Id值數組
      defKeys: [],
      // 當前即將分配權限的角色id
      roleId: ''
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    // 獲取所有角色的列表
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')

      if (res.meta.status !== 200) {
        return this.$message.error('獲取角色列表失敗!')
      }

      this.rolelist = res.data

      console.log(this.rolelist)
    },
    // 根據Id刪除對應的權限
    async removeRightById(role, rightId) {
      // 彈框提示用戶是否要刪除
      const confirmResult = await this.$confirm(
        '此操作將永久刪除該文件, 是否繼續?',
        '提示',
        {
          confirmButtonText: '確定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了刪除!')
      }

      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${rightId}`
      )

      if (res.meta.status !== 200) {
        return this.$message.error('刪除權限失敗!')
      }

      // this.getRolesList()
      role.children = res.data
    },
    // 展示分配權限的對話框
    async showSetRightDialog(role) {
      this.roleId = role.id
      // 獲取所有權限的數據
      const { data: res } = await this.$http.get('rights/tree')

      if (res.meta.status !== 200) {
        return this.$message.error('獲取權限數據失敗!')
      }

      // 把獲取到的權限數據保存到 data 中
      this.rightslist = res.data
      console.log(this.rightslist)

      // 遞歸獲取三級節點的Id
      this.getLeafKeys(role, this.defKeys)

      this.setRightDialogVisible = true
    },
    // 通過遞歸的形式,獲取角色下所有三級權限的id,並保存到 defKeys 數組中
    getLeafKeys(node, arr) {
      // 如果當前 node 節點不包含 children 屬性,則是三級節點
      if (!node.children) {
        return arr.push(node.id)
      }

      node.children.forEach(item => this.getLeafKeys(item, arr))
    },
    // 監聽分配權限對話框的關閉事件
    setRightDialogClosed() {
      this.defKeys = []
    },
    // 點擊爲角色分配權限
    async allotRights() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]

      const idStr = keys.join(',')

      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        { rids: idStr }
      )

      if (res.meta.status !== 200) {
        return this.$message.error('分配權限失敗!')
      }

      this.$message.success('分配權限成功!')
      this.getRolesList()
      this.setRightDialogVisible = false
    }
  }
}
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px;
}

.bdtop {
  border-top: 1px solid #eee;
}

.bdbottom {
  border-bottom: 1px solid #eee;
}

.vcenter {
  display: flex;
  align-items: center;
}
</style>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章