Files
jczxw-pc/server/api/cms/[...path].ts
T
2026-04-23 16:30:57 +08:00

28 lines
893 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { defineEventHandler, getHeader, getRequestURL, getRouterParam, proxyRequest } from 'h3'
import { useRuntimeConfig } from '#imports'
function joinURL(base: string, path: string) {
if (!path) return base
return base.replace(/\/+$/, '') + '/' + path.replace(/^\/+/, '')
}
/**
* 公开 CMS 接口代理
* 将 /api/cms/* 代理到 mp-api,无需登录(不要求 Authorization
*/
export default defineEventHandler((event) => {
const config = useRuntimeConfig()
const mpApiBase = config.public.mpApiBase || 'https://mp-api.websoft.top'
const path = getRouterParam(event, 'path') || ''
const search = getRequestURL(event).search
const target = joinURL(mpApiBase + '/api/cms', path) + search
const tenantId = getHeader(event, 'tenantid') || config.public.tenantId
return proxyRequest(event, target, {
headers: {
TenantId: String(tenantId)
}
})
})