初始化2
This commit is contained in:
34
server/api/_app/[...path].ts
Normal file
34
server/api/_app/[...path].ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { defineEventHandler, getHeader, getRequestURL, getRouterParam, proxyRequest, getCookie } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
|
||||
function joinURL(base: string, path: string) {
|
||||
if (!path) return base
|
||||
return base.replace(/\/+$/, '') + '/' + path.replace(/^\/+/, '')
|
||||
}
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
// 从 Cookie 读取环境设置,优先于环境变量
|
||||
const envCookie = getCookie(event, 'websopy_api_env')
|
||||
const isDevEnv = envCookie === 'dev'
|
||||
|
||||
// 开发环境使用本地 9500 端口,生产环境使用配置的环境变量
|
||||
const appApiBase = isDevEnv
|
||||
? 'http://127.0.0.1:9500'
|
||||
: (config.public.appApiBase || 'https://websopy-api.websoft.top')
|
||||
|
||||
const path = getRouterParam(event, 'path') || ''
|
||||
const search = getRequestURL(event).search
|
||||
const target = joinURL(appApiBase + '/api/app', path) + search
|
||||
|
||||
const tenantId = getHeader(event, 'tenantid') || config.public.tenantId
|
||||
const authorization = getHeader(event, 'authorization')
|
||||
|
||||
return proxyRequest(event, target, {
|
||||
headers: {
|
||||
TenantId: String(tenantId),
|
||||
...(authorization ? { Authorization: String(authorization) } : {})
|
||||
}
|
||||
})
|
||||
})
|
||||
25
server/api/_file/[...path].ts
Normal file
25
server/api/_file/[...path].ts
Normal file
@@ -0,0 +1,25 @@
|
||||
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(/^\/+/, '')
|
||||
}
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const fileServerBase = config.public.fileServerBase || 'https://server.websoft.top'
|
||||
const path = getRouterParam(event, 'path') || ''
|
||||
const search = getRequestURL(event).search
|
||||
const target = joinURL(fileServerBase, path) + search
|
||||
|
||||
const tenantId = getHeader(event, 'tenantid') || config.public.tenantId
|
||||
const authorization = getHeader(event, 'authorization')
|
||||
|
||||
return proxyRequest(event, target, {
|
||||
headers: {
|
||||
TenantId: String(tenantId),
|
||||
...(authorization ? { Authorization: String(authorization) } : {})
|
||||
}
|
||||
})
|
||||
})
|
||||
34
server/api/_modules/[...path].ts
Normal file
34
server/api/_modules/[...path].ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { defineEventHandler, getHeader, getRequestURL, getRouterParam, proxyRequest, getCookie } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
|
||||
function joinURL(base: string, path: string) {
|
||||
if (!path) return base
|
||||
return base.replace(/\/+$/, '') + '/' + path.replace(/^\/+/, '')
|
||||
}
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
// 从 Cookie 读取环境设置,优先于环境变量
|
||||
const envCookie = getCookie(event, 'websopy_api_env')
|
||||
const isDevEnv = envCookie === 'dev'
|
||||
|
||||
// 开发环境使用本地 9500 端口,生产环境使用配置的环境变量
|
||||
const mpApiBase = isDevEnv
|
||||
? 'http://127.0.0.1:9500'
|
||||
: (config.public.mpApiBase || 'https://mp-api.websoft.top')
|
||||
|
||||
const path = getRouterParam(event, 'path') || ''
|
||||
const search = getRequestURL(event).search
|
||||
const target = joinURL(mpApiBase, path) + search
|
||||
|
||||
const tenantId = getHeader(event, 'tenantid') || config.public.tenantId
|
||||
const authorization = getHeader(event, 'authorization')
|
||||
|
||||
return proxyRequest(event, target, {
|
||||
headers: {
|
||||
TenantId: String(tenantId),
|
||||
...(authorization ? { Authorization: String(authorization) } : {})
|
||||
}
|
||||
})
|
||||
})
|
||||
34
server/api/_server/[...path].ts
Normal file
34
server/api/_server/[...path].ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { defineEventHandler, getHeader, getRequestURL, getRouterParam, proxyRequest, getCookie } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
|
||||
function joinURL(base: string, path: string) {
|
||||
if (!path) return base
|
||||
return base.replace(/\/+$/, '') + '/' + path.replace(/^\/+/, '')
|
||||
}
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
// 从 Cookie 读取环境设置,优先于环境变量
|
||||
const envCookie = getCookie(event, 'websopy_api_env')
|
||||
const isDevEnv = envCookie === 'dev'
|
||||
|
||||
// 开发环境使用本地 9500 端口,生产环境使用配置的环境变量
|
||||
const serverApiBase = isDevEnv
|
||||
? 'https://server.websoft.top/api'
|
||||
: (config.public.serverApiBase || config.public.ServerApi || 'https://server.websoft.top/api')
|
||||
|
||||
const path = getRouterParam(event, 'path') || ''
|
||||
const search = getRequestURL(event).search
|
||||
const target = joinURL(serverApiBase, path) + search
|
||||
|
||||
const tenantId = getHeader(event, 'tenantid') || config.public.tenantId
|
||||
const authorization = getHeader(event, 'authorization')
|
||||
|
||||
return proxyRequest(event, target, {
|
||||
headers: {
|
||||
TenantId: String(tenantId),
|
||||
...(authorization ? { Authorization: String(authorization) } : {})
|
||||
}
|
||||
})
|
||||
})
|
||||
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