authRole.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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" :selectable="checkSelectable" 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>{{ proxy.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. if (checkSelectable(row)) {
  79. row.flag = !row.flag;
  80. tableRef.value?.toggleRowSelection(row, row.flag);
  81. }
  82. };
  83. /** 多选框选中数据 */
  84. const handleSelectionChange = (selection: RoleVO[]) => {
  85. roleIds.value = selection.map((item) => item.roleId);
  86. };
  87. /** 保存选中的数据编号 */
  88. const getRowKey = (row: RoleVO): string => {
  89. return String(row.roleId);
  90. };
  91. /** 检查角色状态 */
  92. const checkSelectable = (row: RoleVO): boolean => {
  93. return row.status === '0';
  94. };
  95. /** 关闭按钮 */
  96. const close = () => {
  97. const obj: RouteLocationNormalized = {
  98. fullPath: '',
  99. hash: '',
  100. matched: [],
  101. meta: undefined,
  102. name: undefined,
  103. params: undefined,
  104. query: undefined,
  105. redirectedFrom: undefined,
  106. path: '/system/user'
  107. };
  108. proxy?.$tab.closeOpenPage(obj);
  109. };
  110. /** 提交按钮 */
  111. const submitForm = async () => {
  112. const userId = form.value.userId;
  113. const rIds = roleIds.value.join(',');
  114. await updateAuthRole({ userId: userId as string, roleIds: rIds });
  115. proxy?.$modal.msgSuccess('授权成功');
  116. close();
  117. };
  118. const getList = async () => {
  119. const userId = route.params && route.params.userId;
  120. if (userId) {
  121. loading.value = true;
  122. const res = await getAuthRole(userId as string);
  123. Object.assign(form.value, res.data.user);
  124. Object.assign(roles.value, res.data.roles);
  125. total.value = roles.value.length;
  126. await nextTick(() => {
  127. roles.value.forEach((row) => {
  128. if (row?.flag) {
  129. tableRef.value?.toggleRowSelection(row, true);
  130. }
  131. });
  132. });
  133. loading.value = false;
  134. }
  135. };
  136. onMounted(() => {
  137. getList();
  138. });
  139. </script>