diff --git a/404.svg b/404.svg deleted file mode 100644 index d79b7a22..00000000 --- a/404.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/code/base-project/base-app/src/app/(tabs)/money/components/AddDrawer.tsx b/code/base-project/base-app/src/app/(tabs)/money/components/AddDrawer.tsx index 59c31986..04c8e91b 100644 --- a/code/base-project/base-app/src/app/(tabs)/money/components/AddDrawer.tsx +++ b/code/base-project/base-app/src/app/(tabs)/money/components/AddDrawer.tsx @@ -1,7 +1,7 @@ import type { MoneyCategoryItem, QueryMoneyCategoryResult } from '@/types/money/category' -import type { InsertMoneyRecordResult, MoneyRecordBase } from '@/types/money/record' +import type { MoneyRecordBase, MoneyRecordItem } from '@/types/money/record' import Big from 'big.js' -import { useEffect, useMemo, useState } from 'react' +import { useMemo, useState } from 'react' import { FlatList, Keyboard, Pressable, StyleSheet, Text, TouchableWithoutFeedback, View } from 'react-native' import { useSafeAreaInsets } from 'react-native-safe-area-context' import { AInput, LucideIcon } from '@/components/common/ao' @@ -10,11 +10,9 @@ import SegmentedTabs from '@/components/common/ao/SegmentedTabs' import { Drawer, DrawerBackdrop, - DrawerBody, DrawerContent, DrawerHeader, } from '@/components/ui/drawer' -import { Grid, GridItem } from '@/components/ui/grid' import { useKeyboardHeight } from '@/hooks/useKeyboardHeight' import { queryMoneyCategoryApi } from '@/request/api/money/category' import { insertMoneyRecordApi } from '@/request/api/money/record' @@ -25,7 +23,7 @@ import MoneyCategoryIcon from './MoneyCategoryIcon' interface AddMoneyDrawerProps { isOpen: boolean onClose: () => void - onConfirm?: (result: InsertMoneyRecordResult) => void + onConfirm?: (result: MoneyRecordItem) => void } interface KeydownItem { @@ -81,6 +79,8 @@ export default function AddMoneyDrawer({ income: [], expense: [], }) + // 选中分类 + const [selectedCategory, setSelectedCategory] = useState(null) const keyboardHeight = useKeyboardHeight() @@ -96,11 +96,8 @@ export default function AddMoneyDrawer({ const [moneyRecord, setMoneyRecord] = useState(defaultMoneyRecord) - function selectCategory(item: MoneyCategoryItem) { - setMoneyRecord({ - ...moneyRecord, - category_id: item.id, - }) + function handleSelectCategory(item: MoneyCategoryItem) { + setSelectedCategory(item) setShowKeydown(true) } @@ -128,6 +125,8 @@ export default function AddMoneyDrawer({ setSelectedIndex(0) // 关闭日期 setShowDatePicker(false) + // 清空选中类别 + setSelectedCategory(null) } // 关闭整个drawer处理 @@ -315,36 +314,53 @@ export default function AddMoneyDrawer({ } } + // 保存 // 保存 function saveRecord() { - if (moneyRecord.category_id === -1) { + if (selectedCategory === null) { toast.show({ title: '请选择分类', }) return } + if (moneyRecord.amount === '0.00') { toast.show({ title: '请输入金额', }) + return } + // 去掉金额负号 + const amount = moneyRecord.amount.replace(/^-+/, '') + insertMoneyRecordApi({ ...moneyRecord, - }).then((res) => { - if (res.isSuccess) { - resetData() - - onConfirm?.(res.data) - } - else { - toast.show({ - title: res.message || '保存失败', - }) - } - }).catch((err) => { - console.log(err) + amount, + category_id: selectedCategory.id, }) + .then((res) => { + if (res.isSuccess) { + resetData() + + const result: MoneyRecordItem = { + ...res.data, + category_name: selectedCategory.name, + category_icon: selectedCategory.icon, + category_type: selectedCategory.type, + } + + onConfirm?.(result) + } + else { + toast.show({ + title: res.message || '保存失败', + }) + } + }) + .catch((err) => { + console.log(err) + }) } return ( @@ -418,11 +434,11 @@ export default function AddMoneyDrawer({ return } - const isSelected = moneyRecord.category_id === item.id + const isSelected = selectedCategory?.id === item.id return ( selectCategory(item)} + onPress={() => handleSelectCategory(item)} > { + const next = structuredClone(prev) + + // 查找分组 + const group = next.records.find(g => g.record_time === recordTime) + + // === 已存在分组 === + if (group) { + // 插入到最前面 + group.records.unshift(data) + + // 更新分组 & 总计 + if (data.category_type === 0) { + // 收入 + group.income = add(group.income, data.amount) + next.total_income = add(next.total_income, data.amount) + } + else if (data.category_type === 1) { + // 支出 + group.expense = add(group.expense, data.amount) + next.total_expense = add(next.total_expense, data.amount) + } + + return next + } + + // === 无分组,新建 === + const income = data.category_type === 0 ? data.amount : '0' + const expense = data.category_type === 1 ? data.amount : '0' + + const newGroup: MoneyRecordGroup = { + record_time: recordTime, + income, + expense, + records: [data], + } + + // ---- 按日期(YYYY-MM-DD)倒序插入 ---- + // 最新日期插入前面,比如:2025-01-05 > 2025-01-03 + const index = next.records.findIndex(g => g.record_time < recordTime) + + if (index === -1) { + // 所有日期都比它大 → 插入最后 + next.records.push(newGroup) + } + else { + // 插入正确位置 + next.records.splice(index, 0, newGroup) + } + + // 更新总计 + if (data.category_type === 0) { + next.total_income = add(next.total_income, data.amount) + } + else if (data.category_type === 1) { + next.total_expense = add(next.total_expense, data.amount) + } + + return next + }) + } + return ( { setShowAddDrawer(false) + handleAddMoneyRecord(res) }} /> diff --git a/code/base-project/base-app/src/app/(tabs)/user.tsx b/code/base-project/base-app/src/app/(tabs)/user.tsx index c39a8855..44bfdaf9 100644 --- a/code/base-project/base-app/src/app/(tabs)/user.tsx +++ b/code/base-project/base-app/src/app/(tabs)/user.tsx @@ -1,6 +1,7 @@ import { router } from 'expo-router' import { StyleSheet, Text, View } from 'react-native' import { AButton, AInput } from '@/components/common/ao' +import { LucideIcon } from '@/components/common/ao/index' import { useUserStore } from '@/store/user' export default function User() { @@ -44,13 +45,15 @@ export default function User() { { - router.replace('/abc') + router.replace('/auth/login') }} > 跳转到登陆页面 + + ) } diff --git a/code/base-project/base-app/src/types/money/record.ts b/code/base-project/base-app/src/types/money/record.ts index 5b2a3162..83b1a508 100644 --- a/code/base-project/base-app/src/types/money/record.ts +++ b/code/base-project/base-app/src/types/money/record.ts @@ -24,7 +24,6 @@ export interface MoneyRecordItem extends MoneyRecordBase { } // 分组 & 查询结果 - export interface MoneyRecordGroup { record_time: string expense: string diff --git a/code/base-project/base-app/src/utils/math.ts b/code/base-project/base-app/src/utils/math.ts new file mode 100644 index 00000000..cc05729b --- /dev/null +++ b/code/base-project/base-app/src/utils/math.ts @@ -0,0 +1,32 @@ +// utils/math.ts +import Big from 'big.js' + +/** + * 加法 + * @param a string number + * @param b string number + * @returns string number + */ +export function add(a: string | number, b: string | number): string { + try { + return new Big(a).plus(new Big(b)).toString() + } + catch { + return '0' + } +} + +/** + * 减法 + * @param a string number + * @param b string number + * @returns string number + */ +export function sub(a: string | number, b: string | number): string { + try { + return new Big(a).minus(new Big(b)).toString() + } + catch { + return '0' + } +} diff --git a/empty.svg b/empty.svg deleted file mode 100644 index 90fb7ef7..00000000 --- a/empty.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/error.svg b/error.svg deleted file mode 100644 index 37e2934f..00000000 --- a/error.svg +++ /dev/null @@ -1 +0,0 @@ -server down \ No newline at end of file