Add update service

This commit is contained in:
2025-06-23 19:42:48 +08:00
parent 4f8272e290
commit ea025e3f5d
19 changed files with 639 additions and 270 deletions

View File

@@ -186,11 +186,6 @@ const parseKeyBinding = (keyStr: string, command?: string): string[] => {
return part.charAt(0).toUpperCase() + part.slice(1).toLowerCase();
}).filter(part => part.length > 0);
};
// 组件挂载时加载快捷键数据
onMounted(async () => {
await keybindingStore.loadKeyBindings();
});
</script>
<template>

View File

@@ -1,87 +1,123 @@
<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import { ref } from 'vue';
import {useI18n} from 'vue-i18n';
import {computed, onMounted} from 'vue';
import {useConfigStore} from '@/stores/configStore';
import {useUpdateStore} from '@/stores/updateStore';
import SettingSection from '../components/SettingSection.vue';
import SettingItem from '../components/SettingItem.vue';
import ToggleSwitch from '../components/ToggleSwitch.vue';
const { t } = useI18n();
const {t} = useI18n();
const configStore = useConfigStore();
const updateStore = useUpdateStore();
// 模拟版本数据
const currentVersion = ref('1.0.0');
const isCheckingForUpdates = ref(false);
const updateAvailable = ref(false);
const latestVersion = ref('1.1.0');
const updateNotes = ref([
'优化编辑器性能',
'新增自动保存功能',
'修复多个界面显示问题',
'添加更多编辑器主题'
]);
// 计算属性
const autoCheckUpdates = computed({
get: () => configStore.config.updates.autoUpdate,
set: async (value: boolean) => {
await configStore.setAutoUpdate(value);
}
});
// 自动检查更新选项
const autoCheckUpdates = ref(true);
// 格式化发布说明
const formatReleaseNotes = (notes: string) => {
if (!notes) return [];
// 模拟检查更新
const checkForUpdates = () => {
isCheckingForUpdates.value = true;
// 模拟网络请求延迟
setTimeout(() => {
isCheckingForUpdates.value = false;
updateAvailable.value = true;
}, 1500);
// 简单的Markdown列表解析
return notes
.split('\n')
.filter(line => line.trim().startsWith('-') || line.trim().startsWith('*'))
.map(line => line.replace(/^[\s\-\*]+/, '').trim())
.filter(line => line.length > 0);
};
// 模拟下载更新
const downloadUpdate = () => {
// 在实际应用中这里会调用后端API下载更新
alert('开始下载更新...');
// 处理查看更新
const viewUpdate = () => {
updateStore.openReleaseURL();
};
// 获取错误信息的国际化文本
const getErrorMessage = (error: string) => {
if (error.includes('Network') || error.includes('network')) {
return t('settings.networkError');
}
return error;
};
</script>
<template>
<div class="settings-page">
<SettingSection :title="t('settings.updates')">
<div class="update-info">
<div class="version-info">
<div class="current-version">
<span class="label">当前版本:</span>
<span class="version">{{ currentVersion }}</span>
<!-- 自动更新设置 -->
<SettingSection :title="t('settings.updateSettings')">
<SettingItem
:title="t('settings.autoCheckUpdates')"
:description="t('settings.autoCheckUpdatesDescription')"
>
<ToggleSwitch v-model="autoCheckUpdates"/>
</SettingItem>
</SettingSection>
<!-- 手动检查更新 -->
<SettingSection :title="t('settings.manualCheck')">
<SettingItem
:title="`${t('settings.currentVersion')}: ${updateStore.updateResult?.currentVer || configStore.config.updates.version}`"
>
<button
class="check-button"
@click="updateStore.checkForUpdates"
:disabled="updateStore.isChecking"
>
<span v-if="updateStore.isChecking" class="loading-spinner"></span>
{{ updateStore.isChecking ? t('settings.checking') : t('settings.checkForUpdates') }}
</button>
</SettingItem>
<!-- 检查结果 -->
<div class="check-results" v-if="updateStore.updateResult || updateStore.errorMessage">
<!-- 错误信息 -->
<div v-if="updateStore.errorMessage" class="result-item error-result">
<div class="result-text">
<span class="result-icon"></span>
<span class="result-message">{{ getErrorMessage(updateStore.errorMessage) }}</span>
</div>
<button
class="check-button"
@click="checkForUpdates"
:disabled="isCheckingForUpdates"
>
<span v-if="isCheckingForUpdates" class="loading-spinner"></span>
{{ isCheckingForUpdates ? '检查中...' : '检查更新' }}
</button>
</div>
<div v-if="updateAvailable" class="update-available">
<div class="update-header">
<div class="update-title">发现新版本: {{ latestVersion }}</div>
<button class="download-button" @click="downloadUpdate">
下载更新
<!-- 有新版本 -->
<div v-else-if="updateStore.hasUpdate" class="result-item update-result">
<div class="result-header">
<div class="result-text">
<span class="result-icon">🎉</span>
<span class="result-message">
{{ t('settings.newVersionAvailable') }}: {{ updateStore.updateResult?.latestVer }}
</span>
</div>
<button class="view-button" @click="viewUpdate">
{{ t('settings.viewUpdate') }}
</button>
</div>
<div class="update-notes">
<div class="notes-title">更新内容:</div>
<ul class="notes-list">
<li v-for="(note, index) in updateNotes" :key="index">
<div v-if="updateStore.updateResult?.releaseNotes" class="release-notes">
<div class="notes-title">{{ t('settings.releaseNotes') }}:</div>
<ul class="notes-list" v-if="formatReleaseNotes(updateStore.updateResult.releaseNotes).length > 0">
<li v-for="(note, index) in formatReleaseNotes(updateStore.updateResult.releaseNotes)" :key="index">
{{ note }}
</li>
</ul>
<div v-else class="notes-text">
{{ updateStore.updateResult.releaseNotes }}
</div>
</div>
</div>
<!-- 已是最新版本 -->
<div v-else-if="updateStore.updateResult && !updateStore.hasUpdate && !updateStore.errorMessage"
class="result-item success-result">
<div class="result-text">
<span class="result-icon"></span>
<span class="result-message">{{ t('settings.upToDate') }}</span>
</div>
</div>
</div>
<SettingItem title="自动检查更新" description="启动应用时自动检查更新">
<ToggleSwitch v-model="autoCheckUpdates" />
</SettingItem>
</SettingSection>
</div>
</template>
@@ -91,133 +127,167 @@ const downloadUpdate = () => {
max-width: 800px;
}
.update-info {
padding: 15px 16px;
margin-bottom: 20px;
.check-button {
padding: 8px 16px;
background-color: var(--settings-input-bg);
border: 1px solid var(--settings-input-border);
border-radius: 4px;
color: var(--settings-text);
cursor: pointer;
font-size: 12px;
transition: all 0.2s ease;
display: flex;
align-items: center;
gap: 8px;
&:hover:not(:disabled) {
background-color: var(--settings-hover);
border-color: var(--settings-border);
}
&:active:not(:disabled) {
transform: translateY(1px);
}
&:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.loading-spinner {
display: inline-block;
width: 14px;
height: 14px;
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
border-top-color: var(--settings-text);
animation: spin 1s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
}
.check-results {
padding: 0 16px;
margin-top: 16px;
}
.result-item {
padding: 12px 0;
.version-info {
.result-text {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
line-height: 1.4;
}
.result-icon {
font-size: 16px;
flex-shrink: 0;
}
.result-message {
flex: 1;
}
.result-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
.current-version {
font-size: 13px;
.label {
color: var(--text-muted);
margin-right: 5px;
}
.version {
color: var(--settings-text);
font-weight: 500;
}
}
.check-button {
padding: 8px 16px;
margin-bottom: 8px;
.view-button {
padding: 4px 12px;
background-color: var(--settings-input-bg);
border: 1px solid var(--settings-input-border);
border-radius: 4px;
color: var(--settings-text);
cursor: pointer;
font-size: 12px;
font-size: 11px;
transition: all 0.2s ease;
display: flex;
align-items: center;
gap: 8px;
&:hover:not(:disabled) {
flex-shrink: 0;
&:hover {
background-color: var(--settings-hover);
border-color: var(--settings-border);
}
&:active:not(:disabled) {
&:active {
transform: translateY(1px);
}
&:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.loading-spinner {
display: inline-block;
width: 14px;
height: 14px;
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
border-top-color: var(--settings-text);
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
}
}
.update-available {
background-color: var(--settings-card-bg);
border: 1px solid var(--settings-border);
border-radius: 6px;
padding: 16px;
.update-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
.release-notes {
margin-top: 8px;
padding-left: 24px;
.notes-title {
font-size: 12px;
color: var(--text-muted);
margin-bottom: 6px;
}
.notes-list {
margin: 0;
padding-left: 16px;
.update-title {
font-size: 13px;
font-weight: 500;
color: #4a9eff;
}
.download-button {
padding: 8px 16px;
background-color: #2c5a9e;
border: none;
border-radius: 4px;
color: #ffffff;
cursor: pointer;
li {
font-size: 12px;
transition: all 0.2s ease;
&:hover {
background-color: #3867a9;
}
&:active {
transform: translateY(1px);
color: var(--settings-text-secondary);
line-height: 1.4;
margin-bottom: 3px;
&:last-child {
margin-bottom: 0;
}
}
}
.update-notes {
.notes-title {
font-size: 12px;
color: var(--settings-text-secondary);
margin-bottom: 8px;
}
.notes-list {
margin: 0;
padding-left: 20px;
li {
font-size: 12px;
color: var(--settings-text-secondary);
margin-bottom: 6px;
&:last-child {
margin-bottom: 0;
}
}
}
.notes-text {
font-size: 12px;
color: var(--settings-text-secondary);
line-height: 1.4;
white-space: pre-wrap;
word-wrap: break-word;
}
}
}
.error-result {
.result-message {
color: #f44336;
}
.result-icon {
color: #f44336;
}
}
.update-result {
.result-message {
color: #2196f3;
font-weight: 500;
}
.result-icon {
color: #2196f3;
}
}
.success-result {
.result-message {
color: #4caf50;
}
.result-icon {
color: #4caf50;
}
}
</style>