This commit is contained in:
oneao committed 2025-11-11 17:27:41 +08:00
1 parent 994321d049
commit b68a37f40b
7 files changed
+91 -47

No files matched your search

@@ -1,5 +1,6 @@
import type { MoneyCategoryResult } from '@/request/api/money'
import type { MoneyCategory } from '@/types/money' import type { MoneyCategory } from '@/types/money'
import { useState } from 'react' import { useEffect, useState } from 'react'
import { Pressable, Text, View } from 'react-native' import { Pressable, Text, View } from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context' import { useSafeAreaInsets } from 'react-native-safe-area-context'
import SegmentedTabs from '@/components/common/ao/SegmentedTabs' import SegmentedTabs from '@/components/common/ao/SegmentedTabs'
@@ -11,6 +12,7 @@ import {
DrawerHeader, DrawerHeader,
} from '@/components/ui/drawer' } from '@/components/ui/drawer'
import { Grid, GridItem } from '@/components/ui/grid' import { Grid, GridItem } from '@/components/ui/grid'
import { getMoneyCategoryApi } from '@/request/api/money'
import MoneyCategoryIcon from './MoneyCategoryIcon' import MoneyCategoryIcon from './MoneyCategoryIcon'
interface AddMoneyDrawerProps { interface AddMoneyDrawerProps {
@@ -23,8 +25,30 @@ export default function AddMoneyDrawer({
onClose, onClose,
}: AddMoneyDrawerProps) { }: AddMoneyDrawerProps) {
const insets = useSafeAreaInsets() const insets = useSafeAreaInsets()
const [seletedIndex, setSelectedIndex] = useState(0) const [selectedIndex, setSelectedIndex] = useState(0)
const categories: MoneyCategory[] = [] const [selectedCategoryId, setSelectedCategoryId] = useState<number | null>(null)
const [categories, setCategories] = useState<MoneyCategoryResult>({
income: [],
expense: [],
})
useEffect(() => {
if (isOpen) {
const fetchCategories = async () => {
try {
const res = await getMoneyCategoryApi()
if (res.isSuccess) {
setCategories(res.data)
}
}
catch (err) {
console.error('获取分类失败', err)
}
}
fetchCategories()
}
}, [isOpen])
return ( return (
<Drawer <Drawer
@@ -34,36 +58,19 @@ export default function AddMoneyDrawer({
onClose={onClose} onClose={onClose}
> >
<DrawerBackdrop /> <DrawerBackdrop />
<DrawerContent <DrawerContent style={{ padding: 0, paddingLeft: 16, paddingRight: 16 }}>
style={{
padding: 0,
paddingLeft: 16,
paddingRight: 16,
}}
>
<DrawerHeader <DrawerHeader
className="flex items-center justify-between" className="flex items-center justify-between"
style={{ style={{ paddingTop: insets.top, margin: 0, padding: 0 }}
paddingTop: insets.top,
margin: 0,
padding: 0,
}}
> >
<View className="w-1" /> <View className="w-1" />
<SegmentedTabs <SegmentedTabs
value={seletedIndex} value={selectedIndex}
tabs={[ tabs={[{ label: '收入' }, { label: '支出' }]}
{ label: '收入' }, onChange={setSelectedIndex}
{ label: '支出' },
]}
onChange={(index) => {
setSelectedIndex(index)
}}
style={{ width: 180 }} style={{ width: 180 }}
className="mx-auto" className="mx-auto"
/> />
<Pressable onPress={onClose}> <Pressable onPress={onClose}>
<Text className="text-gray-500 font-medium">取消</Text> <Text className="text-gray-500 font-medium">取消</Text>
</Pressable> </Pressable>
@@ -71,20 +78,43 @@ export default function AddMoneyDrawer({
<DrawerBody> <DrawerBody>
<Grid className="gap-4" _extra={{ className: 'grid-cols-4' }}> <Grid className="gap-4" _extra={{ className: 'grid-cols-4' }}>
{categories.map((item, index) => ( {(() => {
<GridItem const list = selectedIndex === 0 ? categories.income : categories.expense
key={index} const items = list.map((item) => {
className="flex flex-col items-center justify-center" const isSelected = selectedCategoryId === item.id
_extra={{ className: 'col-span-1' }} return (
> <GridItem key={item.id} _extra={{ className: 'col-span-1' }}>
<Pressable> <Pressable
<View className="w-14 h-14 rounded-full bg-gray-200 flex items-center justify-center"> className={`flex flex-col items-center justify-center p-1 rounded ${
<MoneyCategoryIcon name={item.name} size={28} /> isSelected ? 'bg-blue-100' : ''
</View> }`}
<Text className="text-sm mt-1">{item.label}</Text> onPress={() => setSelectedCategoryId(item.id)}
</Pressable> >
</GridItem> <View
))} className={`w-14 h-14 rounded-full flex items-center justify-center ${
isSelected ? 'bg-blue-400' : 'bg-gray-200'
}`}
>
<MoneyCategoryIcon name={item.icon} size={28} />
</View>
<Text className={`text-sm mt-1 ${isSelected ? 'text-blue-600 font-semibold' : ''}`}>
{item.name}
</Text>
</Pressable>
</GridItem>
)
})
const remainder = list.length % 4
if (remainder > 0) {
const emptyCount = 4 - remainder
for (let i = 0; i < emptyCount; i++) {
items.push(<GridItem key={`empty-${i}`} _extra={{ className: 'col-span-1' }} />)
}
}
return items
})()}
</Grid> </Grid>
</DrawerBody> </DrawerBody>
</DrawerContent> </DrawerContent>
@@ -80,7 +80,7 @@ const alovaInstance = createAlova({
return return
} }
catch (err) { catch (err) {
router.replace('/auth/login') // router.replace('/auth/login')
return Promise.reject(err) return Promise.reject(err)
} }
} }
@@ -1,8 +1,7 @@
export interface MoneyCategory { export interface MoneyCategory {
id: string id: number
user_id: number user_id: number
name: string name: string
label: string
icon: string icon: string
sort_number: number sort_number: number
type: number type: number
@@ -4,12 +4,17 @@ import (
response "base-go-v2/internal/reponse" response "base-go-v2/internal/reponse"
"base-go-v2/internal/router" "base-go-v2/internal/router"
"base-go-v2/internal/utils/jwtx" "base-go-v2/internal/utils/jwtx"
"fmt"
) )
func AuthMiddleware() router.HandlerFunc { func AuthMiddleware() router.HandlerFunc {
return func(c *router.Context) error { return func(c *router.Context) error {
fmt.Println("===请求进入===")
token := c.Header("token") token := c.Header("token")
if token == "" { if token == "" {
fmt.Println("===未提供 Token===")
c.Abort() c.Abort()
return response.Fail(c). return response.Fail(c).
Code(response.HttpCode.Unauthorized). Code(response.HttpCode.Unauthorized).
@@ -22,11 +27,13 @@ func AuthMiddleware() router.HandlerFunc {
// Access Token 有效 // Access Token 有效
if tokenVerify.IsValid { if tokenVerify.IsValid {
c.SetUserID(tokenVerify.Claims.UserID) c.SetUserID(tokenVerify.Claims.UserID)
return nil return c.Next()
} }
// Access Token 失效 → 返回刷新提示 // Access Token 失效 → 返回刷新提示
if !tokenVerify.IsValid { if !tokenVerify.IsValid {
fmt.Println("===Access Token 失效===")
c.Abort() c.Abort()
return response.Fail(c). return response.Fail(c).
Code(response.HttpCode.RefreshToken). Code(response.HttpCode.RefreshToken).
@@ -34,6 +41,7 @@ func AuthMiddleware() router.HandlerFunc {
Send() Send()
} }
fmt.Println("===请求通过===")
return c.Next() return c.Next()
} }
} }
@@ -28,7 +28,9 @@ func InitDefaultCategories(userID int64) error {
fmt.Printf("系统分类数量: %d\n", len(sysCategories)) fmt.Printf("系统分类数量: %d\n", len(sysCategories))
// 2. 构建批量插入数据 // 2. 构建批量插入数据
userCategories := make([]mapx.M, 0, len(sysCategories)) //userCategories := make([]mapx.M, 0, len(sysCategories))
userCategories := mapx.NewArr(len(sysCategories))
for i, row := range sysCategories { for i, row := range sysCategories {
m := mapx.New().SetKV( m := mapx.New().SetKV(
"id", idgen.NextId(), "id", idgen.NextId(),
@@ -52,6 +54,7 @@ func InitDefaultCategories(userID int64) error {
} }
func getUserCategory(c *router.Context) error { func getUserCategory(c *router.Context) error {
fmt.Println("请求进入了====")
userId := c.GetUserID() userId := c.GetUserID()
// 查询当前用户的分类 // 查询当前用户的分类
@@ -19,6 +19,6 @@ func InitAppRouter() *router.Router {
auth.InitAuthRouter(r) auth.InitAuthRouter(r)
money.InitMoneyRouter(r) money.InitMoneyRouter(r)
return r return r
} }
@@ -13,8 +13,12 @@ func New() M {
return M{} return M{}
} }
func NewArr() { func NewArr(cap ...int) []M {
//return make([]M, 0, len()) c := 0
if len(cap) > 0 {
c = cap[0]
}
return make([]M, 0, c)
} }
// Set 设置单个键值,支持链式调用 // Set 设置单个键值,支持链式调用