authority.vue 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
<template>
  <div>
    <div class="button-box clearflex">
      <el-button @click="addAuthority" type="primary">新增角色</el-button>
    </div>
    <el-table :data="tableData" border stripe>
      <el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
      <el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      hide-on-single-page
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog :visible.sync="dialogFormVisible" title="新增角色">
      <el-form :model="form">
        <el-form-item label="角色ID">
          <el-input autocomplete="off" v-model="form.authorityId"></el-input>
        </el-form-item>
        <el-form-item label="角色姓名">
          <el-input autocomplete="off" v-model="form.authorityName"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAuthorityList, deleteAuthority, createAuthority } from '@/api/authority'
export default {
  name: 'Authority',
  data() {
    return {
      page: 1,
      total: 10,
      pageSize: 10,
      tableData: [],
      dialogFormVisible: false,
      form:{
          authorityId:"",
          authorityName:""
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.getAuthList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getAuthList()
    },
    deleteAuth(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            const res = await deleteAuthority({ authorityId: row.authorityId })
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getAuthList()
          } catch (err) {
            this.$message({
              type: 'error',
              message: '删除失败!' + err
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    initForm(){
        for(const key in this.form){
            this.form[key] = ''
        }
    },
    closeDialog(){
        this.initForm()
        this.dialogFormVisible = false
    },
    async enterDialog(){
        const res = await createAuthority(this.form)
        if(res.success){
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            this.getAuthList()
            this.closeDialog()
        }else{
            this.$message({
              type: 'error',
              message: '添加失败!'
            })
            this.closeDialog()
        }
        this.initForm()
        this.dialogFormVisible = false
    },
    addAuthority() {
        this.dialogFormVisible = true
    },
    async getAuthList(page = this.page, pageSize = this.pageSize) {
      try {
        const table = await getAuthorityList({ page, pageSize })
        this.tableData = table.data.authList
      } catch (err) {
        console.log(err)
      }
    }
  },
  created() {
    this.getAuthList()
  }
}
</script>
<style lang="scss">
.button-box {
  padding: 10px 20px;
  .el-button {
    float: right;
  }
}
</style>