修复环境变量问题

This commit is contained in:
2026-04-14 11:34:28 +08:00
parent 9a8a1826c9
commit 319baea5e8
8 changed files with 60 additions and 8 deletions

View File

@@ -12,6 +12,13 @@ export type TokenResp = {
scope?: string
}
export async function getApiUrl() {
return {
success: true,
data: API_BASE_URL,
} satisfies ApiResponse<string>
}
// ======================
// public
// ======================

View File

@@ -8,6 +8,8 @@ import {LayoutStoreProvider} from '@/components/stores/layout'
import {ClientStoreProvider} from '@/components/stores/client'
import {getProfile} from '@/actions/auth'
import Script from 'next/script'
import {AppStoreProvider} from '@/components/stores/app'
import {getApiUrl} from '@/actions/base'
export async function generateMetadata(): Promise<Metadata> {
return {
@@ -30,12 +32,14 @@ export default async function RootLayout(props: Readonly<{
)
}
function StoreProviders(props: {children: ReactNode}) {
async function StoreProviders(props: {children: ReactNode}) {
return (
<ProfileStoreProvider profile={getProfile().then(resp => resp.success ? resp.data : null)}>
<LayoutStoreProvider>
<ClientStoreProvider>
{props.children}
<AppStoreProvider url={await getApiUrl().then(r => r.data)}>
{props.children}
</AppStoreProvider>
</ClientStoreProvider>
</LayoutStoreProvider>
</ProfileStoreProvider>

View File

@@ -7,6 +7,7 @@ import {PaymentProps} from './type'
import {payClose} from '@/actions/resource'
import {useEffect} from 'react'
import {UniversalDesktopPayment} from './universal-desktop-payment'
import {useAppStore} from '@/components/stores/app'
export type PaymentModalProps = {
onConfirm: (showFail: boolean) => Promise<void>
@@ -34,9 +35,10 @@ export function PaymentModal(props: PaymentModalProps) {
}
// SSE处理方式检查支付状态
const apiUrl = useAppStore('apiUrl')
useEffect(() => {
const eventSource = new EventSource(
`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/trade/check?trade_no=${props.inner_no}&method=${props.method}`,
`${apiUrl}/api/trade/check?trade_no=${props.inner_no}&method=${props.method}`,
)
eventSource.onmessage = async (event) => {
switch (event.data) {
@@ -53,7 +55,7 @@ export function PaymentModal(props: PaymentModalProps) {
return () => {
eventSource.close()
}
}, [props])
}, [apiUrl, props])
return (
<Dialog

View File

@@ -0,0 +1,38 @@
'use client'
import {createStore, StoreApi} from 'zustand/vanilla'
import {createContext, ReactNode, useContext, useState} from 'react'
import {useStore} from 'zustand/react'
// store
type AppStoreState = {
apiUrl: string
}
export function createAppStore(url: string) {
return createStore<AppStoreState>()(() => ({
apiUrl: url,
}))
}
// provider
const AppStoreContext = createContext<StoreApi<AppStoreState> | null>(null)
export function AppStoreProvider(props: {
url: string
children: ReactNode
}) {
const [store] = useState(() => createAppStore(props.url))
return (
<AppStoreContext.Provider value={store}>
{props.children}
</AppStoreContext.Provider>
)
}
export function useAppStore(name: keyof AppStoreState) {
const context = useContext(AppStoreContext)
if (!context) {
throw new Error('AppStoreContext 没有正确初始化')
}
return useStore(context, c => c[name])
}

View File

@@ -1,6 +1,6 @@
// 定义后端服务URL和OAuth2配置
const _api_base_url = process.env.NEXT_PUBLIC_API_BASE_URL
if (!_api_base_url) throw new Error('NEXT_PUBLIC_API_BASE_URL is not set')
const _api_base_url = process.env.API_BASE_URL
if (!_api_base_url) throw new Error('API_BASE_URL is not set')
const API_BASE_URL = _api_base_url
const _client_id = process.env.CLIENT_ID