// QR Scanner Module - Self-contained QR scanning page // Adapted from existing camera implementation Page({ /** * Page initial data */ data: { hint_text: '查找二维码', enable_debug: false, camera_flash: 'off', phone_model: 'unknown', zoom: -1, max_zoom: 1, use_worker: false, show_tip: false, show_modal: '', busy: true, should_check_auto_torch: true, done_checking_auto_torch: false, camera_sensitivity: 1, frame_uploaded: 0, frame_upload_time_cost: 0, qrarc_class: 'sm', qrmarkers_class: 'hidden', frame_upload_interval_ms: 2000, return_page: '', // Page to navigate to after successful scan server_url: 'https://themblem.com', // Default server URL debug_msgs: [], debug_image_data_url: '' }, /** * Lifecycle function--Called when page load */ onLoad(options) { console.log('QR Scanner module loaded', options); // Store return page from query parameters if (options.return_page) { this.setData({ return_page: options.return_page }); } // Initialize image data storage this.image_data_urls = []; // Get system information this.initializeSystem(); // Load camera rules this.loadCameraRules(); }, /** * Initialize system information and device detection */ initializeSystem() { const systemInfo = wx.getSystemInfoSync(); const phone_model = systemInfo.model; const use_worker = phone_model.toLowerCase().includes('iphone'); this.setData({ phone_model, window_width: systemInfo.windowWidth, window_height: systemInfo.windowHeight, use_worker }); console.log(`Phone model: ${phone_model}, Use worker: ${use_worker}`); // Initialize worker for iPhone devices if (use_worker) { this.initializeWorker(); } }, /** * Initialize WebAssembly worker for QR processing */ initializeWorker() { // TODO: Initialize worker - requires qrtool.wx.js and worker setup console.log('Worker initialization would happen here'); }, /** * Load camera rules from API */ loadCameraRules() { // TODO: Implement camera rules loading from API console.log('Camera rules loading would happen here'); // For now, set default values this.setData({ zoom: 1, camera_sensitivity: 1, busy: false }); }, /** * Camera initialization callback */ setup_camera(e) { console.log('Camera setup', e); this.camera_context = wx.createCameraContext(); // Set up camera frame listener this.camera_context.onCameraFrame((frame) => { this.processFrame(frame); }); this.setData({ busy: false, hint_text: '查找二维码' }); }, /** * Process camera frame for QR detection */ processFrame(frame) { if (this.data.busy) return; // TODO: Implement QR detection logic console.log('Processing frame', frame.width, frame.height); // For demo purposes, simulate QR detection if (Math.random() < 0.01) { // 1% chance to simulate QR found this.simulateQRFound(); } }, /** * Simulate QR code found (for testing) */ simulateQRFound() { this.setData({ hint_text: '识别成功', show_modal: 'verifying' }); // Simulate verification process setTimeout(() => { this.onVerificationSuccess('test-qr-code'); }, 2000); }, /** * Handle successful QR verification */ onVerificationSuccess(qrCode) { console.log('QR verification successful:', qrCode); if (this.data.return_page) { wx.navigateTo({ url: `${this.data.return_page}?qr_code=${encodeURIComponent(qrCode)}`, success: () => { console.log(`Navigated to return page: ${this.data.return_page}`); }, fail: (err) => { console.error('Failed to navigate to return page:', err); this.restart_camera(); } }); } else { console.warn('No return page specified'); this.restart_camera(); } }, /** * Toggle torch/flash */ toggle_torch() { const newFlash = this.data.camera_flash === 'torch' ? 'off' : 'torch'; this.setData({ camera_flash: newFlash }); console.log('Torch toggled to:', newFlash); }, /** * Show scan guide */ show_scanguide() { this.setData({ show_modal: 'scanguide', show_tip: false }); }, /** * Show service modal */ show_service() { this.setData({ show_modal: 'service' }); }, /** * Close modal and restart camera */ restart_camera() { this.setData({ show_modal: '', hint_text: '查找二维码', busy: false }); }, /** * Close any modal */ close_modal() { this.setData({ show_modal: '' }); }, /** * Debug tap handler */ debug_tap() { const count = (this.debug_tap_count || 0) + 1; this.debug_tap_count = count; if (count >= 5) { this.setData({ enable_debug: !this.data.enable_debug }); this.debug_tap_count = 0; } // Clear count after 3 seconds setTimeout(() => { this.debug_tap_count = 0; }, 3000); }, /** * Generate hint text based on QR detection result */ make_hint_text(result) { if (result && result.qrcode && result.qrcode.length > 0) { const err = result.err || ''; if (err.includes('margin too small')) { return '对齐定位点'; } else if (err.includes('energy check failed') || err.includes('cannot detect angle')) { return '移近一点'; } return '对齐定位点'; } return '查找二维码'; } });