Selaa lähdekoodia

重新部署项目后, 强制刷新浏览器

lxf 1 vuosi sitten
vanhempi
commit
024c155398
4 muutettua tiedostoa jossa 49 lisäystä ja 3 poistoa
  1. 1 1
      .env.production
  2. 22 0
      build/build-pro.js
  3. 1 1
      package.json
  4. 25 1
      src/permission.js

+ 1 - 1
.env.production

@@ -7,7 +7,7 @@ VITE_APP_ENV = 'production'
 # 若依管理系统/生产环境
 VITE_APP_BASE_API = '/prod-api'
 
-VITE_APP_WS_API = '/prod-api'
+VITE_APP_WS_API = ':10000/prod-api'
 
 # 是否在打包时开启压缩,支持 gzip 和 brotli
 VITE_BUILD_COMPRESS = gzip

+ 22 - 0
build/build-pro.js

@@ -0,0 +1,22 @@
+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 > 文件执行结束!");

+ 1 - 1
package.json

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

+ 25 - 1
src/permission.js

@@ -8,12 +8,36 @@ 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 });
 
 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("sd_web_version");
+  if (version && (!localVersion || (localVersion && JSON.parse(localVersion).version !== version))) {
+    console.log(JSON.parse(localVersion).version, "localVersion.version");
+    localStorage.setItem("sd_web_version", JSON.stringify({ version: version }));
+    window.location.reload();
+  }
+  localStorage.setItem("sd_web_version", JSON.stringify({ version: version }));
+};
 router.beforeEach((to, from, next) => {
+  // 检测版本更新;
+  if (from.path !== "/") {
+    checkVersion();
+  }
   NProgress.start();
   if (getToken()) {
     to.meta.title && useSettingsStore().setTitle(to.meta.title);