Files
glkvm-cloud/ui/src/api/device.ts
T
GL.iNet-Yongping.Xie 8648908793 fix(cmd): fix execute-command execution and result display
- address the live device by ddns and call /cmd via bare axios
- write the response body once on timeout (was written twice)
- group results by outcome and show stderr in the detail dialog
- default user to root, minimum wait 1s, warn on long/streaming commands

Signed-off-by: GL.iNet-Yongping.Xie <yongping.xie@gl-inet.com>
2026-08-20 00:39:17 -07:00

89 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* @Author: shufei.han
* @Date: 2025-06-11 11:48:02
* @LastEditors: LPY
* @LastEditTime: 2026-03-09 12:08:45
* @FilePath: \glkvm-cloud\ui\src\api\device.ts
* @Description: 设备相关API
*/
import { ExecuteCommandParams, type DeviceInfo } from '@/models/device'
import request from './request'
import axios from 'axios'
/** 获取设备列表(服务端分页:page/pageSize;q 为搜索词;unassigned 仅未分配) */
export const getDeviceListApi = (params?: {
groupId?: number
sortBy?: string
order?: 'asc' | 'desc'
page?: number
pageSize?: number
q?: string
unassigned?: boolean
status?: 'online' | 'offline'
}) => {
return request<{ items: DeviceInfo[], page: number, pageSize: number, total: number }>({
url: '/api/devices',
params,
})
}
/** 获取添加设备脚本 */
export const getAddDeviceScriptInfoApi = () => {
return request({
url: '/api/script-info',
})
}
/** 执行命令 */
// The /cmd endpoint is served directly by rttys and does NOT use the cloud
// `{code:"OK",...}` envelope: success is {code:0,stdout,stderr,devid}, an rtty error is
// {err:1xxx,msg}, and wait=0 returns an empty body. Routing it through httpService's
// interceptor would (a) reject even a successful command (code 0 !== "OK") and (b) pop a
// "Server Error Message" toast via showErrorMessage. Use a bare axios call to bypass the
// interceptor entirely; it is same-origin, so the `sid` auth cookie is still sent, and the
// caller reads the native body from res.data.
export const reqExecuteCommand = (data: ExecuteCommandParams) => {
return axios.post(`/cmd/${data.id}?group=${data.group}&wait=${data.wait}`, data)
}
/** 修改描述 */
export const reqEditDescription = (deviceId: number, data: { description: string }) => {
return request({
url: `/api/devices/${deviceId}`,
method: 'PUT',
data,
})
}
/** 删除设备 */
export const reqDeleteDevice = (deviceId: number) => {
return request({
url: `/api/devices/${deviceId}`,
method: 'DELETE',
})
}
/** 获取设备组列表(用于“设备移动至设备组”的下拉) */
export const reqDeviceGroupListOptions = () => {
return request<{ items: { groupId: number, name: string }[] }>({
url: '/api/device-groups/options',
})
}
/** 将设备移动至设备组(批量) */
export const reqMoveDevicesToDeviceGroup = (data: { deviceIds: number[], groupId: number }) => {
return request({
url: '/api/devices/move-to-device-group',
method: 'POST',
data,
})
}
/** 添加设备组 */
export const reqAddDeviceGroup = (data: { name: string, description?: string, userGroupIds?: number[], deviceIds?: number[] }) => {
return request<{ id: number }>({
url: '/api/device-groups',
method: 'POST',
data,
})
}