fix: logout requires both device_id and instance_id

Only clear specific instance on specific device
Remove cross-device and all-instances clearing

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 18:24:23 +08:00
parent 008e66f84e
commit 65a9465af4
+7 -31
View File
@@ -43,17 +43,11 @@ func handleAppLogout(c *gin.Context) {
} }
var req struct { var req struct {
DeviceID string `json:"device_id"` DeviceID string `json:"device_id" binding:"required"`
InstanceID string `json:"instance_id"` InstanceID string `json:"instance_id" binding:"required"`
} }
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, 400, "参数错误") response.Error(c, 400, "device_id 和 instance_id 为必填参数")
return
}
// 只有 device_id 没有 instance_id 时提示错误
if req.DeviceID != "" && req.InstanceID == "" {
response.Error(c, 400, "缺少 instance_id 参数")
return return
} }
@@ -69,15 +63,7 @@ func handleAppLogout(c *gin.Context) {
return return
} }
deviceIDs := make([]uint, len(devices)) // 查找指定设备
for i, d := range devices {
deviceIDs[i] = d.ID
}
var deletedCount int64
if req.InstanceID != "" && req.DeviceID != "" {
// 清除指定设备的指定实例
var targetDeviceID uint var targetDeviceID uint
for _, d := range devices { for _, d := range devices {
if d.DeviceID == req.DeviceID { if d.DeviceID == req.DeviceID {
@@ -91,20 +77,10 @@ func handleAppLogout(c *gin.Context) {
return return
} }
// 清除指定实例
result := database.DB.Where("device_id = ? AND instance_id = ?", targetDeviceID, req.InstanceID).Delete(&model.DeviceSession{}) result := database.DB.Where("device_id = ? AND instance_id = ?", targetDeviceID, req.InstanceID).Delete(&model.DeviceSession{})
deletedCount = result.RowsAffected deletedCount := result.RowsAffected
log.Printf("[INFO] Logout: deleted session for device %s, instance %s, count %d", req.DeviceID, req.InstanceID, deletedCount) log.Printf("[INFO] Logout: user %d, device %s, instance %s, deleted %d", userID, req.DeviceID, req.InstanceID, deletedCount)
} else if req.InstanceID != "" {
// 清除指定实例(跨设备)
result := database.DB.Where("device_id IN ? AND instance_id = ?", deviceIDs, req.InstanceID).Delete(&model.DeviceSession{})
deletedCount = result.RowsAffected
log.Printf("[INFO] Logout: deleted instance %s across all devices, count %d", req.InstanceID, deletedCount)
} else {
// 清除该用户所有实例
result := database.DB.Where("device_id IN ?", deviceIDs).Delete(&model.DeviceSession{})
deletedCount = result.RowsAffected
log.Printf("[INFO] Logout: deleted all sessions for user %d, count %d", userID, deletedCount)
}
service.LogVerification(c, &app.ID, uintPtr(userID.(uint)), "logout", fmt.Sprintf("用户登出,清除 %d 个实例", deletedCount), req.DeviceID, nil) service.LogVerification(c, &app.ID, uintPtr(userID.(uint)), "logout", fmt.Sprintf("用户登出,清除 %d 个实例", deletedCount), req.DeviceID, nil)