兼容新旧设备二维码
This commit is contained in:
22
lib/core/utils/device_qr_code_parser.dart
Normal file
22
lib/core/utils/device_qr_code_parser.dart
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
/// 提取设备二维码中的序列号,同时兼容旧版纯序列号码。
|
||||||
|
String? parseDeviceQrSerialNumber(String? rawValue) {
|
||||||
|
final value = rawValue?.trim();
|
||||||
|
if (value == null || value.isEmpty) return null;
|
||||||
|
|
||||||
|
if (!value.startsWith('{') && !value.startsWith('[')) return value;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final payload = jsonDecode(value);
|
||||||
|
if (payload is! Map<String, dynamic>) return null;
|
||||||
|
|
||||||
|
final serialNumber = payload['serialNumber'];
|
||||||
|
if (serialNumber is! String) return null;
|
||||||
|
|
||||||
|
final serial = serialNumber.trim();
|
||||||
|
return serial.isEmpty ? null : serial;
|
||||||
|
} on FormatException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
|||||||
import 'package:flutter_webrtc/flutter_webrtc.dart';
|
import 'package:flutter_webrtc/flutter_webrtc.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
|
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
|
||||||
|
import 'package:maibu_satabot_v2/core/utils/device_qr_code_parser.dart';
|
||||||
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
||||||
import 'package:maibu_satabot_v2/features/my/presentation/bloc/my_cubit.dart';
|
import 'package:maibu_satabot_v2/features/my/presentation/bloc/my_cubit.dart';
|
||||||
import 'package:maibu_satabot_v2/features/my/presentation/bloc/my_state.dart';
|
import 'package:maibu_satabot_v2/features/my/presentation/bloc/my_state.dart';
|
||||||
@@ -215,7 +216,7 @@ class _UserInfoCardState extends State<UserInfoCard> {
|
|||||||
if (isScanned) return;
|
if (isScanned) return;
|
||||||
final List<Barcode> barcodes = capture.barcodes;
|
final List<Barcode> barcodes = capture.barcodes;
|
||||||
if (barcodes.isNotEmpty) {
|
if (barcodes.isNotEmpty) {
|
||||||
final String? code = barcodes.first.rawValue;
|
final String? code = parseDeviceQrSerialNumber(barcodes.first.rawValue);
|
||||||
if (code != null && code.isNotEmpty) {
|
if (code != null && code.isNotEmpty) {
|
||||||
isScanned = true;
|
isScanned = true;
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:maibu_satabot_v2/core/utils/device_qr_code_parser.dart';
|
||||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||||
|
|
||||||
class QrScannerPage extends StatefulWidget {
|
class QrScannerPage extends StatefulWidget {
|
||||||
@@ -23,7 +24,7 @@ class _QrScannerPageState extends State<QrScannerPage> {
|
|||||||
|
|
||||||
final List<Barcode> barcodes = capture.barcodes;
|
final List<Barcode> barcodes = capture.barcodes;
|
||||||
if (barcodes.isNotEmpty) {
|
if (barcodes.isNotEmpty) {
|
||||||
final String? code = barcodes.first.rawValue;
|
final String? code = parseDeviceQrSerialNumber(barcodes.first.rawValue);
|
||||||
if (code != null && code.isNotEmpty) {
|
if (code != null && code.isNotEmpty) {
|
||||||
_isScanned = true;
|
_isScanned = true;
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import 'package:image_picker/image_picker.dart';
|
|||||||
import 'package:mobile_scanner/mobile_scanner.dart';
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
||||||
import 'package:maibu_satabot_v2/components/capsule_toast.dart';
|
import 'package:maibu_satabot_v2/components/capsule_toast.dart';
|
||||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||||
|
import 'package:maibu_satabot_v2/core/utils/device_qr_code_parser.dart';
|
||||||
|
|
||||||
/// 添加机器人扫码页 V2 —— 按「01_添加设备_支持跳过」设计稿换皮。
|
/// 添加机器人扫码页 V2 —— 按「01_添加设备_支持跳过」设计稿换皮。
|
||||||
///
|
///
|
||||||
/// 由 qr_scanner_page.dart 复制而来:MobileScanner 识别到二维码后 pop 回传码值、
|
/// MobileScanner 识别到二维码后提取 serialNumber,通过 pop 回传序列号;
|
||||||
/// 闪光灯 toggleTorch 等逻辑与旧页一致;新增的手动输入 SN + 「识别设备」按钮
|
/// 闪光灯 toggleTorch 等逻辑与旧页一致;新增的手动输入 SN + 「识别设备」按钮
|
||||||
/// 走同一 pop 契约回传,调用方(BindDevicePage(scanResult:))无需改动。
|
/// 走同一 pop 契约回传,调用方(BindDevicePage(scanResult:))无需改动。
|
||||||
/// 旧页保留可随时回退。
|
/// 旧页保留可随时回退。
|
||||||
@@ -50,13 +51,13 @@ class _QrScannerPageV2State extends State<QrScannerPageV2>
|
|||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 与旧页逐行一致:识别到码值后 pop 回传(防重复触发)
|
/// 提取新二维码的 serialNumber 后回传,兼容旧码并防止重复触发。
|
||||||
void _onDetect(BarcodeCapture capture) {
|
void _onDetect(BarcodeCapture capture) {
|
||||||
if (_isScanned) return;
|
if (_isScanned) return;
|
||||||
|
|
||||||
final List<Barcode> barcodes = capture.barcodes;
|
final List<Barcode> barcodes = capture.barcodes;
|
||||||
if (barcodes.isNotEmpty) {
|
if (barcodes.isNotEmpty) {
|
||||||
final String? code = barcodes.first.rawValue;
|
final String? code = parseDeviceQrSerialNumber(barcodes.first.rawValue);
|
||||||
if (code != null && code.isNotEmpty) {
|
if (code != null && code.isNotEmpty) {
|
||||||
_isScanned = true;
|
_isScanned = true;
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
|||||||
54
test/device_qr_code_parser_test.dart
Normal file
54
test/device_qr_code_parser_test.dart
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:maibu_satabot_v2/core/utils/device_qr_code_parser.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
const serialNumber = 'STNT0126101500001D914';
|
||||||
|
|
||||||
|
test('extracts serialNumber from the new device QR payload', () {
|
||||||
|
expect(
|
||||||
|
parseDeviceQrSerialNumber('{ "serialNumber": "$serialNumber" }'),
|
||||||
|
serialNumber,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ignores additional fields and trims the serial number', () {
|
||||||
|
expect(
|
||||||
|
parseDeviceQrSerialNumber(
|
||||||
|
' {"serialNumber":" $serialNumber ","name":"robot"} ',
|
||||||
|
),
|
||||||
|
serialNumber,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves legacy plain serial numbers', () {
|
||||||
|
expect(parseDeviceQrSerialNumber(' $serialNumber '), serialNumber);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('rejects empty scan results', () {
|
||||||
|
for (final value in [null, '', ' ']) {
|
||||||
|
expect(parseDeviceQrSerialNumber(value), isNull);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('rejects payloads without a nonempty string serialNumber', () {
|
||||||
|
for (final value in [
|
||||||
|
'{}',
|
||||||
|
'{"deviceCode":"$serialNumber"}',
|
||||||
|
'{"serialNumber":null}',
|
||||||
|
'{"serialNumber":123}',
|
||||||
|
'{"serialNumber":{}}',
|
||||||
|
'{"serialNumber":" "}',
|
||||||
|
]) {
|
||||||
|
expect(parseDeviceQrSerialNumber(value), isNull, reason: value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'rejects malformed JSON and arrays instead of using them as device codes',
|
||||||
|
() {
|
||||||
|
for (final value in ['{"serialNumber":', '["$serialNumber"]']) {
|
||||||
|
expect(parseDeviceQrSerialNumber(value), isNull, reason: value);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user