142 lines
3.5 KiB
JavaScript
142 lines
3.5 KiB
JavaScript
// Emblem Scanner Library - Utility functions for QR scanning
|
|
// Self-contained utility functions for camera rules, device detection, and query building
|
|
|
|
var camera_rules = null;
|
|
|
|
/**
|
|
* Get system information
|
|
*/
|
|
function get_system_info() {
|
|
return wx.getSystemInfoSync();
|
|
}
|
|
|
|
/**
|
|
* Get phone model from system info
|
|
*/
|
|
function get_phone_model() {
|
|
var ret = get_system_info().model;
|
|
console.log("phone model", ret);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* Match camera rules based on phone model
|
|
*/
|
|
function match_camera_rules(model, rules, default_zoom) {
|
|
console.log(model, "apply zoom rules:", rules);
|
|
var best_match = null;
|
|
for (var rule of rules) {
|
|
if (model.toLowerCase().startsWith(rule.model.toLowerCase())) {
|
|
if (!best_match || rule.model.length > best_match.model.length) {
|
|
best_match = rule;
|
|
}
|
|
}
|
|
}
|
|
if (best_match) {
|
|
console.log("found best match", best_match);
|
|
return best_match;
|
|
}
|
|
var ret = {
|
|
zoom: default_zoom,
|
|
web_view: false
|
|
};
|
|
console.log("using default", ret);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* Get camera rule from API or cache
|
|
*/
|
|
function get_camera_rule(max_zoom, cb) {
|
|
var default_zoom = 4;
|
|
if (max_zoom && max_zoom >= 60) {
|
|
/*
|
|
* 2024.06.01: in some Huawei/Honor models, the scale is different, use 40
|
|
* in this case so we don't need to set up rules for each specific model
|
|
*/
|
|
console.log(`max zoom is ${max_zoom}, default zoom will be 40`);
|
|
default_zoom = 40;
|
|
}
|
|
if (camera_rules) {
|
|
let rule = match_camera_rules(get_phone_model(), camera_rules, default_zoom);
|
|
cb(rule);
|
|
} else {
|
|
var url = 'https://themblem.com/api/v1/camera-rules/';
|
|
wx.request({
|
|
url,
|
|
complete: (res) => {
|
|
var rules = res.data;
|
|
camera_rules = rules;
|
|
let rule = match_camera_rules(get_phone_model(), rules, default_zoom);
|
|
cb(rule);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Build query string for web-view camera
|
|
*/
|
|
function make_query(zoom, return_page, real_ip, tenant_id) {
|
|
var ret = "zoom=" + zoom;
|
|
var ui = wx.getStorageSync('userinfo') || {};
|
|
ret += "&phonemodel=" + encodeURIComponent(get_phone_model());
|
|
ret += "&realip=" + (real_ip || "");
|
|
ret += "&emblem_id=" + (ui.emblem_id || "");
|
|
ret += "&nick_name=" + encodeURIComponent(ui.nickName || "");
|
|
ret += "&tenant=" + (tenant_id || "");
|
|
ret += "&tk=" + Date.now();
|
|
if (return_page) {
|
|
ret += "&wx_redirect_to=" + encodeURIComponent(return_page);
|
|
}
|
|
console.log("Web-view query:", ret);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* Fetch real IP address
|
|
*/
|
|
function fetch_real_ip(callback) {
|
|
wx.request({
|
|
url: 'https://whatsmyip.hondcloud.com',
|
|
success: (res) => {
|
|
const ip = res.data || '';
|
|
console.log('Real IP fetched:', ip);
|
|
callback(null, ip);
|
|
},
|
|
fail: (err) => {
|
|
console.error('Failed to fetch real IP:', err);
|
|
callback(err, '');
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get tenant ID from storage
|
|
*/
|
|
function get_tenant_id() {
|
|
const tenant_id = wx.getStorageSync('tenant_id') || '';
|
|
console.log('Tenant ID:', tenant_id);
|
|
return tenant_id;
|
|
}
|
|
|
|
/**
|
|
* Check if QR code matches Emblem pattern
|
|
*/
|
|
function is_emblem_qr_pattern(p) {
|
|
if (p.search(/code=[0-9a-zA-Z]+/) >= 0) return true;
|
|
if (p.search(/id=[0-9a-zA-Z]+/) >= 0) return true;
|
|
if (p.search(/c=[0-9a-zA-Z]+/) >= 0) return true;
|
|
if (p.search(/https:\/\/xy.ltd\/v\/[0-9a-zA-Z]+/) >= 0) return true;
|
|
return false;
|
|
}
|
|
|
|
module.exports = {
|
|
get_system_info,
|
|
get_phone_model,
|
|
get_camera_rule,
|
|
make_query,
|
|
fetch_real_ip,
|
|
get_tenant_id,
|
|
is_emblem_qr_pattern
|
|
}; |