feat: AMT设备列表支持批量获取/更新UUID

This commit is contained in:
lvfengfree 2026-01-21 21:33:55 +08:00
parent 8acd7b0ab6
commit 3703cd438e
3 changed files with 149 additions and 1 deletions

View File

@ -105,6 +105,14 @@ export const deviceApi = {
data: data, data: data,
showSuccessMessage: true showSuccessMessage: true
}) })
},
// 批量获取/更新设备 UUID
batchFetchUuid(deviceIds: number[]) {
return request.post({
url: '/api/devices/batch-fetch-uuid',
data: { deviceIds }
})
} }
} }

View File

@ -35,6 +35,10 @@
<span v-else class="hint-text">请勾选设备进行操作</span> <span v-else class="hint-text">请勾选设备进行操作</span>
</div> </div>
<div class="batch-actions"> <div class="batch-actions">
<ElButton type="primary" :disabled="selectedDevices.length === 0" :loading="batchFetchingUuid" @click="handleBatchFetchUuid">
<el-icon><Connection /></el-icon>
获取UUID
</ElButton>
<ElButton type="info" :disabled="selectedDevices.length === 0" @click="handleBatchSetCredentials"> <ElButton type="info" :disabled="selectedDevices.length === 0" @click="handleBatchSetCredentials">
<el-icon><Key /></el-icon> <el-icon><Key /></el-icon>
配置AMT账号 配置AMT账号
@ -171,7 +175,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue' import { ref, onMounted, onUnmounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus' import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, ArrowDown, VideoPlay, VideoPause, RefreshRight, CircleClose, Delete, Lightning, Key } from '@element-plus/icons-vue' import { Search, Refresh, ArrowDown, VideoPlay, VideoPause, RefreshRight, CircleClose, Delete, Lightning, Key, Connection } from '@element-plus/icons-vue'
import { deviceApi, powerApi, hardwareApi } from '@/api/amt' import { deviceApi, powerApi, hardwareApi } from '@/api/amt'
import HardwareInfoModal from './modules/hardware-info-modal.vue' import HardwareInfoModal from './modules/hardware-info-modal.vue'
import RemoteDesktopModal from './modules/remote-desktop-modal.vue' import RemoteDesktopModal from './modules/remote-desktop-modal.vue'
@ -191,6 +195,7 @@ const selectedDevice = ref<any>(null)
const credentialsTargetDevices = ref<any[]>([]) const credentialsTargetDevices = ref<any[]>([])
const credentialsForm = ref({ username: '', password: '' }) const credentialsForm = ref({ username: '', password: '' })
const savingCredentials = ref(false) const savingCredentials = ref(false)
const batchFetchingUuid = ref(false)
let statusCheckInterval: number | null = null let statusCheckInterval: number | null = null
@ -393,6 +398,41 @@ const handleFetchUuid = async (device: any) => {
} }
} }
// UUID
const handleBatchFetchUuid = async () => {
if (selectedDevices.value.length === 0) return
batchFetchingUuid.value = true
try {
const deviceIds = selectedDevices.value.map(d => d.id)
const response = await deviceApi.batchFetchUuid(deviceIds)
//
if (response.results) {
for (const result of response.results) {
if (result.success && result.uuid) {
const device = devices.value.find(d => d.id === result.deviceId)
if (device) {
device.systemUuid = result.uuid
}
}
}
}
if (response.successCount > 0 && response.failCount === 0) {
ElMessage.success(`成功获取 ${response.successCount} 台设备的 UUID`)
} else if (response.successCount > 0) {
ElMessage.warning(`成功 ${response.successCount} 台,失败 ${response.failCount}`)
} else {
ElMessage.error('获取 UUID 失败')
}
} catch (error: any) {
ElMessage.error('批量获取 UUID 失败: ' + (error.message || '未知错误'))
} finally {
batchFetchingUuid.value = false
}
}
// //
const handleBatchPowerCommand = async (command: string) => { const handleBatchPowerCommand = async (command: string) => {

View File

@ -235,6 +235,76 @@ public class DevicesController : ControllerBase
return Ok(ApiResponse<object>.Success(null, "AMT凭据设置成功")); return Ok(ApiResponse<object>.Success(null, "AMT凭据设置成功"));
} }
/// <summary>
/// 批量获取/更新设备 UUID
/// </summary>
[HttpPost("batch-fetch-uuid")]
public async Task<ActionResult<ApiResponse<BatchFetchUuidResponse>>> BatchFetchUuid([FromBody] BatchFetchUuidRequest request)
{
if (request.DeviceIds == null || request.DeviceIds.Count == 0)
{
return Ok(ApiResponse<BatchFetchUuidResponse>.Fail(400, "请选择要获取 UUID 的设备"));
}
var results = new List<FetchUuidResult>();
var hardwareService = HttpContext.RequestServices.GetRequiredService<IHardwareInfoService>();
foreach (var deviceId in request.DeviceIds)
{
var result = new FetchUuidResult { DeviceId = deviceId };
try
{
var device = await _context.AmtDevices.FindAsync(deviceId);
if (device == null)
{
result.Success = false;
result.Error = "设备不存在";
results.Add(result);
continue;
}
result.IpAddress = device.IpAddress;
// 获取硬件信息(强制刷新)
var hardwareInfo = await hardwareService.GetHardwareInfoAsync(deviceId, true);
if (hardwareInfo?.SystemInfo?.Uuid != null)
{
device.SystemUuid = hardwareInfo.SystemInfo.Uuid;
await _context.SaveChangesAsync();
result.Success = true;
result.Uuid = hardwareInfo.SystemInfo.Uuid;
_logger.LogInformation("Successfully fetched UUID for device {Ip}: {Uuid}", device.IpAddress, device.SystemUuid);
}
else
{
result.Success = false;
result.Error = "未能从设备获取 UUID";
}
}
catch (Exception ex)
{
result.Success = false;
result.Error = ex.Message;
_logger.LogWarning(ex, "Failed to fetch UUID for device {DeviceId}", deviceId);
}
results.Add(result);
}
var successCount = results.Count(r => r.Success);
var failCount = results.Count(r => !r.Success);
return Ok(ApiResponse<BatchFetchUuidResponse>.Success(new BatchFetchUuidResponse
{
Results = results,
SuccessCount = successCount,
FailCount = failCount
}, $"成功获取 {successCount} 台设备的 UUID失败 {failCount} 台"));
}
/// <summary> /// <summary>
/// 检测所有设备的在线状态 /// 检测所有设备的在线状态
/// </summary> /// </summary>
@ -467,3 +537,33 @@ public class SetAmtCredentialsRequest
public string Username { get; set; } = string.Empty; public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty; public string Password { get; set; } = string.Empty;
} }
/// <summary>
/// 批量获取 UUID 请求
/// </summary>
public class BatchFetchUuidRequest
{
public List<long> DeviceIds { get; set; } = new();
}
/// <summary>
/// 批量获取 UUID 响应
/// </summary>
public class BatchFetchUuidResponse
{
public List<FetchUuidResult> Results { get; set; } = new();
public int SuccessCount { get; set; }
public int FailCount { get; set; }
}
/// <summary>
/// 单个设备获取 UUID 结果
/// </summary>
public class FetchUuidResult
{
public long DeviceId { get; set; }
public string? IpAddress { get; set; }
public bool Success { get; set; }
public string? Uuid { get; set; }
public string? Error { get; set; }
}