import {useEffect, useState} from "react"; import Taro from '@tarojs/taro' import {Button, Cell, Space, Empty, ConfigProvider, Card} from '@nutui/nutui-react-taro' import {View} from '@tarojs/components' import {pageUserPointsLog, getUserPointsStats} from "@/api/user/points"; import {UserPointsLog as UserPointsLogType, UserPointsStats} from "@/api/user/points/model"; const UserPoints = () => { const [list, setList] = useState([]) const [loading, setLoading] = useState(false) const [stats, setStats] = useState({}) const reload = () => { setLoading(true) const userId = Taro.getStorageSync('UserId') console.log('Loading points log for userId:', userId) if (!userId) { console.warn('No userId found in storage') Taro.showToast({ title: '请先登录', icon: 'error' }); setLoading(false) return } pageUserPointsLog({ userId: parseInt(userId), page: 1, limit: 20 }) .then((res: any) => { console.log('Points log response:', res) setList(res?.list || []) }) .catch((error: any) => { console.error('Points log error:', error) Taro.showToast({ title: error?.message || '获取失败', icon: 'error' }); }) .finally(() => { setLoading(false) }) } const loadPointsStats = () => { const userId = Taro.getStorageSync('UserId') if (!userId) return getUserPointsStats(parseInt(userId)) .then((res: any) => { setStats(res) }) .catch((error: any) => { console.error('Points stats error:', error) }) } useEffect(() => { reload() loadPointsStats() }, []); const getPointsTypeText = (type?: number) => { switch (type) { case 1: return '获得积分' case 2: return '消费积分' case 3: return '积分过期' case 4: return '管理员调整' default: return '积分变动' } } const getPointsTypeColor = (type?: number) => { switch (type) { case 1: return 'text-green-500' case 2: return 'text-red-500' case 3: return 'text-gray-500' case 4: return 'text-blue-500' default: return 'text-gray-500' } } if (loading) { return (
加载中...
) } return ( {/* 积分统计卡片 */} {stats.currentPoints || 0} 当前积分 {stats.totalEarned || 0} 累计获得 {stats.totalUsed || 0} 累计消费 {stats.expiringSoon || 0} 即将过期 {/* 积分记录 */} 积分明细 {list.length === 0 ? (
) : ( list.map((item, index) => ( {getPointsTypeText(item.type)} {item.reason || '无备注'} {item.type === 1 ? '+' : item.type === 2 ? '-' : ''} {item.points || 0} {item.createTime ? new Date(item.createTime).toLocaleString() : ''} {item.orderId && ( 订单: {item.orderId} )} {item.comments && ( 备注: {item.comments} )} )) )}
); }; export default UserPoints;