feat: 更新二维码扫描组件,添加文件上传功能和错误处理

This commit is contained in:
2026-01-11 21:44:17 +07:00
parent 9f0f0a076c
commit 93ece1f31b
6 changed files with 299 additions and 48 deletions

View File

@@ -1,11 +1,10 @@
import { CapacitorBarcodeScanner, CapacitorBarcodeScannerCameraDirection, CapacitorBarcodeScannerScanOrientation, CapacitorBarcodeScannerTypeHint } from "@capacitor/barcode-scanner";
import { toastController } from "@ionic/vue";
import { modalController, toastController } from "@ionic/vue";
import QRCode from "@/components/qr-scanner/index.vue";
export interface QRScanResult {
text: string;
format: string;
rawValue: string;
displayValue: string;
}
export interface ScannerOptions {
@@ -15,36 +14,71 @@ export interface ScannerOptions {
export function useQRScanner() {
const { t } = useI18n();
const { vibrate } = useHaptics();
const platform = usePlatform();
async function open(options?: ScannerOptions) {
try {
vibrate();
return new Promise<string>(async (resolve, reject) => {
try {
vibrate();
if (platform === "browser") {
const modal = await modalController.create({
component: QRCode,
componentProps: {
onClose: () => modal.dismiss(),
onError: (error) => {
toastController.create({
message: String(error),
duration: 2000,
position: "bottom",
color: "danger",
}).then(toast => toast.present());
},
onSuccess: (result: string) => {
toastController.create({
message: String(result),
duration: 2000,
position: "bottom",
color: "success",
}).then(toast => toast.present());
modal.dismiss(result);
},
},
animated: false,
});
await modal.present();
modal.onDidDismiss<string>().then((res) => {
if (res.data)
resolve(res.data);
const result = await CapacitorBarcodeScanner.scanBarcode({
hint: CapacitorBarcodeScannerTypeHint.QR_CODE,
scanInstructions: options?.title || t("scanner.hint"),
cameraDirection: CapacitorBarcodeScannerCameraDirection.BACK,
scanOrientation: CapacitorBarcodeScannerScanOrientation.PORTRAIT,
});
reject();
});
}
else {
const result = await CapacitorBarcodeScanner.scanBarcode({
hint: CapacitorBarcodeScannerTypeHint.QR_CODE,
scanInstructions: options?.title || t("scanner.hint"),
cameraDirection: CapacitorBarcodeScannerCameraDirection.BACK,
scanOrientation: CapacitorBarcodeScannerScanOrientation.PORTRAIT,
});
vibrate();
return {
text: result.ScanResult,
format: result.format,
};
}
catch (error: any) {
console.log("error.message", error.message);
if (error.code !== "OS-PLUG-BARC-0006") {
const toast = await toastController.create({
message: t("scanner.openError"),
duration: 2000,
position: "bottom",
color: "danger",
});
await toast.present();
vibrate();
resolve(result.ScanResult);
}
}
}
catch (error: any) {
console.log("error.message", error.message);
if (error.code !== "OS-PLUG-BARC-0006") {
const toast = await toastController.create({
message: t("scanner.openError"),
duration: 2000,
position: "bottom",
color: "danger",
});
await toast.present();
reject();
}
}
});
}
function close() {
}