Browse Source

feat(config): 实现多环境配置并优化 URL 生成逻辑

- 新增 env 配置项,用于区分不同环境
- 重构 tkkWebUrl 获取逻辑,支持多环境配置
- 优化 API 地址配置,实现动态设置
- 更新 config.json 结构,支持多环境配置
panqiuyao 4 months ago
parent
commit
c56c294ec8

+ 2 - 2
electron/utils/config.default.json

@@ -1,9 +1,9 @@
 {
   "openDevTools":false,
-  "remoteUrl": false,
   "controlType": "SmartShooter",
   "controlPath": "C:\\Program Files\\Smart Shooter 5",
   "digiCamControlPath":"C:\\Program Files (x86)\\digiCamControl",
   "SmartShooterPath":"C:\\Program Files\\Smart Shooter 5",
-  "pyapp": "127.0.0.1"
+  "pyapp": "127.0.0.1",
+  "env": "dev"
 }

+ 12 - 2
frontend/src/config.json

@@ -1,4 +1,14 @@
 {
-    "api": "https://dev2.pubdata.cn",
-    "tkkWebUrl": "http://localhost:3001/tkk"
+    "dev": {
+        "api": "https://dev2.pubdata.cn",
+        "tkkWebUrl": "https://tkk.pubdata.cn"
+    },
+    "prod": {
+        "api": "https://dev2.valimart.net",
+        "tkkWebUrl": "https://tkk.valimart.net"
+    },
+    "local": {
+        "api": "",
+        "tkkWebUrl": "https://localhost:3000/tkk"
+    }
 }

+ 8 - 2
frontend/src/utils/appfun.ts

@@ -1,7 +1,8 @@
 
-import { configs } from '@/utils/appconfig';
+import ENV_CONFIG from "@/config.json";
 import tokenInfo from '@/stores/modules/token';
 const tokenInfoStore = tokenInfo();
+import configInfo from "@/stores/modules/config";
 
 //获取文件路径
 export  function getFilePath (file_path){
@@ -23,6 +24,11 @@ export  function getWebUrlrUrl (config:{
     query:Object
 }){
 
+    const useConfigInfoStore = configInfo();
+    let env =  useConfigInfoStore.appConfig.env
+    const tkkWebUrl = ENV_CONFIG[env]?.tkkWebUrl || 'https://tkk.valimart.net';
+
+
     let params = '?source=camera&token=' + tokenInfoStore.getToken
     if(config.query){
         params +=  '&'
@@ -30,6 +36,6 @@ export  function getWebUrlrUrl (config:{
             return encodeURIComponent(key) + '=' + encodeURIComponent(config.query[key])
         }).join('&')
     }
-    let url  = configs.tkkWebUrl + config.url + params
+    let url  = tkkWebUrl + config.url + params
     return url
 }

+ 18 - 1
frontend/src/utils/http.ts

@@ -2,6 +2,8 @@ import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
 import { ElMessage as Message, ElMessageBox as MessageBox, ElLoading as Loading } from 'element-plus';
 import  tokenInfo  from '@/stores/modules/token';
 import useUserInfo from "@/stores/modules/user";
+import pinia from "@/stores/index";
+import ENV_CONFIG from "@/config.json";
 
 // 加载动画的并发管理
 const activeRequests = new Set<string>();
@@ -24,13 +26,28 @@ function loadingClose(requestId: string) {
  */
 const service = axios.create({
     timeout: 5000, // 设置请求超时时间
-    baseURL: '__API__',
+   // baseURL: '__API__',
 });
 console.log('__API__');
 
 // 请求拦截器
 service.interceptors.request.use(
     (config: AxiosRequestConfig) => {
+
+        // 动态设置baseURL
+        const appConfig = pinia.state.value.config?.appConfig;
+        let env = 'local'; // 默认环境
+
+        if (appConfig?.env) {
+            env = appConfig.env;
+            // 从ENV_CONFIG获取对应环境的API地址
+            const apiConfig = ENV_CONFIG[env];
+            if (apiConfig?.api) {
+                config.baseURL = apiConfig.api;
+            }
+        }
+        console.log(`使用环境: ${env}, API地址: ${config.baseURL || '__API__'}`);
+
         // 在发送请求之前做些什么,例如添加 token
         const tokenInfoStore = tokenInfo();
         const token = tokenInfoStore.getToken; // 使用 getToken() 获取 token