fix: 主子账号登录,数据隔离问题修复。

This commit is contained in:
tianyongbao
2026-01-20 16:10:01 +08:00
parent 22726ca92b
commit 0a9e808b6f
2 changed files with 22 additions and 25 deletions

View File

@@ -716,7 +716,7 @@ public class IotController extends BaseController {
@Operation(summary = "添加测控一体机") @Operation(summary = "添加测控一体机")
@PostMapping("/device/add_device_controller") @PostMapping("/device/add_device_controller")
public R<Void> addDeviceController(@RequestBody AddDeviceControllerBo bo) { public R<Void> addDeviceController(@RequestBody AddDeviceControllerBo bo, @RequestParam Long rootUserId) {
try { try {
if (iotDeviceService == null) { if (iotDeviceService == null) {
return R.fail("飞燕平台配置未启用"); return R.fail("飞燕平台配置未启用");
@@ -728,10 +728,10 @@ public class IotController extends BaseController {
return R.fail("开关数据库服务未启用"); return R.fail("开关数据库服务未启用");
} }
// 获取当前登录用户ID // 获取用户ID(从URL查询参数rootUserId获取)
Long userId = LoginHelper.getUserId(); Long userId = rootUserId;
if (userId == null) { if (userId == null) {
return R.fail("未登录或登录已过期"); return R.fail("根用户ID不能为空");
} }
// 验证塘口是否存在且属于当前用户 // 验证塘口是否存在且属于当前用户
@@ -1153,7 +1153,7 @@ public class IotController extends BaseController {
@Operation(summary = "添加设备探测器(水质检测仪)") @Operation(summary = "添加设备探测器(水质检测仪)")
@PostMapping("/device/add_device_detector") @PostMapping("/device/add_device_detector")
public R<Void> addDeviceDetector(@RequestBody AddDeviceDetectorBo bo) { public R<Void> addDeviceDetector(@RequestBody AddDeviceDetectorBo bo, @RequestParam Long rootUserId) {
try { try {
if (iotDeviceService == null) { if (iotDeviceService == null) {
return R.fail("飞燕平台配置未启用"); return R.fail("飞燕平台配置未启用");
@@ -1162,17 +1162,16 @@ public class IotController extends BaseController {
return R.fail("设备数据库服务未启用"); return R.fail("设备数据库服务未启用");
} }
// 获取当前登录用户ID // 获取用户ID(从URL查询参数rootUserId获取)
Long userId = LoginHelper.getUserId(); if (rootUserId == null) {
if (userId == null) { return R.fail("根用户ID不能为空");
return R.fail("未登录或登录已过期");
} }
// 验证塘口是否存在且属于当前用户 // 验证塘口是否存在且属于当前用户
if (bo.getPondId() != null && bo.getPondId() > 0 && pondMapper != null) { if (bo.getPondId() != null && bo.getPondId() > 0 && pondMapper != null) {
long count = pondMapper.selectCount( long count = pondMapper.selectCount(
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Pond>() new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Pond>()
.eq(Pond::getUserId, userId) .eq(Pond::getUserId, rootUserId)
.eq(Pond::getId, bo.getPondId()) .eq(Pond::getId, bo.getPondId())
); );
if (count == 0) { if (count == 0) {
@@ -1265,7 +1264,7 @@ public class IotController extends BaseController {
// 计算设备数量,用于生成设备名称 // 计算设备数量,用于生成设备名称
long deviceCount = deviceMapper.selectCount( long deviceCount = deviceMapper.selectCount(
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Device>() new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Device>()
.eq(Device::getUserId, userId) .eq(Device::getUserId, rootUserId)
.eq(Device::getDeviceType, 1) .eq(Device::getDeviceType, 1)
); );
@@ -1288,7 +1287,7 @@ public class IotController extends BaseController {
} }
// 设置基本信息 // 设置基本信息
device.setUserId(userId); device.setUserId(rootUserId);
device.setDeviceName("溶解氧" + (deviceCount + 1)); device.setDeviceName("溶解氧" + (deviceCount + 1));
device.setBindTime(now); device.setBindTime(now);
device.setPondId(bo.getPondId() != null && bo.getPondId() > 0 ? bo.getPondId() : null); device.setPondId(bo.getPondId() != null && bo.getPondId() > 0 ? bo.getPondId() : null);
@@ -1394,10 +1393,10 @@ public class IotController extends BaseController {
// 保存到数据库 // 保存到数据库
if (isNew) { if (isNew) {
deviceMapper.insert(device); deviceMapper.insert(device);
log.info("新设备添加成功: userId={}, iotId={}, serialNum={}", userId, iotId, bo.getSerialNum()); log.info("新设备添加成功: userId={}, iotId={}, serialNum={}", rootUserId, iotId, bo.getSerialNum());
} else { } else {
deviceMapper.updateById(device); deviceMapper.updateById(device);
log.info("设备更新成功: userId={}, iotId={}, serialNum={}", userId, iotId, bo.getSerialNum()); log.info("设备更新成功: userId={}, iotId={}, serialNum={}", rootUserId, iotId, bo.getSerialNum());
} }
return R.ok(); return R.ok();
@@ -1533,8 +1532,8 @@ public class IotController extends BaseController {
Device::getWarnCode, Device::getDeadTime) Device::getWarnCode, Device::getDeadTime)
); );
if (device == null || device.getUserId() == null || !device.getUserId().equals(userId)) { if (device == null) {
return R.fail("设备不存在或无权限访问"); return R.fail("设备不存在");
} }
// 如果是控制器且未启用溶解氧功能 // 如果是控制器且未启用溶解氧功能
@@ -1732,7 +1731,7 @@ public class IotController extends BaseController {
Device::getDeviceName, Device::getWarnCode, Device::getDeadTime) Device::getDeviceName, Device::getWarnCode, Device::getDeadTime)
); );
if (device == null || device.getUserId() == null || !device.getUserId().equals(userId)) { if (device == null) {
return R.fail("开关不存在或无权限访问"); return R.fail("开关不存在或无权限访问");
} }
@@ -1838,8 +1837,8 @@ public class IotController extends BaseController {
for (DeviceSwitchVo switchVo : listSwitch) { for (DeviceSwitchVo switchVo : listSwitch) {
// 权限验证 // 权限验证
if (switchVo.getUserId() == null || !switchVo.getUserId().equals(userId)) { if (switchVo.getUserId() == null) {
return R.fail("开关不存在或无权限访问"); return R.fail("开关不存在");
} }
// 如果状态已经是目标状态,跳过 // 如果状态已经是目标状态,跳过
@@ -1961,7 +1960,7 @@ public class IotController extends BaseController {
); );
// 权限验证 // 权限验证
if (device == null || device.getUserId() == null || !device.getUserId().equals(userId)) { if (device == null) {
return R.fail("开关不存在或无权限访问"); return R.fail("开关不存在或无权限访问");
} }
@@ -2161,8 +2160,6 @@ public class IotController extends BaseController {
if (deviceSwitchMapper == null || pondMapper == null || deviceMapper == null) { if (deviceSwitchMapper == null || pondMapper == null || deviceMapper == null) {
return R.fail("系统配置未完成"); return R.fail("系统配置未完成");
} }
Long userId = LoginHelper.getUserId();
Long switchId = request.getId(); Long switchId = request.getId();
Long pondId = request.getPondId(); Long pondId = request.getPondId();
@@ -2348,7 +2345,7 @@ public class IotController extends BaseController {
int closeTimeInMinutes = closeHour * 60 + closeMinute; int closeTimeInMinutes = closeHour * 60 + closeMinute;
if (currentTimeInMinutes >= openTimeInMinutes && currentTimeInMinutes <= closeTimeInMinutes) { if (currentTimeInMinutes >= openTimeInMinutes && currentTimeInMinutes <= closeTimeInMinutes) {
return R.fail("开启失败"); return R.fail("开启失败,当前时间不在定时范围内");
} }
} }
@@ -2590,7 +2587,7 @@ public class IotController extends BaseController {
); );
// 权限验证 // 权限验证
if (device == null || device.getUserId() == null || !device.getUserId().equals(userId)) { if (device == null) {
return R.fail("定时控制不存在"); return R.fail("定时控制不存在");
} }

View File

@@ -63,7 +63,7 @@ public class AddDeviceControllerBo implements Serializable {
private Boolean isOxygenUsed; private Boolean isOxygenUsed;
/** /**
* 开关放置塘口分区ID列表0表示设备自身1-4表示开关1-4 * 开关放置塘口分区ID列表(0表示设备自身,1-4表示开关1-4)
*/ */
@Schema(description = "开关放置塘口分区ID列表") @Schema(description = "开关放置塘口分区ID列表")
private List<Integer> listPutPondPartId; private List<Integer> listPutPondPartId;