调查问卷小程序 📋 调查问卷小程序 快速、简单、专业的在线问卷调查平台 🎯 精准定位 📊 数据分析 ⚡ 快速部署 🔒 数据安全 📱 扫描二维码 📝 输入问卷名称 扫描二维码 📷 点击上传图片识别二维码 或使用摄像头扫描 📹 打开摄像头 返回 输入问卷名称 搜索问卷 返回 const API_URL = '/api.php'; let currentSurvey = null; let answers = {}; let videoStream = null; let scanning = false; // 页面切换函数 function showPage(pageId) { document.querySelectorAll('.page').forEach(p => p.classList.remove('active')); document.getElementById(pageId).classList.add('active'); // 如果是扫描页面,禁止body滚动 if (pageId === 'scanPage') { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = ''; } } function goHome() { showPage('homePage'); } function goToScan() { showPage('scanPage'); // 延迟启动摄像头,等待页面渲染完成 setTimeout(() => { startCamera(); }, 100); } function goToInput() { showPage('inputPage'); document.getElementById('surveyNameInput').focus(); } // 摄像头扫码功能 async function startCamera() { try { const video = document.getElementById('video'); videoStream = await ************diaDevices.getUserMedia({ video: { facingMode: 'environment' } }); video.srcObject = videoStream; video.style.display = 'block'; scanning = true; requestAnimationFrame(scanQRCode); } catch (error) { // 摄像头启动失败,显示备用选项 console.error('摄像头启动失败:', error); document.getElementById('uploadArea').style.display = 'block'; document.getElementById('startCameraBtn').style.display = 'inline-block'; alert('无法访问摄像头,请使用图片上传功能或手动打开摄像头'); } } function stopCamera() { if (videoStream) { videoStream.getTracks().forEach(track => track.stop()); videoStream = null; } scanning = false; document.getElementById('video').style.display = 'none'; } function scanQRCode() { if (!scanning) return; const video = document.getElementById('video'); const canvas = document.getElementById('canvas'); const context = canvas.getContext('2d'); if (video.readyState === video.HAVE_ENOUGH_DATA) { canvas.width = video.videoWidth; canvas.height = video.videoHeight; context.drawImage(video, 0, 0, canvas.width, canvas.height); const imageData = context.getImageData(0, 0, canvas.width, canvas.height); const code = jsQR(imageData.data, imageData.width, imageData.height); if (code) { stopCamera(); handleQRCodeData(code.data); return; } } requestAnimationFrame(scanQRCode); } // 处理图片上传 function handleImageUpload(event) { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = function(e) { const img = new Image(); img.onload = function() { const canvas = document.getElementById('canvas'); const context = canvas.getContext('2d'); canvas.width = img.width; canvas.height = img.height; context.drawImage(img, 0, 0); const imageData = context.getImageData(0, 0, canvas.width, canvas.height); const code = jsQR(imageData.data, imageData.width, imageData.height); if (code) { handleQRCodeData(code.data); } else { alert('未识别到二维码,请重试'); } }; img.src = e.target.result; }; reader.readAsDataURL(file); } // 处理二维码数据 function handleQRCodeData(data) { // 尝试解析URL中的survey_id参数 try { const url = new URL(data); const surveyId = url.searchParams.get('survey_id'); if (surveyId) { // 在新页面打开问卷 window.open(`survey.html?survey_id=${surveyId}`, '_blank'); goHome(); } else { alert('二维码中未找到问卷ID'); } } catch (e) { alert('无效的二维码格式'); } } // 搜索问卷(根据名称) async function searchSurvey() { const surveyName = document.getElementById('surveyNameInput').value.trim(); if (!surveyName) { alert('请输入问卷名称'); return; } try { const response = await fetch(`/search_api.php?action=search_survey&name=${encodeURIComponent(surveyName)}`); const data = await response.json(); if (*******de === 200 && data.data) { // 在新页面打开问卷 window.open(`survey.html?survey_id=${data.data.id}`, '_blank'); // 搜索成功后不返回首页,让用户继续停留在输入页面 } else { alert('未找到该问卷: ' + *******ssage); } } catch (error) { alert('搜索失败: ' + ********ssage); } } // 加载问卷(保留原有功能,用于直接访问) async function loadSurvey() { const surveyId = document.getElementById('surveyIdInput').value.trim(); if (!surveyId) { showAlert('请输入问卷ID', 'error'); return; } try { const response = await fetch(`${API_URL}?action=get_survey&survey_id=${surveyId}`); const data = await response.json(); if (*******de === 200) { currentSurvey = data.data; answers = {}; renderSurvey(); showPage('surveyPage'); } else { showAlert('问卷不存在: ' + *******ssage, 'error'); } } catch (error) { showAlert('加载失败: ' + ********ssage, 'error'); } } // 渲染问卷 function renderSurvey() { if (!currentSurvey) return; let html = ` ${currentSurvey.title} ${currentSurvey.description || ''} `; currentSurvey.questions.forEach((q, idx) => { html += ` ${idx + 1}. ${q.text}`; if (q.type === 'single') { q.options.forEach(opt => { html += ` ${opt.text} `; }); } else if (q.type === 'multiple' || q.type === 'checkbox') { q.options.forEach(opt => { html += ` ${opt.text} `; }); } else if (q.type === 'rating') { for (let i = 1; i goHome(), 2000); } else { showAlert('提交失败: ' + *******ssage, 'error'); } } catch (error) { showAlert('提交失败: ' + ********ssage, 'error'); } } // 显示提示 function showAlert(msg, type) { const box = document.getElementById('alertBox'); box.innerHTML = `${msg}`; setTimeout(() => { box.innerHTML = ''; }, 5000); }