puning-real-estate/scripts/getEncryptedUrl.js
2026-01-22 14:40:42 +08:00

48 lines
1.6 KiB
JavaScript

import axios from './axios.js';
// XML Encode
function xmlEncode(text) {
if (typeof text !== 'string') return '';
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;');
}
/**
* Takes a relative URL from the website and returns a fully formed, encrypted URL.
* @param {string} relativeUrl - The relative URL like 'ProjectDetailsInfo.aspx?a=1&b=2'.
* @param {string} origin - The origin of the website, e.g., 'http://120.236.48.169:89'.
* @returns {Promise<string>} - The full, encrypted URL.
*/
export async function getEncryptedUrl(relativeUrl, origin) {
if (!relativeUrl) {
return '';
}
const urlParts = relativeUrl.split('?');
if (urlParts.length < 2) {
return new URL(relativeUrl, origin).href;
}
const path = urlParts[0];
const queryString = urlParts[1];
const xmlPayload = `<?xml version="1.0" encoding="utf-8" standalone="yes"?> <param funname="SouthDigital.BasicFun.MyEncrypt.DefaultEncrypt"> <item>${xmlEncode(queryString)}</item> </param>`;
const encryptionUrl = new URL('/Common/Agents/ExeFunCommon.aspx', origin).href;
try {
const response = await axios.post(encryptionUrl, xmlPayload, {
headers: {
'Content-Type': 'application/xml',
}
});
const encryptedQuery = response.data;
if (encryptedQuery) {
return `${new URL(path, origin).href}?${encodeURIComponent(encryptedQuery)}`;
}
} catch (error) {
console.error(`加密链接失败: ${relativeUrl}`, error.message);
}
// Fallback to original url on error
return new URL(relativeUrl, origin).href;
}