🐛 fix: Update getAppSubLink function to improve URL handling and encoding logic

This commit is contained in:
web 2025-09-04 22:51:59 -07:00
parent 4da59609b4
commit 351fffcc78

View File

@ -110,45 +110,71 @@ export const useGlobalStore = create<GlobalStore>((set, get) => ({
getAppSubLink: (url: string, schema?: string) => { getAppSubLink: (url: string, schema?: string) => {
const name = get().common?.site?.site_name || ''; const name = get().common?.site?.site_name || '';
if (!schema) return url; if (!schema) return 'url';
try { try {
let result = schema; let result = schema.replace(/\${url}/g, url).replace(/\${name}/g, name);
result = result.replace(/\${url}/g, url); const maxLoop = 10;
result = result.replace(/\${name}/g, name); let prev;
let loop = 0;
do {
prev = result;
result = result.replace(
/\${encodeURIComponent\(JSON\.stringify\(([^)]+)\)\)}/g,
(match, expr) => {
try {
const processedExpr = expr.replace(/url/g, `"${url}"`).replace(/name/g, `"${name}"`);
if (processedExpr.includes('server_remote')) {
const serverRemoteValue = `${url}, tag=${name}`;
return encodeURIComponent(JSON.stringify({ server_remote: [serverRemoteValue] }));
}
const obj = eval(`(${processedExpr})`);
return encodeURIComponent(JSON.stringify(obj));
} catch {
return match;
}
},
);
result = result.replace(/\${encodeURIComponent\(([^)]+)\)}/g, (match, expr) => { result = result.replace(/\${encodeURIComponent\(([^)]+)\)}/g, (match, expr) => {
if (expr === 'url') return encodeURIComponent(url); if (expr === 'url') return encodeURIComponent(url);
if (expr === 'name') return encodeURIComponent(name); if (expr === 'name') return encodeURIComponent(name);
return match; try {
}); return encodeURIComponent(expr);
} catch {
result = result.replace(/\${window\.btoa\(([^)]+)\)}/g, (match, expr) => { return match;
const btoa = typeof window !== 'undefined' ? window.btoa : (str: string) => str;
if (expr === 'url') return btoa(url);
if (expr === 'name') return btoa(name);
return match;
});
result = result.replace(/\${JSON\.stringify\(([^}]+)\)}/g, (match, expr) => {
try {
const processedExpr = expr.replace(/url/g, `"${url}"`).replace(/name/g, `"${name}"`);
if (processedExpr.includes('server_remote')) {
const serverRemoteValue = `${url}, tag=${name}`;
return JSON.stringify({ server_remote: [serverRemoteValue] });
} }
});
const result = eval(`(${processedExpr})`); result = result.replace(/\${window\.btoa\(([^)]+)\)}/g, (match, expr) => {
return JSON.stringify(result); const btoa = typeof window !== 'undefined' ? window.btoa : (str: string) => str;
} catch { if (expr === 'url') return btoa(url);
return match; if (expr === 'name') return btoa(name);
} try {
}); return btoa(expr);
} catch {
return match;
}
});
result = result.replace(/\${JSON\.stringify\(([^}]+)\)}/g, (match, expr) => {
try {
const processedExpr = expr.replace(/url/g, `"${url}"`).replace(/name/g, `"${name}"`);
if (processedExpr.includes('server_remote')) {
const serverRemoteValue = `${url}, tag=${name}`;
return JSON.stringify({ server_remote: [serverRemoteValue] });
}
const result = eval(`(${processedExpr})`);
return JSON.stringify(result);
} catch {
return match;
}
});
loop++;
} while (result !== prev && loop < maxLoop);
return result; return result;
} catch (error) { } catch (error) {
return url; return '';
} }
}, },
})); }));