增加登录器客户端心跳

This commit is contained in:
2026-06-22 12:03:58 +08:00
parent b0629b1001
commit b103192fac
7 changed files with 382 additions and 3 deletions
+81
View File
@@ -637,3 +637,84 @@ func (c *ApiCursorEquipmentController) ActivateByCode() {
"expiredAt": expireTime,
})
}
type cursorHeartbeatPayload struct {
MachineCode string `json:"machineCode"`
MachineCodeSnake string `json:"machine_code"`
}
// Heartbeat POST /api/cursor/equipment/heartbeat
//
// 客户端心跳接口(无需登录),用于上报在线状态。
//
// JSON 示例:
//
// {
// "machineCode": "ABC-123"
// }
func (c *ApiCursorEquipmentController) Heartbeat() {
var p cursorHeartbeatPayload
body := c.Ctx.Input.RequestBody
if len(body) > 0 {
if err := json.Unmarshal(body, &p); err != nil {
c.jsonResult(400, "参数错误", nil)
return
}
}
machineCode := cursorFirstNonEmpty(p.MachineCode, p.MachineCodeSnake)
if machineCode == "" {
machineCode = c.GetString("machineCode")
}
if machineCode == "" {
machineCode = c.GetString("machine_code")
}
if machineCode == "" {
c.jsonResult(400, "缺少参数 machineCode/machine_code(机器码)", nil)
return
}
now := time.Now()
// 查询设备是否存在
var row models.PlatformCursorEquipment
err := models.Orm.QueryTable(new(models.PlatformCursorEquipment)).
Filter("machine_code", machineCode).
Filter("delete_time__isnull", true).
One(&row)
if err == orm.ErrNoRows {
// 设备不存在,可能是第一次运行心跳,也可以允许在此处静默创建,或者返回 404 让客户端先进行 report
// 为了鲁棒性,如果设备未上报过,我们可以直接创建一个基础设备记录
row = models.PlatformCursorEquipment{
MachineCode: machineCode,
Status: 0, // 未激活
LastHeartbeatAt: &now,
CreateTime: now,
}
if _, insertErr := models.Orm.Insert(&row); insertErr != nil {
c.jsonResult(500, "保存设备心跳失败", nil)
return
}
} else if err != nil {
c.jsonResult(500, "设备查询失败", nil)
return
} else {
// 更新最后心跳时间
if _, updateErr := models.Orm.QueryTable(new(models.PlatformCursorEquipment)).
Filter("id", row.ID).
Update(map[string]interface{}{
"last_heartbeat_at": &now,
"update_time": now,
}); updateErr != nil {
c.jsonResult(500, "更新设备心跳失败", nil)
return
}
}
c.jsonResult(200, "success", map[string]interface{}{
"machineCode": machineCode,
"online": true,
})
}