diff --git a/.claude/settings.json b/.claude/settings.json index 58f630f..ac8360c 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -171,7 +171,8 @@ "Bash(curl -s http://localhost:8000/api/card/category/dance/)", "Bash(C:/python/python.exe -X utf8 -c \"import sys,json; sys.stdout.reconfigure\\(encoding=''''utf-8''''\\); d=json.load\\(sys.stdin\\); [print\\(f''''ID={i[\"\"id\"\"]} status={i.get\\(\"\"status\"\",\"\"?\"\"\\)} name={i[\"\"name\"\"]}''''\\) for i in d.get\\(''''data'''',d\\).get\\(''''results'''',d.get\\(''''data'''',[]\\)\\)]\")", "Bash(ls c:/Users/admin/Desktop/Lila-Server/qy_lty/settings*)", - "Bash(python manage.py makemigrations card)" + "Bash(python manage.py makemigrations card)", + "Bash(python manage.py runserver)" ], "additionalDirectories": [ "C:\\Users\\admin\\.claude" diff --git a/qy_lty/device_interaction/serializers.py b/qy_lty/device_interaction/serializers.py index 1a7eb74..a4d5f7d 100644 --- a/qy_lty/device_interaction/serializers.py +++ b/qy_lty/device_interaction/serializers.py @@ -112,17 +112,7 @@ class DeviceBindSerializer(serializers.Serializer): def validate_mac_address(self, value): try: - device = Device.objects.get(mac_address=value) - # 检查设备是否已被激活 - if not device.is_active: - device.is_active = True - device.activated_at = timezone.now() - device.save() + Device.objects.get(mac_address=value) except Device.DoesNotExist: raise serializers.ValidationError("设备不存在") - - # 检查设备是否已被其他用户绑定 - if UserDevice.objects.filter(device=device).exists(): - raise serializers.ValidationError("设备已被其他用户绑定") - - return value \ No newline at end of file + return value \ No newline at end of file diff --git a/qy_lty/device_interaction/views.py b/qy_lty/device_interaction/views.py index f89e4ae..e1c736c 100644 --- a/qy_lty/device_interaction/views.py +++ b/qy_lty/device_interaction/views.py @@ -669,7 +669,25 @@ class UserDeviceViewSet(viewsets.ModelViewSet): try: # 获取设备 device = Device.objects.get(mac_address=mac_address) - + + # 检查是否已被当前用户绑定 + existing = UserDevice.objects.filter(device=device, user=request.user).first() + if existing: + return success_response( + data=UserDeviceSerializer(existing).data, + message='设备已绑定' + ) + + # 检查是否已被其他用户绑定 + if UserDevice.objects.filter(device=device).exists(): + return error_response(message='设备已被其他用户绑定', code=status.HTTP_400_BAD_REQUEST) + + # 激活设备 + if not device.is_active: + device.is_active = True + device.activated_at = timezone.now() + device.save() + # 创建用户设备关联 user_device = UserDevice( user=request.user, @@ -678,19 +696,13 @@ class UserDeviceViewSet(viewsets.ModelViewSet): is_primary=is_primary ) user_device.save() - - # 更新设备状态 - if not device.is_active: - device.is_active = True - device.activated_at = timezone.now() - device.save() - + # 返回绑定结果 return success_response( data=UserDeviceSerializer(user_device).data, message='设备绑定成功' ) - + except Device.DoesNotExist: return not_found_response(message='设备不存在') except Exception as e: