瀏覽代碼

feature/版本控制添加

dengrui 10 月之前
父節點
當前提交
e112a68f68
共有 6 個文件被更改,包括 129 次插入59 次删除
  1. 57 57
      index.html
  2. 1 0
      package.json
  3. 3 0
      public/version.json
  4. 2 1
      src/plugins/permission.ts
  5. 40 0
      src/utils/version.js
  6. 26 1
      vite.config.ts

+ 57 - 57
index.html

@@ -1,66 +1,66 @@
 <!doctype html>
 <!doctype html>
 <html lang="en">
 <html lang="en">
-<head>
-  <meta charset="UTF-8"/>
-  <link href="/favicon.ico" rel="icon"/>
-  <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
-  <meta
-    content="Vue3 + Vite5 + TypeScript5 + Element-Plus 的后台管理模板,配套接口文档和后端源码,vue-element-admin 的 Vue3 版本"
-    name="description"
-  />
-  <meta
-    content="vue,element-plus,typescript,vue-element-admin,vue3-element-admin"
-    name="keywords"
-  />
-  <title>晶格智汇</title>
-</head>
+  <head>
+    <meta charset="UTF-8" />
+    <link href="/favicon.ico" rel="icon" />
+    <meta content="width=device-width, initial-scale=1.0" name="viewport" />
+    <meta
+      content="Vue3 + Vite5 + TypeScript5 + Element-Plus 的后台管理模板,配套接口文档和后端源码,vue-element-admin 的 Vue3 版本"
+      name="description"
+    />
+    <meta
+      content="vue,element-plus,typescript,vue-element-admin,vue3-element-admin"
+      name="keywords"
+    />
+    <title>综合管理平台</title>
+  </head>
 
 
-<body>
-<div id="app">
-  <div class="loader"></div>
-</div>
-</body>
-<script src="/src/main.ts" type="module"></script>
+  <body>
+    <div id="app">
+      <div class="loader"></div>
+    </div>
+  </body>
+  <script src="/src/main.ts" type="module"></script>
 
 
-<style>
-  html,
-  body,
-  #app {
-    position: relative;
-    display: flex;
-    align-items: center;
-    justify-content: center;
-    width: 100%;
-    height: 100%;
-  }
+  <style>
+    html,
+    body,
+    #app {
+      position: relative;
+      display: flex;
+      align-items: center;
+      justify-content: center;
+      width: 100%;
+      height: 100%;
+    }
 
 
-  .loader {
-    position: relative;
-    width: 40px;
-    aspect-ratio: 0.577;
-    overflow: hidden;
-    clip-path: polygon(0 0, 100% 100%, 0 100%, 100% 0);
-    animation: l19 2s infinite linear;
-  }
+    .loader {
+      position: relative;
+      width: 40px;
+      aspect-ratio: 0.577;
+      overflow: hidden;
+      clip-path: polygon(0 0, 100% 100%, 0 100%, 100% 0);
+      animation: l19 2s infinite linear;
+    }
 
 
-  .loader::before {
-    position: absolute;
-    inset: -150%;
-    content: "";
-    background: repeating-conic-gradient(
-      from 30deg,
-      #ffabab 0 60deg,
-      #abe4ff 0 120deg,
-      #ff7373 0 180deg
-    );
-    animation: inherit;
-    animation-direction: reverse;
-  }
+    .loader::before {
+      position: absolute;
+      inset: -150%;
+      content: "";
+      background: repeating-conic-gradient(
+        from 30deg,
+        #ffabab 0 60deg,
+        #abe4ff 0 120deg,
+        #ff7373 0 180deg
+      );
+      animation: inherit;
+      animation-direction: reverse;
+    }
 
 
-  @keyframes l19 {
-    100% {
-      transform: rotate(360deg);
+    @keyframes l19 {
+      100% {
+        transform: rotate(360deg);
+      }
     }
     }
-  }
-</style>
+  </style>
 </html>
 </html>

+ 1 - 0
package.json

@@ -52,6 +52,7 @@
     "echarts": "^5.5.0",
     "echarts": "^5.5.0",
     "element-plus": "^2.6.0",
     "element-plus": "^2.6.0",
     "lodash-es": "^4.17.21",
     "lodash-es": "^4.17.21",
+    "nanoid": "^5.0.7",
     "net": "^1.0.2",
     "net": "^1.0.2",
     "nprogress": "^0.2.0",
     "nprogress": "^0.2.0",
     "path-browserify": "^1.0.1",
     "path-browserify": "^1.0.1",

+ 3 - 0
public/version.json

@@ -0,0 +1,3 @@
+{
+  "version": "1.8"
+}

