diff --git a/frontend/src/composables/useSettingsApi.ts b/frontend/src/composables/useSettingsApi.ts new file mode 100644 index 0000000..b102ccf --- /dev/null +++ b/frontend/src/composables/useSettingsApi.ts @@ -0,0 +1,100 @@ +import { ref } from 'vue' +import { message } from 'ant-design-vue' +import type { SettingsData, GetSettingsResponse, UpdateSettingsResponse } from '../types/settings.ts' + +const API_BASE_URL = 'http://localhost:8000/api' + +export function useSettingsApi() { + const loading = ref(false) + const error = ref(null) + + // 获取设置 + const getSettings = async (): Promise => { + loading.value = true + error.value = null + + try { + const response = await fetch(`${API_BASE_URL}/setting/get`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({}), // 空请求体 + }) + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) + } + + const apiResponse: GetSettingsResponse = await response.json() + + // 根据code判断是否成功(非200就是不成功) + if (apiResponse.code !== 200) { + const errorMsg = apiResponse.message || '获取设置失败' + message.error(errorMsg) + throw new Error(errorMsg) + } + + return apiResponse.data + } catch (err) { + const errorMsg = err instanceof Error ? err.message : '获取设置失败' + error.value = errorMsg + if (!err.message?.includes('HTTP error')) { + message.error(errorMsg) + } + return null + } finally { + loading.value = false + } + } + + // 更新设置 + const updateSettings = async (settings: Partial): Promise => { + loading.value = true + error.value = null + + try { + const response = await fetch(`${API_BASE_URL}/setting/update`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + data: settings, + }), + }) + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) + } + + const apiResponse: UpdateSettingsResponse = await response.json() + + // 根据code判断是否成功(非200就是不成功) + if (apiResponse.code !== 200) { + const errorMsg = apiResponse.message || '设置修改失败' + message.error(errorMsg) + throw new Error(errorMsg) + } + + // message.success(apiResponse.message || '设置修改成功') + return true + } catch (err) { + const errorMsg = err instanceof Error ? err.message : '设置修改失败' + error.value = errorMsg + if (!err.message?.includes('HTTP error')) { + message.error(errorMsg) + } + return false + } finally { + loading.value = false + } + } + + return { + loading, + error, + getSettings, + updateSettings, + } +} diff --git a/frontend/src/types/script.ts b/frontend/src/types/script.ts index 838f085..100f296 100644 --- a/frontend/src/types/script.ts +++ b/frontend/src/types/script.ts @@ -182,4 +182,4 @@ export interface UpdateScriptResponse { code: number status: string message: string -} \ No newline at end of file +} diff --git a/frontend/src/types/settings.ts b/frontend/src/types/settings.ts new file mode 100644 index 0000000..9db8149 --- /dev/null +++ b/frontend/src/types/settings.ts @@ -0,0 +1,69 @@ + +// 设置相关类型定义 +export interface SettingsData { + Function: { + BossKey: string + HistoryRetentionTime: number + HomeImageMode: string + IfAgreeBilibili: boolean + IfAllowSleep: boolean + IfSilence: boolean + IfSkipMumuSplashAds: boolean + UnattendedMode: boolean + } + Notify: { + AuthorizationCode: string + CompanyWebHookBotUrl: string + FromAddress: string + IfCompanyWebHookBot: boolean + IfPushPlyer: boolean + IfSendMail: boolean + IfSendSixStar: boolean + IfSendStatistic: boolean + IfServerChan: boolean + SMTPServerAddress: string + SendTaskResultTime: string + ServerChanChannel: string + ServerChanKey: string + ServerChanTag: string + ToAddress: string + } + Update: { + IfAutoUpdate: boolean + MirrorChyanCDK: string + ProxyAddress: string + ProxyUrlList: string[] + ThreadNumb: number + UpdateType: string + } + Start: { + IfMinimizeDirectly: boolean + IfSelfStart: boolean + } + UI: { + IfShowTray: boolean + IfToTray: boolean + location: string + maximized: boolean + size: string + } + Voice: { + Enabled: boolean + Type: string + } +} + +// 获取设置API响应 +export interface GetSettingsResponse { + code: number + status: string + message: string + data: SettingsData +} + +// 更新设置API响应 +export interface UpdateSettingsResponse { + code: number + status: string + message: string +} \ No newline at end of file diff --git a/frontend/src/views/Settings.vue b/frontend/src/views/Settings.vue index 9606272..f9a1a3e 100644 --- a/frontend/src/views/Settings.vue +++ b/frontend/src/views/Settings.vue @@ -1,33 +1,129 @@