This commit is contained in:
18631081161 2026-03-20 15:48:11 +08:00
parent 83785dc20c
commit d18eec529f
6 changed files with 101 additions and 9 deletions

View File

@ -67,6 +67,14 @@ export function deleteUser(id: number): Promise<void> {
return request.delete(`/admin/users/${id}`)
}
/**
*
* @returns
*/
export function deleteAllTestUsers(): Promise<number> {
return request.delete('/admin/users/test-users')
}
/**
*
* @param id ID

View File

@ -14,7 +14,7 @@ import { View, Edit, Plus, Delete, Refresh } from '@element-plus/icons-vue'
import SearchForm from '@/components/SearchForm/index.vue'
import Pagination from '@/components/Pagination/index.vue'
import StatusTag from '@/components/StatusTag/index.vue'
import { getUserList, updateUserStatus, createTestUsers, deleteUser, refreshRecommend } from '@/api/user'
import { getUserList, updateUserStatus, createTestUsers, deleteUser, deleteAllTestUsers, refreshRecommend } from '@/api/user'
import { getFullImageUrl } from '@/utils/image'
import type { UserListItem, UserQueryParams } from '@/types/user.d'
@ -207,6 +207,29 @@ const formatTime = (time: string) => {
//
const testUserDialogVisible = ref(false)
const testUserLoading = ref(false)
//
const handleDeleteAllTestUsers = async () => {
try {
await ElMessageBox.confirm(
'确定要删除所有测试用户吗?此操作不可恢复!',
'批量删除测试用户',
{
confirmButtonText: '确定删除',
cancelButtonText: '取消',
type: 'warning'
}
)
const count = await deleteAllTestUsers()
ElMessage.success(`成功删除${count}个测试用户`)
fetchUserList()
} catch (error) {
if (error !== 'cancel') {
console.error('批量删除测试用户失败:', error)
}
}
}
const testUserForm = reactive({
count: 10,
gender: undefined as number | undefined
@ -387,13 +410,22 @@ onMounted(() => {
<template #header>
<div class="card-header">
<span>用户列表</span>
<el-button
type="primary"
:icon="Plus"
@click="handleOpenTestUserDialog"
>
创建测试用户
</el-button>
<div>
<el-button
type="danger"
:icon="Delete"
@click="handleDeleteAllTestUsers"
>
批量删除测试用户
</el-button>
<el-button
type="primary"
:icon="Plus"
@click="handleOpenTestUserDialog"
>
创建测试用户
</el-button>
</div>
</div>
</template>
<el-table

View File

@ -23,7 +23,7 @@ const ENV = {
}
// 当前环境 - 开发时使用 development打包时改为 production
const CURRENT_ENV = 'development'
const CURRENT_ENV = 'production'
// 导出配置
export const config = {

View File

@ -97,6 +97,19 @@ public class AdminUserController : ControllerBase
return ApiResponse<List<long>>.Success(result, $"成功创建{result.Count}个测试用户");
}
/// <summary>
/// 批量删除所有测试用户
/// </summary>
/// <returns>删除的数量</returns>
[HttpDelete("test-users")]
[OperationLog("用户管理", "删除", Description = "批量删除测试用户")]
public async Task<ApiResponse<int>> DeleteAllTestUsers()
{
var adminId = GetCurrentAdminId();
var count = await _adminUserService.DeleteAllTestUsersAsync(adminId);
return ApiResponse<int>.Success(count, $"成功删除{count}个测试用户");
}
/// <summary>
/// 获取当前管理员ID
/// </summary>

View File

@ -53,6 +53,13 @@ public interface IAdminUserService
/// <returns>是否成功</returns>
Task<bool> DeleteUserAsync(long userId, long adminId);
/// <summary>
/// 批量删除所有测试用户
/// </summary>
/// <param name="adminId">操作管理员ID</param>
/// <returns>删除的用户数量</returns>
Task<int> DeleteAllTestUsersAsync(long adminId);
/// <summary>
/// 更新用户联系次数
/// </summary>

View File

@ -511,6 +511,38 @@ public class AdminUserService : IAdminUserService
return true;
}
/// <inheritdoc />
public async Task<int> DeleteAllTestUsersAsync(long adminId)
{
var testUsers = await _userRepository.GetListAsync(u => u.OpenId.StartsWith("test_openid_"));
if (!testUsers.Any())
{
return 0;
}
var deletedCount = 0;
foreach (var user in testUsers)
{
try
{
await _photoRepository.DeleteAsync(p => p.UserId == user.Id);
await _requirementRepository.DeleteAsync(r => r.UserId == user.Id);
await _profileRepository.DeleteAsync(p => p.UserId == user.Id);
await _userRepository.DeleteAsync(user.Id);
deletedCount++;
}
catch (Exception ex)
{
_logger.LogError(ex, "删除测试用户失败: UserId={UserId}", user.Id);
}
}
_logger.LogInformation("管理员批量删除测试用户: AdminId={AdminId}, DeletedCount={DeletedCount}, TotalCount={TotalCount}",
adminId, deletedCount, testUsers.Count);
return deletedCount;
}
/// <inheritdoc />
public async Task<bool> UpdateContactCountAsync(long userId, int contactCount, long adminId)
{