-
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 将二维码放入框内,即可自动扫描
+
+
+
+
+
+
+
+
-
+
diff --git a/src/composables/useQRScanner.ts b/src/composables/useQRScanner.ts
index 506001a..8388d48 100644
--- a/src/composables/useQRScanner.ts
+++ b/src/composables/useQRScanner.ts
@@ -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
(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().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() {
}