Parcourir la source

更新配置文件

fxs il y a 7 mois
Parent
commit
0d4c6e8138
6 fichiers modifiés avec 45 ajouts et 9 suppressions
  1. 4 0
      .env
  2. 13 0
      config/api.ts
  3. 9 0
      env.d.ts
  4. 2 0
      package.json
  5. 9 8
      src/hooks/useRequest.ts
  6. 8 1
      tsconfig.app.json

+ 4 - 0
.env

@@ -0,0 +1,4 @@
+# 测试服和本地开发
+VITE_API_URL_TEST='http://server.ichunhao.cn'
+# 线上
+VITE_API_URL_PRODUCT='http://service.ichunhao.cn'

+ 13 - 0
config/api.ts

@@ -0,0 +1,13 @@
+let BASE_URL: string = ''
+
+switch (import.meta.env.MODE) {
+  case 'production':
+    BASE_URL = import.meta.env.VITE_API_URL_PRODUCT
+    break
+  case 'test':
+    BASE_URL = import.meta.env.VITE_API_URL_TEST
+    break
+  default:
+    BASE_URL = import.meta.env.VITE_API_URL_TEST // 默认为开发环境
+}
+export default BASE_URL

+ 9 - 0
env.d.ts

@@ -1 +1,10 @@
 /// <reference types="vite/client" />
+interface ImportMetaEnv {
+  readonly VITE_API_URL_TEST: string
+  readonly VITE_API_URL_PRODUCT: string
+  // 更多环境变量...
+}
+
+interface ImportMeta {
+  readonly env: ImportMetaEnv
+}

+ 2 - 0
package.json

@@ -6,6 +6,8 @@
   "scripts": {
     "dev": "vite --host 0.0.0.0",
     "build": "run-p type-check \"build-only {@}\" --",
+    "build:test": "vite build --mode test",
+    "build:prod": "vite build --mode production",
     "preview": "vite preview",
     "build-only": "vite build",
     "type-check": "vue-tsc --build --force",

+ 9 - 8
src/hooks/useRequest.ts

@@ -2,8 +2,8 @@
  * @Author: fxs bjnsfxs@163.com
  * @Date: 2024-08-20 17:24:06
  * @LastEditors: fxs bjnsfxs@163.com
- * @LastEditTime: 2024-10-10 12:08:47
- * @FilePath: \Game-Backstage-Management-System\src\hooks\useRequest.ts
+ * @LastEditTime: 2024-10-17 09:26:16
+ * @FilePath: \Quantity-Creation-Management-Systemc:\Users\NINGMEI\Desktop\Game-Backstage-Management-System\src\hooks\useRequest.ts
  * @Description:
  *
  */
@@ -13,17 +13,18 @@ import type { ResponseInfo } from '@/types/res'
 
 import { ElMessage } from 'element-plus'
 import { MessageType } from '@/types/res'
+import BASE_URL from '../../config/api'
 
 import 'element-plus/theme-chalk/el-message.css'
 import 'element-plus/theme-chalk/el-message-box.css'
 
 export function useRequest() {
-  let baseURL = ''
-  if (import.meta.env.MODE === 'development') {
-    baseURL = 'http://server.ichunhao.cn'
-  } else {
-    baseURL = 'http://service.ichunhao.cn'
-  }
+  let baseURL = BASE_URL
+  // if (import.meta.env.MODE === 'test') {
+  //   baseURL = import.meta.env.VITE_API_URL_TEST
+  // } else {
+  //   baseURL = import.meta.env.VITE_API_URL_PRODUCT
+  // }
 
   const AllApi = {
     // mock: `http://127.0.0.1:8003/mock`,

+ 8 - 1
tsconfig.app.json

@@ -1,6 +1,13 @@
 {
   "extends": "@vue/tsconfig/tsconfig.dom.json",
-  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "auto-imports.d.ts"],
+  "include": [
+    "src/**/*.ts",
+    "src/**/*.d.ts",
+    "src/**/*.tsx",
+    "src/**/*.vue",
+    "auto-imports.d.ts",
+    "config/**/*.ts"
+  ],
   "exclude": ["src/**/__tests__/*"],
   "compilerOptions": {
     "composite": true,