优化webdav相关

This commit is contained in:
谦君
2025-12-27 01:37:35 +08:00
parent 66783f896c
commit 49753566c3
5 changed files with 25 additions and 52 deletions

View File

@@ -81,6 +81,7 @@
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { useI18n } from '@/composables/useI18n'
import { useGameStore } from '@/stores/gameStore'
import {
Dialog,
DialogContent,
@@ -95,13 +96,11 @@ import { Label } from '@/components/ui/label'
import { Loader2 } from 'lucide-vue-next'
import {
type WebDAVConfig,
getWebDAVConfig,
saveWebDAVConfig,
clearWebDAVConfig,
testWebDAVConnection
} from '@/services/webdavService'
const { t } = useI18n()
const gameStore = useGameStore()
const isOpen = defineModel<boolean>('open', { default: false })
@@ -130,7 +129,7 @@ const isConfigValid = computed(() => {
// 加载已保存的配置
watch(isOpen, (open) => {
if (open) {
const saved = getWebDAVConfig()
const saved = gameStore.webdavConfig
if (saved) {
config.value = { ...saved }
hasExistingConfig.value = true
@@ -163,14 +162,14 @@ const handleTest = async () => {
}
const handleSave = () => {
saveWebDAVConfig(config.value)
gameStore.webdavConfig = { ...config.value }
hasExistingConfig.value = true
emit('saved', config.value)
isOpen.value = false
}
const handleClear = () => {
clearWebDAVConfig()
gameStore.webdavConfig = null
hasExistingConfig.value = false
config.value = {
serverUrl: '',

View File

@@ -1,14 +1,13 @@
/**
* WebDAV 同步服务
* 支持将存档上传到 WebDAV 服务器如坚果云、Nextcloud、NAS等
* 注意WebDAV 配置存储在 gameStore.webdavConfig 中,与用户数据一起持久化
*/
export interface WebDAVConfig {
serverUrl: string // WebDAV 服务器地址
username: string // 用户名
password: string // 密码或应用专用密码
basePath: string // 存档存放路径
}
import type { WebDAVConfig } from '@/types/game'
// 重新导出类型以保持向后兼容
export type { WebDAVConfig }
export interface WebDAVFile {
name: string
@@ -61,31 +60,6 @@ export interface WebDAVResult {
files?: WebDAVFile[]
}
const STORAGE_KEY = 'ogame-webdav-config'
// 获取保存的 WebDAV 配置
export const getWebDAVConfig = (): WebDAVConfig | null => {
try {
const saved = localStorage.getItem(STORAGE_KEY)
if (saved) {
return JSON.parse(saved)
}
} catch (e) {
console.error('Failed to load WebDAV config:', e)
}
return null
}
// 保存 WebDAV 配置
export const saveWebDAVConfig = (config: WebDAVConfig): void => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(config))
}
// 清除 WebDAV 配置
export const clearWebDAVConfig = (): void => {
localStorage.removeItem(STORAGE_KEY)
}
// 构建 Authorization header
const buildAuthHeader = (config: WebDAVConfig): string => {
const credentials = btoa(`${config.username}:${config.password}`)

View File

@@ -12,7 +12,8 @@ import type {
IncomingFleetAlert,
MissileAttack,
AchievementStats,
AchievementProgress
AchievementProgress,
WebDAVConfig
} from '@/types/game'
import { TechnologyType, OfficerType } from '@/types/game'
import { initializeAchievementStats, initializeAchievements } from '@/logic/achievementLogic'
@@ -61,7 +62,8 @@ export const useGameStore = defineStore('game', {
research: true,
unlock: true
}
}
},
webdavConfig: null as WebDAVConfig | null
}),
actions: {
async requestBrowserPermission(): Promise<boolean> {

View File

@@ -1226,3 +1226,11 @@ export interface QuestNotification {
rewards?: QuestReward
read?: boolean
}
// WebDAV 配置
export interface WebDAVConfig {
serverUrl: string // WebDAV 服务器地址
username: string // 用户名
password: string // 密码或应用专用密码
basePath: string // 存档存放路径
}

View File

@@ -334,7 +334,7 @@
<PrivacyDialog v-model:open="showPrivacyDialog" />
<!-- WebDAV 配置对话框 -->
<WebDAVConfigDialog v-model:open="showWebDAVConfig" @saved="onWebDAVConfigSaved" />
<WebDAVConfigDialog v-model:open="showWebDAVConfig" />
<!-- WebDAV 文件列表对话框 -->
<WebDAVFileListDialog v-model:open="showWebDAVFiles" :config="webdavConfig" @select="handleWebDAVDownload" />
@@ -389,12 +389,7 @@
import WebDAVConfigDialog from '@/components/settings/WebDAVConfigDialog.vue'
import WebDAVFileListDialog from '@/components/settings/WebDAVFileListDialog.vue'
import { useHints } from '@/composables/useHints'
import {
type WebDAVConfig,
getWebDAVConfig,
uploadToWebDAV,
downloadFromWebDAV
} from '@/services/webdavService'
import { uploadToWebDAV, downloadFromWebDAV } from '@/services/webdavService'
const { t } = useI18n()
const { hintsEnabled, setHintsEnabled, resetHints } = useHints()
@@ -415,7 +410,7 @@
// WebDAV 相关状态
const showWebDAVConfig = ref(false)
const showWebDAVFiles = ref(false)
const webdavConfig = ref<WebDAVConfig | null>(getWebDAVConfig())
const webdavConfig = computed(() => gameStore.webdavConfig)
const isWebDAVUploading = ref(false)
// 确保通知设置存在
@@ -758,11 +753,6 @@
gameStore.player.backgroundEnabled = val
}
// WebDAV 配置保存回调
const onWebDAVConfigSaved = () => {
webdavConfig.value = getWebDAVConfig()
}
// WebDAV 上传
const handleWebDAVUpload = async () => {
if (!webdavConfig.value) return