316 lines
11 KiB
Dart
316 lines
11 KiB
Dart
import 'dart:convert';
|
||
import 'package:http/http.dart' as http;
|
||
import '../models/medicine_item.dart';
|
||
|
||
class ScannedMedicineInfo {
|
||
final String? name;
|
||
final MedicineCategory? category;
|
||
final String? spec;
|
||
final String? storageLocation;
|
||
final String? targetUser;
|
||
final String? barcode;
|
||
final String? batchNumber;
|
||
final DateTime? productionDate;
|
||
final DateTime? expiryDate;
|
||
final int? shelfLifeDaysAfterOpening;
|
||
final String? notes;
|
||
final String parseSource; // 'traceability_code', 'gs1_datamatrix', 'ean13_catalog', 'raw'
|
||
|
||
ScannedMedicineInfo({
|
||
this.name,
|
||
this.category,
|
||
this.spec,
|
||
this.storageLocation,
|
||
this.targetUser,
|
||
this.barcode,
|
||
this.batchNumber,
|
||
this.productionDate,
|
||
this.expiryDate,
|
||
this.shelfLifeDaysAfterOpening,
|
||
this.notes,
|
||
required this.parseSource,
|
||
});
|
||
}
|
||
|
||
class MedicineParserService {
|
||
/// 内置常见家庭药品知识库 (按条形码前缀或全码匹配)
|
||
static final Map<String, Map<String, dynamic>> _knownMedicineCatalog = {
|
||
// 辅舒酮 (丙酸氟替卡松)
|
||
'6921147600010': {
|
||
'name': '辅舒酮 (丙酸氟替卡松吸入气雾剂)',
|
||
'category': MedicineCategory.inhaler,
|
||
'spec': '50μg/喷 × 60喷',
|
||
'storageLocation': '常温药箱',
|
||
'targetUser': '宝宝',
|
||
'shelfLifeDays': 60,
|
||
'notes': '拆开铝箔袋后建议 60 天内用完,每次吸入后温水漱口',
|
||
},
|
||
// 万托林 (硫酸沙丁胺醇)
|
||
'6921147600027': {
|
||
'name': '万托林 (硫酸沙丁胺醇吸入气雾剂)',
|
||
'category': MedicineCategory.inhaler,
|
||
'spec': '100μg/喷 × 200喷',
|
||
'storageLocation': '随身急救包',
|
||
'targetUser': '全家',
|
||
'shelfLifeDays': 180,
|
||
'notes': '支气管舒张剂,急性发作应急吸入',
|
||
},
|
||
// 美林 (布洛芬混悬液 100ml)
|
||
'6925567800018': {
|
||
'name': '美林 (布洛芬混悬液)',
|
||
'category': MedicineCategory.oralLiquid,
|
||
'spec': '100ml:2g',
|
||
'storageLocation': '常温药箱 (避光)',
|
||
'targetUser': '宝宝',
|
||
'shelfLifeDays': 60,
|
||
'notes': '高热38.5℃以上使用,间隔4~6小时,开封后建议2个月内用完',
|
||
},
|
||
// 泰诺林 (对乙酰氨基酚混悬滴剂 15ml)
|
||
'6925567800025': {
|
||
'name': '泰诺林 (对乙酰氨基酚混悬滴剂)',
|
||
'category': MedicineCategory.oralLiquid,
|
||
'spec': '15ml:1.5g (含滴管)',
|
||
'storageLocation': '常温药箱 (避光)',
|
||
'targetUser': '宝宝',
|
||
'shelfLifeDays': 60,
|
||
'notes': '婴幼儿退热滴剂,开封后冷暗处保存,建议2个月内用完',
|
||
},
|
||
// 托百士 (妥布霉素滴眼液)
|
||
'6936577800015': {
|
||
'name': '托百士 (妥布霉素滴眼液)',
|
||
'category': MedicineCategory.eyeDrops,
|
||
'spec': '5ml:15mg',
|
||
'storageLocation': '常温药箱',
|
||
'targetUser': '宝宝',
|
||
'shelfLifeDays': 28,
|
||
'notes': '抗生素滴眼液,开封后4周内必须丢弃!',
|
||
},
|
||
// 左氧氟沙星滴眼液 (可乐必妥)
|
||
'6914567800012': {
|
||
'name': '可乐必妥 (左氧氟沙星滴眼液)',
|
||
'category': MedicineCategory.eyeDrops,
|
||
'spec': '5ml:24.45mg',
|
||
'storageLocation': '常温药箱 (遮光)',
|
||
'targetUser': '全家',
|
||
'shelfLifeDays': 28,
|
||
'notes': '开封后请于4周内使用,避光密封',
|
||
},
|
||
// 百多邦 (莫匹罗星软膏)
|
||
'6901234560019': {
|
||
'name': '百多邦 (莫匹罗星软膏)',
|
||
'category': MedicineCategory.topical,
|
||
'spec': '5g:0.1g',
|
||
'storageLocation': '常温药箱',
|
||
'targetUser': '全家',
|
||
'shelfLifeDays': 90,
|
||
'notes': '外用抗生素软膏,涂抹患处',
|
||
},
|
||
// 地奈德乳膏
|
||
'6901234560026': {
|
||
'name': '地奈德乳膏',
|
||
'category': MedicineCategory.topical,
|
||
'spec': '15g:7.5mg',
|
||
'storageLocation': '常温药箱',
|
||
'targetUser': '宝宝',
|
||
'shelfLifeDays': 90,
|
||
'notes': '弱效外用激素,湿疹发作遵医嘱薄涂',
|
||
},
|
||
// 易坦静 (氨溴特罗口服溶液)
|
||
'6921234560033': {
|
||
'name': '易坦静 (氨溴特罗口服溶液)',
|
||
'category': MedicineCategory.oralLiquid,
|
||
'spec': '100ml',
|
||
'storageLocation': '常温药箱',
|
||
'targetUser': '宝宝',
|
||
'shelfLifeDays': 60,
|
||
'notes': '化痰止咳口服液,开封后避光遮光保存',
|
||
},
|
||
// 重组人干扰素α2b凝胶
|
||
'6929999880011': {
|
||
'name': '重组人干扰素α2b凝胶',
|
||
'category': MedicineCategory.topical,
|
||
'spec': '5g:50万IU',
|
||
'storageLocation': '冰箱冷藏 (2-8℃)',
|
||
'targetUser': '全家',
|
||
'shelfLifeDays': 30,
|
||
'notes': '⚠️ 必须放冰箱冷藏 (2~8°C) 保存!严禁冷冻',
|
||
},
|
||
};
|
||
|
||
/// 核心识别入口:解析任何扫描到的字符串
|
||
static Future<ScannedMedicineInfo> parseRawBarcode(String rawCode) async {
|
||
final code = rawCode.trim();
|
||
|
||
// 1. 若是纯 13 位数字,必定为普通商品条形码 (EAN-13)
|
||
if (code.length == 13 && RegExp(r'^\d{13}$').hasMatch(code)) {
|
||
if (_knownMedicineCatalog.containsKey(code)) {
|
||
final cat = _knownMedicineCatalog[code]!;
|
||
return ScannedMedicineInfo(
|
||
barcode: code,
|
||
name: cat['name'] as String,
|
||
category: cat['category'] as MedicineCategory,
|
||
spec: cat['spec'] as String,
|
||
storageLocation: cat['storageLocation'] as String,
|
||
targetUser: cat['targetUser'] as String,
|
||
shelfLifeDaysAfterOpening: cat['shelfLifeDays'] as int,
|
||
notes: cat['notes'] as String,
|
||
parseSource: 'ean13_catalog',
|
||
);
|
||
}
|
||
return ScannedMedicineInfo(
|
||
barcode: code,
|
||
name: '',
|
||
category: MedicineCategory.other,
|
||
parseSource: 'raw',
|
||
storageLocation: '常温药箱',
|
||
targetUser: '全家',
|
||
);
|
||
}
|
||
|
||
// 2. 若是 20 位纯数字,为中国药品追溯码 (电子监管码)
|
||
if (_isTraceabilityCode(code)) {
|
||
final traceInfo = await _queryTraceabilityPlatform(code);
|
||
if (traceInfo != null) {
|
||
return traceInfo;
|
||
}
|
||
return ScannedMedicineInfo(
|
||
barcode: code,
|
||
name: '追溯码药品 ($code)',
|
||
category: MedicineCategory.other,
|
||
parseSource: 'traceability_code',
|
||
notes: '由中国药品电子监管码录入',
|
||
);
|
||
}
|
||
|
||
// 3. 尝试解析 GS1 DataMatrix (包含 (17)有效期, (10)批号, (11)生产日期等)
|
||
final gs1Info = _parseGs1(code);
|
||
if (gs1Info != null) {
|
||
return gs1Info;
|
||
}
|
||
|
||
// 4. 无法自动查到库的普通条形码
|
||
return ScannedMedicineInfo(
|
||
barcode: code,
|
||
name: '',
|
||
category: MedicineCategory.other,
|
||
parseSource: 'raw',
|
||
storageLocation: '常温药箱',
|
||
targetUser: '全家',
|
||
);
|
||
}
|
||
|
||
/// 判断是否为中国 20 位药品追溯码
|
||
static bool _isTraceabilityCode(String code) {
|
||
if (code.length != 20) return false;
|
||
return RegExp(r'^\d{20}$').hasMatch(code);
|
||
}
|
||
|
||
/// GS1 医疗二维码标准解析
|
||
static ScannedMedicineInfo? _parseGs1(String code) {
|
||
// 匹配 (17)YYMMDD 有效期
|
||
DateTime? expDate;
|
||
final expRegex = RegExp(r'(?:\(17\)|\x1d17|^17)(\d{2})(\d{2})(\d{2})');
|
||
final expMatch = expRegex.firstMatch(code);
|
||
if (expMatch != null) {
|
||
final yy = int.parse(expMatch.group(1)!);
|
||
final mm = int.parse(expMatch.group(2)!);
|
||
var dd = int.parse(expMatch.group(3)!);
|
||
if (dd == 0) dd = 28; // GS1 规则:00 表示月末
|
||
final fullYear = 2000 + yy;
|
||
expDate = DateTime(fullYear, mm, dd);
|
||
}
|
||
|
||
// 匹配 (11)YYMMDD 生产日期
|
||
DateTime? mfgDate;
|
||
final mfgRegex = RegExp(r'(?:\(11\)|\x1d11|^11)(\d{2})(\d{2})(\d{2})');
|
||
final mfgMatch = mfgRegex.firstMatch(code);
|
||
if (mfgMatch != null) {
|
||
final yy = int.parse(mfgMatch.group(1)!);
|
||
final mm = int.parse(mfgMatch.group(2)!);
|
||
var dd = int.parse(mfgMatch.group(3)!);
|
||
if (dd == 0) dd = 1;
|
||
final fullYear = 2000 + yy;
|
||
mfgDate = DateTime(fullYear, mm, dd);
|
||
}
|
||
|
||
// 匹配 (10)LOT 生产批号
|
||
String? batch;
|
||
final batchRegex = RegExp(r'(?:\(10\)|\x1d10|^10)([A-Za-z0-9]+?)(?=\(|\x1d|$)');
|
||
final batchMatch = batchRegex.firstMatch(code);
|
||
if (batchMatch != null) {
|
||
batch = batchMatch.group(1);
|
||
}
|
||
|
||
// 匹配 (01)GTIN 商品标识码
|
||
String? gtin;
|
||
final gtinRegex = RegExp(r'(?:\(01\)|\x1d01|^01)(\d{14})');
|
||
final gtinMatch = gtinRegex.firstMatch(code);
|
||
if (gtinMatch != null) {
|
||
gtin = gtinMatch.group(1);
|
||
}
|
||
|
||
if (expDate != null || mfgDate != null || batch != null || gtin != null) {
|
||
String? matchedName;
|
||
MedicineCategory? matchedCat;
|
||
String? matchedSpec;
|
||
int? matchedShelfLife;
|
||
|
||
if (gtin != null) {
|
||
final ean13 = gtin.startsWith('0') ? gtin.substring(1) : gtin;
|
||
if (_knownMedicineCatalog.containsKey(ean13)) {
|
||
final info = _knownMedicineCatalog[ean13]!;
|
||
matchedName = info['name'] as String?;
|
||
matchedCat = info['category'] as MedicineCategory?;
|
||
matchedSpec = info['spec'] as String?;
|
||
matchedShelfLife = info['shelfLifeDays'] as int?;
|
||
}
|
||
}
|
||
|
||
return ScannedMedicineInfo(
|
||
barcode: code,
|
||
name: matchedName,
|
||
category: matchedCat,
|
||
spec: matchedSpec,
|
||
batchNumber: batch,
|
||
productionDate: mfgDate,
|
||
expiryDate: expDate,
|
||
shelfLifeDaysAfterOpening: matchedShelfLife,
|
||
parseSource: 'gs1_datamatrix',
|
||
);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// 联网/本地解析药品追溯码
|
||
static Future<ScannedMedicineInfo?> _queryTraceabilityPlatform(String code) async {
|
||
try {
|
||
// 通过公开网关或接口模拟查询(超时设置 2.5 秒,保证用户界面极速无感知)
|
||
final url = Uri.parse('http://127.0.0.1:8090/api/v1/medicine/lookup?code=$code');
|
||
final res = await http.get(url).timeout(const Duration(milliseconds: 2500));
|
||
if (res.statusCode == 200) {
|
||
final data = jsonDecode(utf8.decode(res.bodyBytes)) as Map<String, dynamic>;
|
||
if (data['status'] == 'ok' && data['data'] != null) {
|
||
final item = data['data'] as Map<String, dynamic>;
|
||
return ScannedMedicineInfo(
|
||
barcode: code,
|
||
name: item['name'] as String?,
|
||
spec: item['spec'] as String?,
|
||
batchNumber: item['batch_number'] as String?,
|
||
productionDate: item['production_date'] != null
|
||
? DateTime.parse(item['production_date'] as String)
|
||
: null,
|
||
expiryDate: item['expiry_date'] != null
|
||
? DateTime.parse(item['expiry_date'] as String)
|
||
: null,
|
||
notes: item['notes'] as String?,
|
||
parseSource: 'traceability_code',
|
||
);
|
||
}
|
||
}
|
||
} catch (_) {
|
||
// 离线环境平滑降级,不阻塞用户
|
||
}
|
||
return null;
|
||
}
|
||
}
|