Parcourir la source

部署之后用户如有操作会自动刷新

cz il y a 1 an
Parent
commit
f1a4f70e76
7 fichiers modifiés avec 99 ajouts et 18 suppressions
  1. 4 1
      .env.production
  2. 4 1
      .env.staging
  3. 23 0
      build/build-pro.js
  4. 2 2
      package.json
  5. 3 2
      src/main.js
  6. 60 9
      src/permission.js
  7. 3 3
      src/views/login.vue

+ 4 - 1
.env.production

@@ -14,4 +14,7 @@ VITE_BUILD_COMPRESS = gzip
 
 VITE_APP_IP = '139.9.102.170'
 
-VITE_APP_WS_API = ':9900/prod-api'
+VITE_APP_WS_API = ':9900/prod-api'
+
+# 是否强刷新
+VITE_REFRESH = true

+ 4 - 1
.env.staging

@@ -14,4 +14,7 @@ VITE_BUILD_COMPRESS = gzip
 
 VITE_APP_IP = '139.9.102.170'
 
-VITE_APP_WS_API = ':9901/test-api'
+VITE_APP_WS_API = ':9901/test-api'
+
+# 是否强刷新
+VITE_REFRESH = true

+ 23 - 0
build/build-pro.js

@@ -0,0 +1,23 @@
+console.log('build > 文件开始执行!')
+const fs = require('fs')
+const path = require('path')
+
+function getRootPath(...dir) {
+  return path.resolve(process.cwd(), ...dir)
+}
+const runBuild = async () => {
+  try {
+    const OUTPUT_DIR = 'dist'
+    const VERSION = 'version.json'
+    const versionJson = {
+      version: 'V_' + Math.floor(Math.random() * 10000) + Date.now()
+    }
+    fs.writeFileSync(getRootPath(`${OUTPUT_DIR}/${VERSION}`), JSON.stringify(versionJson))
+    console.log(`version file is build successfully!`)
+  } catch (error) {
+    console.error('version build error:\n' + error)
+    process.exit(1)
+  }
+}
+runBuild()
+console.log('build > 文件执行结束!')

+ 2 - 2
package.json

@@ -6,8 +6,8 @@
   "license": "MIT",
   "scripts": {
     "dev": "vite",
-    "build:prod": "vite build",
-    "build:stage": "vite build --mode staging",
+    "build:prod": "vite build && node ./build/build-pro.js",
+    "build:stage": "vite build --mode staging && node ./build/build-pro.js",
     "preview": "vite preview"
   },
   "repository": {

+ 3 - 2
src/main.js

@@ -15,8 +15,6 @@ import store from './store'
 import router from './router'
 import directive from './directive' // directive
 
-
-
 // 注册指令
 import plugins from './plugins' // plugins
 import {
@@ -46,6 +44,8 @@ import {
   selectDictLabels
 } from '@/utils/ruoyi'
 
+
+
 import {
   dictDataEcho,
   dictValueLabel,
@@ -128,6 +128,7 @@ app.use(router)
 app.use(store)
 app.use(plugins)
 
+
 app.use(i18n)
 app.use(elementIcons)
 app.use(print)

+ 60 - 9
src/permission.js

@@ -1,25 +1,71 @@
 import router from './router'
-import { ElMessage } from 'element-plus'
+import {
+  ElMessage
+} from 'element-plus'
 import NProgress from 'nprogress'
 import 'nprogress/nprogress.css'
-import { getToken } from '@/utils/auth'
-import { isHttp } from '@/utils/validate'
-import { isRelogin } from '@/utils/request'
+import {
+  getToken
+} from '@/utils/auth'
+import {
+  isHttp
+} from '@/utils/validate'
+import {
+  isRelogin
+} from '@/utils/request'
 import useUserStore from '@/store/modules/user'
 import useSettingsStore from '@/store/modules/settings'
 import usePermissionStore from '@/store/modules/permission'
+import axios from "axios";
 
-NProgress.configure({ showSpinner: false });
+
+NProgress.configure({
+  showSpinner: false
+});
 
 const whiteList = ['/login', '/register'];
 
+// 版本判断
+const checkVersion = async () => {
+  const url = `/version.json?t=${Date.now()}`;
+  let res = null;
+  try {
+    res = await axios.get(url);
+  } catch (err) {
+    console.error("checkVersion error: ", err);
+  }
+  let version = null;
+  if (res) {
+    version = res.data.version;
+  }
+  const localVersion = localStorage.getItem("ehsd_web_version");
+  if (version && localVersion && JSON.parse(localVersion).version && JSON.parse(localVersion).version !== version) {
+    localStorage.setItem("ehsd_web_version", JSON.stringify({
+      version: version
+    }));
+    window.location.reload();
+  } else {
+    localStorage.setItem("ehsd_web_version", JSON.stringify({
+      version: version
+    }));
+  }
+};
+
+const isRefresh =
+  import.meta.env.VITE_REFRESH
 router.beforeEach((to, from, next) => {
+  // 检测版本更新;
+  if (from.path !== "/" && isRefresh === "true") {
+    checkVersion();
+  }
   NProgress.start()
   if (getToken()) {
     to.meta.title && useSettingsStore().setTitle(to.meta.title)
     /* has token*/
     if (to.path === '/login') {
-      next({ path: '/' })
+      next({
+        path: '/'
+      })
       NProgress.done()
     } else {
       if (useUserStore().roles.length === 0) {
@@ -34,12 +80,17 @@ router.beforeEach((to, from, next) => {
                 router.addRoute(route) // 动态添加可访问路由表
               }
             })
-            next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
+            next({
+              ...to,
+              replace: true
+            }) // hack方法 确保addRoutes已完成
           })
         }).catch(err => {
           useUserStore().logOut().then(() => {
             ElMessage.error(err)
-            next({ path: '/' })
+            next({
+              path: '/'
+            })
           })
         })
       } else {
@@ -60,4 +111,4 @@ router.beforeEach((to, from, next) => {
 
 router.afterEach(() => {
   NProgress.done()
-})
+})

+ 3 - 3
src/views/login.vue

@@ -164,9 +164,9 @@ function handleLogin() {
         .then(() => {
           router.push({ path: redirect.value || "/" });
           // 登录刷新
-          setTimeout(() => {
-            location.reload();
-          }, 1000);
+          // setTimeout(() => {
+          //   location.reload();
+          // }, 1000);
         })
         .catch(() => {
           loading.value = false;