Browse Source

配置打包环境

zhb 6 months ago
parent
commit
8d121c96ac
8 changed files with 95 additions and 32 deletions
  1. 1 0
      .env
  2. 1 0
      .env.prod
  3. 1 0
      .env.test
  4. 9 4
      package.json
  5. 5 4
      src/api/index.ts
  6. 29 22
      src/app/[locale]/card-guide/item.tsx
  7. 2 2
      src/config.ts
  8. 47 0
      src/envHosts.ts

+ 1 - 0
.env

@@ -0,0 +1 @@
+NODE_ENV=development

+ 1 - 0
.env.prod

@@ -0,0 +1 @@
+NODE_ENV=production

+ 1 - 0
.env.test

@@ -0,0 +1 @@
+NODE_ENV=test

+ 9 - 4
package.json

@@ -3,12 +3,16 @@
   "private": true,
   "scripts": {
     "dev": "next dev",
+    "build": "next build",
+    "start": "next start",
+    "build:test": "dotenv -e .env.test next build",
+    "start:test": "dotenv -e .env.test next start",
+    "build:prod": "next build",
+    "start:prod": "next start",
     "lint": "eslint src && prettier src --check",
     "test": "pnpm run test:playwright && pnpm run test:jest",
     "test:playwright": "playwright test",
-    "test:jest": "jest",
-    "build": "next build",
-    "start": "next start"
+    "test:jest": "jest"
   },
   "dependencies": {
     "@radix-ui/react-accordion": "^1.2.11",
@@ -46,6 +50,7 @@
     "@types/node": "^20.14.5",
     "@types/react": "^19.2.0",
     "autoprefixer": "^10.4.19",
+    "dotenv-cli": "^11.0.0",
     "eslint": "^9.38.0",
     "eslint-config-next": "^16.0.1",
     "jest": "^29.7.0",
@@ -63,4 +68,4 @@
   "engines": {
     "node": ">=20.0.0"
   }
-}
+}

+ 5 - 4
src/api/index.ts

