authRole.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <div class="p-2">
  3. <div class="panel">
  4. <h4 class="panel-title">基本信息</h4>
  5. <el-form :model="form" :inline="true">
  6. <el-row :gutter="10">
  7. <el-col :span="2.5">
  8. <el-form-item label="用户昵称" prop="nickName">
  9. <el-input v-model="form.nickName" disabled />
  10. </el-form-item>
  11. </el-col>
  12. <el-col :span="2.5">
  13. <el-form-item label="登录账号" prop="userName">
  14. <el-input v-model="form.userName" disabled />
  15. </el-form-item>
  16. </el-col>
  17. </el-row>
  18. </el-form>
  19. </div>
  20. <div class="panel">
  21. <h4 class="panel-title">角色信息</h4>
  22. <div>
  23. <el-table
  24. ref="tableRef"
  25. v-loading="loading"
  26. :row-key="getRowKey"
  27. :data="roles.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
  28. @row-click="clickRow"
  29. @selection-change="handleSelectionChange"
  30. >
  31. <el-table-column label="序号" width="55" type="index" align="center">
  32. <template #default="scope">
  33. <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
  34. </template>
  35. </el-table-column>
  36. <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
  37. <el-table-column label="角色编号" align="center" prop="roleId" />
  38. <el-table-column label="角色名称" align="center" prop="roleName" />
  39. <el-table-column label="权限字符" align="center" prop="roleKey" />
  40. <el-table-column label="创建时间" align="center" prop="createTime" width="180">
  41. <template #default="scope">
  42. <span>{{ parseTime(scope.row.createTime) }}</span>
  43. </template>
  44. </el-table-column>
  45. </el-table>
  46. <pagination v-show="total > 0" v-model:page="pageNum" v-model:limit="pageSize" :total="total" />
  47. <div style="text-align: center; margin-left: -120px; margin-top: 30px">
  48. <el-button type="primary" @click="submitForm()">提交</el-button>
  49. <el-button @click="close()">返回</el-button>
  50. </div>
  51. <div></div>
  52. </div>
  53. </div>
  54. </div>
  55. </template>
  56. <script setup name="AuthRole" lang="ts">
  57. import { RoleVO } from '@/api/system/role/types';
  58. import { getAuthRole, updateAuthRole } from '@/api/system/user';
  59. import { UserForm } from '@/api/system/user/types';
  60. import { RouteLocationNormalized } from 'vue-router';
  61. import { parseTime } from '@/utils/ruoyi';
  62. const route = useRoute();
  63. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  64. const loading = ref(true);
  65. const total = ref(0);
  66. const pageNum = ref(1);
  67. const pageSize = ref(10);
  68. const roleIds = ref<Array<string | number>>([]);
  69. const roles = ref<RoleVO[]>([]);
  70. const form = ref<Partial<UserForm>>({
  71. nickName: undefined,
  72. userName: '',
  73. userId: undefined
  74. });
  75. const tableRef = ref<ElTableInstance>();
  76. /** 单击选中行数据 */
  77. const clickRow = (row: RoleVO) => {
  78. // ele的方法有问题,selected应该为可选参数
  79. tableRef.value?.toggleRowSelection(row, false);
  80. };
  81. /** 多选框选中数据 */
  82. const handleSelectionChange = (selection: RoleVO[]) => {
  83. roleIds.value = selection.map((item) => item.roleId);
  84. };
  85. /** 保存选中的数据编号 */
  86. const getRowKey = (row: RoleVO): string => {
  87. return String(row.roleId);
  88. };
  89. /** 关闭按钮 */
  90. const close = () => {
  91. const obj: RouteLocationNormalized = {
  92. fullPath: '',
  93. hash: '',
  94. matched: [],
  95. meta: undefined,
  96. name: undefined,
  97. params: undefined,
  98. query: undefined,
  99. redirectedFrom: undefined,
  100. path: '/system/user'
  101. };
  102. proxy?.$tab.closeOpenPage(obj);
  103. };
  104. /** 提交按钮 */
  105. const submitForm = async () => {
  106. const userId = form.value.userId;
  107. const rIds = roleIds.value.join(',');
  108. await updateAuthRole({ userId: userId as string, roleIds: rIds });
  109. proxy?.$modal.msgSuccess('授权成功');
  110. close();
  111. };
  112. const getList = async () => {
  113. const userId = route.params && route.params.userId;
  114. if (userId) {
  115. loading.value = true;
  116. const res = await getAuthRole(userId as string);
  117. Object.assign(form.value, res.data.user);
  118. Object.assign(roles.value, res.data.roles);
  119. total.value = roles.value.length;
  120. await nextTick(() => {
  121. roles.value.forEach((row) => {
  122. if (row?.flag) {
  123. tableRef.value?.toggleRowSelection(row, true);
  124. }
  125. });
  126. });
  127. loading.value = false;
  128. }
  129. };
  130. onMounted(() => {
  131. getList();
  132. });
  133. </script>