scanner: support q= code in url

This commit is contained in:
Fam Zheng 2025-08-16 18:35:38 +01:00
parent 4b6d6e7afa
commit e9d32b739d
3 changed files with 45 additions and 2 deletions

View File

@ -1282,6 +1282,12 @@ class MiniProgContentView(BaseView):
def get(self, request):
pk = request.GET.get('tenant')
code = request.GET.get('code')
if code:
sc = get_object_or_404(SerialCode, code=code)
if not sc.tenant:
return http404('code has no tenant')
pk = sc.tenant.pk
if not pk or not pk.isnumeric():
x = get_object_or_404(MiniProgramContent, tenant=None)
else:

View File

@ -8,6 +8,7 @@ import {
get_camera_rule,
goto_camera,
get_phone_model,
extract_code_from_url,
} from '../../utils.js'
Page({
@ -43,11 +44,14 @@ Page({
this.patch_url(c.static);
return c;
},
load_content(tenant_id) {
load_content(tenant_id, code) {
var url = app.globalData.server_url + '/api/v1/mini-program-content/';
if (tenant_id) {
url += "?tenant=" + tenant_id;
}
if (code) {
url += "&code=" + code;
}
console.log(url);
var r = wx.request({
url,
@ -89,7 +93,11 @@ Page({
});
return;
}
this.load_content(options.tenant);
var code = null;
if (options.q) {
code = extract_code_from_url(decodeURIComponent(options.q));
}
this.load_content(options.tenant, code);
},
doGetUserProfile() {

View File

@ -108,6 +108,34 @@ function send_event(event_name, data, success_cb, fail_cb) {
});
}
function extract_code_from_url(url) {
/*
def get_code_from_url(url):
code = None
patterns = [
'[?&]code=([0-9a-zA-Z]+)',
'[?&]c=([0-9a-zA-Z]+)',
'xy.ltd/v/([0-9a-zA-Z]+)'
]
for p in patterns:
m = re.search(p, url)
if m:
return m.group(1)
*/
var patterns = [
/[?&]code=([0-9a-zA-Z]+)/,
/[?&]c=([0-9a-zA-Z]+)/,
/xy\.ltd\/v\/([0-9a-zA-Z]+)/,
];
for (var p of patterns) {
var code_match = url.match(p);
if (code_match) {
return code_match[1];
}
}
return null;
}
module.exports = {
get_system_info,
get_phone_model,
@ -116,5 +144,6 @@ module.exports = {
formatTime,
make_new_session_id,
send_event,
extract_code_from_url,
}