index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <el-card shadow="never" class="page">
  3. <el-form :model="tableData.param" inline ref="queryRef">
  4. <el-form-item label="角色名称" prop="name">
  5. <el-input v-model="tableData.param.name" placeholder="请输入角色名称" class="w-50" clearable />
  6. </el-form-item>
  7. <el-form-item label="状态" prop="status">
  8. <el-select placeholder="请选择状态" style="width: 80px;" v-model="tableData.param.status">
  9. <el-option label="全部" :value="-1" />
  10. <el-option label="启用" :value="1" />
  11. <el-option label="禁用" :value="0" />
  12. </el-select>
  13. </el-form-item>
  14. <el-form-item>
  15. <el-button type="primary" @click="roleList">
  16. <el-icon>
  17. <ele-Search />
  18. </el-icon>
  19. 查询
  20. </el-button>
  21. <el-button @click="resetQuery()">
  22. <el-icon>
  23. <ele-Refresh />
  24. </el-icon>
  25. 重置
  26. </el-button>
  27. <el-button type="primary" @click="onOpenAddRole" v-auth="'add'">
  28. <el-icon>
  29. <ele-FolderAdd />
  30. </el-icon>
  31. 新增角色
  32. </el-button>
  33. </el-form-item>
  34. </el-form>
  35. <el-table :data="tableData.data" style="width: 100%" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" v-loading="tableData.loading">
  36. <el-table-column type="index" label="序号" width="60" align="center" />
  37. <el-table-column prop="name" v-col="'name'" label="角色名称" show-overflow-tooltip></el-table-column>
  38. <el-table-column prop="remark" v-col="'remark'" label="角色描述" show-overflow-tooltip></el-table-column>
  39. <el-table-column prop="listOrder" v-col="'listOrder'" label="排序" width="60" align="center"></el-table-column>
  40. <el-table-column prop="status" v-col="'status'" label="角色状态" width="100" align="center">
  41. <template #default="scope">
  42. <el-tag type="success" size="small" v-if="scope.row.status === 1">启用</el-tag>
  43. <el-tag type="info" size="small" v-else>禁用</el-tag>
  44. </template>
  45. </el-table-column>
  46. <el-table-column prop="createdAt" v-col="'createdAt'" label="创建时间" width="170" align="center"></el-table-column>
  47. <el-table-column label="操作" width="220" v-col="'handle'" align="center" fixed="right">
  48. <template #default="scope">
  49. <el-button size="small" type="text" @click="onOpenEditRole(scope.row)" v-auth="'edit'">修改</el-button>
  50. <el-button size="small" text type="info" @click="onRowDel(scope.row)" v-auth="'del'">删除</el-button>
  51. <el-button size="small" text type="success" @click="permission(scope.row)" v-auth="'role-premission'">角色权限</el-button>
  52. <el-button size="small" text type="info" @click="dataPermission(scope.row)" v-auth="'data-premission'">数据权限</el-button>
  53. <!-- <el-dropdown size="small">
  54. <el-button type="text" size="small" style="margin-top:1px;margin-left:10px">更多
  55. <el-icon>
  56. <ele-ArrowDown />
  57. </el-icon>
  58. </el-button>
  59. <template #dropdown>
  60. <el-dropdown-menu>
  61. <el-dropdown-item>角色成员</el-dropdown-item>
  62. <el-dropdown-item @click.native="permission(scope.row)">角色权限</el-dropdown-item>
  63. </el-dropdown-menu>
  64. </template>
  65. </el-dropdown> -->
  66. </template>
  67. </el-table-column>
  68. </el-table>
  69. <!-- <pagination v-show="tableData.total>0" :total="tableData.total" v-model:page="tableData.param.pageNum" v-model:limit="tableData.param.pageSize" @pagination="roleList" /> -->
  70. <EditRole ref="editRoleRef" @getList="roleList" :list="tableData.data" />
  71. <permissionVue ref="permissionRef" />
  72. <EditPer ref="dataPermissionRef" :dept-data="deptData" />
  73. </el-card>
  74. </template>
  75. <script lang="ts">
  76. import { toRefs, reactive, onMounted, ref, defineComponent, toRaw, getCurrentInstance } from 'vue';
  77. import { ElMessageBox, ElMessage } from 'element-plus';
  78. import EditRole from './component/editRole.vue';
  79. import EditPer from './component/editPer.vue';
  80. import permissionVue from './component/permission.vue';
  81. import api from '/@/api/system';
  82. // 定义接口来定义对象的类型
  83. interface TableData {
  84. id: number;
  85. status: number;
  86. listOrder: number;
  87. name: string;
  88. remark: string;
  89. dataScope: number;
  90. createdAt: string;
  91. }
  92. interface TableDataState {
  93. deptData: any[];
  94. tableData: {
  95. data: Array<TableData>;
  96. // total: number;
  97. loading: boolean;
  98. param: {
  99. name: string;
  100. status: number;
  101. };
  102. };
  103. }
  104. export default defineComponent({
  105. name: 'apiV1SystemRoleList',
  106. components: { EditRole, permissionVue, EditPer },
  107. setup() {
  108. const { proxy } = getCurrentInstance() as any;
  109. const addRoleRef = ref();
  110. const queryRef = ref();
  111. const editRoleRef = ref();
  112. const permissionRef = ref();
  113. const dataPermissionRef = ref();
  114. const state = reactive<TableDataState>({
  115. deptData: [],
  116. tableData: {
  117. data: [],
  118. // total: 0,
  119. loading: false,
  120. param: {
  121. name: '',
  122. status: -1,
  123. },
  124. },
  125. });
  126. // 初始化表格数据
  127. const initTableData = () => {
  128. roleList();
  129. api.dept
  130. .getList({
  131. status: 1,
  132. })
  133. .then((res: any) => {
  134. state.deptData = res;
  135. });
  136. };
  137. const roleList = () => {
  138. state.tableData.loading = true;
  139. api.role
  140. .getList(state.tableData.param)
  141. .then((res: Array<TableData>) => {
  142. state.tableData.data = res || [];
  143. })
  144. .finally(() => (state.tableData.loading = false));
  145. };
  146. // 打开新增角色弹窗
  147. const onOpenAddRole = () => {
  148. editRoleRef.value.openDialog();
  149. };
  150. // 打开修改角色弹窗
  151. const onOpenEditRole = (row: Object) => {
  152. editRoleRef.value.openDialog(toRaw(row));
  153. };
  154. // 删除角色
  155. const onRowDel = (row: any) => {
  156. ElMessageBox.confirm(`此操作将永久删除角色:“${row.name}”,是否继续?`, '提示', {
  157. confirmButtonText: '确认',
  158. cancelButtonText: '取消',
  159. type: 'warning',
  160. }).then(() => {
  161. api.role.deleteRole(row.id).then(() => {
  162. ElMessage.success('删除成功');
  163. proxy.$refs['editRoleRef'].resetMenuSession();
  164. roleList();
  165. });
  166. });
  167. };
  168. // 设置权限
  169. const permission = async (row: any) => {
  170. const { isAllow } = await api.role.auth.isAllow(row.id);
  171. if (isAllow) {
  172. permissionRef.value.openDialog(row);
  173. } else {
  174. ElMessage.error('该角色禁止被授权');
  175. }
  176. };
  177. // 设置数据权限
  178. const dataPermission = async (row: any) => {
  179. dataPermissionRef.value.openDialog(row);
  180. };
  181. // 页面加载时
  182. onMounted(() => {
  183. initTableData();
  184. });
  185. // 重置表单
  186. const resetQuery = () => {
  187. queryRef.value.resetFields();
  188. initTableData();
  189. };
  190. return {
  191. queryRef,
  192. resetQuery,
  193. addRoleRef,
  194. editRoleRef,
  195. permissionRef,
  196. dataPermissionRef,
  197. dataPermission,
  198. permission,
  199. onOpenAddRole,
  200. onOpenEditRole,
  201. onRowDel,
  202. roleList,
  203. ...toRefs(state),
  204. };
  205. },
  206. });
  207. </script>