From 319baea5e87104d1558af03de305fde6acd83043 Mon Sep 17 00:00:00 2001 From: luorijun Date: Tue, 14 Apr 2026 11:34:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 2 +- .github/copilot-instructions.md | 2 +- Dockerfile | 1 + src/actions/base.ts | 7 ++++ src/app/layout.tsx | 8 +++- .../composites/payment/payment-modal.tsx | 6 ++- src/components/stores/app.tsx | 38 +++++++++++++++++++ src/lib/api.ts | 4 +- 8 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 src/components/stores/app.tsx diff --git a/.env.example b/.env.example index 1550b88..8438d07 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ # 开发环境配置 -NEXT_PUBLIC_API_BASE_URL=http://192.168.3.42:8080 +API_BASE_URL=http://192.168.3.42:8080 CLIENT_ID=web CLIENT_SECRET=web diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 70c4154..38a885c 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -122,7 +122,7 @@ type UserProfile = ExtraResp ## 环境变量 需要配置: -- `NEXT_PUBLIC_API_BASE_URL` - 后端 API 地址 +- `API_BASE_URL` - 后端 API 地址 - `CLIENT_ID`, `CLIENT_SECRET` - OAuth2 设备认证凭据 ## 部署 diff --git a/Dockerfile b/Dockerfile index 9527b43..170a669 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,6 +32,7 @@ RUN adduser --system --uid 1001 nextjs COPY --from=builder /app/public ./public COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static +RUN rm .env USER nextjs diff --git a/src/actions/base.ts b/src/actions/base.ts index c01ca6e..8daa771 100644 --- a/src/actions/base.ts +++ b/src/actions/base.ts @@ -12,6 +12,13 @@ export type TokenResp = { scope?: string } +export async function getApiUrl() { + return { + success: true, + data: API_BASE_URL, + } satisfies ApiResponse +} + // ====================== // public // ====================== diff --git a/src/app/layout.tsx b/src/app/layout.tsx index b173c9d..59faa57 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -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 { return { @@ -30,12 +32,14 @@ export default async function RootLayout(props: Readonly<{ ) } -function StoreProviders(props: {children: ReactNode}) { +async function StoreProviders(props: {children: ReactNode}) { return ( resp.success ? resp.data : null)}> - {props.children} + r.data)}> + {props.children} + diff --git a/src/components/composites/payment/payment-modal.tsx b/src/components/composites/payment/payment-modal.tsx index aa1bac7..c264b72 100644 --- a/src/components/composites/payment/payment-modal.tsx +++ b/src/components/composites/payment/payment-modal.tsx @@ -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 @@ -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 ( ()(() => ({ + apiUrl: url, + })) +} + +// provider +const AppStoreContext = createContext | null>(null) + +export function AppStoreProvider(props: { + url: string + children: ReactNode +}) { + const [store] = useState(() => createAppStore(props.url)) + return ( + + {props.children} + + ) +} + +export function useAppStore(name: keyof AppStoreState) { + const context = useContext(AppStoreContext) + if (!context) { + throw new Error('AppStoreContext 没有正确初始化') + } + return useStore(context, c => c[name]) +} diff --git a/src/lib/api.ts b/src/lib/api.ts index a673f50..c90acf0 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -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