[fix]优化前端代码写法

This commit is contained in:
周继风
2024-06-25 10:16:11 +08:00
parent fe5e44913d
commit 0529c2747a
3 changed files with 10 additions and 8 deletions

3
web/.gitignore vendored
View File

@@ -21,4 +21,5 @@ pnpm-debug.log*
*.njsproj *.njsproj
*.sln *.sln
*.sw? *.sw?
public/version # 构建版本文件无需上传git
public/version-build

View File

@@ -5,8 +5,9 @@ import {ElNotification} from "element-plus";
import fs from "fs"; import fs from "fs";
// 是否显示升级提示信息框 // 是否显示升级提示信息框
export const IS_SHOW_UPGRADE_SESSION_KEY = 'isShowUpgrade'; const IS_SHOW_UPGRADE_SESSION_KEY = 'isShowUpgrade';
const versionKey = 'DVADMIN3_VERSION' const VERSION_KEY = 'DVADMIN3_VERSION'
const VERSION_FILE_NAME = 'version-build'
export function showUpgrade () { export function showUpgrade () {
const isShowUpgrade = Session.get(IS_SHOW_UPGRADE_SESSION_KEY) ?? false const isShowUpgrade = Session.get(IS_SHOW_UPGRADE_SESSION_KEY) ?? false
@@ -28,13 +29,13 @@ export async function checkVersion(){
return return
} }
// 获取线上版本号 t为时间戳防止缓存 // 获取线上版本号 t为时间戳防止缓存
await axios.get(`/version?t=${new Date().getTime()}`).then(res => { await axios.get(`/${VERSION_FILE_NAME}?t=${new Date().getTime()}`).then(res => {
const {status, data} = res || {} const {status, data} = res || {}
if (status === 200) { if (status === 200) {
// 获取当前版本号 // 获取当前版本号
const localVersion = Local.get(versionKey) const localVersion = Local.get(VERSION_KEY)
// 将当前版本号持久缓存至本地 // 将当前版本号持久缓存至本地
Local.set(versionKey, data) Local.set(VERSION_KEY, data)
// 当用户本地存在版本号并且和线上版本号不一致时,进行页面刷新操作 // 当用户本地存在版本号并且和线上版本号不一致时,进行页面刷新操作
if (localVersion && localVersion !== data) { if (localVersion && localVersion !== data) {
// 本地缓存版本号和线上版本号不一致,弹出升级提示框 // 本地缓存版本号和线上版本号不一致,弹出升级提示框
@@ -50,5 +51,5 @@ export async function checkVersion(){
export function generateVersionFile (){ export function generateVersionFile (){
// 生成版本文件到public目录下version文件中 // 生成版本文件到public目录下version文件中
const version = `${process.env.npm_package_version}.${new Date().getTime()}`; const version = `${process.env.npm_package_version}.${new Date().getTime()}`;
fs.writeFileSync('public/version', version); fs.writeFileSync(`public/${VERSION_FILE_NAME}`, version);
} }

View File

@@ -3,7 +3,7 @@ import { resolve } from 'path';
import { defineConfig, loadEnv, ConfigEnv } from 'vite'; import { defineConfig, loadEnv, ConfigEnv } from 'vite';
import vueSetupExtend from 'vite-plugin-vue-setup-extend'; import vueSetupExtend from 'vite-plugin-vue-setup-extend';
import vueJsx from '@vitejs/plugin-vue-jsx' import vueJsx from '@vitejs/plugin-vue-jsx'
import { generateVersionFile } from "./src/utils/upgrade"; import { generateVersionFile } from "/@/utils/upgrade";
const pathResolve = (dir: string) => { const pathResolve = (dir: string) => {
return resolve(__dirname, '.', dir); return resolve(__dirname, '.', dir);