修复 SGS 导览数据本地加载异常
为 H5 本地联调补充 SGS API、SDK 和模型资源代理,避免 /app-api 与 /minio 请求回落到前端 HTML。 增强 SGS API 响应解析诊断,遇到非 JSON 或 HTML 响应时输出请求地址、content-type 和响应片段。
This commit is contained in:
@@ -209,9 +209,33 @@ const resolveAppApiBaseUrl = () => {
|
|||||||
return baseUrl.endsWith('/app-api') ? baseUrl : `${baseUrl}/app-api`
|
return baseUrl.endsWith('/app-api') ? baseUrl : `${baseUrl}/app-api`
|
||||||
}
|
}
|
||||||
|
|
||||||
const parseJsonPayload = <T>(payload: unknown): T => {
|
const getHeaderValue = (
|
||||||
|
header: Record<string, string> | undefined,
|
||||||
|
key: string
|
||||||
|
) => {
|
||||||
|
if (!header) return ''
|
||||||
|
const matchedKey = Object.keys(header).find((item) => item.toLowerCase() === key.toLowerCase())
|
||||||
|
return matchedKey ? String(header[matchedKey] || '') : ''
|
||||||
|
}
|
||||||
|
|
||||||
|
const previewPayload = (payload: unknown) => {
|
||||||
|
const text = typeof payload === 'string' ? payload : JSON.stringify(payload) || ''
|
||||||
|
return text.replace(/\s+/g, ' ').trim().slice(0, 120)
|
||||||
|
}
|
||||||
|
|
||||||
|
const parseJsonPayload = <T>(payload: unknown, requestUrl: string, contentType: string): T => {
|
||||||
if (typeof payload === 'string') {
|
if (typeof payload === 'string') {
|
||||||
return JSON.parse(payload) as T
|
const trimmedPayload = payload.trim()
|
||||||
|
const looksLikeHtml = /^<!doctype\s+html/i.test(trimmedPayload) || /^<html[\s>]/i.test(trimmedPayload)
|
||||||
|
if (looksLikeHtml || contentType.toLowerCase().includes('text/html')) {
|
||||||
|
throw new Error(`SGS 数据接口返回非 JSON: ${requestUrl} content-type=${contentType || 'unknown'} body="${previewPayload(payload)}"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(trimmedPayload) as T
|
||||||
|
} catch {
|
||||||
|
throw new Error(`SGS 数据接口 JSON 解析失败: ${requestUrl} content-type=${contentType || 'unknown'} body="${previewPayload(payload)}"`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return payload as T
|
return payload as T
|
||||||
@@ -222,9 +246,10 @@ const requestJson = <T>(
|
|||||||
options: { method?: 'GET' | 'POST'; data?: unknown } = {}
|
options: { method?: 'GET' | 'POST'; data?: unknown } = {}
|
||||||
): Promise<T> => new Promise((resolve, reject) => {
|
): Promise<T> => new Promise((resolve, reject) => {
|
||||||
const baseUrl = resolveAppApiBaseUrl()
|
const baseUrl = resolveAppApiBaseUrl()
|
||||||
|
const requestUrl = `${baseUrl}${path}`
|
||||||
|
|
||||||
uni.request({
|
uni.request({
|
||||||
url: `${baseUrl}${path}`,
|
url: requestUrl,
|
||||||
method: options.method || 'GET',
|
method: options.method || 'GET',
|
||||||
data: options.method === 'POST' ? JSON.stringify(options.data || {}) : undefined,
|
data: options.method === 'POST' ? JSON.stringify(options.data || {}) : undefined,
|
||||||
header: options.method === 'POST'
|
header: options.method === 'POST'
|
||||||
@@ -235,15 +260,16 @@ const requestJson = <T>(
|
|||||||
timeout: dataSourceConfig.sgsSdkTimeoutMs,
|
timeout: dataSourceConfig.sgsSdkTimeoutMs,
|
||||||
success: (response) => {
|
success: (response) => {
|
||||||
const statusCode = Number(response.statusCode || 0)
|
const statusCode = Number(response.statusCode || 0)
|
||||||
|
const contentType = getHeaderValue(response.header as Record<string, string> | undefined, 'content-type')
|
||||||
if (statusCode < 200 || statusCode >= 300) {
|
if (statusCode < 200 || statusCode >= 300) {
|
||||||
reject(new Error(`SGS 数据接口请求失败: ${statusCode} ${path}`))
|
reject(new Error(`SGS 数据接口请求失败: ${statusCode} ${requestUrl} content-type=${contentType || 'unknown'} body="${previewPayload(response.data)}"`))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const body = parseJsonPayload<CommonResult<T>>(response.data)
|
const body = parseJsonPayload<CommonResult<T>>(response.data, requestUrl, contentType)
|
||||||
if (!body || body.code !== 0) {
|
if (!body || body.code !== 0) {
|
||||||
reject(new Error(`SGS 数据接口业务失败: ${path} code=${body?.code} msg=${body?.msg || ''}`))
|
reject(new Error(`SGS 数据接口业务失败: ${requestUrl} code=${body?.code} msg=${body?.msg || ''}`))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,35 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
import uni from '@dcloudio/vite-plugin-uni'
|
import uni from '@dcloudio/vite-plugin-uni'
|
||||||
|
|
||||||
|
const sgsDevServer = 'http://1.92.206.90:3001'
|
||||||
|
const sgsAssetServer = 'http://1.92.206.90:9000'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [uni()],
|
plugins: [uni()],
|
||||||
server: {
|
server: {
|
||||||
host: '0.0.0.0'
|
host: '0.0.0.0',
|
||||||
|
proxy: {
|
||||||
|
'/app-api': {
|
||||||
|
target: sgsDevServer,
|
||||||
|
changeOrigin: true
|
||||||
|
},
|
||||||
|
'/h5-sdk': {
|
||||||
|
target: sgsDevServer,
|
||||||
|
changeOrigin: true
|
||||||
|
},
|
||||||
|
'/sdk': {
|
||||||
|
target: sgsDevServer,
|
||||||
|
changeOrigin: true
|
||||||
|
},
|
||||||
|
'/museum-assets': {
|
||||||
|
target: sgsAssetServer,
|
||||||
|
changeOrigin: true
|
||||||
|
},
|
||||||
|
'/minio': {
|
||||||
|
target: sgsDevServer,
|
||||||
|
changeOrigin: true
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
|
|||||||
Reference in New Issue
Block a user