fix(admin): 修复新增管理员无角色后不可见

This commit is contained in:
yml
2026-11-17 18:02:22 +08:00
parent ab68cce595
commit 8620d0a733
7 changed files with 175 additions and 18 deletions
@@ -24,6 +24,7 @@ export interface CreateAdminRequest {
username: string
password: string
nickname?: string
role_ids: number[]
}
export interface UpdateAdminRequest {
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { readError } from '@/shared/utils/error'
import { ElMessage } from 'element-plus'
import { ref, watch } from 'vue'
import { computed, ref, watch } from 'vue'
import {
createAdminMgrUser,
@@ -10,6 +10,7 @@ import {
type CreateAdminRequest,
type UpdateAdminRequest,
} from '@/features/admin/api/adminMgr'
import { fetchRoles, type Role } from '@/features/admin/api/adminRoles'
const props = defineProps<{
modelValue: boolean
@@ -22,18 +23,22 @@ const emit = defineEmits<{
}>()
const submitting = ref(false)
const rolesLoading = ref(false)
const allRoles = ref<Role[]>([])
const assignableRoles = computed(() => allRoles.value.filter(role => role.code !== 'super_admin'))
const form = ref({
username: '',
password: '',
nickname: '',
status: 'active',
role_ids: [] as number[],
})
const isEdit = ref(false)
watch(
() => props.modelValue,
val => {
async val => {
if (val) {
if (props.admin) {
isEdit.value = true
@@ -42,10 +47,25 @@ watch(
password: '',
nickname: props.admin.nickname,
status: props.admin.status,
role_ids: [],
}
} else {
isEdit.value = false
form.value = { username: '', password: '', nickname: '', status: 'active' }
form.value = {
username: '',
password: '',
nickname: '',
status: 'active',
role_ids: [],
}
rolesLoading.value = true
try {
allRoles.value = await fetchRoles()
} catch {
ElMessage.error('加载角色列表失败')
} finally {
rolesLoading.value = false
}
}
}
},
@@ -53,6 +73,10 @@ watch(
)
async function handleSave() {
if (!isEdit.value && form.value.role_ids.length === 0) {
ElMessage.warning('请至少选择一个角色')
return
}
submitting.value = true
try {
if (isEdit.value && props.admin) {
@@ -67,6 +91,7 @@ async function handleSave() {
username: form.value.username,
password: form.value.password,
nickname: form.value.nickname,
role_ids: form.value.role_ids,
}
await createAdminMgrUser(req)
ElMessage.success('管理员已创建')
@@ -97,9 +122,25 @@ async function handleSave() {
v-model="form.password"
type="password"
show-password
placeholder="请输入密码(至少6位)"
placeholder="请输入密码(至少8位,包含字母和数字)"
/>
</el-form-item>
<el-form-item v-if="!isEdit" label="角色" class="full-control">
<el-select
v-model="form.role_ids"
:loading="rolesLoading"
multiple
placeholder="请选择角色"
style="width: 100%"
>
<el-option
v-for="role in assignableRoles"
:key="role.id"
:label="role.name"
:value="role.id"
/>
</el-select>
</el-form-item>
<el-form-item label="昵称" class="full-control">
<el-input v-model="form.nickname" placeholder="请输入昵称" />
</el-form-item>