Initial commit

This commit is contained in:
admin
2025-12-10 17:22:27 +08:00
parent 9292ef8d8d
commit 61c61a77a2
17 changed files with 142 additions and 19 deletions

View File

@@ -1,6 +1,54 @@
const app = getApp()
function getOrigin(url) {
if (!url) return ''
const match = url.match(/^(https?:\/\/[^/]+)/)
return match ? match[1] : ''
}
function replaceUrl(data, targetOrigin) {
if (!data || !targetOrigin) return data
if (typeof data === 'string') {
// Replace localhost/127.0.0.1 with target origin
// Handle both http and https, though localhost usually http
return data.replace(/https?:\/\/(localhost|127\.0\.0\.1):8000/g, targetOrigin)
}
if (Array.isArray(data)) {
return data.map(item => replaceUrl(item, targetOrigin))
}
if (typeof data === 'object') {
const newData = {}
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
newData[key] = replaceUrl(data[key], targetOrigin)
}
}
return newData
}
return data
}
function unwrap(data) {
// First, apply URL replacement if needed
// We need to access app.globalData which might not be ready if called too early,
// but usually request is called after app launch.
// Safely get app instance again inside function
try {
const app = getApp()
if (app && app.globalData && app.globalData.baseUrl) {
const origin = getOrigin(app.globalData.baseUrl)
if (origin) {
data = replaceUrl(data, origin)
}
}
} catch (e) {
console.warn('URL replacement failed', e)
}
if (data && typeof data === 'object' && 'code' in data && 'data' in data) {
return data.data
}