SwitchRoles.vue 1.48 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
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
<template>
  <div>
    <div style="margin-bottom:15px;">
      {{ $t('permission.roles') }}: {{ roles }}
    </div>
    <div style="margin-bottom:15px;">
      {{ $t('permission.switchRoles') }}:
    </div>
    <div>
      <el-radio-group v-model="switchRoles" v-loading="loading">
        <el-radio-button label="admin" />
        <el-radio-button label="manager" />
        <el-radio-button label="editor" />
        <el-radio-button label="user" />
        <el-radio-button label="visitor" />
      </el-radio-group>
    </div>
  </div>
</template>

<script>
import RoleResource from '@/api/role';
const roleResource = new RoleResource();
export default {
  data() {
    return {
      avaiableRoles: [],
      loading: false,
      list: [],
    };
  },
  computed: {
    roles() {
      return this.$store.getters.roles;
    },
    switchRoles: {
      get() {
        return this.roles[0];
      },
      set(val) {
        const found = this.list.find(role => role.name === val);
        this.$store.dispatch('user/changeRoles', found).then(() => {
          this.$emit('change');
        });
      },
    },
  },
  created() {
    this.getRoles();
  },
  methods: {
    async getRoles() {
      this.loading = true;
      const { data } = await roleResource.list({});
      this.list = data;
      this.list.forEach((role, index) => {
        role['index'] = index + 1;
        role['description'] = this.$t('roles.description.' + role.name);
      });
      this.loading = false;
    },
  },
};
</script>