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) => {
console.log("loading wasm...", info);
// Use dynamic path from global data if available, otherwise fallback to hardcoded path
var wasmPath = "pages/emblemscanner/qrtool.wx.wasm.br";
try {
// 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);
}
// Use provided wasmPath parameter, fallback to hardcoded path
var wasmPath = Module.wasmFilePath || "pages/emblemscanner/qrtool.wx.wasm.br";
console.log("Using WASM path:", wasmPath);
WebAssembly.instantiate(wasmPath, info).then((result) => {
console.log("result:", result);

View File

@ -107,7 +107,10 @@ Page({
scan_mode: 'unknown', // 'camera', 'webview'
no_web_view: false, // Override web-view rule, force native camera
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) {
@ -123,12 +126,17 @@ Page({
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();
getApp().globalData.currentPackagePath = currentPackagePath;
getApp().globalData.wasmFilePath = get_wasm_file_path('qrtool.wx.wasm.br');
const wasmFilePath = get_wasm_file_path('qrtool.wx.wasm.br');
this.setData({
currentPackagePath: currentPackagePath,
wasmFilePath: wasmFilePath
});
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)
this.log("emblemscanner page load");
@ -163,7 +171,7 @@ Page({
},
initializeQRTool() {
load_qrtool();
load_qrtool(this.data.wasmFilePath);
const checkReady = () => {
if (is_qrtool_ready()) {
@ -205,7 +213,7 @@ Page({
*/
setupWorker() {
// 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`;
console.log('Creating worker with path:', workerPath);
@ -213,6 +221,9 @@ Page({
useExperimentalWorker: true,
});
// Pass the WASM file path to the worker
worker.wasmFilePath = this.data.wasmFilePath;
worker.onMessage((msg) => {
console.log('Worker message:', msg.type);
@ -733,8 +744,8 @@ Page({
resp = res.data;
}
// Store verification response
getApp().globalData.verify_resp = resp;
// Store verification response in page data
this.setData({ verify_resp: resp });
if (resp.serial_code) {
// Let verification animation run for a bit, then redirect with success
@ -761,12 +772,7 @@ Page({
this.goToResult(qrCode, null, false);
};
// Store global data like camera.js
const gd = getApp().globalData;
gd.image_data_urls = dataUrls;
gd.qr_code = qrCode;
// Use enhanced upload function with local metadata instead of global data
// Use enhanced upload function with local metadata (no global data needed)
const metadata = {
real_ip: this.data.real_ip,
phone_model: this.data.phone_model,
@ -774,7 +780,7 @@ Page({
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: '检查补光设置...'
});
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}`);
if (auto_torch) {

View File

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

View File

@ -34,20 +34,9 @@ var performance = {
};
Module["instantiateWasm"] = (info, receiveInstance) => {
console.log("loading wasm...", info);
// Use dynamic path from global data if available, otherwise fallback to hardcoded path
var wasmPath = "pages/emblemscanner/qrtool.wx.wasm.br";
try {
// 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);
}
// Use provided wasmPath parameter, fallback to hardcoded path
var wasmPath = Module.wasmFilePath || "pages/emblemscanner/qrtool.wx.wasm.br";
console.log("Using WASM path:", wasmPath);
WebAssembly.instantiate(wasmPath, info).then(result => {
console.log("result:", result);
var inst = result["instance"];

View File

@ -1,6 +1,5 @@
function check_auto_torch(qrcode, cb) {
var gd = getApp().globalData;
var url = gd.server_url + '/api/v1/check-auto-torch/?qrcode=' + encodeURIComponent(qrcode);
function check_auto_torch(qrcode, server_url, cb) {
var url = server_url + '/api/v1/check-auto-torch/?qrcode=' + encodeURIComponent(qrcode);
console.log("check_auto_torch:", url);
const fail = (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 gd = getApp().globalData;
var fd = {
emblem_id: ui.emblem_id,
nick_name: ui.nickName,
realip: gd.real_ip,
qrcode: gd.qr_code,
realip: real_ip,
qrcode: qr_code,
angle: 0,
phonemodel: gd.phone_model,
phonemodel: phone_model,
image_data_urls,
use_roi_verify: 1,
log,
};
var ci = gd.caller_info;
var ci = caller_info;
if (ci && 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);
wx.request({
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) {
var gd = getApp().globalData;
function upload_image_data_urls_with_metadata(image_data_urls, qrcode, metadata, success, fail, log, caller_info) {
var fd = {
emblem_id: metadata.userinfo.emblem_id,
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,
log,
};
var ci = gd.caller_info;
var ci = caller_info;
if (ci && ci.token) {
fd.token = ci.token;
}

View File

@ -1,6 +1,14 @@
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) => {
console.log("loading wasm...", info);
// Use dynamic path from global data if available, otherwise fallback to hardcoded path
var wasmPath = "pages/emblemscanner/qrtool.wx.wasm.br";
try {
// 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);
}
// Use provided wasmPath parameter, fallback to hardcoded path
var wasmPath = Module.wasmFilePath || "pages/emblemscanner/qrtool.wx.wasm.br";
console.log("Using WASM path:", wasmPath);
WebAssembly.instantiate(wasmPath, info).then(result => {
console.log("result:", result);
var inst = result["instance"];