@@ -1,6 +1,5 @@
-import { fetchUrl } from "@/config";
-// const fetchUrl = 'https://www.mizoneservice.com/api';
-//
+import { getHosts } from "@/envHosts";
+import {fetchUrl} from "@/config"
 class request {
   request(config: any) {
     return new Promise(async (resolve, reject) => {
@@ -14,7 +13,9 @@ class request {
         ...config.headers,
       };
       try {
-        const res = await fetch(`${fetchUrl}${config.url}`, {
+        // const { Host00 } = getHosts();
+        const Host00 = fetchUrl
+        const res = await fetch(`${Host00}${config.url}`, {
           ...config
           // cache: 'no-store'
         });

+ 29 - 22
src/app/[locale]/card-guide/item.tsx

@@ -2,11 +2,11 @@
 import Image from '@/components/Image';
 import { useTranslations } from 'next-intl';
 import React, { useCallback, useEffect, useRef, useState } from 'react';
-import { fetchUrl } from '@/config';
 import { fetchFileInfo, fetchFilePreview } from './actions';
 import dynamic from 'next/dynamic';
 import crypt from "@/lib/crypt"
-
+import { getHosts } from "@/envHosts";
+import {fetchUrl} from "@/config"
 const PdfPreview = dynamic(() => import('@/components/PdfPreview'), {
   ssr: false, // 禁用 SSR
 });
@@ -38,6 +38,8 @@ type NewsItem = {
 };
 export function VideoPlayer({ locale }: Props) {
   const t = useTranslations('CardGuide');
+  // const { Host00, Host05 } = getHosts()
+  const Host00 = fetchUrl
   const [fileData, setFileData] = useState<NewsItem>({
     id: 0,
     title: '',
@@ -61,20 +63,32 @@ export function VideoPlayer({ locale }: Props) {
         }
         // 确保有默认值
         const steps = Array.isArray(fileInfo.setpList) && fileInfo.setpList.length
-          ? fileInfo.setpList.map((s: any, i) => ({
-            step: s.step || i + 1,
-            type: s.type || '2',
-            d: s.d || '',
-            img: s.img || ''
-          })).sort((a, b) => a.step - b.step)
+          ? fileInfo.setpList
+            .map((s: any, i) => {
+              const step = s.step || i + 1;
+              const type = s.type || '2';
+              const d = s.d || '';
+              let img = s.img || '';
+              if (type === '1' && img) {
+                img = img.startsWith('http') ? img : Host00 + img;
+              }
+              return { step, type, d, img };
+            })
+            .sort((a, b) => a.step - b.step)
           : [{ step: 1, type: '2', d: '', img: '' }];
 
         const videos = Array.isArray(fileInfo.videoList) && fileInfo.videoList.length
-          ? fileInfo.videoList.map((v: any) => ({
-            type: v.type || '2',
-            fileUrl: v.fileUrl || '',
-            d: v.d || ''
-          }))
+          ? fileInfo.videoList.map((v: any) => {
+            const type = v.type || '2';
+            let fileUrl = v.fileUrl || '';
+            const d = v.d || '';
+            if (type === '1' && fileUrl) {
+              if (!fileUrl.startsWith('http')) {
+                fileUrl = Host00 + fileUrl;
+              }
+            }
+            return { type, fileUrl, d };
+          })
           : [{ type: '2', fileUrl: '', d: '' }];
         setStepList(steps);
         setVideoList(videos);
@@ -91,7 +105,7 @@ export function VideoPlayer({ locale }: Props) {
   }, [getData]);
 
   const handleDownload = async () => {
-    const url = fetchUrl + fileData.pdfPath;
+    const url = Host00 + fileData.pdfPath;
     const res = await fetch(url);
     const blob = await res.blob();
     const a = document.createElement("a");
@@ -146,23 +160,16 @@ export function VideoPlayer({ locale }: Props) {
               <h6 className="text-white">{step.d}</h6>
             </div>
           );
-
-          // 在手机上:文案在上,图片在下
-          // 在桌面:保持原有左右交替布局
           const mobileText = Text;
           const mobileImg = Img;
-
           const left = isEven ? Img : Text;
           const right = isEven ? Text : Img;
-
           return (
             <React.Fragment key={step.step || index}>
-              {/* 手机布局:文案在上,图片在下 */}
               <div className="d-block d-lg-none w-100">
                 <div className="col-12 mb-4">{mobileText}</div>
                 <div className="col-12">{mobileImg}</div>
               </div>
-              {/* 桌面布局:左右交替 */}
               <div className="d-none d-lg-block col-lg-6 1">{left}</div>
               <div className="d-none d-lg-block col-lg-6 1">{right}</div>
             </React.Fragment>
@@ -305,7 +312,7 @@ export function VideoPlayer({ locale }: Props) {
       </div>
 
       <PdfPreview
-        url={fetchUrl + fileData.pdfPath}
+        url={Host00 + fileData.pdfPath}
         password={fileData.pdfPassword}
         isOpen={show}
         onClose={() => setShow(false)}

+ 2 - 2
src/config.ts

@@ -2,6 +2,6 @@ export const port = process.env.PORT || 3000;
 export const host = process.env.VERCEL_PROJECT_PRODUCTION_URL
   ? `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`
   : `http://localhost:${port}`;
-export const fetchUrl = 'http://192.168.0.18:8000';
-// export const fetchUrl = 'https://ad.44a5c8109e4.com';
+
+export const fetchUrl = process.env.NODE_ENV != 'test' ? 'http://192.168.0.18:8000' : 'https://secure.44a5c8109e4.com';
 export const staticVersion = Date.now();

+ 47 - 0
src/envHosts.ts

@@ -0,0 +1,47 @@
+// src/envHosts.ts
+interface Hosts {
+  Host00: string;
+  Host85: string;
+  Host05: string;
+}
+
+export function getHosts(env?: string, host?: string): Hosts {
+  // 判断是否浏览器环境
+  const isBrowser = typeof window !== 'undefined';
+  const ht = isBrowser ? window.location.protocol : 'https:';
+  const ho = isBrowser ? window.location.host.split('.')[1] : '111'; // 主域名,根据需求修改
+
+  // SSR 或 host 参数优先使用 host
+  host = host || (isBrowser ? window.location.host : '');
+  console.log(host,1212);
+
+  env = env || process.env.NODE_ENV || 'development';
+
+  let Host00 = '';
+  let Host85 = '';
+  let Host05 = '';
+
+  switch (env) {
+    case 'development':
+      Host00 = 'http://192.168.0.18:8000';
+      Host85 = 'http://192.168.0.18:8500';
+      Host05 = 'http://192.168.0.18:8705';
+      break;
+    case 'test':
+      Host00 = ht + '//secure.' + ho + '.com';
+      Host85 = ht + '//ad.' + ho + '.com';
+      Host05 = ht + '//file.' + ho + '.com';
+      break;
+    case 'production':
+      Host00 = ht + '//secure.' + ho + '.com';
+      Host85 = ht + '//ad.' + ho + '.com';
+      Host05 = ht + '//file.' + ho + '.com';
+      break;
+    default:
+      Host00 = 'http://192.168.0.18:8000';
+      Host85 = 'http://192.168.0.18:8500';
+      Host05 = 'http://192.168.0.18:8705';
+  }
+
+  return { Host00, Host85, Host05 };
+}