|
| 1 | +<template> |
| 2 | + <a-card size="small" :title="t('Timer')" class="settings-card"> |
| 3 | + <div class="settings-card-row flex-vertical-center"> |
| 4 | + <a-tooltip> |
| 5 | + <template #title>{{ t('TimerServerList') }}</template> |
| 6 | + <span>{{ mt('Server', 'ws', 'List') }}</span> |
| 7 | + </a-tooltip> |
| 8 | + |
| 9 | + <a-select |
| 10 | + v-model:value="store.settings.AutoTimerServerList" |
| 11 | + :options="TimerServerOptions" |
| 12 | + mode="multiple" |
| 13 | + style="flex: 1" |
| 14 | + placeholder="请选择" |
| 15 | + @change="AutoTimerServerChange" |
| 16 | + ></a-select> |
| 17 | + </div> |
| 18 | + |
| 19 | + <div class="interval-card-row flex-vertical-center"> |
| 20 | + <a-tooltip> |
| 21 | + <template #title>{{ t('RestartIntervalText') }}</template> |
| 22 | + <span>{{ t('RestartIntervalText') }}:</span> |
| 23 | + </a-tooltip> |
| 24 | + |
| 25 | + <a-select v-model:value="store.settings.AutoTimerInterval" :options="intervalOptions" placeholder="选择重启间隔" style="flex: 1" @change="changeAutoTimerInterval"></a-select> |
| 26 | + </div> |
| 27 | + |
| 28 | + <div class="settings-card-row flex-vertical-center"> |
| 29 | + <a-switch v-model:checked="store.settings.AutoTimerRestartServer" class="settings-switch" @change="changeAutoTimerRestartServer" /> |
| 30 | + <span>{{ t('ServerAutoRestartText') }}</span> |
| 31 | + </div> |
| 32 | + </a-card> |
| 33 | +</template> |
| 34 | + |
| 35 | +<script setup> |
| 36 | +import { setInterval } from 'timers' |
| 37 | +import { computed, watch, inject, onMounted } from 'vue' |
| 38 | +import { useMainStore } from '@/renderer/store' |
| 39 | +import { storeToRefs } from 'pinia' |
| 40 | +import { mt, t } from '@/renderer/utils/i18n' |
| 41 | +import { createAsyncComponent } from '@/renderer/utils/utils' |
| 42 | +import Settings from '@/main/Settings' |
| 43 | +
|
| 44 | +function formatUnitLabel(seconds) { |
| 45 | + const timeUnits = [ |
| 46 | + { unit: 604800, label: t('Weeks') }, |
| 47 | + { unit: 86400, label: t('Days') }, |
| 48 | + { unit: 3600, label: t('Hours') }, |
| 49 | + { unit: 60, label: t('Minutes') }, |
| 50 | + { unit: 1, label: t('Seconds') } |
| 51 | + ] |
| 52 | + for (let { unit, label } of timeUnits) { |
| 53 | + if (seconds >= unit) { |
| 54 | + return `${seconds / unit} ${label}` |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +
|
| 59 | +const intervalOptions = computed(() => { |
| 60 | + const intervals = [600, 1800, 3600, 7200, 10800, 43200, 86400, 172800, 259200, 604800] |
| 61 | + return intervals.map((value) => ({ |
| 62 | + value, |
| 63 | + label: formatUnitLabel(value) |
| 64 | + })) |
| 65 | +}) |
| 66 | +
|
| 67 | +const ACard = createAsyncComponent(import('ant-design-vue'), 'Card') |
| 68 | +const store = useMainStore() |
| 69 | +const { serverList } = storeToRefs(store) |
| 70 | +const { serverReactive } = inject('GlobalProvide') |
| 71 | +const TimerServerOptions = computed(() => { |
| 72 | + const options = serverList.value.map((item) => { |
| 73 | + const name = item.Name |
| 74 | + const obj = { value: name, label: item.ServerName ? item.ServerName : name } |
| 75 | + if (name === 'Nginx') { |
| 76 | + obj.disabled = true |
| 77 | + } |
| 78 | + return obj |
| 79 | + }) |
| 80 | + options.unshift({ label: t('Website') + ' PHP-FPM', value: 'PHP-FPM' }) |
| 81 | + return options |
| 82 | +}) |
| 83 | +
|
| 84 | +onMounted(async () => { |
| 85 | + if (serverList?.value?.length > 0 && Settings.get('AutoTimerRestartServer') && Settings.get('AutoTimerServerList')) { |
| 86 | + setRestartTimer() |
| 87 | + } |
| 88 | +}) |
| 89 | +
|
| 90 | +const restartServer = async () => { |
| 91 | + const initServerStatus = async (item) => { |
| 92 | + const processList = Settings.get('AutoTimerServerList') |
| 93 | + if (processList.length > 0 && processList.includes(item.Name)) serverReactive.restartFn(item.Name) |
| 94 | + } |
| 95 | +
|
| 96 | + const promiseArray = serverList.value.map((item) => initServerStatus(item)) |
| 97 | + await Promise.all(promiseArray) |
| 98 | +} |
| 99 | +
|
| 100 | +// 定时器管理 |
| 101 | +let restartTimer = null |
| 102 | +
|
| 103 | +const setRestartTimer = () => { |
| 104 | + if (restartTimer) { |
| 105 | + clearInterval(restartTimer) |
| 106 | + } |
| 107 | + if (store.settings.AutoTimerRestartServer && store.settings.AutoTimerInterval) { |
| 108 | + restartTimer = setInterval(() => { |
| 109 | + restartServer() |
| 110 | + }, store.settings.AutoTimerInterval * 1000) |
| 111 | + } |
| 112 | +} |
| 113 | +
|
| 114 | +// 重设定重启间隔时自动更新定时器 |
| 115 | +watch(() => store.settings.AutoTimerInterval, setRestartTimer) |
| 116 | +
|
| 117 | +watch(() => store.settings.AutoTimerRestartServer, setRestartTimer) |
| 118 | +
|
| 119 | +const AutoTimerServerChange = () => { |
| 120 | + store.setSettings('AutoTimerServerList') |
| 121 | +} |
| 122 | +
|
| 123 | +const changeAutoTimerInterval = () => { |
| 124 | + store.setSettings('AutoTimerInterval') |
| 125 | +} |
| 126 | +
|
| 127 | +const changeAutoTimerRestartServer = () => { |
| 128 | + store.setSettings('AutoTimerRestartServer') |
| 129 | + setRestartTimer() |
| 130 | +} |
| 131 | +</script> |
| 132 | +
|
| 133 | +<style scoped></style> |
0 commit comments