diff --git a/sql/ttstd/sn_home_layout.sql b/sql/ttstd/sn_home_layout.sql new file mode 100644 index 00000000..8d4576f7 --- /dev/null +++ b/sql/ttstd/sn_home_layout.sql @@ -0,0 +1,32 @@ +-- ============================================================================= +-- 设备首页分页布局表(sys_sn_home_layout) +-- 说明:家属端可远程调整老人端(ElderlyDialer)桌面首页的分页顺序与默认页。 +-- page_order 为逗号分隔的页面槽位顺序,取值: +-- settings - 设置页 +-- contact - 联系人页 +-- home - 首页(天气/时间/应用宫格) +-- apps - 应用页,数量随安装应用动态生成,恒排在最后,不参与排序 +-- default_page 为开机 / 按 Home 键时展示的页面,必须是 page_order 中的槽位。 +-- 无记录的设备按默认布局处理(settings,contact,home + home), +-- 与历史版本行为保持一致。 +-- ============================================================================= +CREATE TABLE IF NOT EXISTS `sys_sn_home_layout` +( + `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID', + `serialno` VARCHAR(64) NOT NULL COMMENT '设备序列号(关联 sys_sn.serialno)', + + -- ===================== 布局配置 ===================== + `page_order` VARCHAR(128) NOT NULL DEFAULT 'settings,contact,home' COMMENT '首页分页顺序(逗号分隔:settings/contact/home,apps 恒在最后)', + `default_page` VARCHAR(32) NOT NULL DEFAULT 'home' COMMENT '默认展示页(home/contact/settings)', + + -- ===================== 通用字段 ===================== + `create_time` DATETIME DEFAULT NULL COMMENT '创建时间', + `update_time` DATETIME DEFAULT NULL COMMENT '更新时间', + `is_deleted` TINYINT DEFAULT 0 COMMENT '逻辑删除(0未删 1已删)', + + PRIMARY KEY (`id`), + UNIQUE KEY `uk_serialno` (`serialno`), + KEY `idx_is_deleted` (`is_deleted`) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COMMENT = '设备首页分页布局表'; diff --git a/src/main/java/com/youlai/boot/client/controller/ClientController.java b/src/main/java/com/youlai/boot/client/controller/ClientController.java index 68aa305f..d5847a8c 100644 --- a/src/main/java/com/youlai/boot/client/controller/ClientController.java +++ b/src/main/java/com/youlai/boot/client/controller/ClientController.java @@ -11,11 +11,13 @@ import com.youlai.boot.device.converter.DevicePolicyConverter; import com.youlai.boot.device.model.dto.WallpaperFileDTO; import com.youlai.boot.device.model.entity.SnDeviceInfo; import com.youlai.boot.device.model.entity.SnDevicePolicy; +import com.youlai.boot.device.model.req.DeviceHomeLayoutReq; import com.youlai.boot.device.model.req.DevicePolicyReq; import com.youlai.boot.device.model.req.DevicePolicyToggleReq; import com.youlai.boot.device.model.req.WallpaperOrderReq; import com.youlai.boot.device.model.vo.DeviceAlarmVO; import com.youlai.boot.device.model.vo.DeviceApkInfoVO; +import com.youlai.boot.device.model.vo.DeviceHomeLayoutVO; import com.youlai.boot.device.model.vo.DevicePictureVO; import com.youlai.boot.device.model.vo.WallpaperUploadVO; import com.youlai.boot.device.model.vo.DeviceBriefVO; @@ -27,6 +29,7 @@ import com.youlai.boot.device.model.vo.DeviceWallpaperVO; import com.youlai.boot.device.model.vo.ScreenshotVO; import com.youlai.boot.device.service.ApkInstallService; import com.youlai.boot.device.service.DeviceAlarmService; +import com.youlai.boot.device.service.DeviceHomeLayoutService; import com.youlai.boot.device.service.DeviceOnlineService; import com.youlai.boot.device.service.DevicePictureService; import com.youlai.boot.device.service.DevicePolicyService; @@ -88,6 +91,7 @@ public class ClientController { private final DevicePolicyService devicePolicyService; private final DevicePolicyConverter devicePolicyConverter; private final DeviceWallpaperService deviceWallpaperService; + private final DeviceHomeLayoutService deviceHomeLayoutService; private final DevicePictureService devicePictureService; private final DeviceAlarmService deviceAlarmService; private final DeviceOnlineService deviceOnlineService; @@ -907,6 +911,57 @@ public class ClientController { } } + // ===================== 首页布局(家属端) ===================== + + @Operation(summary = "获取设备首页布局(家属端)", + description = "返回首页分页顺序(pages)与默认展示页(defaultPage),未配置时返回默认布局") + @PreAuthorize("isAuthenticated()") + @GetMapping("/home-layout") + public Result getHomeLayout( + @Parameter(description = "设备序列号") @RequestParam("sn") String sn + ) { + try { + checkBound(sn); + return Result.success(deviceHomeLayoutService.getBySn(sn)); + } catch (BusinessException be) { + return Result.failed(be.getMessage()); + } catch (Exception e) { + log.error("getHomeLayout error, sn: {}", sn, e); + return Result.failed("获取首页布局失败: " + e.getMessage()); + } + } + + @Operation(summary = "设置设备首页布局(家属端)", + description = "保存首页分页顺序与默认展示页,并推送设备(contentType = 31);设备收到后重建分页") + @PreAuthorize("isAuthenticated()") + @PutMapping("/home-layout") + public Result updateHomeLayout( + @Parameter(description = "设备序列号") @RequestParam("sn") String sn, + @RequestBody DeviceHomeLayoutReq req + ) { + try { + checkBound(sn); + if (req == null) { + return Result.failed("首页布局不能为空"); + } + DeviceHomeLayoutVO vo = deviceHomeLayoutService.saveLayout(sn, req.getPages(), req.getDefaultPage()); + if (vo == null) { + return Result.failed("首页布局保存失败"); + } + // 配置已落库即为成功,推送失败不影响返回的顺序(与壁纸排序接口一致); + // 设备离线时由 JPush 保留离线消息,联网后重新投递再触发拉取 + if (!deviceHomeLayoutService.pushLayoutToDevice(sn)) { + log.warn("updateHomeLayout saved but push failed, sn: {}", sn); + } + return Result.success(vo); + } catch (BusinessException be) { + return Result.failed(be.getMessage()); + } catch (Exception e) { + log.error("updateHomeLayout error, sn: {}", sn, e); + return Result.failed("设置首页布局失败: " + e.getMessage()); + } + } + private void copyPolicy(DevicePolicyReq src, SnDevicePolicy target) { target.setUsbData(src.getUsbData()); target.setTimeSetting(src.getTimeSetting()); diff --git a/src/main/java/com/youlai/boot/device/controller/MobileController.java b/src/main/java/com/youlai/boot/device/controller/MobileController.java index 595cd984..ed36e226 100644 --- a/src/main/java/com/youlai/boot/device/controller/MobileController.java +++ b/src/main/java/com/youlai/boot/device/controller/MobileController.java @@ -28,6 +28,7 @@ import com.youlai.boot.device.model.req.DevicePolicyReq; import com.youlai.boot.device.model.req.DevicePolicyToggleReq; import com.youlai.boot.device.model.vo.DeveloperOptionsVO; import com.youlai.boot.device.model.vo.DeviceAlarmVO; +import com.youlai.boot.device.model.vo.DeviceHomeLayoutVO; import com.youlai.boot.device.model.vo.DevicePolicyVO; import com.youlai.boot.device.model.vo.DeviceWallpaperVO; import com.youlai.boot.device.converter.DevicePolicyConverter; @@ -76,6 +77,7 @@ public class MobileController { private final DevicePolicyConverter devicePolicyConverter; private final DeviceSignatureVerifier deviceSignatureVerifier; private final DeviceWallpaperService deviceWallpaperService; + private final DeviceHomeLayoutService deviceHomeLayoutService; private final DeviceAlarmService deviceAlarmService; private final FileService fileService; @@ -693,6 +695,23 @@ public class MobileController { } } + @Operation(summary = "获取设备首页布局配置", + description = "返回首页分页顺序(pages,按展示先后)与默认展示页(defaultPage);未配置时返回默认布局") + @GetMapping("/get_home_layout") + public Result getHomeLayout( + @RequestHeader(value = "X-Device-SN") String sn + ) { + try { + if (sn == null || sn.trim().isEmpty()) { + return Result.failed("设备序列号不能为空"); + } + return Result.success(deviceHomeLayoutService.getBySn(sn.trim())); + } catch (Exception e) { + logger.error("getHomeLayout error, sn: {}", sn, e); + return Result.failed("获取首页布局失败: " + e.getMessage()); + } + } + // ===================== 设备闹钟(设备端同步) ===================== @Operation(summary = "获取设备闹钟列表", description = "返回该设备全部闹钟(按时间升序),设备收到推送或启动时拉取并覆盖本地 Room") diff --git a/src/main/java/com/youlai/boot/device/mapper/DeviceHomeLayoutMapper.java b/src/main/java/com/youlai/boot/device/mapper/DeviceHomeLayoutMapper.java new file mode 100644 index 00000000..390e174f --- /dev/null +++ b/src/main/java/com/youlai/boot/device/mapper/DeviceHomeLayoutMapper.java @@ -0,0 +1,16 @@ +package com.youlai.boot.device.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.youlai.boot.device.model.entity.SnDeviceHomeLayout; +import org.apache.ibatis.annotations.Mapper; + +/** + * 设备首页分页布局 Mapper + * + * @author TTSTD + * @since 2026-11-25 + */ +@Mapper +public interface DeviceHomeLayoutMapper extends BaseMapper { + +} diff --git a/src/main/java/com/youlai/boot/device/model/entity/SnDeviceHomeLayout.java b/src/main/java/com/youlai/boot/device/model/entity/SnDeviceHomeLayout.java new file mode 100644 index 00000000..1aaaa049 --- /dev/null +++ b/src/main/java/com/youlai/boot/device/model/entity/SnDeviceHomeLayout.java @@ -0,0 +1,38 @@ +package com.youlai.boot.device.model.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.youlai.boot.common.base.BaseEntity; +import lombok.Getter; +import lombok.Setter; + +/** + * 设备首页分页布局实体(家属端远程配置老人端桌面分页顺序,每个 SN 唯一一条) + * + * @author TTSTD + * @since 2026-11-25 + */ +@Getter +@Setter +@TableName("sys_sn_home_layout") +public class SnDeviceHomeLayout extends BaseEntity { + + /** + * 设备序列号(关联 sys_sn.serialno) + */ + private String serialno; + + /** + * 首页分页顺序(逗号分隔:settings/contact/home;apps 恒在最后,不参与排序) + */ + private String pageOrder; + + /** + * 默认展示页(home/contact/settings) + */ + private String defaultPage; + + /** + * 逻辑删除标志(0未删除 1已删除) + */ + private boolean isDeleted; +} diff --git a/src/main/java/com/youlai/boot/device/model/req/DeviceHomeLayoutReq.java b/src/main/java/com/youlai/boot/device/model/req/DeviceHomeLayoutReq.java new file mode 100644 index 00000000..7eae8fe6 --- /dev/null +++ b/src/main/java/com/youlai/boot/device/model/req/DeviceHomeLayoutReq.java @@ -0,0 +1,30 @@ +package com.youlai.boot.device.model.req; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.List; + +/** + * 首页分页布局设置请求体(家属端) + * + * @author TTSTD + * @since 2026-11-25 + */ +@Schema(description = "首页分页布局设置请求") +@Data +public class DeviceHomeLayoutReq { + + /** + * 首页分页顺序,按展示先后排列;取值 home/contact/settings。 + * 非法值、重复项会被忽略;home 为必备项,缺失时自动补到首位。 + */ + @Schema(description = "首页分页顺序(home/contact/settings)") + private List pages; + + /** + * 默认展示页;不在 pages 中时回退为 home + */ + @Schema(description = "默认展示页(home/contact/settings)") + private String defaultPage; +} diff --git a/src/main/java/com/youlai/boot/device/model/vo/DeviceHomeLayoutVO.java b/src/main/java/com/youlai/boot/device/model/vo/DeviceHomeLayoutVO.java new file mode 100644 index 00000000..33ac1e0f --- /dev/null +++ b/src/main/java/com/youlai/boot/device/model/vo/DeviceHomeLayoutVO.java @@ -0,0 +1,32 @@ +package com.youlai.boot.device.model.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.List; + +/** + * 设备首页分页布局视图对象 + * + * @author TTSTD + * @since 2026-11-25 + */ +@Schema(description = "设备首页分页布局信息") +@Data +public class DeviceHomeLayoutVO { + + @Schema(description = "设备序列号") + private String serialno; + + /** + * 首页分页顺序,按展示先后排列;取值 home/contact/settings + */ + @Schema(description = "首页分页顺序(home/contact/settings,按展示先后排列)") + private List pages; + + /** + * 默认展示页,必须是 pages 中的一项 + */ + @Schema(description = "默认展示页(home/contact/settings)") + private String defaultPage; +} diff --git a/src/main/java/com/youlai/boot/device/service/DeviceHomeLayoutService.java b/src/main/java/com/youlai/boot/device/service/DeviceHomeLayoutService.java new file mode 100644 index 00000000..d2f11893 --- /dev/null +++ b/src/main/java/com/youlai/boot/device/service/DeviceHomeLayoutService.java @@ -0,0 +1,63 @@ +package com.youlai.boot.device.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.youlai.boot.device.model.entity.SnDeviceHomeLayout; +import com.youlai.boot.device.model.vo.DeviceHomeLayoutVO; + +import java.util.List; + +/** + * 设备首页分页布局服务 + * + *

家属端调整老人端桌面分页顺序后落库,并通过 JPush 推送变更通知(contentType = 31), + * 设备端收到后主动拉取本配置并重建分页。 + * + * @author TTSTD + * @since 2026-11-25 + */ +public interface DeviceHomeLayoutService extends IService { + + /** 首页槽位:首页(天气/时间/应用宫格) */ + String SLOT_HOME = "home"; + + /** 首页槽位:联系人页 */ + String SLOT_CONTACT = "contact"; + + /** 首页槽位:设置页 */ + String SLOT_SETTINGS = "settings"; + + /** 首页槽位:应用页(动态生成,恒排在最后,不参与排序) */ + String SLOT_APPS = "apps"; + + /** 默认分页顺序,与历史版本保持一致(设置 → 联系人 → 首页) */ + List DEFAULT_PAGES = List.of(SLOT_SETTINGS, SLOT_CONTACT, SLOT_HOME); + + /** 默认展示页 */ + String DEFAULT_PAGE = SLOT_HOME; + + /** + * 查询设备首页布局配置;无记录时返回默认布局,不会返回 null。 + * + * @param sn 设备序列号 + * @return 首页布局视图对象(pages 已规范化、defaultPage 一定包含其中) + */ + DeviceHomeLayoutVO getBySn(String sn); + + /** + * 保存设备首页布局配置(按 SN 新增或更新)。 + * + * @param sn 设备序列号 + * @param pages 首页分页顺序(可含非法值,内部会规范化) + * @param defaultPage 默认展示页(不在 pages 中时回退为 home) + * @return 保存后的布局视图对象;参数异常或保存失败时返回 null + */ + DeviceHomeLayoutVO saveLayout(String sn, List pages, String defaultPage); + + /** + * 推送首页布局变更到设备(contentType = 31,设备收到后主动拉取配置并重建分页)。 + * + * @param sn 设备序列号 + * @return 是否推送成功 + */ + boolean pushLayoutToDevice(String sn); +} diff --git a/src/main/java/com/youlai/boot/device/service/impl/DeviceHomeLayoutServiceImpl.java b/src/main/java/com/youlai/boot/device/service/impl/DeviceHomeLayoutServiceImpl.java new file mode 100644 index 00000000..57eafed4 --- /dev/null +++ b/src/main/java/com/youlai/boot/device/service/impl/DeviceHomeLayoutServiceImpl.java @@ -0,0 +1,228 @@ +package com.youlai.boot.device.service.impl; + +import cn.jiguang.sdk.api.PushApi; +import cn.jiguang.sdk.bean.push.PushSendParam; +import cn.jiguang.sdk.bean.push.PushSendResult; +import cn.jiguang.sdk.bean.push.message.custom.CustomMessage; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.youlai.boot.device.mapper.DeviceHomeLayoutMapper; +import com.youlai.boot.device.model.entity.SnDeviceHomeLayout; +import com.youlai.boot.device.model.vo.DeviceHomeLayoutVO; +import com.youlai.boot.device.service.DeviceHomeLayoutService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +/** + * 设备首页分页布局服务实现 + * + *

布局以「一个 SN 一行」存储({@code sys_sn_home_layout}),{@code page_order} 为逗号分隔的 + * 槽位顺序,{@code default_page} 为默认展示页。保存后通过 JPush 推送变更通知 + * (contentType = 31),设备端收到后主动拉取并重建分页。 + * + *

规范化规则(服务端为唯一数据源,脏数据不得下发到设备): + *

    + *
  • 只接受 home / contact / settings,其余(含 apps)忽略;
  • + *
  • 重复项去重;
  • + *
  • home 为必备页,缺失时补到首位,避免设备端没有任何固定分页;
  • + *
  • defaultPage 不在顺序中时回退为 home。
  • + *
+ * + * @author TTSTD + * @since 2026-11-25 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class DeviceHomeLayoutServiceImpl + extends ServiceImpl + implements DeviceHomeLayoutService { + + /** 首页布局推送业务码(与设备端 PushExecutor 约定,contentType = 31) */ + private static final String DEVICE_HOME_LAYOUT = "31"; + + private static final String PUSH_APP_KEY = "d779178d9900d4fb5d633678"; + private static final String PUSH_MASTER_SECRET = "be0e197d30fec7bec118a70d"; + + /** 可排序的固定分页槽位(apps 由设备端动态生成,恒在最后,不参与排序) */ + private static final List SORTABLE_SLOTS = + List.of(SLOT_HOME, SLOT_CONTACT, SLOT_SETTINGS); + + private final PushApi pushApi = new PushApi.Builder() + .setAppKey(PUSH_APP_KEY) + .setMasterSecret(PUSH_MASTER_SECRET) + .build(); + + @Override + public DeviceHomeLayoutVO getBySn(String sn) { + if (StringUtils.isBlank(sn)) { + return null; + } + SnDeviceHomeLayout layout = getRecordBySn(sn); + if (layout == null) { + return buildVo(sn, new ArrayList<>(DEFAULT_PAGES), DEFAULT_PAGE); + } + List pages = sanitizePages(splitOrder(layout.getPageOrder())); + return buildVo(sn, pages, normalizeDefaultPage(layout.getDefaultPage(), pages)); + } + + @Override + public DeviceHomeLayoutVO saveLayout(String sn, List pages, String defaultPage) { + if (StringUtils.isBlank(sn)) { + log.warn("首页布局保存失败, 设备序列号为空"); + return null; + } + List order = sanitizePages(pages); + String page = normalizeDefaultPage(defaultPage, order); + String orderText = String.join(",", order); + + SnDeviceHomeLayout existing = getRecordBySn(sn); + boolean saved; + if (existing == null) { + SnDeviceHomeLayout entity = new SnDeviceHomeLayout(); + entity.setSerialno(sn); + entity.setPageOrder(orderText); + entity.setDefaultPage(page); + saved = this.save(entity); + } else { + saved = this.lambdaUpdate() + .eq(SnDeviceHomeLayout::getId, existing.getId()) + .set(SnDeviceHomeLayout::getPageOrder, orderText) + .set(SnDeviceHomeLayout::getDefaultPage, page) + .update(); + } + if (!saved) { + log.warn("首页布局保存失败, sn: {}, order: {}, default: {}", sn, orderText, page); + return null; + } + log.info("首页布局保存成功, sn: {}, order: {}, default: {}", sn, orderText, page); + return buildVo(sn, order, page); + } + + @Override + public boolean pushLayoutToDevice(String sn) { + if (StringUtils.isBlank(sn)) { + log.warn("首页布局推送:设备序列号不能为空"); + return false; + } + try { + PushSendParam pushSendParam = buildLayoutPushParam(sn); + PushSendResult result = pushApi.send(pushSendParam); + log.info("首页布局推送成功, sn: {}, result: {}", sn, result); + return true; + } catch (Exception e) { + log.error("首页布局推送失败, sn: {}", sn, e); + return false; + } + } + + /** + * 按 SN 查询布局记录;同 SN 存在多行时取最早一行(正常情况由唯一索引保证只有一行)。 + */ + private SnDeviceHomeLayout getRecordBySn(String sn) { + List rows = this.lambdaQuery() + .eq(SnDeviceHomeLayout::getSerialno, sn) + .orderByAsc(SnDeviceHomeLayout::getId) + .list(); + return rows.isEmpty() ? null : rows.get(0); + } + + private DeviceHomeLayoutVO buildVo(String sn, List pages, String defaultPage) { + DeviceHomeLayoutVO vo = new DeviceHomeLayoutVO(); + vo.setSerialno(sn); + vo.setPages(pages); + vo.setDefaultPage(defaultPage); + return vo; + } + + /** + * 把逗号分隔的顺序文本拆成列表(空值安全)。 + */ + private List splitOrder(String orderText) { + if (StringUtils.isBlank(orderText)) { + return new ArrayList<>(); + } + return Arrays.asList(orderText.split(",")); + } + + /** + * 规范化分页顺序:过滤非法槽位、去重、保证 home 存在。 + * + * @param pages 原始顺序(可为 null / 含非法值) + * @return 可安全下发给设备的顺序列表(保证非空且包含 home) + */ + private List sanitizePages(List pages) { + List order = new ArrayList<>(SORTABLE_SLOTS.size()); + if (pages != null) { + for (String raw : pages) { + if (StringUtils.isBlank(raw)) { + continue; + } + String slot = raw.trim().toLowerCase(Locale.ROOT); + if (!SORTABLE_SLOTS.contains(slot) || order.contains(slot)) { + continue; + } + order.add(slot); + } + } + if (order.isEmpty()) { + return new ArrayList<>(DEFAULT_PAGES); + } + if (!order.contains(SLOT_HOME)) { + // 首页承载天气/时间/应用宫格,不允许被隐藏 + order.add(0, SLOT_HOME); + } + return order; + } + + /** + * 规范化默认展示页:不在顺序中时回退为 home,再退化为顺序首项。 + */ + private String normalizeDefaultPage(String defaultPage, List pages) { + String page = defaultPage == null ? "" : defaultPage.trim().toLowerCase(Locale.ROOT); + if (pages.contains(page)) { + return page; + } + if (pages.contains(DEFAULT_PAGE)) { + return DEFAULT_PAGE; + } + return pages.isEmpty() ? DEFAULT_PAGE : pages.get(0); + } + + /** + * 构建首页布局推送参数(contentType = 31)。 + *

只作为「变更通知」,extras 仅携带 SN 便于设备端日志定位, + * 配置本体由设备端回调 {@code /api/v1/sn/get_home_layout} 拉取,避免出现两份不一致的真相。 + */ + private PushSendParam buildLayoutPushParam(String sn) { + PushSendParam pushSendParam = new PushSendParam(); + pushSendParam.setPlatform("android"); + + Map> audience = new HashMap<>(); + Set aliasSet = new HashSet<>(); + aliasSet.add(sn); + audience.put("alias", aliasSet); + pushSendParam.setAudience(audience); + + CustomMessage customMessage = new CustomMessage(); + customMessage.setTitle("deviceHomeLayout"); + customMessage.setContentType(DEVICE_HOME_LAYOUT); + customMessage.setContent("homeLayout"); + + Map extras = new HashMap<>(); + extras.put("serialno", sn); + customMessage.setExtras(extras); + pushSendParam.setCustom(customMessage); + return pushSendParam; + } +}