Files
AUTO-MAS-test/frontend/src/composables/useSettingsApi.ts
AoXuan c116efd6f4 feat(settings): 新增设置页面和相关功能
- 添加设置页面组件和路由
- 实现设置数据的获取和更新逻辑
- 新增多个设置选项,包括功能设置、通知设置、更新设置等
- 优化设置页面样式和交互
2025-08-04 21:19:24 +08:00

101 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<string | null>(null)
// 获取设置
const getSettings = async (): Promise<SettingsData | null> => {
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<SettingsData>): Promise<boolean> => {
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,
}
}