fix device binding status
All checks were successful
Build and Deploy Backend / build-and-deploy (push) Successful in 23m29s

This commit is contained in:
repair-agent 2026-03-26 10:10:23 +08:00
parent 784115ce58
commit cccb1b6213

View File

@ -33,12 +33,12 @@ class DeviceViewSet(viewsets.ViewSet):
authentication_classes = [AppJWTAuthentication]
permission_classes = [IsAuthenticated]
@action(detail=False, methods=['get'], url_path='query-by-mac',
authentication_classes=[], permission_classes=[AllowAny])
@action(detail=False, methods=['get'], url_path='query-by-mac')
def query_by_mac(self, request):
"""
通过MAC地址查询SN码无需登录
通过MAC地址查询设备信息及绑定状态
GET /api/v1/devices/query-by-mac?mac=AA:BB:CC:DD:EE:FF
返回 bound_by_me / bound_by_other / unbound
"""
mac = request.query_params.get('mac', '')
if not mac:
@ -49,13 +49,6 @@ class DeviceViewSet(viewsets.ViewSet):
try:
device = Device.objects.select_related('device_type').get(mac_address=mac)
return success(data={
'sn': device.sn,
'mac_address': device.mac_address,
'device_type': DeviceTypeSerializer(device.device_type).data if device.device_type else None,
'status': device.status,
'is_bound': device.status == 'bound'
})
except Device.DoesNotExist:
return error(
code=404,
@ -63,6 +56,25 @@ class DeviceViewSet(viewsets.ViewSet):
status_code=status.HTTP_404_NOT_FOUND
)
# 判断绑定归属
bind_status = 'unbound'
if device.status == 'bound':
owner = UserDevice.objects.filter(
device=device, is_active=True
).first()
if owner and owner.user == request.user:
bind_status = 'bound_by_me'
elif owner:
bind_status = 'bound_by_other'
return success(data={
'sn': device.sn,
'mac_address': device.mac_address,
'device_type': DeviceTypeSerializer(device.device_type).data if device.device_type else None,
'status': device.status,
'bind_status': bind_status,
})
@action(detail=False, methods=['get'])
def latest(self, request):
"""