- 删除 FilePath 与 WebMvcConfig,取消 /static/** 本地磁盘映射 - 安装包、壁纸、截图、分类图标、家属端图片全部使用存储服务返回的完整 URL - 设备截图与拍照改存 StorageDir.DEVICE_SCREENSHOT,file_path 保存完整访问地址 - 壁纸/图片收藏按库中完整 URL 直接匹配,去掉相对路径归一化 - 家属于已绑定同一账号的设备再次发起绑定时下发确认请求,支持重置后重新授权
839 lines
37 KiB
Java
839 lines
37 KiB
Java
package com.youlai.boot.device.controller;
|
||
|
||
import com.youlai.boot.common.annotation.Log;
|
||
import com.youlai.boot.common.config.StorageDir;
|
||
import com.youlai.boot.common.enums.ActionTypeEnum;
|
||
import com.youlai.boot.common.enums.LogModuleEnum;
|
||
import com.youlai.boot.common.exception.BusinessException;
|
||
import com.youlai.boot.common.result.Result;
|
||
import com.youlai.boot.common.result.ResultCode;
|
||
import com.youlai.boot.common.util.HashUtils;
|
||
import com.youlai.boot.device.model.entity.SnDeviceSystemInfo;
|
||
import com.youlai.boot.device.model.entity.SnDeviceHardwareInfo;
|
||
import com.youlai.boot.device.model.entity.SnDeviceNetworkInfo;
|
||
import com.youlai.boot.device.model.entity.SnDeviceSecurityInfo;
|
||
import com.youlai.boot.device.model.entity.SnDeviceOtherInfo;
|
||
import com.youlai.boot.device.model.entity.SnDeviceInfo;
|
||
import com.youlai.boot.device.model.req.ApkInstallInfoReq;
|
||
import com.youlai.boot.device.model.req.SnSystemInfoReq;
|
||
import com.youlai.boot.device.model.req.SnHardwareInfoReq;
|
||
import com.youlai.boot.device.model.req.SnNetworkInfoReq;
|
||
import com.youlai.boot.device.model.req.SnSecurityInfoReq;
|
||
import com.youlai.boot.device.model.req.SnOtherInfoReq;
|
||
import com.youlai.boot.device.model.req.SnLocationReq;
|
||
import com.youlai.boot.device.model.entity.SnLocation;
|
||
import com.youlai.boot.device.model.entity.SnDevicePolicy;
|
||
import com.youlai.boot.device.model.entity.SnScreenshot;
|
||
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.DevicePolicyVO;
|
||
import com.youlai.boot.device.model.vo.DeviceWallpaperVO;
|
||
import com.youlai.boot.device.converter.DevicePolicyConverter;
|
||
import com.youlai.boot.device.service.*;
|
||
import com.youlai.boot.framework.security.signature.DeviceSignatureVerifier;
|
||
import com.youlai.boot.support.file.model.FileInfo;
|
||
import com.youlai.boot.support.file.service.FileService;
|
||
import io.swagger.v3.oas.annotations.Operation;
|
||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
import lombok.RequiredArgsConstructor;
|
||
import org.apache.commons.io.FilenameUtils;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.BeanUtils;
|
||
import org.springframework.util.StringUtils;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
import java.time.LocalDateTime;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 设备控制层
|
||
* @author TTSTD
|
||
* @since 2026/04/05
|
||
*/
|
||
@Tag(name = "16.移动设备管理")
|
||
@RestController
|
||
@RequestMapping("/api/v1/sn")
|
||
@RequiredArgsConstructor
|
||
public class MobileController {
|
||
private final DeviceService deviceService;
|
||
private final ScreenshotService screenshotService;
|
||
private final LocationService locationService;
|
||
private final DeveloperService developerService;
|
||
private final ApkInstallService apkInstallService;
|
||
private final AppIconService appIconService;
|
||
private final SystemInfoService systemInfoService;
|
||
private final HardwareInfoService hardwareInfoService;
|
||
private final NetworkInfoService networkInfoService;
|
||
private final SecurityInfoService securityInfoService;
|
||
private final OtherInfoService otherInfoService;
|
||
private final DevicePolicyService devicePolicyService;
|
||
private final DevicePolicyConverter devicePolicyConverter;
|
||
private final DeviceSignatureVerifier deviceSignatureVerifier;
|
||
private final DeviceWallpaperService deviceWallpaperService;
|
||
private final DeviceAlarmService deviceAlarmService;
|
||
private final FileService fileService;
|
||
|
||
private final Logger logger = LoggerFactory.getLogger(MobileController.class);
|
||
|
||
|
||
@Operation(summary = "设备确认绑定手机号(绑定即注册)",
|
||
description = "设备端同意家属绑定请求后调用。接口豁免标准签名过滤,改用 PoP 验签:\n" +
|
||
"1) 用请求内公钥验证设备对 challenge(sn:ts:nonce) 的 ECDSA 签名;\n" +
|
||
"2) 自动注册设备(SN ↔ 公钥,不存在则新建,已存在则更新公钥);\n" +
|
||
"3) 写入 sys_sn.snMobile 完成绑定,并向设备回推绑定成功。\n" +
|
||
"设备首次绑定前可能尚未注册(无公钥),故不能走标准 ECDSA 验签,而是与注册接口相同的 PoP 方式。")
|
||
@PostMapping("/bind_phone")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.UPDATE)
|
||
public Result<?> bindPhone(
|
||
@RequestParam() String sn,
|
||
@RequestParam(value = "model", required = false) String model,
|
||
@RequestParam(value = "publicKey", required = false) String publicKey,
|
||
@RequestParam(value = "challenge", required = false) String challenge,
|
||
@RequestParam(value = "signature", required = false) String signature,
|
||
@RequestParam() String phone) {
|
||
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
if (phone == null || phone.trim().isEmpty()) {
|
||
return Result.failed("绑定手机号不能为空");
|
||
}
|
||
|
||
try {
|
||
// 1. PoP 验签:设备可能尚未注册(无公钥),用请求内公钥验证 challenge 签名
|
||
if (publicKey == null || publicKey.trim().isEmpty()
|
||
|| challenge == null || challenge.trim().isEmpty()
|
||
|| signature == null || signature.trim().isEmpty()) {
|
||
return Result.failed("设备公钥/挑战值/签名不能为空");
|
||
}
|
||
if (!verifyRegisterChallenge(publicKey, challenge, signature)) {
|
||
return Result.failed(ResultCode.MOBILE_SIGN_INVALID, "设备签名验证失败");
|
||
}
|
||
|
||
// 2. 查已有记录:封禁与手机号冲突必须在写公钥之前校验,
|
||
// 否则错误手机号的请求会先把公钥换掉,导致真正绑定的设备无法再验签
|
||
SnDeviceInfo device = deviceService.lambdaQuery()
|
||
.eq(SnDeviceInfo::getSerialno, sn)
|
||
.one();
|
||
if (device != null) {
|
||
if (device.getStatus() != null && device.getStatus() == 0) {
|
||
return Result.failed(ResultCode.MOBILE_DEVICE_BANNED);
|
||
}
|
||
if (StringUtils.hasText(device.getSnMobile())
|
||
&& !device.getSnMobile().equals(phone.trim())) {
|
||
return Result.failed("该设备已绑定其他手机号");
|
||
}
|
||
if (StringUtils.hasText(device.getPublicKey())
|
||
&& !device.getPublicKey().equals(publicKey)) {
|
||
logger.info("设备公钥变更(重新授权), sn: {}, phone: {}", sn, phone.trim());
|
||
}
|
||
}
|
||
|
||
// 3. 注册设备(绑定即注册):SN ↔ 公钥 + 手机号一次写入
|
||
if (device == null) {
|
||
device = new SnDeviceInfo();
|
||
device.setSerialno(sn);
|
||
device.setStatus(1);
|
||
device.setSnModel(model);
|
||
device.setPublicKey(publicKey);
|
||
device.setSnMobile(phone.trim());
|
||
boolean saved = deviceService.save(device);
|
||
if (!saved) {
|
||
return Result.failed("设备注册失败");
|
||
}
|
||
} else {
|
||
device.setPublicKey(publicKey);
|
||
if (model != null && !model.trim().isEmpty()) {
|
||
device.setSnModel(model);
|
||
}
|
||
device.setSnMobile(phone.trim());
|
||
deviceService.updateById(device);
|
||
}
|
||
|
||
logger.info("设备确认绑定成功(绑定即注册), sn: {}, phone: {}", sn, phone.trim());
|
||
|
||
// 4. 回推绑定成功通知到设备端(contentType=21)
|
||
deviceService.pushBindSuccess(sn, phone.trim());
|
||
|
||
Map<String, Object> result = new HashMap<>();
|
||
result.put("sn", sn);
|
||
result.put("phone", phone.trim());
|
||
result.put("message", "设备绑定成功");
|
||
return Result.success(result);
|
||
} catch (BusinessException be) {
|
||
return Result.failed(be.getMessage());
|
||
} catch (Exception e) {
|
||
logger.error("bindPhone error, sn: {}, phone: {}", sn, phone, e);
|
||
return Result.failed("绑定手机号失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 校验绑定请求的 PoP 签名:用请求内上传的公钥验证设备对 challenge(sn:ts:nonce)的 ECDSA 签名。
|
||
*
|
||
* <p><b>不校验 challenge 内时间戳的时效</b>,原因:</p>
|
||
* <ul>
|
||
* <li>设备(老人机/定制 ROM)系统时间可能长期未校准,强制 2 分钟窗口会导致绑定失败
|
||
* (实测设备时间与服务器偏差可达数天);</li>
|
||
* <li>{@code bind_phone} 的绑定语义天然防重放:一台设备只能绑定一个手机号,
|
||
* 重复绑定同一手机号是幂等操作,攻击者重放也无法篡改为其他手机号
|
||
* (必须持有私钥重新签名)。</li>
|
||
* </ul>
|
||
* PoP 验签本身已证明「请求者持有该公钥对应的私钥」。
|
||
*/
|
||
private boolean verifyRegisterChallenge(String publicKey, String challenge, String signature) {
|
||
try {
|
||
// 解析 challenge:sn:ts:nonce(格式校验,时间戳仅作为签名原文的一部分,不校验时效)
|
||
String[] parts = challenge.split(":", 3);
|
||
if (parts.length != 3) {
|
||
return false;
|
||
}
|
||
Long.parseLong(parts[1]);
|
||
// 用公钥验签
|
||
return deviceSignatureVerifier.verify(publicKey, challenge, signature);
|
||
} catch (Exception e) {
|
||
logger.error("verifyRegisterChallenge error, sn: {}, challenge: {}", challenge, e.getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传设备硬件信息")
|
||
@PostMapping("/update_system_info")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.UPDATE_SYSTEM_INFO)
|
||
public Result<Void> updateSystemInfo(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody SnSystemInfoReq snSystemInfoReq
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
if (snSystemInfoReq == null) {
|
||
return Result.failed("硬件信息不能为空");
|
||
}
|
||
|
||
if (snSystemInfoReq.getDeviceModel() != null && !snSystemInfoReq.getDeviceModel().trim().isEmpty()) {
|
||
SnDeviceInfo deviceInfo = deviceService.lambdaQuery()
|
||
.eq(SnDeviceInfo::getSerialno, sn)
|
||
.one();
|
||
if (deviceInfo != null) {
|
||
deviceInfo.setSnModel(snSystemInfoReq.getDeviceModel());
|
||
deviceInfo.setUpdateTime(LocalDateTime.now());
|
||
deviceService.updateById(deviceInfo);
|
||
}
|
||
}
|
||
|
||
SnDeviceSystemInfo systemInfo = new SnDeviceSystemInfo();
|
||
BeanUtils.copyProperties(snSystemInfoReq, systemInfo);
|
||
systemInfo.setSerialno(sn);
|
||
systemInfo.setUpdateTime(LocalDateTime.now());
|
||
|
||
SnDeviceSystemInfo existingSystemInfo = systemInfoService.lambdaQuery()
|
||
.eq(SnDeviceSystemInfo::getSerialno, sn)
|
||
.one();
|
||
|
||
boolean saved;
|
||
if (existingSystemInfo != null) {
|
||
systemInfo.setId(existingSystemInfo.getId());
|
||
saved = systemInfoService.updateById(systemInfo);
|
||
} else {
|
||
systemInfo.setCreateTime(LocalDateTime.now());
|
||
saved = systemInfoService.save(systemInfo);
|
||
}
|
||
|
||
if (!saved) {
|
||
return Result.failed("保存硬件信息失败");
|
||
}
|
||
|
||
logger.info("硬件信息上传成功, sn: {}", sn);
|
||
return Result.success();
|
||
|
||
} catch (Exception e) {
|
||
logger.error("updateSystemInfo error, sn: {}", sn, e);
|
||
return Result.failed("上传硬件信息失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传设备其他信息")
|
||
@PostMapping("/update_other_info")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.UPDATE_OTHER_INFO)
|
||
public Result<Void> updateOtherInfo(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody SnOtherInfoReq snOtherInfoReq
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
if (snOtherInfoReq == null) {
|
||
return Result.failed("其他信息不能为空");
|
||
}
|
||
|
||
SnDeviceOtherInfo otherInfo = new SnDeviceOtherInfo();
|
||
BeanUtils.copyProperties(snOtherInfoReq, otherInfo);
|
||
otherInfo.setSerialno(sn);
|
||
otherInfo.setUpdateTime(LocalDateTime.now());
|
||
|
||
SnDeviceOtherInfo existingOtherInfo = otherInfoService.lambdaQuery()
|
||
.eq(SnDeviceOtherInfo::getSerialno, sn)
|
||
.one();
|
||
|
||
boolean saved;
|
||
if (existingOtherInfo != null) {
|
||
otherInfo.setId(existingOtherInfo.getId());
|
||
saved = otherInfoService.updateById(otherInfo);
|
||
} else {
|
||
otherInfo.setCreateTime(LocalDateTime.now());
|
||
saved = otherInfoService.save(otherInfo);
|
||
}
|
||
|
||
if (!saved) {
|
||
return Result.failed("保存其他信息失败");
|
||
}
|
||
|
||
// 如果推送ID不为空,则同步更新设备表的推送ID
|
||
if (snOtherInfoReq.getPushId() != null && !snOtherInfoReq.getPushId().trim().isEmpty()) {
|
||
SnDeviceInfo deviceInfo = deviceService.lambdaQuery()
|
||
.eq(SnDeviceInfo::getSerialno, sn)
|
||
.one();
|
||
if (deviceInfo != null) {
|
||
deviceInfo.setPushId(snOtherInfoReq.getPushId());
|
||
deviceInfo.setUpdateTime(LocalDateTime.now());
|
||
deviceService.updateById(deviceInfo);
|
||
}
|
||
}
|
||
|
||
// 设备上报手机号:仅当设备未建立绑定(snMobile 为空)时同步,避免覆盖家属端手动建立的绑定
|
||
if (snOtherInfoReq.getPhoneNumber() != null && !snOtherInfoReq.getPhoneNumber().trim().isEmpty()) {
|
||
SnDeviceInfo deviceInfo = deviceService.lambdaQuery()
|
||
.eq(SnDeviceInfo::getSerialno, sn)
|
||
.one();
|
||
if (deviceInfo != null && !StringUtils.hasText(deviceInfo.getSnMobile())) {
|
||
deviceInfo.setSnMobile(snOtherInfoReq.getPhoneNumber());
|
||
deviceInfo.setUpdateTime(LocalDateTime.now());
|
||
deviceService.updateById(deviceInfo);
|
||
}
|
||
}
|
||
|
||
logger.info("其他信息上传成功, sn: {}", sn);
|
||
return Result.success();
|
||
|
||
} catch (Exception e) {
|
||
logger.error("updateOtherInfo error, sn: {}", sn, e);
|
||
return Result.failed("上传其他信息失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传设备硬件信息")
|
||
@PostMapping("/update_hardware_info")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.UPDATE_HARDWARE_INFO)
|
||
public Result<Void> updateHardwareInfo(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody SnHardwareInfoReq snHardwareInfoReq
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
if (snHardwareInfoReq == null) {
|
||
return Result.failed("硬件信息不能为空");
|
||
}
|
||
|
||
SnDeviceHardwareInfo hardwareInfo = new SnDeviceHardwareInfo();
|
||
BeanUtils.copyProperties(snHardwareInfoReq, hardwareInfo);
|
||
hardwareInfo.setSerialno(sn);
|
||
hardwareInfo.setUpdateTime(LocalDateTime.now());
|
||
|
||
SnDeviceHardwareInfo existingHardwareInfo = hardwareInfoService.lambdaQuery()
|
||
.eq(SnDeviceHardwareInfo::getSerialno, sn)
|
||
.one();
|
||
|
||
boolean saved;
|
||
if (existingHardwareInfo != null) {
|
||
hardwareInfo.setId(existingHardwareInfo.getId());
|
||
saved = hardwareInfoService.updateById(hardwareInfo);
|
||
} else {
|
||
hardwareInfo.setCreateTime(LocalDateTime.now());
|
||
saved = hardwareInfoService.save(hardwareInfo);
|
||
}
|
||
|
||
if (!saved) {
|
||
return Result.failed("保存硬件信息失败");
|
||
}
|
||
|
||
logger.info("硬件信息上传成功, sn: {}", sn);
|
||
return Result.success();
|
||
|
||
} catch (Exception e) {
|
||
logger.error("updateHardwareInfo error, sn: {}", sn, e);
|
||
return Result.failed("上传硬件信息失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传设备网络信息")
|
||
@PostMapping("/update_network_info")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.UPDATE_NETWORK_INFO)
|
||
public Result<Void> updateNetworkInfo(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody SnNetworkInfoReq snNetworkInfoReq
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
if (snNetworkInfoReq == null) {
|
||
return Result.failed("网络信息不能为空");
|
||
}
|
||
|
||
SnDeviceNetworkInfo networkInfo = new SnDeviceNetworkInfo();
|
||
BeanUtils.copyProperties(snNetworkInfoReq, networkInfo);
|
||
networkInfo.setSerialno(sn);
|
||
networkInfo.setUpdateTime(LocalDateTime.now());
|
||
|
||
SnDeviceNetworkInfo existingNetworkInfo = networkInfoService.lambdaQuery()
|
||
.eq(SnDeviceNetworkInfo::getSerialno, sn)
|
||
.one();
|
||
|
||
boolean saved;
|
||
if (existingNetworkInfo != null) {
|
||
networkInfo.setId(existingNetworkInfo.getId());
|
||
saved = networkInfoService.updateById(networkInfo);
|
||
} else {
|
||
networkInfo.setCreateTime(LocalDateTime.now());
|
||
saved = networkInfoService.save(networkInfo);
|
||
}
|
||
|
||
if (!saved) {
|
||
return Result.failed("保存网络信息失败");
|
||
}
|
||
|
||
logger.info("网络信息上传成功, sn: {}", sn);
|
||
return Result.success();
|
||
|
||
} catch (Exception e) {
|
||
logger.error("updateNetworkInfo error, sn: {}", sn, e);
|
||
return Result.failed("上传网络信息失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传设备安全信息")
|
||
@PostMapping("/update_security_info")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.UPDATE_SECURITY_INFO)
|
||
public Result<Void> updateSecurityInfo(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody SnSecurityInfoReq snSecurityInfoReq
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
if (snSecurityInfoReq == null) {
|
||
return Result.failed("安全信息不能为空");
|
||
}
|
||
|
||
SnDeviceSecurityInfo securityInfo = new SnDeviceSecurityInfo();
|
||
BeanUtils.copyProperties(snSecurityInfoReq, securityInfo);
|
||
securityInfo.setSerialno(sn);
|
||
securityInfo.setUpdateTime(LocalDateTime.now());
|
||
|
||
SnDeviceSecurityInfo existingSecurityInfo = securityInfoService.lambdaQuery()
|
||
.eq(SnDeviceSecurityInfo::getSerialno, sn)
|
||
.one();
|
||
|
||
boolean saved;
|
||
if (existingSecurityInfo != null) {
|
||
securityInfo.setId(existingSecurityInfo.getId());
|
||
saved = securityInfoService.updateById(securityInfo);
|
||
} else {
|
||
securityInfo.setCreateTime(LocalDateTime.now());
|
||
saved = securityInfoService.save(securityInfo);
|
||
}
|
||
|
||
if (!saved) {
|
||
return Result.failed("保存安全信息失败");
|
||
}
|
||
|
||
logger.info("安全信息上传成功, sn: {}", sn);
|
||
return Result.success();
|
||
|
||
} catch (Exception e) {
|
||
logger.error("updateSecurityInfo error, sn: {}", sn, e);
|
||
return Result.failed("上传安全信息失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传设备截图/拍照")
|
||
@PostMapping("/upload_screenshot")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.SCREENSHOT)
|
||
public Result<Void> uploadScreenshot(
|
||
@RequestPart(value = "file") MultipartFile file,
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestParam(value = "camera", required = false) Integer camera
|
||
) {
|
||
try {
|
||
if (file.isEmpty()) {
|
||
return Result.failed("上传文件不能为空");
|
||
}
|
||
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
String originName = file.getOriginalFilename();
|
||
if (originName == null || originName.isEmpty()) {
|
||
return Result.failed("文件名无效");
|
||
}
|
||
// 清洗文件名,去除路径穿越风险(只保留纯文件名)
|
||
String safeBaseName = FilenameUtils.getName(originName);
|
||
if (safeBaseName.isEmpty()) {
|
||
return Result.failed("文件名无效");
|
||
}
|
||
|
||
String fileExtension = FilenameUtils.getExtension(safeBaseName);
|
||
String md5 = HashUtils.calculateMultipartFileMd5(file);
|
||
String sha1 = HashUtils.calculateMultipartFileSha1(file);
|
||
String sha256 = HashUtils.calculateMultipartFileSha256(file);
|
||
|
||
FileInfo stored = fileService.uploadFile(file, StorageDir.DEVICE_SCREENSHOT);
|
||
String url = stored.getUrl();
|
||
int slash = url.lastIndexOf('/');
|
||
|
||
SnScreenshot screenshotInfo = new SnScreenshot();
|
||
screenshotInfo.setSn(sn);
|
||
// 保存原始文件名,便于追溯/调试
|
||
screenshotInfo.setOriginName(safeBaseName);
|
||
// 保存MIME类型与扩展名,便于前端筛选/预览
|
||
screenshotInfo.setMimeType(file.getContentType());
|
||
screenshotInfo.setFileExtension(fileExtension);
|
||
// 对象名由存储服务生成(UUID),fileName 仅存末段便于展示
|
||
screenshotInfo.setFileName(slash >= 0 ? url.substring(slash + 1) : url);
|
||
// 保存存储返回的完整 URL(s3 / aliyun / local),前端按 URL 直接访问
|
||
screenshotInfo.setFilePath(url);
|
||
screenshotInfo.setFileSize(file.getSize());
|
||
screenshotInfo.setFileMd5(md5);
|
||
screenshotInfo.setFileSha1(sha1);
|
||
screenshotInfo.setFileSha256(sha256);
|
||
screenshotInfo.setCamera(camera);
|
||
screenshotInfo.setUploadTime(LocalDateTime.now());
|
||
|
||
screenshotService.save(screenshotInfo);
|
||
|
||
logger.info("截图上传成功, sn: {}, url: {}", sn, url);
|
||
return Result.success();
|
||
|
||
} catch (Exception e) {
|
||
logger.error("uploadScreenshot error, sn: {}", sn, e);
|
||
return Result.failed("上传截图失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传设备定位信息")
|
||
@PostMapping("/upload_location")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.LOCATE)
|
||
public Result<Void> uploadLocation(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody SnLocationReq locationReq
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
if (locationReq == null) {
|
||
return Result.failed("定位信息不能为空");
|
||
}
|
||
|
||
SnLocation location = new SnLocation();
|
||
BeanUtils.copyProperties(locationReq, location);
|
||
location.setSn(sn);
|
||
// 上次定位成功时间由服务端统一写入,避免移动端时间不准或 LocalDateTime 序列化格式不一致导致为空
|
||
location.setLastSuccessfulTime(LocalDateTime.now());
|
||
|
||
SnLocation existingLocation = locationService.lambdaQuery()
|
||
.eq(SnLocation::getSn, sn)
|
||
.one();
|
||
|
||
boolean saved;
|
||
if (existingLocation != null) {
|
||
location.setId(existingLocation.getId());
|
||
saved = locationService.updateById(location);
|
||
} else {
|
||
saved = locationService.save(location);
|
||
}
|
||
|
||
if (!saved) {
|
||
return Result.failed("保存定位信息失败");
|
||
}
|
||
|
||
logger.info("定位信息上传成功, sn: {}, location: {}", sn, locationReq);
|
||
return Result.success();
|
||
|
||
} catch (Exception e) {
|
||
logger.error("uploadLocation error, sn: {}", sn, e);
|
||
return Result.failed("上传定位信息失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "获取开发者选项开关")
|
||
@GetMapping("/get_developer_options")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.VIEW_DEVELOPER_OPTIONS)
|
||
public Result<DeveloperOptionsVO> getDeveloperOptions(@RequestHeader(value = "X-Device-SN") String sn) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
// 查询该设备的开发者选项配置,未配置时默认关闭
|
||
DeveloperOptionsVO vo = new DeveloperOptionsVO();
|
||
vo.setDeveloperOptions(developerService.getDeveloperOptionsBySn(sn));
|
||
// 返回开发者选项开关状态
|
||
return Result.success(vo);
|
||
|
||
} catch (Exception e) {
|
||
logger.error("getDeveloperOptions error, sn: {}", sn, e);
|
||
return Result.failed("获取开发者选项失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传设备已安装应用列表")
|
||
@PostMapping("/upload_install_apks")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.UPLOAD_APK_LIST)
|
||
public Result<Void> uploadInstallApks(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody List<ApkInstallInfoReq> apkInfos
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
|
||
if (apkInfos == null || apkInfos.isEmpty()) {
|
||
return Result.failed("应用列表不能为空");
|
||
}
|
||
|
||
boolean saved = apkInstallService.saveOrUpdateDeviceApkInfo(sn, apkInfos);
|
||
|
||
if (!saved) {
|
||
return Result.failed("保存应用信息失败");
|
||
}
|
||
|
||
logger.info("应用列表上传成功, sn: {}, count: {}", sn, apkInfos.size());
|
||
return Result.success();
|
||
|
||
} catch (Exception e) {
|
||
logger.error("uploadInstallApks error, sn: {}", sn, e);
|
||
return Result.failed("上传应用列表失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传应用图标")
|
||
@PostMapping("/upload_app_icon")
|
||
@Log(module = LogModuleEnum.MOBILE, value = ActionTypeEnum.UPLOAD_APP_ICON)
|
||
public Result<String> uploadAppIcon(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestParam String packageName,
|
||
@RequestParam(required = false) String appName,
|
||
@RequestPart(value = "file") MultipartFile file
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
if (packageName == null || packageName.trim().isEmpty()) {
|
||
return Result.failed("应用包名不能为空");
|
||
}
|
||
String iconUrl = appIconService.uploadIcon(packageName, appName, file, "device");
|
||
return Result.success(iconUrl);
|
||
} catch (BusinessException e) {
|
||
// 业务异常(如图标已锁定)需保留原始业务码,供设备端识别为终态、不再重试;
|
||
// 不可被下方 catch(Exception) 压平成通用系统错误码
|
||
logger.info("uploadAppIcon rejected, sn: {}, packageName: {}, reason: {}", sn, packageName, e.getMessage());
|
||
throw e;
|
||
} catch (Exception e) {
|
||
logger.error("uploadAppIcon error, sn: {}", sn, e);
|
||
return Result.failed("上传应用图标失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "获取设备壁纸配置", description = "返回设备的壁纸配置(mode:0-单个 1-轮换;images:图片URL列表),未配置时返回 null")
|
||
@GetMapping("/get_wallpaper")
|
||
public Result<DeviceWallpaperVO> getWallpaper(
|
||
@RequestHeader(value = "X-Device-SN") String sn
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
DeviceWallpaperVO vo = deviceWallpaperService.getBySn(sn.trim());
|
||
// 未配置壁纸时返回 success(null),设备端按默认壁纸处理
|
||
return Result.success(vo);
|
||
} catch (Exception e) {
|
||
logger.error("getWallpaper error, sn: {}", sn, e);
|
||
return Result.failed("获取壁纸配置失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
// ===================== 设备闹钟(设备端同步) =====================
|
||
|
||
@Operation(summary = "获取设备闹钟列表", description = "返回该设备全部闹钟(按时间升序),设备收到推送或启动时拉取并覆盖本地 Room")
|
||
@GetMapping("/get_alarms")
|
||
public Result<List<DeviceAlarmVO>> getAlarms(
|
||
@RequestHeader(value = "X-Device-SN") String sn
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
return Result.success(deviceAlarmService.listAlarms(sn.trim()));
|
||
} catch (Exception e) {
|
||
logger.error("getAlarms error, sn: {}", sn, e);
|
||
return Result.failed("获取闹钟列表失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "整表同步设备闹钟", description = "设备端本地增删改闹钟后整表上传,服务端逻辑删除旧数据后按设备本地 id 重建")
|
||
@PostMapping("/sync_alarms")
|
||
public Result<?> syncAlarms(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody(required = false) List<DeviceAlarmVO> alarms
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
boolean ok = deviceAlarmService.replaceAlarms(sn.trim(), alarms);
|
||
if (!ok) {
|
||
return Result.failed("闹钟同步失败");
|
||
}
|
||
return Result.success();
|
||
} catch (Exception e) {
|
||
logger.error("syncAlarms error, sn: {}", sn, e);
|
||
return Result.failed("闹钟同步失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
// ===================== 设备策略信息(设备端) =====================
|
||
|
||
@Operation(summary = "获取设备策略信息", description = "SN 合法且无记录时自动新建默认策略(默认全开)")
|
||
@GetMapping("/get_device_policy")
|
||
public Result<DevicePolicyVO> getDevicePolicy(
|
||
@RequestHeader(value = "X-Device-SN") String sn
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
SnDevicePolicy policy = devicePolicyService.getOrInitBySn(sn);
|
||
if (policy == null) {
|
||
return Result.failed("设备SN不合法或不存在");
|
||
}
|
||
return Result.success(devicePolicyConverter.toVo(policy));
|
||
} catch (Exception e) {
|
||
logger.error("getDevicePolicy error, sn: {}", sn, e);
|
||
return Result.failed("获取设备策略失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "上传/全量设置设备策略信息", description = "SN 合法且无记录时新建,存在时更新(空字段保持原值)")
|
||
@PostMapping("/update_device_policy")
|
||
public Result<Void> updateDevicePolicy(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody DevicePolicyReq req
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
if (req == null) {
|
||
return Result.failed("策略信息不能为空");
|
||
}
|
||
SnDevicePolicy policy = new SnDevicePolicy();
|
||
policy.setSerialno(sn);
|
||
copyPolicy(req, policy);
|
||
boolean success = devicePolicyService.saveOrUpdateBySn(sn, policy);
|
||
if (!success) {
|
||
return Result.failed("保存设备策略失败(设备SN不合法或参数异常)");
|
||
}
|
||
logger.info("设备策略上传成功, sn: {}", sn);
|
||
return Result.success();
|
||
} catch (Exception e) {
|
||
logger.error("updateDevicePolicy error, sn: {}", sn, e);
|
||
return Result.failed("上传设备策略失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "单独设置设备策略开关", description = "通过 field 指定策略字段,value 设置 0/1")
|
||
@PostMapping("/update_device_policy_toggle")
|
||
public Result<Void> toggleDevicePolicy(
|
||
@RequestHeader(value = "X-Device-SN") String sn,
|
||
@RequestBody DevicePolicyToggleReq req
|
||
) {
|
||
try {
|
||
if (sn == null || sn.trim().isEmpty()) {
|
||
return Result.failed("设备序列号不能为空");
|
||
}
|
||
if (req == null || req.getField() == null || req.getValue() == null) {
|
||
return Result.failed("field 与 value 不能为空");
|
||
}
|
||
boolean success = devicePolicyService.updateSingleBySn(sn, req.getField(), req.getValue());
|
||
if (!success) {
|
||
return Result.failed("设置设备策略失败(设备SN不合法或字段不支持)");
|
||
}
|
||
return Result.success();
|
||
} catch (Exception e) {
|
||
logger.error("toggleDevicePolicy error, sn: {}", sn, e);
|
||
return Result.failed("设置设备策略失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
private void copyPolicy(DevicePolicyReq src, SnDevicePolicy target) {
|
||
target.setUsbData(src.getUsbData());
|
||
target.setTimeSetting(src.getTimeSetting());
|
||
target.setStorageCard(src.getStorageCard());
|
||
target.setFactoryReset(src.getFactoryReset());
|
||
target.setWifiHotspot(src.getWifiHotspot());
|
||
target.setBluetooth(src.getBluetooth());
|
||
target.setWallpaper(src.getWallpaper());
|
||
target.setNotificationBar(src.getNotificationBar());
|
||
target.setStatusBarPullDown(src.getStatusBarPullDown());
|
||
target.setSystemNavBarShow(src.getSystemNavBarShow());
|
||
target.setSystemNavBarSetting(src.getSystemNavBarSetting());
|
||
target.setNavBarOptions(src.getNavBarOptions());
|
||
target.setConfigBluetooth(src.getConfigBluetooth());
|
||
target.setOtaUpgrade(src.getOtaUpgrade());
|
||
target.setInstallApps(src.getInstallApps());
|
||
target.setAutoRotate(src.getAutoRotate());
|
||
target.setAutoBrightness(src.getAutoBrightness());
|
||
target.setEyeProtection(src.getEyeProtection());
|
||
target.setDarkMode(src.getDarkMode());
|
||
target.setSafeBoot(src.getSafeBoot());
|
||
target.setAdbUsbDebug(src.getAdbUsbDebug());
|
||
target.setMountPhysicalStorage(src.getMountPhysicalStorage());
|
||
target.setModifySystemTimeZone(src.getModifySystemTimeZone());
|
||
target.setToggleAirplaneMode(src.getToggleAirplaneMode());
|
||
target.setModifyMobileApn(src.getModifyMobileApn());
|
||
target.setUninstallApps(src.getUninstallApps());
|
||
target.setCamera(src.getCamera());
|
||
target.setUsbFileTransfer(src.getUsbFileTransfer());
|
||
target.setModifyLocationSwitch(src.getModifyLocationSwitch());
|
||
target.setBluetoothSharing(src.getBluetoothSharing());
|
||
target.setResetNetworkSettings(src.getResetNetworkSettings());
|
||
target.setAdjustGlobalVolume(src.getAdjustGlobalVolume());
|
||
target.setModifyScreenTimeout(src.getModifyScreenTimeout());
|
||
target.setManualAdjustTimeZone(src.getManualAdjustTimeZone());
|
||
target.setModifySystemLanguage(src.getModifySystemLanguage());
|
||
target.setManualAdjustBrightness(src.getManualAdjustBrightness());
|
||
}
|
||
}
|