Selaa lähdekoodia

增加配置文件写入方式,优化大屏项目配置文件引用

yanglzh 11 kuukautta sitten
vanhempi
sitoutus
f52f5def06
4 muutettua tiedostoa jossa 33 lisäystä ja 10 poistoa
  1. 1 0
      .gitignore
  2. 17 5
      pnpm-lock.yaml
  3. 6 5
      public/plugin/screen/index.html
  4. 9 0
      writeEnv.mjs

+ 1 - 0
.gitignore

@@ -7,6 +7,7 @@ dist.zip
 iotDist.zip
 
 public/config.json
+public/config.js
 # local env files
 .env.local
 .env.*.local

+ 17 - 5
pnpm-lock.yaml

@@ -165,9 +165,12 @@ importers:
       '@vue/compiler-sfc':
         specifier: 3.2.45
         version: 3.2.45
+      cross-env:
+        specifier: ^7.0.3
+        version: 7.0.3
       dotenv:
-        specifier: 16.0.0
-        version: 16.0.0
+        specifier: ^16.4.5
+        version: 16.4.5
       eslint:
         specifier: 8.10.0
         version: 8.10.0
@@ -851,6 +854,11 @@ packages:
   cropperjs@1.5.12:
     resolution: {integrity: sha512-re7UdjE5UnwdrovyhNzZ6gathI4Rs3KGCBSc8HCIjUo5hO42CtzyblmWLj6QWVw7huHyDMfpKxhiO2II77nhDw==}
 
+  cross-env@7.0.3:
+    resolution: {integrity: sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==}
+    engines: {node: '>=10.14', npm: '>=6', yarn: '>=1'}
+    hasBin: true
+
   cross-spawn@7.0.3:
     resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==}
     engines: {node: '>= 8'}
@@ -948,8 +956,8 @@ packages:
     resolution: {integrity: sha512-yS+Q5i3hBf7GBkd4KG8a7eBNNWNGLTaEwwYWUijIYM7zrlYDM0BFXHjjPWlWZ1Rg7UaddZeIDmi9jF3HmqiQ2w==}
     engines: {node: '>=6.0.0'}
 
-  dotenv@16.0.0:
-    resolution: {integrity: sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==}
+  dotenv@16.4.5:
+    resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==}
     engines: {node: '>=12'}
 
   dotignore@0.1.2:
@@ -3065,6 +3073,10 @@ snapshots:
 
   cropperjs@1.5.12: {}
 
+  cross-env@7.0.3:
+    dependencies:
+      cross-spawn: 7.0.3
+
   cross-spawn@7.0.3:
     dependencies:
       path-key: 3.1.1
@@ -3159,7 +3171,7 @@ snapshots:
     dependencies:
       esutils: 2.0.3
 
-  dotenv@16.0.0: {}
+  dotenv@16.4.5: {}
 
   dotignore@0.1.2:
     dependencies:

+ 6 - 5
public/plugin/screen/index.html

@@ -40,12 +40,13 @@
   <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" />
   <link rel="icon" href="./favicon.ico" />
   <title>SagooDataView</title>
-  
+  <script src="/config.js"></script>
   <script>
-    // 获取配置文件中的 服务地址,定义为全局变量
-    fetch('/config.json').then(function (res) { return res.json() }).then(function (config) {
-      window.env = config.screen
-    })
+    window.env = window.allEnv.screen
+    // // 获取配置文件中的 服务地址,定义为全局变量
+    // fetch('/config.json').then(function (res) { return res.json() }).then(function (config) {
+    //   window.env = config.screen
+    // })
   </script>
   <script type="module" crossorigin src="/plugin/screen/static/js/index-99ee991e.js"></script>
   <link rel="stylesheet" href="/plugin/screen/static/css/index-a2f98856.css">

+ 9 - 0
writeEnv.mjs

@@ -10,6 +10,7 @@ const { VITE_SERVER_ORIGIN, VITE_NGINX_PROXY, VITE_API_SUFFIX_URL, VITE_RULE_SER
 const baseUrl = VITE_SERVER_ORIGIN + VITE_NGINX_PROXY + VITE_API_SUFFIX_URL
 
 const configFile = './public/config.json'
+const configJsFile = './public/config.js'
 const configJson = {
 	topo: baseUrl,
 	topo: {
@@ -27,6 +28,10 @@ const configJson = {
 	media: VITE_MEDIA_SERVER_URL,
 }
 
+// 大屏项目会存在配置找不到的情况,增加同步请求的 js 配置文件,避免异步请求延迟导致的异常
+const configJs = `window.allEnv = ${JSON.stringify(configJson, null, 2)}
+`
+
 // 文件写入
 try {
 	// 将.env 中的值进行赋值之后写入 config.json
@@ -38,3 +43,7 @@ try {
 	console.error(`-> 更新配置成功: `)
 	console.error(err)
 }
+// js文件写入
+try {
+	writeFileSync(configJsFile, configJs, { flag: 'w+', encoding: 'utf-8' })
+} catch (err) {}