浏览代码

优化打包

ZaiZai 2 年之前
父节点
当前提交
a080bfccdc
共有 4 个文件被更改,包括 55 次插入1 次删除
  1. 3 0
      public/version.json
  2. 19 0
      scripts/build.js
  3. 28 1
      src/App.vue
  4. 5 0
      src/api/modules/other.js

+ 3 - 0
public/version.json

@@ -0,0 +1,3 @@
+{
+  "value": "20230705162022"
+}

+ 19 - 0
scripts/build.js

@@ -6,6 +6,19 @@ const currentDirectory = process.cwd();
 console.log(`----------------------------`)
 console.log(`正在处理编译打包前的准备...`)
 
+//修改版本更新信息
+console.log(`更新版本更新信息...`)
+const versionPath = path.join(currentDirectory, '/public/version.json');
+const versionContent = fs.readFileSync(versionPath, 'utf8');
+//修改配置文件
+const versionJson = JSON.parse(versionContent);
+versionJson.value = dateFormat(new Date());         //版本号
+
+//更新版本更新信息
+fs.writeFileSync(versionPath, JSON.stringify(versionJson, null, 2));
+
+console.log(`----------------------------`)
+
 //删除上次打包相关的文件
 console.log(`正在删除上次打包相关的文件...`)
 const distZipPath = path.join(currentDirectory, '/zip/hac.zip');
@@ -13,11 +26,15 @@ if(fs.existsSync(distZipPath)) {
     fs.unlinkSync(distZipPath);
 }
 
+console.log(`----------------------------`)
+
 // 获取配置文件
 console.log(`获取当前的配置文件...`)
 const indexJsonPath = path.join(currentDirectory, 'src/config/index.json');
 const indexJsonContent = fs.readFileSync(indexJsonPath, 'utf8');
 
+console.log(`----------------------------`)
+
 // 检测上次打包异常中断的缓存文件是否存在
 console.log(`检测上次打包异常中断的缓存文件是否存在...`)
 const cacheFilePath = path.join(currentDirectory, 'scripts/cache.json');
@@ -28,6 +45,8 @@ if(!fs.existsSync(cacheFilePath)) {
     fs.writeFileSync(cacheJsonPath, indexJsonContent, 'utf8');
 }
 
+console.log(`----------------------------`)
+
 //修改配置文件
 const indexJson = JSON.parse(indexJsonContent);
 indexJson.version = dateFormat(new Date());         //版本号

+ 28 - 1
src/App.vue

@@ -9,6 +9,8 @@ import {nextTick, ref, watch} from "vue";
 import {useAppStore} from "~src/store";
 import {useOsTheme} from '~src/plugins/useOsTheme';
 import {setElementMainColor, ulog, getObjValue} from "js-fast-way"
+import {getStoreValue, setStoreValue} from "~uti/storage";
+import {getVersionJson} from "~api/other";
 import config from '~src/config/index';
 
 //初始变量
@@ -27,9 +29,34 @@ watch(() => [
 
 nextTick(() => {
     setUserTheme(appStore.getThemeVal, appStore.getColor)
-    ulog('启动成功', '当前开发版本 v' + config?.version)
+    //检测新版本
+    setTimeout(() => {
+        getVersionJsonApi()
+    }, 1000)
 })
 
+//获取版本更新信息
+const getVersionJsonApi = async () => {
+    const cache_version = getStoreValue('version')
+    const {res} = await getVersionJson()
+    const version = getObjValue(res)?.value
+    setStoreValue('version', version)
+    if (cache_version && cache_version !== version) {
+        window?.$messageBox?.alert('检测到有新版本更新,请点击更新,或手动刷新网页更新,如果不更新,将无法使用相关功能', '更新提醒', {
+            showCancelButton: true,
+            confirmButtonText: '理解刷新',
+            cancelButtonText: '暂不更新',
+            type: 'warning',
+            callback: (action) => {
+                if (action === 'confirm') {
+                    //刷新页面
+                    window.location.reload()
+                }
+            }
+        })
+    }
+}
+
 //设置主题 #0081ff
 const setUserTheme = (theme, appColor) => {
     const colorVal = getObjValue(appColor)

+ 5 - 0
src/api/modules/other.js

@@ -59,3 +59,8 @@ export const getApprovesList = (form, msg = true) => httpApi({
 
 
 
+//获取更新信息
+export const getVersionJson = () => httpApi({
+    url: 'version.json',
+    method: 'get'
+}, false);