新版官网模板
This commit is contained in:
27
server/api/cms/[...path].ts
Normal file
27
server/api/cms/[...path].ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
})
|
||||
33
server/api/cms/cms-website/getSiteInfo.get.ts
Normal file
33
server/api/cms/cms-website/getSiteInfo.get.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { $fetch } from 'ofetch'
|
||||
import { createError, defineEventHandler, getHeader, getQuery } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const query = getQuery(event)
|
||||
const modulesApiBase =
|
||||
config.public.modulesApiBase || config.public.ApiBase || 'https://websopy-api.websoft.top/api'
|
||||
|
||||
const tenantId =
|
||||
getHeader(event, 'tenantid') ||
|
||||
config.public.tenantId ||
|
||||
config.public.TenantId ||
|
||||
'5'
|
||||
const authorization = getHeader(event, 'authorization')
|
||||
|
||||
try {
|
||||
return await $fetch('/cms/cms-website/getSiteInfo', {
|
||||
baseURL: modulesApiBase,
|
||||
headers: {
|
||||
TenantId: String(tenantId),
|
||||
...(authorization ? { Authorization: String(authorization) } : {})
|
||||
},
|
||||
query
|
||||
})
|
||||
} catch (error: any) {
|
||||
throw createError({
|
||||
statusCode: error?.statusCode || error?.response?.status || 502,
|
||||
statusMessage: error?.statusMessage || 'Failed to fetch site info'
|
||||
})
|
||||
}
|
||||
})
|
||||
33
server/api/cms/cms-website/pageAll.get.ts
Normal file
33
server/api/cms/cms-website/pageAll.get.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { $fetch } from 'ofetch'
|
||||
import { createError, defineEventHandler, getHeader, getQuery } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const query = getQuery(event)
|
||||
const modulesApiBase =
|
||||
config.public.modulesApiBase || config.public.ApiBase || 'https://websopy-api.websoft.top/api'
|
||||
|
||||
const tenantId =
|
||||
getHeader(event, 'tenantid') ||
|
||||
config.public.tenantId ||
|
||||
config.public.TenantId ||
|
||||
'5'
|
||||
const authorization = getHeader(event, 'authorization')
|
||||
|
||||
try {
|
||||
return await $fetch('/cms/cms-website/pageAll', {
|
||||
baseURL: modulesApiBase,
|
||||
headers: {
|
||||
TenantId: String(tenantId),
|
||||
...(authorization ? { Authorization: String(authorization) } : {})
|
||||
},
|
||||
query
|
||||
})
|
||||
} catch (error: any) {
|
||||
throw createError({
|
||||
statusCode: error?.statusCode || error?.response?.status || 502,
|
||||
statusMessage: error?.statusMessage || 'Failed to fetch pageAll'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user