细节优化——1
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
link
|
||||
:type="scope.row.status === 'ACTIVE' ? 'danger' : 'primary'"
|
||||
size="small"
|
||||
:disabled="isLastAdmin(scope.row)"
|
||||
@click="toggleStatus(scope.row)"
|
||||
>
|
||||
{{ scope.row.status === 'ACTIVE' ? "禁用" : "启用" }}
|
||||
@@ -57,7 +58,7 @@
|
||||
/>
|
||||
</div>
|
||||
</el-card>
|
||||
<UserForm v-model:visible="formVisible" :user="editingUser" @saved="loadUsers" />
|
||||
<UserForm v-model:visible="formVisible" :user="editingUser" :admin-count="activeAdminCount" @saved="loadUsers" />
|
||||
<UserResetPassword v-model:visible="resetVisible" :user="resetUser" @reset="loadUsers" />
|
||||
</div>
|
||||
</template>
|
||||
@@ -77,6 +78,7 @@ const total = ref(0);
|
||||
const page = ref(1);
|
||||
const pageSize = ref(10);
|
||||
const loading = ref(false);
|
||||
const activeAdminCount = ref(0);
|
||||
const formVisible = ref(false);
|
||||
const resetVisible = ref(false);
|
||||
const editingUser = ref<UserInfo | null>(null);
|
||||
@@ -86,10 +88,15 @@ const auth = useAuthStore();
|
||||
const loadUsers = async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const { data } = await fetchUsers({ skip: (page.value - 1) * pageSize.value, limit: pageSize.value });
|
||||
const [{ data }, { data: adminData }] = await Promise.all([
|
||||
fetchUsers({ skip: (page.value - 1) * pageSize.value, limit: pageSize.value }),
|
||||
fetchUsers({ skip: 0, limit: 10000 }),
|
||||
]);
|
||||
const items = (data as any).items || [];
|
||||
const allItems = (adminData as any).items || [];
|
||||
users.value = items;
|
||||
total.value = (data as any).total || items.length;
|
||||
activeAdminCount.value = allItems.filter((u: UserInfo) => u.role === "ADMIN" && u.status === "ACTIVE").length;
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.response?.data?.message || "用户列表加载失败");
|
||||
} finally {
|
||||
@@ -108,6 +115,10 @@ const openEdit = (row: UserInfo) => {
|
||||
};
|
||||
|
||||
const toggleStatus = async (row: UserInfo) => {
|
||||
if (isLastAdmin(row)) {
|
||||
ElMessage.warning("至少保留一个管理员账号,不能禁用该账号");
|
||||
return;
|
||||
}
|
||||
const disable = row.status === "ACTIVE";
|
||||
const ok = await ElMessageBox.confirm(
|
||||
disable ? "该操作将影响用户账号,请确认是否继续" : "确认启用该用户账号?",
|
||||
@@ -192,6 +203,9 @@ const statusLabel = (status: string) => {
|
||||
return status || "未知";
|
||||
}
|
||||
};
|
||||
|
||||
const isLastAdmin = (row: UserInfo) =>
|
||||
row.role === "ADMIN" && row.status === "ACTIVE" && activeAdminCount.value <= 1;
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
Reference in New Issue
Block a user