fix: 设备最后连接时间返回 UTC 时间戳,修复时区显示问题

This commit is contained in:
rainv123
2026-05-27 17:35:10 +08:00
parent ff011d3c44
commit ec5063f050
7 changed files with 52 additions and 7 deletions
@@ -29,6 +29,7 @@ import xiaozhi.modules.device.dto.DeviceUnBindDTO;
import xiaozhi.modules.device.dto.DeviceUpdateDTO;
import xiaozhi.modules.device.entity.DeviceEntity;
import xiaozhi.modules.device.service.DeviceService;
import xiaozhi.modules.device.vo.UserShowDeviceListVO;
import xiaozhi.modules.security.user.SecurityUser;
import xiaozhi.modules.sys.service.SysParamsService;
@@ -78,10 +79,10 @@ public class DeviceController {
@GetMapping("/bind/{agentId}")
@Operation(summary = "获取已绑定设备")
@RequiresPermissions("sys:role:normal")
public Result<List<DeviceEntity>> getUserDevices(@PathVariable String agentId) {
public Result<List<UserShowDeviceListVO>> getUserDevices(@PathVariable String agentId) {
UserDetail user = SecurityUser.getUser();
List<DeviceEntity> devices = deviceService.getUserDevices(user.getId(), agentId);
return new Result<List<DeviceEntity>>().ok(devices);
List<UserShowDeviceListVO> devices = deviceService.getUserDeviceList(user.getId(), agentId);
return new Result<List<UserShowDeviceListVO>>().ok(devices);
}
@PostMapping("/bind/{agentId}")
@@ -30,6 +30,11 @@ public interface DeviceService extends BaseService<DeviceEntity> {
*/
List<DeviceEntity> getUserDevices(Long userId, String agentId);
/**
* 获取用户指定智能体的设备列表(带时区处理),
*/
List<UserShowDeviceListVO> getUserDeviceList(Long userId, String agentId);
/**
* 解绑设备
*/
@@ -298,6 +298,21 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
return baseDao.selectList(wrapper);
}
@Override
public List<UserShowDeviceListVO> getUserDeviceList(Long userId, String agentId) {
List<DeviceEntity> devices = getUserDevices(userId, agentId);
return devices.stream().map(device -> {
UserShowDeviceListVO vo = ConvertUtils.sourceToTarget(device, UserShowDeviceListVO.class);
// 手动设置deviceTypeConvertUtils按字段名匹配,board无法直接映射到deviceType
vo.setDeviceType(device.getBoard());
// 设置UTC时间戳供前端使用时区转换
if (device.getLastConnectedAt() != null) {
vo.setLastConnectedAtTimestamp(device.getLastConnectedAt().getTime());
}
return vo;
}).toList();
}
@Override
public void unbindDevice(Long userId, String deviceId) {
// 先查询设备信息,获取agentId
@@ -356,6 +371,10 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
sysUserUtilService.assignUsername(device.getUserId(),
vo::setBindUserName);
vo.setDeviceType(device.getBoard());
// 设置UTC时间戳供前端使用时区转换
if (device.getLastConnectedAt() != null) {
vo.setLastConnectedAtTimestamp(device.getLastConnectedAt().getTime());
}
return vo;
}).toList();
// 计算页数
@@ -1,8 +1,11 @@
package xiaozhi.modules.device.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Date;
@Data
@Schema(description = "用户显示设备列表VO")
public class UserShowDeviceListVO {
@@ -28,4 +31,11 @@ public class UserShowDeviceListVO {
@Schema(description = "最近对话时间")
private String recentChatTime;
@Schema(description = "最后连接时间(UTC毫秒)")
private Long lastConnectedAtTimestamp;
@Schema(description = "绑定时间")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTC")
private Date createDate;
}
@@ -12,7 +12,7 @@ spring:
druid:
#MySQL
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
url: jdbc:mysql://127.0.0.1:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&nullCatalogMeansCurrent=true
username: root
password: 123456
initial-size: 10