authRole.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div class="p-2">
  3. <div class="panel">
  4. <h4 class="panel-title">基本信息</h4>
  5. <el-form :model="form" label-width="80px" :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. v-loading="loading"
  25. :row-key="getRowKey"
  26. @row-click="clickRow"
  27. ref="tableRef"
  28. @selection-change="handleSelectionChange"
  29. :data="roles.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
  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" :total="total" v-model:page="pageNum" v-model:limit="pageSize" />
  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. const route = useRoute();
  61. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  62. const loading = ref(true);
  63. const total = ref(0);
  64. const pageNum = ref(1);
  65. const pageSize = ref(10);
  66. const roleIds = ref<Array<string | number>>([]);
  67. const roles = ref<RoleVO[]>([]);
  68. const form = ref<Partial<UserForm>>({
  69. nickName: undefined,
  70. userName: "",
  71. userId: undefined
  72. });
  73. const tableRef = ref<ElTableInstance>();
  74. /** 单击选中行数据 */
  75. const clickRow = (row: RoleVO) => {
  76. // ele的方法有问题,selected应该为可选参数
  77. tableRef.value?.toggleRowSelection(row, false);
  78. };
  79. /** 多选框选中数据 */
  80. const handleSelectionChange = (selection: RoleVO[]) => {
  81. roleIds.value = selection.map(item => item.roleId);
  82. };
  83. /** 保存选中的数据编号 */
  84. const getRowKey = (row: RoleVO): string => {
  85. return String(row.roleId);
  86. };
  87. /** 关闭按钮 */
  88. const close = () => {
  89. const obj = { path: "/system/user" };
  90. proxy?.$tab.closeOpenPage(obj);
  91. };
  92. /** 提交按钮 */
  93. const submitForm = async () => {
  94. const userId = form.value.userId;
  95. const rIds = roleIds.value.join(",");
  96. await updateAuthRole({ userId: userId as string, roleIds: rIds });
  97. proxy?.$modal.msgSuccess("授权成功");
  98. close();
  99. };
  100. const getList = async () => {
  101. const userId = route.params && route.params.userId;
  102. if (userId) {
  103. loading.value = true;
  104. const res = await getAuthRole(userId as string);
  105. Object.assign(form.value, res.data.user);
  106. Object.assign(roles.value, res.data.roles);
  107. total.value = roles.value.length;
  108. await nextTick(() => {
  109. roles.value.forEach(row => {
  110. if (row?.flag) {
  111. tableRef.value?.toggleRowSelection(row, true);
  112. }
  113. });
  114. });
  115. loading.value = false;
  116. }
  117. };
  118. onMounted(() => {
  119. getList();
  120. });
  121. </script>