refactor(client):优化设备管理与登录逻辑

- 移除冗余的日志记录器声明
- 简化设备心跳接口,合并注册与更新逻辑
- 调整设备数量限制检查逻辑,提高代码可读性
- 修改默认设备数量限制从3台调整为1台- 更新客户端登出提示文案- 固定启动窗口尺寸并移除延迟启动逻辑
- 调整设备移除时的消息提示内容
This commit is contained in:
2025-10-17 16:14:43 +08:00
parent d9f91b77e3
commit 0c85aa5677
5 changed files with 41 additions and 117 deletions

View File

@@ -24,40 +24,6 @@ public class ClientDeviceController {
private ClientAccountMapper clientAccountMapper;
@Autowired
private SseHubService sseHubService;
private static final int DEFAULT_LIMIT = 3;
/**
* 获取账号的设备数量限制
*
* @param username 用户名
* @return 设备数量限制,如果账号不存在或未配置则返回默认值
*/
private int getDeviceLimit(String username) {
if (username == null || username.isEmpty()) {
return DEFAULT_LIMIT;
}
ClientAccount account = clientAccountMapper.selectClientAccountByUsername(username);
if (account == null || account.getDeviceLimit() == null) {
return DEFAULT_LIMIT;
}
return account.getDeviceLimit();
}
/**
* 检查设备数量限制
*
* @param username 用户名
* @param currentDeviceId 当前设备ID检查时排除此设备
* @throws RuntimeException 如果设备数量已达上限
*/
private void checkDeviceLimit(String username, String currentDeviceId) {
int deviceLimit = getDeviceLimit(username);
List<ClientDevice> userDevices = clientDeviceMapper.selectByUsername(username);
if (userDevices.size() >= deviceLimit) {
throw new RuntimeException("设备数量已达上限(" + deviceLimit + "个),请先移除其他设备");
}
}
/**
* 查询设备配额与已使用数量
@@ -72,7 +38,8 @@ public class ClientDeviceController {
for (ClientDevice d : all) {
if (!"removed".equals(d.getStatus())) used++;
}
int limit = getDeviceLimit(username);
ClientAccount account = clientAccountMapper.selectClientAccountByUsername(username);
int limit = (account != null && account.getDeviceLimit() != null) ? account.getDeviceLimit() : 3;
Map<String, Object> map = new HashMap<>();
map.put("limit", limit);
map.put("used", used);
@@ -102,14 +69,21 @@ public class ClientDeviceController {
String deviceId = device.getDeviceId();
String os = device.getOs();
String deviceName = username + "@" + ip + " (" + os + ")";
// 检查设备数量限制
ClientAccount account = clientAccountMapper.selectClientAccountByUsername(username);
int deviceLimit = (account != null && account.getDeviceLimit() != null) ? account.getDeviceLimit() : 3;
List<ClientDevice> userDevices = clientDeviceMapper.selectByUsername(username);
int userDevice = userDevices.size();
boolean deviceExists = userDevices.stream().anyMatch(d -> deviceId.equals(d.getDeviceId()));
if (deviceExists) userDevice--;
if (userDevice >= deviceLimit) {
return AjaxResult.error("设备数量已达上限(" + deviceLimit + "个),请先移除其他设备");
}
ClientDevice exists = clientDeviceMapper.selectByDeviceIdAndUsername(deviceId, username);
if (exists == null) {
// 检查设备数量限制
try {
checkDeviceLimit(username, deviceId);
} catch (RuntimeException e) {
return AjaxResult.error(e.getMessage());
}
device.setIp(ip);
device.setStatus("online");
device.setLastActiveAt(new java.util.Date());
@@ -117,6 +91,7 @@ public class ClientDeviceController {
device.setName(deviceName);
clientDeviceMapper.insert(device);
} else {
exists.setName(deviceName);
exists.setOs(os);
exists.setStatus("online");
@@ -188,48 +163,6 @@ public class ClientDeviceController {
return AjaxResult.success();
}
/**
* 设备心跳
* 若设备未注册则按注册逻辑插入;已注册则更新在线状态和设备信息
* 所有情况都检查设备数量限制,被移除设备允许重新注册(需检查配额)
*/
@PostMapping("/heartbeat")
public AjaxResult heartbeat(@RequestBody ClientDevice device, HttpServletRequest request) {
String ip = IpUtils.getIpAddr(request);
String username = device.getUsername();
String deviceId = device.getDeviceId();
String os = device.getOs();
String deviceName = username + "@" + ip + " (" + os + ")";
ClientDevice exists = clientDeviceMapper.selectByDeviceIdAndUsername(deviceId, username);
if (exists == null) {
// 新设备注册
device.setIp(ip);
device.setStatus("online");
device.setLastActiveAt(new java.util.Date());
device.setTrialExpireTime(new java.util.Date(System.currentTimeMillis() + 3 * 24L * 60 * 60 * 1000));
device.setName(deviceName);
clientDeviceMapper.insert(device);
} else if ("removed".equals(exists.getStatus())) {
// 被移除设备重新激活
exists.setName(deviceName);
exists.setOs(os);
exists.setStatus("online");
exists.setIp(ip);
exists.setLocation(device.getLocation());
exists.setLastActiveAt(new java.util.Date());
clientDeviceMapper.updateByDeviceIdAndUsername(exists);
} else {
// 已存在设备更新
exists.setStatus("online");
exists.setIp(ip);
exists.setLastActiveAt(new java.util.Date());
exists.setName(deviceName);
clientDeviceMapper.updateByDeviceIdAndUsername(exists);
}
return AjaxResult.success();
}
}