+ 2 - 1
src/plugins/permission.ts

@@ -1,12 +1,13 @@
 import router from "@/router";
 import router from "@/router";
 import NProgress from "@/utils/nprogress";
 import NProgress from "@/utils/nprogress";
-
+import { isNewVersion } from "@/utils/version.js";
 export function setupPermission() {
 export function setupPermission() {
   // 白名单路由
   // 白名单路由
   const whiteList = ["/login"];
   const whiteList = ["/login"];
 
 
   router.beforeEach(async (to, from, next) => {
   router.beforeEach(async (to, from, next) => {
     NProgress.start();
     NProgress.start();
+    isNewVersion();
     const hasToken = localStorage.getItem("token");
     const hasToken = localStorage.getItem("token");
     if (hasToken) {
     if (hasToken) {
       if (to.path === "/login") {
       if (to.path === "/login") {

+ 40 - 0
src/utils/version.js

@@ -0,0 +1,40 @@
+// utils/version.js
+
+import axios from "axios";
+// 生成一个随机的字符串作为请求的查询参数
+import { nanoid } from "nanoid";
+import { ElMessageBox } from "element-plus";
+
+export async function isNewVersion() {
+  // 构建了一个 URL,用于请求版本信息的 JSON 文件。这个 URL 包括当前页面的主机名和一个随机生成的长度为 10 的字符串作为查询参数。
+  const url = `//${window.location.host}/version.json?id=${nanoid(10)}`;
+  // 使用 Axios 发起了一个 get 请求,获取版本信息的 json 文件。
+  try {
+    const res = await axios.get(url);
+    //   从响应数据中获取了版本号。
+    const version = res.data.version;
+    //   从本地存储中获取了之前保存的版本号
+    const localVersion = localStorage.getItem("version");
+    //   检查本地存储的版本号是否存在且与当前版本号不一致
+    if (localVersion && localVersion !== version) {
+      ElMessageBox.confirm("发现新的版本,请刷新页面后使用!", "系统提示", {
+        confirmButtonText: "立即刷新",
+        // cancelButtonText: this.$t('btn.cancel'),
+        showCancelButton: false,
+        closeOnClickModal: false,
+        type: "warning",
+      })
+        .then(() => {
+          localStorage.setItem("version", version);
+          window.location.reload();
+        })
+        .catch(() => {});
+    } else {
+      // 如果没有发现新版本,则仅将当前版本号保存到本地存储中。
+      // 这段代码的作用是在页面加载时检查是否有新版本可用,如果有新版本则重新加载页面以获取最新版本
+      localStorage.setItem("version", version);
+    }
+  } catch (error) {
+    console.log(error);
+  }
+}

+ 26 - 1
vite.config.ts

@@ -1,7 +1,7 @@
 import vue from "@vitejs/plugin-vue";
 import vue from "@vitejs/plugin-vue";
 import vueJsx from "@vitejs/plugin-vue-jsx";
 import vueJsx from "@vitejs/plugin-vue-jsx";
 import { ConfigEnv, defineConfig, loadEnv, UserConfig } from "vite";
 import { ConfigEnv, defineConfig, loadEnv, UserConfig } from "vite";
-
+import fs from "fs";
 import AutoImport from "unplugin-auto-import/vite";
 import AutoImport from "unplugin-auto-import/vite";
 import Components from "unplugin-vue-components/vite";
 import Components from "unplugin-vue-components/vite";
 import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
 import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
@@ -126,6 +126,7 @@ export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
         // 指定symbolId格式
         // 指定symbolId格式
         symbolId: "icon-[dir]-[name]",
         symbolId: "icon-[dir]-[name]",
       }),
       }),
+      process.env.NODE_ENV === "production" && updateVersion(),
     ],
     ],
     // 预加载项目必需的组件
     // 预加载项目必需的组件
     optimizeDeps: {
     optimizeDeps: {
@@ -245,3 +246,27 @@ export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
     },
     },
   };
   };
 });
 });
+function updateVersion() {
+  return {
+    name: "update-version",
+    buildStart() {
+      let version = "";
+      try {
+        // 读取 json 文件里面版本号
+        const packageJson = JSON.parse(
+          fs.readFileSync("public/version.json", "utf-8")
+        );
+        version = Number(packageJson.version);
+        version = (version + 0.1).toFixed(1); // 增加0.1并保留1位小数
+      } catch (error) {
+        console.log(error);
+      }
+      const versionData = { version: version.toString() }; // 根据需要生成新版本号
+      // 写入 json 文件里面版本号
+      fs.writeFileSync(
+        "public/version.json",
+        JSON.stringify(versionData, null, 2)
+      );
+    },
+  };
+}