index.vue 6.01 KB
<script setup lang="ts">
import { reactive, ref, onMounted, getCurrentInstance } from "vue";
import { roleList, roleStatus } from "@/api/user/role";
// import * as store from "@/pinia/index";
import { useLoginStore } from '@/pinia/login';
import router from "@/router";
import { parseTime } from "@/utils/tool";
import Pagination from "@/components/Pagination/index.vue";
import { ElMessageBox } from "element-plus";
const {
  appContext: {
    config: { globalProperties },
  },
} = getCurrentInstance();
interface Form {
  page: number;
  pageSize: number;
  roleName: string;
}
interface Params {
  total: number;
  query: Form;
}
interface Data {
  loading: boolean;
  list: any[];
  params: Params;
}
let data: Data = reactive({
  list: [],
  loading: false,
  params: {
    total: 0,
    query: {
      page: 1,
      pageSize: 10,
      roleName: "",
    },
  },
});
let asyncData = reactive({
  userInfo: "",
});
/**
 * 列表
 */
const getList = async () => {
  data.loading = true;
  try {
    const res = await roleList(data.params.query);
    data.list = res?.data?.data?.list;
    data.params.total = res?.data?.data?.total;
  } catch (e) {
    console.log(e);
  } finally {
    data.loading = false;
  }
};
/**
 * 删除
 * @param {object} obj 要删除的数据对象
 */
const handleDelete = (obj: any) => {
  ElMessageBox.confirm("确认删除吗?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      try {
        let res = await roleStatus({
          roleId: obj.id,
          status: 2,
        });
        if (res?.data) {
          globalProperties.$message.success("操作成功");
          getList();
        } else {
          globalProperties.$message.error("操作失败");
        }
      } catch (e) {
        console.log(e);
      }
    })
    .catch(() => {
      console.log("取消");
    });
};
/**
 * 状态
 * @param {object} obj 要操作的数据对象
 */
const handleStatus = async (obj) => {
  try {
    const res = await roleStatus({
      roleId: obj.id,
      status: obj.status ? 0 : 1,
    });
    if (res?.data) {
      globalProperties.$message.success("操作成功");
      getList();
    } else {
      globalProperties.$message.error("操作失败");
    }
  } catch (e) {
    console.log(e);
  }
};
/**
 * 状态值
 * @enum {number}
 */
enum statusEnum {
  "禁用" = 0,
  "启用",
}
onMounted(() => {
  // asyncData.userInfo = store.family.loginStore().id;
  asyncData.userInfo = useLoginStore()?.getId;
  getList();
});
</script>

<template>
  <div class="page-container">
    <h1>角色列表</h1>
    <el-row style="padding-bottom: 20px">
      <el-form :inline="true" label-width="70px" @submit.native.prevent>
        <el-form-item label="角色名">
          <el-input
            v-model.trim="data.params.query.roleName"
            placeholder="请填写角色名"
            maxlength="15"
            @change="getList"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
          <!-- 超管不能创建角色 -->
          <el-button
            type="primary"
            @click="router.push({ path: '/role/edit' })"
            :disabled="asyncData.userInfo === 1"
            >新增角色</el-button
          >
        </el-form-item>
      </el-form>
    </el-row>
    <el-table
      :data="data.list"
      style="width: 100%"
      border
      v-loading="data.loading"
      align="center"
      element-loading-text="拼命加载中"
    >
      <el-table-column prop="id" label="ID" align="center"> </el-table-column>
      <el-table-column prop="roleName" label="角色名称" align="center">
      </el-table-column>
      <el-table-column prop="createTime" label="注册时间" align="center">
        <template #default="scope">
          {{ parseTime(new Date(scope.row.createTime).getTime()) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template #default="scope">
          {{ statusEnum[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <!-- 超级管理员、商务管理员、物业管理员、公益管理员这四个角色是系统角色,不可以编辑、删除。通过code字段判断 -->
        <template #default="scope">
          <el-button
            type="text"
            v-show="
              ![
                'SUPER_ADMIN',
                'BUSINESS_ADMIN',
                'PROPERTY_ADMIN',
                'PUBLIC_BENEFIT_ADMIN',
              ].includes(scope.row.code)
            "
            ><router-link :to="{ path: '/role/edit', query: scope.row }"
              >编辑</router-link
            ></el-button
          >
          <el-button type="text" @click="handleStatus(scope.row)">{{
            scope.row.status ? "禁用" : "启用"
          }}</el-button>
          <el-button type="text"
            ><router-link
              :to="{ path: '/role/user', query: { id: scope.row.id } }"
              >用户</router-link
            ></el-button
          >
          <el-button type="text"
            ><router-link
              :to="{
                path: '/role/permission',
                query: { roleId: scope.row.id, roleName: scope.row.roleName },
              }"
              >权限</router-link
            ></el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-show="
              ![
                'SUPER_ADMIN',
                'BUSINESS_ADMIN',
                'PROPERTY_ADMIN',
                'PUBLIC_BENEFIT_ADMIN',
              ].includes(scope.row.code)
            "
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="data.params.total"
      v-model:currentPage="data.params.query.page"
      v-model:pageSize="data.params.query.pageSize"
      @pageChange="getList"
    ></Pagination>
  </div>
</template>