Role.vue 6.3 KB
Newer Older
E
Evan 已提交
1
<template>
2
  <div>
E
Evan 已提交
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
    <el-dialog
      title="修改角色信息"
      :visible.sync="dialogFormVisible">
      <el-form v-model="selectedRole" style="text-align: left" ref="dataForm">
        <el-form-item label="角色名" label-width="120px" prop="username">
          <el-input v-model="selectedRole.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" label-width="120px" prop="name">
          <el-input v-model="selectedRole.nameZh" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="功能配置" label-width="120px" prop="perms">
          <el-checkbox-group v-model="selectedPerms">
            <el-checkbox v-for="(perm,i) in perms" :key="i" :label="perm.id">{{perm.desc_}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="菜单配置" label-width="120px" prop="menus">
          <el-checkbox-group v-model="selectedMenus">
            <el-checkbox v-for="(menu,i) in menus" :key="i" :label="perm.id">{{menu.nameZh}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit(selectedRole)">确 定</el-button>
      </div>
    </el-dialog>
29 30
    <el-row style="margin: 18px 0px 0px 18px ">
      <el-breadcrumb separator-class="el-icon-arrow-right">
E
Evan 已提交
31
        <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">管理中心</el-breadcrumb-item>
32 33 34 35
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色配置</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
E
Evan 已提交
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
    <el-card style="margin: 18px 2%;width: 95%">
      <el-table
        :data="roles"
        stripe
        style="width: 100%"
        :max-height="tableHeight">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="id"
          label="id"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色名"
          fit>
        </el-table-column>
        <el-table-column
          prop="nameZh"
          label="角色描述"
          fit>
        </el-table-column>
        <el-table-column
          label="状态"
          width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @click.native="beforeUpdate"
E
Evan 已提交
70
              @change="(value) => commitStatusChange(value, scope.row)">
E
Evan 已提交
71 72 73 74 75 76 77 78 79
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
E
Evan 已提交
80 81
              size="small"
              @click="editRole(scope.row)">
E
Evan 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
              编辑
            </el-button>
            <el-button
              type="text"
              size="small">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 20px 0 20px 0;float: left">
        <el-button>取消选择</el-button>
        <el-button>批量删除</el-button>
      </div>
    </el-card>
97
  </div>
E
Evan 已提交
98 99 100
</template>

<script>
E
Evan 已提交
101 102 103 104
  export default {
    name: 'UserRole',
    data () {
      return {
E
Evan 已提交
105 106 107 108 109 110 111
        dialogFormVisible: false,
        roles: [],
        perms: [],
        menus: [],
        selectedRole: [],
        selectedPerms: [],
        selectedMenus: []
E
Evan 已提交
112 113 114
      }
    },
    mounted () {
E
Evan 已提交
115
      this.listRoles()
E
Evan 已提交
116
      this.listPerms()
E
Evan 已提交
117 118 119 120 121 122 123
    },
    computed: {
      tableHeight () {
        return window.innerHeight - 320
      }
    },
    methods: {
E
Evan 已提交
124
      listRoles () {
E
Evan 已提交
125 126 127 128 129 130 131
        var _this = this
        this.$axios.get('/admin/role').then(resp => {
          if (resp && resp.status === 200) {
            _this.roles = resp.data
          }
        })
      },
E
Evan 已提交
132 133 134 135 136 137 138
      listPerms () {
        var _this = this
        this.$axios.get('/admin/perm').then(resp => {
          if (resp && resp.status === 200) {
            _this.perms = resp.data
          }
        })
E
Evan 已提交
139
      },
E
Evan 已提交
140
      commitStatusChange (value, role) {
E
Evan 已提交
141 142 143 144 145 146
        if (role.id !== 1) {
          this.$confirm('是否更改角色状态?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
E
Evan 已提交
147
            this.$axios.put('/admin/role/status', {
E
Evan 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
              enabled: value,
              id: role.id
            }).then(resp => {
              if (resp && resp.status === 200) {
                if (value) {
                  this.$message('角色 [' + role.nameZh + '] 已启用')
                } else {
                  this.$message('角色 [' + role.nameZh + '] 已禁用')
                }
              }
            })
          }).catch(() => {
            role.enabled = !role.enabled
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
        } else {
          role.enabled = true
          this.$alert('无法禁用系统管理员!')
169
        }
E
Evan 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
      },
      onSubmit (role) {
        let _this = this
        // 根据视图绑定的角色 id 向后端传送角色信息
        let perms = []
        for (let i = 0; i < _this.selectedPerms.length; i++) {
          for (let j = 0; j < _this.perms.length; j++) {
            if (_this.selectedPerms[i] === _this.perms[j].id) {
              perms.push(_this.perms[j])
            }
          }
        }
        this.$axios.put('/admin/role', {
          id: role.id,
          name: role.name,
          nameZh: role.nameZh,
          enabled: role.enabled,
          perms: perms
        }).then(resp => {
          if (resp && resp.status === 200) {
            this.$alert(resp.data.data)
            this.dialogFormVisible = false
            this.listRoles()
          }
        })
      },
      editRole (role) {
        this.dialogFormVisible = true
        this.selectedRole = role
        let permIds = []
        for (let i = 0; i < role.perms.length; i++) {
          permIds.push(role.perms[i].id)
        }
        this.selectedPerms = permIds
204
      }
E
Evan 已提交
205
    }
E
Evan 已提交
206
  }
E
Evan 已提交
207 208 209 210 211
</script>

<style scoped>

</style>