From e781525d653026d215c3404cad0853b3c4dd17da Mon Sep 17 00:00:00 2001 From: 12975 <1297598740@qq.com> Date: Thu, 29 Jan 2026 23:00:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=EF=BC=8C=E5=8C=85=E5=90=AB=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E3=80=81=E5=AE=89=E5=85=A8=E3=80=81=E6=95=B0=E6=8D=AE=E5=92=8C?= =?UTF-8?q?=E5=85=B3=E4=BA=8E=E9=80=89=E9=A1=B9=E5=8D=A1=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=81=8F=E5=A5=BD=E8=AE=BE=E7=BD=AE=E5=92=8C?= =?UTF-8?q?=E8=B4=A6=E6=88=B7=E7=AE=A1=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Settings/Settings.tsx | 91 ++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/src/pages/Settings/Settings.tsx b/src/pages/Settings/Settings.tsx index 1d14f87..c79aaa3 100644 --- a/src/pages/Settings/Settings.tsx +++ b/src/pages/Settings/Settings.tsx @@ -5,8 +5,10 @@ import AppLockSettings from '../../components/settings/AppLockSettings'; import { CapsuleSelector } from '../../components/common/CapsuleSelector/CapsuleSelector'; import { CustomSelect } from '../../components/common/CustomSelect/CustomSelect'; import { getSupportedCurrencies } from '../../services/exchangeRateService'; -import { getSettings, updateSettings } from '../../services/settingsService'; -import type { UserSettings } from '../../types'; +import { getSupportedCurrencies } from '../../services/exchangeRateService'; +import { getSettings, updateSettings, updateDefaultAccounts } from '../../services/settingsService'; +import { getAccounts } from '../../services/accountService'; +import type { UserSettings, Account } from '../../types'; import { Icon } from '@iconify/react'; import { ThemePicker } from '../../components/settings/ThemePicker/ThemePicker'; import './Settings.css'; @@ -38,6 +40,7 @@ function Settings() { firstDayOfWeek: parseInt(localStorage.getItem('firstDayOfWeek') || '1'), }); const [userSettings, setUserSettings] = useState(null); + const [accounts, setAccounts] = useState([]); const [saveMessage, setSaveMessage] = useState(''); const currencies = getSupportedCurrencies(); @@ -45,6 +48,7 @@ function Settings() { useEffect(() => { // Load user settings loadUserSettings(); + loadAccounts(); // Sync settings state with global theme mode setSettings(prev => ({ ...prev, theme: themeMode })); }, [themeMode]); @@ -58,6 +62,15 @@ function Settings() { } }; + const loadAccounts = async () => { + try { + const data = await getAccounts(); + setAccounts(data); + } catch (err) { + console.error('Failed to load accounts:', err); + } + }; + const handleUserSettingChange = async (key: keyof UserSettings, value: any) => { if (!userSettings) return; @@ -74,6 +87,38 @@ function Settings() { } }; + const handleDefaultAccountChange = async (key: 'defaultExpenseAccountId' | 'defaultIncomeAccountId', value: number) => { + if (!userSettings) return; + + try { + const updateData = { + [key]: value + }; + await updateDefaultAccounts(updateData); + + // Update local state + let updatedSettings = { ...userSettings }; + if (key === 'defaultExpenseAccountId') { + updatedSettings.defaultExpenseAccountId = value; + // Update the related object for immediate UI feedback if needed + const account = accounts.find(a => a.id === value); + if (account) updatedSettings.defaultExpenseAccount = account; + } else { + updatedSettings.defaultIncomeAccountId = value; + const account = accounts.find(a => a.id === value); + if (account) updatedSettings.defaultIncomeAccount = account; + } + + setUserSettings(updatedSettings); + setSaveMessage('默认账户已更新'); + setTimeout(() => setSaveMessage(''), 2000); + } catch (err) { + console.error('Failed to update default account:', err); + setSaveMessage('更新失败'); + setTimeout(() => setSaveMessage(''), 2000); + } + }; + @@ -276,6 +321,48 @@ function Settings() {

记账设置

+
+
+ +

AI 记账和快捷记账时的默认扣款账户

+
+ handleDefaultAccountChange('defaultExpenseAccountId', Number(value))} + options={[ + { value: '', label: '不设置' }, + ...accounts + .filter(a => a.type !== 'credit_card' && a.type !== 'credit_line' ? true : true) // Show all for now, maybe filter logic later + .map(account => ({ + value: account.id, + label: account.name + })) + ]} + /> +
+ +
+
+ +

记录收入时的默认入账账户

+
+ handleDefaultAccountChange('defaultIncomeAccountId', Number(value))} + options={[ + { value: '', label: '不设置' }, + ...accounts + //.filter(a => a.accountType === 'asset') // Typically income goes to assets + .map(account => ({ + value: account.id, + label: account.name + })) + ]} + /> +
+