Skip to content

Commit

Permalink
feat: add isOpenDeployReload config to handle deploy reload
Browse files Browse the repository at this point in the history
  • Loading branch information
flingyp committed Jan 16, 2024
1 parent eeeb614 commit d833098
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 20 deletions.
7 changes: 4 additions & 3 deletions build/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Plugin as viteCDNPlugin } from 'vite-plugin-cdn-import';
import { VitePWA } from 'vite-plugin-pwa';
import VueDevTools from 'vite-plugin-vue-devtools';

import { useFormatDate } from '@flypeng/tool/browser';
import { useGetCurrentPath } from '@flypeng/tool/node';
import { writeFileSync } from 'fs';

Expand Down Expand Up @@ -183,11 +184,11 @@ export const generatePlugins = (command: string, mode: string, buildTimestamp: n
},
}),

// Build End Plugin
// Build Start Plugin
{
name: 'vite-plugin-vadmire-buildEnding',
name: 'vite-plugin-vadmire-build-start',
buildStart: () => {
console.log('构建开始', buildTimestamp, useGetCurrentPath());
console.log('vite-plugin-vadmire-build-start:', useFormatDate('yyyy-MM-dd hh:mm:ss', buildTimestamp));
writeFileSync(
`${useGetCurrentPath()}/public/config.json`,
`{
Expand Down
4 changes: 2 additions & 2 deletions public/config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"buildTime": 17053021998210
}
"buildTime": 1705375061773
}
23 changes: 19 additions & 4 deletions src/RouterContent.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { t } from '@wangeditor/editor';
import { useDialog } from 'naive-ui';
import { DeployReload } from '~/utils';
Expand All @@ -20,18 +21,20 @@ watch(
// listening system whether updated
const dialog = useDialog();
const isOpenDialog = ref(true);
const { isOpenDeployReload } = storeToRefs(useVAdmireConfigStore());
new DeployReload({
fetchUrl: 'http://localhost:8080/config.json',
const deployReload = new DeployReload({
fetchUrl: 'http://localhost:4173/config.json',
fetchKey: 'buildTime',
isListening: false,
checkTimeout: 60 * 5,
execute: true,
reloadCallback() {
if (isOpenDialog.value) {
isOpenDialog.value = false;
dialog.warning({
title: '提示',
content: '检测到系统已更新最新版本,请重新加载页面',
title: '温馨提示',
content: '检测到系统发生更新,请重新加载页面',
positiveText: '更新',
negativeText: '不更新',
closable: false,
Expand All @@ -48,6 +51,18 @@ new DeployReload({
}
},
});
watch(
() => isOpenDeployReload.value,
(newVal) => {
if (newVal) {
deployReload.open();
} else {
deployReload.close();
}
},
{ immediate: true },
);
</script>

<template>
Expand Down
10 changes: 10 additions & 0 deletions src/layout/components/setting/BaseSystemConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
fixedTabBar,
isVisibleTabBar,
isVisibleFooter,
isOpenDeployReload,
headerHeight,
siderWidth,
footerHeight,
Expand Down Expand Up @@ -56,6 +57,11 @@ const updateVisibilityTabBar = (value: boolean) => {
const updateVisibilityFooter = (value: boolean) => {
isVisibleFooter.value = value;
};
// whether to enable deployment refresh
const updateOpenDeployReload = (value: boolean) => {
isOpenDeployReload.value = value;
};
</script>

<template>
Expand All @@ -72,6 +78,10 @@ const updateVisibilityFooter = (value: boolean) => {
<span class="w-32 text-start mr-1 truncate">{{ $t('setting.showFooter') }}</span>
<NSwitch v-model:value="isVisibleFooter" @update:value="updateVisibilityFooter" />
</div>
<div class="w-full flex justify-between items-center">
<span class="w-44 text-start mr-1 truncate">{{ $t('setting.openDeployReload') }}</span>
<NSwitch v-model:value="isOpenDeployReload" @update:value="updateOpenDeployReload" />
</div>
<div class="w-full flex justify-between items-center">
<span class="w-32 text-start mr-1 truncate">{{ $t('setting.headerHeight') }}</span>
<NInputNumber v-model:value="headerHeight" class="flex-1" placeholder="Min 44, Max 84" :min="44" :max="74" />
Expand Down
3 changes: 2 additions & 1 deletion src/locales/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"footerHeight": "底部栏高度",
"transitionSwitch": "过渡切换",
"copySystemConfig": "拷贝系统配置",
"resetSystemConfig": "重置系统配置"
"resetSystemConfig": "重置系统配置",
"openDeployReload": "开启部署刷新"
},
"header": {
"searchPlaceholder": "搜索关键字"
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"footerHeight": "Footer Height",
"transitionSwitch": "Transition Switch",
"copySystemConfig": "Copy System Config",
"resetSystemConfig": "Reset System Config"
"resetSystemConfig": "Reset System Config",
"openDeployReload": "Enable deploy refresh"
},
"header": {
"searchPlaceholder": "Search for keywords"
Expand Down
3 changes: 2 additions & 1 deletion src/locales/kr.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"footerHeight": "아래 표시줄 높이",
"transitionSwitch": "전환 효과",
"copySystemConfig": "시스템 설정을 복사합니다",
"resetSystemConfig": "시스템 설정을 재설정합니다"
"resetSystemConfig": "시스템 설정을 재설정합니다",
"openDeployReload": "배포 새로 고침 사용"
},
"header": {
"searchPlaceholder": "검색 키워드"
Expand Down
1 change: 1 addition & 0 deletions src/types/config/vadmire.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export interface VAdmireConfig {
fixedTabBar: boolean;
isVisibleTabBar: boolean;
isVisibleFooter: boolean;
isOpenDeployReload: boolean;
defaultLocales: Locales;
}
22 changes: 15 additions & 7 deletions src/utils/reload.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* fetchUrl: 请求的地址
* fetchKey: 获取的字段
* isListening: 是否监听
* checkTimeout: 多久检查一次
* execute: 是否立即执行
* reloadCallback: 更新执行回调
*/
interface DeployReloadOptions {
fetchUrl: string;
fetchKey: string;
isListening?: boolean;
checkTimeout?: number;
execute?: boolean;
reloadCallback?: () => void;
Expand All @@ -16,6 +18,7 @@ interface DeployReloadOptions {
export class DeployReload {
private fetchUrl: string;
private fetchKey: string;
private isListening: boolean;
private stashBuildTime: number = 0;
private checkTimeout: number; // Second
private execute: boolean = false;
Expand All @@ -26,6 +29,7 @@ export class DeployReload {
constructor(options: DeployReloadOptions) {
this.fetchUrl = options.fetchUrl;
this.fetchKey = options.fetchKey;
this.isListening = options.isListening || true;
this.checkTimeout = options.checkTimeout || 5;
this.execute = options.execute || false;

Expand All @@ -46,9 +50,7 @@ export class DeployReload {

document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
console.log('22222');
this.execute && callback();

this.intervalInstance = setInterval(callback, timeoutValue);
} else {
clearInterval(this.intervalInstance);
Expand All @@ -63,13 +65,19 @@ export class DeployReload {
}

private async checkBuildTime() {
const currentBuildTime = await this.getBuildTimeValue();
if (currentBuildTime > this.stashBuildTime) {
this.reload();
if (this.isListening) {
const currentBuildTime = await this.getBuildTimeValue();
if (currentBuildTime > this.stashBuildTime) {
this.reloadCallback();
}
}
}

private reload() {
this.reloadCallback();
public open() {
this.isListening = true;
}

public close() {
this.isListening = false;
}
}
1 change: 1 addition & 0 deletions src/vadmire.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function defaultVAdmireConfig(): VAdmireConfig {
fixedTabBar: false,
isVisibleTabBar: true,
isVisibleFooter: true,
isOpenDeployReload: true,
defaultLocales: 'zh_CN',
};
}
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"compilerOptions": {
"baseUrl": ".",
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"paths": {
"@flypeng/tool/browser": ["node_modules/@flypeng/tool/dist/browser/index.d.ts"],
"@flypeng/tool/node": ["node_modules/@flypeng/tool/dist/node/index.d.ts"]
}
},
"include": ["vite.config.ts", "package.json", "./build/**/*.ts"]
}

0 comments on commit d833098

Please sign in to comment.