Procházet zdrojové kódy

refactor(环境配置): 重构环境配置及MQTT连接逻辑

- 删除.env.single文件,新增.env.test测试环境配置
- 更新package.json脚本,替换dev:sin为dev:test,并拆分build:tag为test-by-tag和pro-by-tag
- 统一MQTT连接配置,从环境变量读取连接参数
- 在App.vue中添加环境信息日志输出
- 优化version-script.js,支持按环境类型构建
liujia před 2 měsíci
rodič
revize
452ee4f6df
6 změnil soubory, kde provedl 49 přidání a 22 odebrání
  1. 1 1
      .env.test
  2. 3 2
      package.json
  3. 5 0
      src/App.vue
  4. 11 4
      src/layout/index.vue
  5. 11 9
      src/views/device/detail/index.vue
  6. 18 6
      version-script.js

+ 1 - 1
.env.single → .env.test

@@ -1,4 +1,4 @@
-VITE_ENV=single
+VITE_ENV=test
 VITE_APP_TITLE="合肥雷能信息技术有限公司"
 VITE_STOTRE_NAME=LEINENG_WEB
 # 接口配置

+ 3 - 2
package.json

@@ -5,12 +5,13 @@
   "type": "module",
   "scripts": {
     "dev": "vite --host --mode development --port 3000",
-    "dev:sin": "vite --host --mode single --port 4000",
+    "dev:test": "vite --host --mode test --port 4000",
     "dev:pro": "vite --host --mode production --port 5000",
     "build": "vite build",
     "build:test": "node version-script.js test",
     "build:publish": "node version-script.js publish",
-    "build:tag": "node version-script.js build-by-tag",
+    "build:tag:test": "node version-script.js test-by-tag",
+    "build:tag:pro": "node version-script.js pro-by-tag",
     "preview": "vite preview",
     "type-check": "vue-tsc --build",
     "lint": "eslint . --fix",

+ 5 - 0
src/App.vue

@@ -43,6 +43,11 @@ const isRouterReady = ref(false)
 onMounted(() => {
   router.isReady().then(() => {
     isRouterReady.value = true
+
+    // 环境信息
+    console.log(`🚀CurrentMode: ${import.meta.env.MODE}`)
+    console.log(`🚀HostAPI: ${import.meta.env.VITE_APP_HOST}`)
+    console.log(`🚀ProxyAPI: ${import.meta.env.VITE_APP_HOST}`)
   })
 })
 

+ 11 - 4
src/layout/index.vue

@@ -93,10 +93,16 @@ const MqttData = ref()
 
 const initMqttManager = async () => {
   try {
-    mqttClient = mqtt.connect('wss://radar-power.cn:8084/mqtt', {
-      clientId: 'mqtt_vue_test' + Math.random().toString(16).substring(2, 8),
-      username: 'admin',
-      password: 'public',
+    const mqttConfig = {
+      host: import.meta.env.VITE_MQTT_HOST,
+      username: import.meta.env.VITE_MQTT_USERNAME,
+      password: import.meta.env.VITE_MQTT_PASSWORD,
+      clientId: `mqtt_client_${Math.random().toString(16).slice(2, 8)}`,
+    }
+    mqttClient = mqtt.connect(mqttConfig.host, {
+      clientId: mqttConfig.clientId,
+      username: mqttConfig.username,
+      password: mqttConfig.password,
       will: {
         // ✅ 添加遗嘱消息配置
         topic: '/mps/client/connect',
@@ -379,6 +385,7 @@ const backHandler = async () => {
     display: -webkit-box;
     -webkit-box-orient: vertical;
     -webkit-line-clamp: 2;
+    line-clamp: 2;
   }
 }
 

+ 11 - 9
src/views/device/detail/index.vue

@@ -334,16 +334,18 @@ const resetMqttTimeout = () => {
 
 onMounted(() => {
   console.log('onMounted', mqttClient)
-  const params = {
-    clientId: 'mqtt_vue_test' + Math.random().toString(16).substring(2, 8),
-    username: 'admin',
-    password: 'public',
+  const mqttConfig = {
+    host: import.meta.env.VITE_MQTT_HOST,
+    username: import.meta.env.VITE_MQTT_USERNAME,
+    password: import.meta.env.VITE_MQTT_PASSWORD,
+    clientId: `mqtt_client_${Math.random().toString(16).slice(2, 8)}`,
   }
-  console.log('🚀mqttClient connect params', params)
-  // ws://119.45.12.173:8083/mqtt 旧
-  // ws://8.130.28.21:1883/mqtt 新
-  mqttClient = mqtt.connect('wss://radar-power.cn:8084/mqtt', params)
-  console.log('mqttClient connect ready', mqttClient, params)
+  mqttClient = mqtt.connect(mqttConfig.host, {
+    clientId: mqttConfig.clientId,
+    username: mqttConfig.username,
+    password: mqttConfig.password,
+  })
+  console.log('mqttClient connect ready', mqttClient)
   mqttClient.on('connect', () => {
     console.log('MQTT已连接')
     // 订阅所有设备的主题

+ 18 - 6
version-script.js

@@ -107,7 +107,7 @@ function handleTestBuild() {
 
   try {
     // 执行构建命令
-    execSync('vite build', { stdio: 'inherit' })
+    execSync('vite build --mode test', { stdio: 'inherit' })
 
     // 创建tag
     execSync(`git tag ${tag}`, { stdio: 'inherit' })
@@ -172,8 +172,13 @@ function handlePublishBuild() {
   })
 }
 
-// 基于特定tag构建
-function handleBuildByTag() {
+// 基于特定tag构建 测试与生产环境 版本
+function handleBuildByTag(modeType) {
+  if (modeType !== 'test' && modeType !== 'pro') {
+    console.error('无效的构建类型,请确认构建类型为 test 或 pro')
+    process.exit(1)
+  }
+  console.log(`🚀🚀🚀 正在基于${modeType}构建...`)
   const rl = readline.createInterface({
     input: process.stdin,
     output: process.stdout,
@@ -201,7 +206,11 @@ function handleBuildByTag() {
       const version = pkgJson.version
 
       // 执行构建命令
-      execSync('vite build', { stdio: 'inherit' })
+      if (modeType === 'test') {
+        execSync('vite build --mode test', { stdio: 'inherit' })
+      } else if (modeType === 'pro') {
+        execSync('vite build', { stdio: 'inherit' })
+      }
 
       // 生成版本信息文件
       const buildType = tag.startsWith('test-') ? 'test' : 'publish'
@@ -239,8 +248,11 @@ function main() {
     case 'publish':
       handlePublishBuild()
       break
-    case 'build-by-tag':
-      handleBuildByTag()
+    case 'test-by-tag':
+      handleBuildByTag('test')
+      break
+    case 'pro-by-tag':
+      handleBuildByTag('pro')
       break
     default:
       console.log('用法: node version-script.js [test|publish|build-by-tag]')