فهرست منبع

二维码扫码申请权限

sunshibo 1 ماه پیش
والد
کامیت
97b70351a8
2فایلهای تغییر یافته به همراه51 افزوده شده و 23 حذف شده
  1. 47 22
      components/TabBar.vue
  2. 4 1
      manifest.json

+ 47 - 22
components/TabBar.vue

@@ -134,31 +134,56 @@ function handleScanOrder() {
 		uni.showToast({ title: '请登录', icon: 'none' });
 		return;
 	}
-	uni.scanCode({
-		scanType: ['qrCode'],
-		success: (res) => {
-			// 普通二维码用 result;小程序码用 path(含 scene)
-			const result = (res?.path || res?.result || '').trim();
-			const { storeId, tableId } = parseScanResult(result);
-			if (!tableId) {
-				console.warn('[扫码] 未解析到桌号,原始内容:', result, '解析结果:', { storeId, tableId });
-			}
-			const payload = { raw: result, storeId, tableId };
-			uni.setStorageSync(SCAN_QR_CACHE, JSON.stringify(payload));
-			if (storeId) uni.setStorageSync('currentStoreId', storeId);
-			if (tableId) uni.setStorageSync('currentTableId', tableId);
-			const diners = uni.getStorageSync('currentDiners') || '1';
-			uni.setStorageSync('currentDiners', diners);
-			uni.reLaunch({
-				url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableId || '')}&diners=${encodeURIComponent(diners)}`
+	// scanCode 真机需相机权限,先申请再扫码
+	uni.authorize({
+		scope: 'scope.camera',
+		success: () => runScan(),
+		fail: () => {
+			uni.showModal({
+				title: '提示',
+				content: '需要相机权限才能扫描桌号二维码,请在设置中开启',
+				confirmText: '去设置',
+				success: (res) => {
+					if (res.confirm) {
+						uni.openSetting({
+							success: (settingRes) => {
+								if (settingRes?.authSetting?.['scope.camera']) runScan();
+							}
+						});
+					}
+				}
 			});
-		},
-		fail: (err) => {
-			if (err?.errMsg && !String(err.errMsg).includes('cancel')) {
-				uni.showToast({ title: '扫码失败', icon: 'none' });
-			}
 		}
 	});
+
+	function runScan() {
+		uni.scanCode({
+			scanType: ['qrCode', 'barCode'],
+			success: (res) => {
+				const result = (res?.path || res?.result || '').trim();
+				const { storeId, tableId } = parseScanResult(result);
+				const payload = { raw: result, storeId, tableId };
+				uni.setStorageSync(SCAN_QR_CACHE, JSON.stringify(payload));
+				if (storeId) uni.setStorageSync('currentStoreId', storeId);
+				if (tableId) uni.setStorageSync('currentTableId', tableId);
+				const diners = uni.getStorageSync('currentDiners') || '1';
+				uni.setStorageSync('currentDiners', diners);
+				if (!tableId) {
+					uni.showToast({ title: '未识别到桌号,请扫描正确的桌号二维码', icon: 'none' });
+					console.warn('[扫码] 未解析到桌号,原始内容:', result);
+					return;
+				}
+				uni.reLaunch({
+					url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableId)}&diners=${encodeURIComponent(diners)}`
+				});
+			},
+			fail: (err) => {
+				if (err?.errMsg && !String(err.errMsg).includes('cancel')) {
+					uni.showToast({ title: err?.errMsg || '扫码失败', icon: 'none' });
+				}
+			}
+		});
+	}
 }
 
 // 底部 tabBar 跳转(需在 pages.json 中配置 tabBar.list)

+ 4 - 1
manifest.json

@@ -67,7 +67,10 @@
             "scope.userLocation" : {
                 "desc" : "你的位置信息将用于小程序位置接口的效果展示"
             },
-            "scope.record" : {}
+            "scope.record" : {},
+            "scope.camera" : {
+                "desc" : "用于扫描桌号二维码进行点餐"
+            }
         },
         "requiredPrivateInfos" : [ "getLocation" ]
     },