emblemscanner: avoid using global data

This commit is contained in:
Fam Zheng 2025-10-15 12:30:43 +01:00
parent e68348fbff
commit c08d830127
7 changed files with 57 additions and 74 deletions

View File

@ -8,20 +8,9 @@ var performance = {
Module['instantiateWasm'] = (info, receiveInstance) => { Module['instantiateWasm'] = (info, receiveInstance) => {
console.log("loading wasm...", info); console.log("loading wasm...", info);
// Use dynamic path from global data if available, otherwise fallback to hardcoded path // Use provided wasmPath parameter, fallback to hardcoded path
var wasmPath = "pages/emblemscanner/qrtool.wx.wasm.br"; var wasmPath = Module.wasmFilePath || "pages/emblemscanner/qrtool.wx.wasm.br";
try { console.log("Using WASM path:", wasmPath);
// Check if we're in a WeChat miniprogram environment and have access to getApp
if (typeof getApp !== 'undefined') {
var app = getApp();
if (app && app.globalData && app.globalData.wasmFilePath) {
wasmPath = app.globalData.wasmFilePath;
console.log("Using dynamic WASM path:", wasmPath);
}
}
} catch (e) {
console.warn("Failed to get dynamic WASM path, using fallback:", e);
}
WebAssembly.instantiate(wasmPath, info).then((result) => { WebAssembly.instantiate(wasmPath, info).then((result) => {
console.log("result:", result); console.log("result:", result);

View File

@ -107,7 +107,10 @@ Page({
scan_mode: 'unknown', // 'camera', 'webview' scan_mode: 'unknown', // 'camera', 'webview'
no_web_view: false, // Override web-view rule, force native camera no_web_view: false, // Override web-view rule, force native camera
worker_processing: false, // Track if worker is currently processing a frame worker_processing: false, // Track if worker is currently processing a frame
verifying_stage: 0 // Verification spinner animation stage verifying_stage: 0, // Verification spinner animation stage
currentPackagePath: 'pages/emblemscanner', // Current package path for dynamic WASM loading
wasmFilePath: '', // WASM file path for dynamic loading
verify_resp: null // Verification response from API
}, },
onLoad(options) { onLoad(options) {
@ -123,12 +126,17 @@ Page({
no_web_view: no_web_view no_web_view: no_web_view
}); });
// Store current package path in global data for dynamic WASM loading // Store current package path in page data for dynamic WASM loading
const currentPackagePath = get_current_package_path(); const currentPackagePath = get_current_package_path();
getApp().globalData.currentPackagePath = currentPackagePath; const wasmFilePath = get_wasm_file_path('qrtool.wx.wasm.br');
getApp().globalData.wasmFilePath = get_wasm_file_path('qrtool.wx.wasm.br');
this.setData({
currentPackagePath: currentPackagePath,
wasmFilePath: wasmFilePath
});
console.log('Current package path:', currentPackagePath); console.log('Current package path:', currentPackagePath);
console.log('WASM file path:', getApp().globalData.wasmFilePath); console.log('WASM file path:', wasmFilePath);
// Log page load for backend reporting (like camera.js) // Log page load for backend reporting (like camera.js)
this.log("emblemscanner page load"); this.log("emblemscanner page load");
@ -163,7 +171,7 @@ Page({
}, },
initializeQRTool() { initializeQRTool() {
load_qrtool(); load_qrtool(this.data.wasmFilePath);
const checkReady = () => { const checkReady = () => {
if (is_qrtool_ready()) { if (is_qrtool_ready()) {
@ -205,13 +213,16 @@ Page({
*/ */
setupWorker() { setupWorker() {
// Create worker with dynamic path based on current package location // Create worker with dynamic path based on current package location
var packagePath = getApp().globalData.currentPackagePath || 'pages/emblemscanner'; var packagePath = this.data.currentPackagePath || 'pages/emblemscanner';
var workerPath = `${packagePath}/worker/index.js`; var workerPath = `${packagePath}/worker/index.js`;
console.log('Creating worker with path:', workerPath); console.log('Creating worker with path:', workerPath);
var worker = wx.createWorker(workerPath, { var worker = wx.createWorker(workerPath, {
useExperimentalWorker: true, useExperimentalWorker: true,
}); });
// Pass the WASM file path to the worker
worker.wasmFilePath = this.data.wasmFilePath;
worker.onMessage((msg) => { worker.onMessage((msg) => {
console.log('Worker message:', msg.type); console.log('Worker message:', msg.type);
@ -733,8 +744,8 @@ Page({
resp = res.data; resp = res.data;
} }
// Store verification response // Store verification response in page data
getApp().globalData.verify_resp = resp; this.setData({ verify_resp: resp });
if (resp.serial_code) { if (resp.serial_code) {
// Let verification animation run for a bit, then redirect with success // Let verification animation run for a bit, then redirect with success
@ -761,12 +772,7 @@ Page({
this.goToResult(qrCode, null, false); this.goToResult(qrCode, null, false);
}; };
// Store global data like camera.js // Use enhanced upload function with local metadata (no global data needed)
const gd = getApp().globalData;
gd.image_data_urls = dataUrls;
gd.qr_code = qrCode;
// Use enhanced upload function with local metadata instead of global data
const metadata = { const metadata = {
real_ip: this.data.real_ip, real_ip: this.data.real_ip,
phone_model: this.data.phone_model, phone_model: this.data.phone_model,
@ -774,7 +780,7 @@ Page({
userinfo: this.data.userinfo userinfo: this.data.userinfo
}; };
upload_image_data_urls_with_metadata(dataUrls, qrCode, metadata, success, fail, this.data.logs.join("\n")); upload_image_data_urls_with_metadata(dataUrls, qrCode, metadata, success, fail, this.data.logs.join("\n"), null);
}, },
@ -798,7 +804,7 @@ Page({
hint_text: '检查补光设置...' hint_text: '检查补光设置...'
}); });
check_auto_torch(qrcode, (auto_torch, camera_sensitivity) => { check_auto_torch(qrcode, this.data.server_url, (auto_torch, camera_sensitivity) => {
this.log(`Auto-torch check: ${auto_torch}, sensitivity: ${camera_sensitivity}`); this.log(`Auto-torch check: ${auto_torch}, sensitivity: ${camera_sensitivity}`);
if (auto_torch) { if (auto_torch) {

View File

@ -7,8 +7,13 @@ var qrtool_ready = false;
/** /**
* Load qrtool WASM module * Load qrtool WASM module
*/ */
function load_qrtool() { function load_qrtool(wasmFilePath) {
// Load the WASM module
var m = require('./qrtool.wx.js'); var m = require('./qrtool.wx.js');
// Set the WASM file path
m.wasmFilePath = wasmFilePath;
m.onRuntimeInitialized = () => { m.onRuntimeInitialized = () => {
qrtool_ready = true; qrtool_ready = true;
qrtool = m; qrtool = m;

View File

@ -34,20 +34,9 @@ var performance = {
}; };
Module["instantiateWasm"] = (info, receiveInstance) => { Module["instantiateWasm"] = (info, receiveInstance) => {
console.log("loading wasm...", info); console.log("loading wasm...", info);
// Use dynamic path from global data if available, otherwise fallback to hardcoded path // Use provided wasmPath parameter, fallback to hardcoded path
var wasmPath = "pages/emblemscanner/qrtool.wx.wasm.br"; var wasmPath = Module.wasmFilePath || "pages/emblemscanner/qrtool.wx.wasm.br";
try { console.log("Using WASM path:", wasmPath);
// Check if we're in a WeChat miniprogram environment and have access to getApp
if (typeof getApp !== "undefined") {
var app = getApp();
if (app && app.globalData && app.globalData.wasmFilePath) {
wasmPath = app.globalData.wasmFilePath;
console.log("Using dynamic WASM path:", wasmPath);
}
}
} catch (e) {
console.warn("Failed to get dynamic WASM path, using fallback:", e);
}
WebAssembly.instantiate(wasmPath, info).then(result => { WebAssembly.instantiate(wasmPath, info).then(result => {
console.log("result:", result); console.log("result:", result);
var inst = result["instance"]; var inst = result["instance"];

View File

@ -1,6 +1,5 @@
function check_auto_torch(qrcode, cb) { function check_auto_torch(qrcode, server_url, cb) {
var gd = getApp().globalData; var url = server_url + '/api/v1/check-auto-torch/?qrcode=' + encodeURIComponent(qrcode);
var url = gd.server_url + '/api/v1/check-auto-torch/?qrcode=' + encodeURIComponent(qrcode);
console.log("check_auto_torch:", url); console.log("check_auto_torch:", url);
const fail = (e) => { const fail = (e) => {
console.log("failed to check auto torch", e); console.log("failed to check auto torch", e);
@ -29,25 +28,24 @@ function check_auto_torch(qrcode, cb) {
}); });
} }
function upload_image_data_urls(image_data_urls, success, fail, log) { function upload_image_data_urls(image_data_urls, success, fail, log, real_ip, qr_code, phone_model, server_url, caller_info) {
var ui = wx.getStorageSync('userinfo'); var ui = wx.getStorageSync('userinfo');
var gd = getApp().globalData;
var fd = { var fd = {
emblem_id: ui.emblem_id, emblem_id: ui.emblem_id,
nick_name: ui.nickName, nick_name: ui.nickName,
realip: gd.real_ip, realip: real_ip,
qrcode: gd.qr_code, qrcode: qr_code,
angle: 0, angle: 0,
phonemodel: gd.phone_model, phonemodel: phone_model,
image_data_urls, image_data_urls,
use_roi_verify: 1, use_roi_verify: 1,
log, log,
}; };
var ci = gd.caller_info; var ci = caller_info;
if (ci && ci.token) { if (ci && ci.token) {
fd.token = ci.token; fd.token = ci.token;
} }
var url = gd.server_url + '/api/v1/qr-verify/'; var url = server_url + '/api/v1/qr-verify/';
console.log("wx.request", url, fd.qrcode, fd.angle, fd.phonemodel, fd.realip); console.log("wx.request", url, fd.qrcode, fd.angle, fd.phonemodel, fd.realip);
wx.request({ wx.request({
url, url,
@ -61,8 +59,7 @@ function upload_image_data_urls(image_data_urls, success, fail, log) {
}); });
} }
function upload_image_data_urls_with_metadata(image_data_urls, qrcode, metadata, success, fail, log) { function upload_image_data_urls_with_metadata(image_data_urls, qrcode, metadata, success, fail, log, caller_info) {
var gd = getApp().globalData;
var fd = { var fd = {
emblem_id: metadata.userinfo.emblem_id, emblem_id: metadata.userinfo.emblem_id,
nick_name: metadata.userinfo.nickName, nick_name: metadata.userinfo.nickName,
@ -74,7 +71,7 @@ function upload_image_data_urls_with_metadata(image_data_urls, qrcode, metadata,
use_roi_verify: 1, use_roi_verify: 1,
log, log,
}; };
var ci = gd.caller_info; var ci = caller_info;
if (ci && ci.token) { if (ci && ci.token) {
fd.token = ci.token; fd.token = ci.token;
} }

View File

@ -1,6 +1,14 @@
console.log("hello from emblemscanner worker"); console.log("hello from emblemscanner worker");
let qrtool = require('./qrtool.wx.js'); // Set WASM file path before loading qrtool
// The wasmFilePath will be passed from the main thread when the worker is created
if (typeof worker !== "undefined" && worker.wasmFilePath) {
let qrtoolModule = require('./qrtool.wx.js');
qrtoolModule.wasmFilePath = worker.wasmFilePath;
let qrtool = qrtoolModule;
} else {
let qrtool = require('./qrtool.wx.js');
}

View File

@ -34,20 +34,9 @@ var performance = {
}; };
Module["instantiateWasm"] = (info, receiveInstance) => { Module["instantiateWasm"] = (info, receiveInstance) => {
console.log("loading wasm...", info); console.log("loading wasm...", info);
// Use dynamic path from global data if available, otherwise fallback to hardcoded path // Use provided wasmPath parameter, fallback to hardcoded path
var wasmPath = "pages/emblemscanner/qrtool.wx.wasm.br"; var wasmPath = Module.wasmFilePath || "pages/emblemscanner/qrtool.wx.wasm.br";
try { console.log("Using WASM path:", wasmPath);
// Check if we're in a WeChat miniprogram environment and have access to getApp
if (typeof getApp !== "undefined") {
var app = getApp();
if (app && app.globalData && app.globalData.wasmFilePath) {
wasmPath = app.globalData.wasmFilePath;
console.log("Using dynamic WASM path:", wasmPath);
}
}
} catch (e) {
console.warn("Failed to get dynamic WASM path, using fallback:", e);
}
WebAssembly.instantiate(wasmPath, info).then(result => { WebAssembly.instantiate(wasmPath, info).then(result => {
console.log("result:", result); console.log("result:", result);
var inst = result["instance"]; var inst = result["instance"];