fix: 清理死代码 + 修复群组模式预算支出计算
- 清理 store 中未使用的 loading 状态(stats/budget/category/user) - 清理 stats store 冗余类型重导出和 catch 重置逻辑 - 清理 transaction store 未使用的 currentPage - 清理 api/group 未使用的 GroupDetail/GroupMember/getGroupDetail - 删除无引用的 api/index.ts barrel 导出文件 - 删除后端未调用的 GET /groups/:id 群组详情路由 - 合并 stats 页面重复的 loadTabData 函数 - 修复预算页群组模式下只算个人支出(不传 group_id)
This commit is contained in:
@@ -4,24 +4,12 @@ import * as api from '@/api/budget'
|
||||
import { getCurrentMonth } from '@/utils/format'
|
||||
import { useGroupStore } from '@/stores/group'
|
||||
|
||||
// 重新导出类型以保持向后兼容
|
||||
export type Budget = api.Budget
|
||||
|
||||
export const useBudgetStore = defineStore('budget', () => {
|
||||
const budget = ref<api.Budget | null>(null)
|
||||
const loading = ref(false)
|
||||
|
||||
async function fetchBudget(month?: string) {
|
||||
loading.value = true
|
||||
try {
|
||||
const groupStore = useGroupStore()
|
||||
budget.value = await api.getBudget(month || getCurrentMonth(), groupStore.currentGroupId)
|
||||
} catch (err) {
|
||||
budget.value = null
|
||||
throw err
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
const groupStore = useGroupStore()
|
||||
budget.value = await api.getBudget(month || getCurrentMonth(), groupStore.currentGroupId)
|
||||
}
|
||||
|
||||
async function setBudget(amount: number, month?: string) {
|
||||
@@ -29,5 +17,5 @@ export const useBudgetStore = defineStore('budget', () => {
|
||||
await fetchBudget(month)
|
||||
}
|
||||
|
||||
return { budget, loading, fetchBudget, setBudget }
|
||||
return { budget, fetchBudget, setBudget }
|
||||
})
|
||||
|
||||
@@ -7,17 +7,9 @@ export type Category = api.Category
|
||||
|
||||
export const useCategoryStore = defineStore('category', () => {
|
||||
const categories = ref<api.Category[]>([])
|
||||
const loading = ref(false)
|
||||
|
||||
async function fetchCategories() {
|
||||
loading.value = true
|
||||
try {
|
||||
categories.value = await api.getCategories()
|
||||
} catch {
|
||||
categories.value = []
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
categories.value = await api.getCategories()
|
||||
}
|
||||
|
||||
function getByType(type: 'expense' | 'income') {
|
||||
@@ -53,5 +45,5 @@ export const useCategoryStore = defineStore('category', () => {
|
||||
await fetchCategories()
|
||||
}
|
||||
|
||||
return { categories, loading, fetchCategories, getByType, getById, addCategory, updateCategory, deleteCategory, migrateCategory, sortCategories }
|
||||
return { categories, fetchCategories, getByType, getById, addCategory, updateCategory, deleteCategory, migrateCategory, sortCategories }
|
||||
})
|
||||
|
||||
@@ -4,70 +4,54 @@ import * as api from '@/api/stats'
|
||||
import { getCurrentMonth } from '@/utils/format'
|
||||
import { useGroupStore } from '@/stores/group'
|
||||
|
||||
// 重新导出类型以保持向后兼容
|
||||
export type Overview = api.Overview
|
||||
export type CategoryStat = api.CategoryStat
|
||||
export type TrendPoint = api.TrendPoint
|
||||
/** 默认概览数据,避免模板中访问属性报错 */
|
||||
const EMPTY_OVERVIEW: api.Overview = {
|
||||
expense: 0, income: 0, expenseCount: 0, incomeCount: 0,
|
||||
count: 0, daily: 0, dailyIncome: 0
|
||||
}
|
||||
|
||||
export const useStatsStore = defineStore('stats', () => {
|
||||
const overview = ref<api.Overview>({ expense: 0, income: 0, expenseCount: 0, incomeCount: 0, count: 0, daily: 0, dailyIncome: 0 })
|
||||
const overview = ref<api.Overview>({ ...EMPTY_OVERVIEW })
|
||||
const categoryStats = ref<api.CategoryStat[]>([])
|
||||
const trendData = ref<api.TrendPoint[]>([])
|
||||
const loading = ref(false)
|
||||
|
||||
/** 获取当前 group_id,统一收口 */
|
||||
function getGroupId() {
|
||||
return useGroupStore().currentGroupId
|
||||
}
|
||||
|
||||
async function fetchOverview(month?: string) {
|
||||
loading.value = true
|
||||
try {
|
||||
const groupStore = useGroupStore()
|
||||
const data = await api.getOverview({
|
||||
month: month || getCurrentMonth(),
|
||||
group_id: groupStore.currentGroupId
|
||||
})
|
||||
overview.value = {
|
||||
expense: Number(data?.expense) || 0,
|
||||
income: Number(data?.income) || 0,
|
||||
expenseCount: Number(data?.expenseCount) || 0,
|
||||
incomeCount: Number(data?.incomeCount) || 0,
|
||||
count: Number(data?.count) || 0,
|
||||
daily: Number(data?.daily) || 0,
|
||||
dailyIncome: Number(data?.dailyIncome) || 0
|
||||
}
|
||||
} catch (err) {
|
||||
overview.value = { expense: 0, income: 0, expenseCount: 0, incomeCount: 0, count: 0, daily: 0, dailyIncome: 0 }
|
||||
throw err
|
||||
} finally {
|
||||
loading.value = false
|
||||
const data = await api.getOverview({
|
||||
month: month || getCurrentMonth(),
|
||||
group_id: getGroupId()
|
||||
})
|
||||
overview.value = {
|
||||
expense: Number(data?.expense) || 0,
|
||||
income: Number(data?.income) || 0,
|
||||
expenseCount: Number(data?.expenseCount) || 0,
|
||||
incomeCount: Number(data?.incomeCount) || 0,
|
||||
count: Number(data?.count) || 0,
|
||||
daily: Number(data?.daily) || 0,
|
||||
dailyIncome: Number(data?.dailyIncome) || 0
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchCategoryStats(month?: string, type: string = 'expense') {
|
||||
try {
|
||||
const groupStore = useGroupStore()
|
||||
const data = await api.getCategoryStats(month || getCurrentMonth(), type, groupStore.currentGroupId)
|
||||
categoryStats.value = Array.isArray(data) ? data.map(item => ({
|
||||
...item,
|
||||
amount: Number(item.amount) || 0,
|
||||
count: Number(item.count) || 0
|
||||
})) : []
|
||||
} catch (err) {
|
||||
categoryStats.value = []
|
||||
throw err
|
||||
}
|
||||
const data = await api.getCategoryStats(month || getCurrentMonth(), type, getGroupId())
|
||||
categoryStats.value = Array.isArray(data) ? data.map(item => ({
|
||||
...item,
|
||||
amount: Number(item.amount) || 0,
|
||||
count: Number(item.count) || 0
|
||||
})) : []
|
||||
}
|
||||
|
||||
async function fetchTrend(month?: string, type: string = 'expense') {
|
||||
try {
|
||||
const groupStore = useGroupStore()
|
||||
const data = await api.getTrend(month || getCurrentMonth(), type, groupStore.currentGroupId)
|
||||
trendData.value = Array.isArray(data) ? data.map(item => ({
|
||||
...item,
|
||||
amount: Number(item.amount) || 0
|
||||
})) : []
|
||||
} catch (err) {
|
||||
trendData.value = []
|
||||
throw err
|
||||
}
|
||||
const data = await api.getTrend(month || getCurrentMonth(), type, getGroupId())
|
||||
trendData.value = Array.isArray(data) ? data.map(item => ({
|
||||
...item,
|
||||
amount: Number(item.amount) || 0
|
||||
})) : []
|
||||
}
|
||||
|
||||
return { overview, categoryStats, trendData, loading, fetchOverview, fetchCategoryStats, fetchTrend }
|
||||
return { overview, categoryStats, trendData, fetchOverview, fetchCategoryStats, fetchTrend }
|
||||
})
|
||||
|
||||
@@ -7,7 +7,6 @@ export const useTransactionStore = defineStore('transaction', () => {
|
||||
const transactions = ref<api.Transaction[]>([])
|
||||
const total = ref(0)
|
||||
const loading = ref(false)
|
||||
const currentPage = ref(1)
|
||||
|
||||
async function fetchTransactions(params?: api.TransactionParams) {
|
||||
loading.value = true
|
||||
@@ -67,5 +66,5 @@ export const useTransactionStore = defineStore('transaction', () => {
|
||||
}
|
||||
}
|
||||
|
||||
return { transactions, total, loading, currentPage, fetchTransactions, addTransaction, updateTransaction, deleteTransaction, fetchTransactionById }
|
||||
return { transactions, total, loading, fetchTransactions, addTransaction, updateTransaction, deleteTransaction, fetchTransactionById }
|
||||
})
|
||||
|
||||
@@ -5,7 +5,6 @@ import { API_BASE } from '@/config'
|
||||
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
const userInfo = ref<api.UserInfo | null>(null)
|
||||
const loading = ref(false)
|
||||
|
||||
const nickname = computed(() => {
|
||||
return userInfo.value?.nickname || uni.getStorageSync('xc:nickname') || '小菜'
|
||||
@@ -21,18 +20,10 @@ export const useUserStore = defineStore('user', () => {
|
||||
})
|
||||
|
||||
async function fetchUserInfo() {
|
||||
loading.value = true
|
||||
try {
|
||||
userInfo.value = await api.getUserInfo()
|
||||
if (userInfo.value) {
|
||||
uni.setStorageSync('xc:nickname', userInfo.value.nickname)
|
||||
uni.setStorageSync('xc:avatar_url', userInfo.value.avatar_url)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[UserStore] fetchUserInfo error:', e)
|
||||
throw e
|
||||
} finally {
|
||||
loading.value = false
|
||||
userInfo.value = await api.getUserInfo()
|
||||
if (userInfo.value) {
|
||||
uni.setStorageSync('xc:nickname', userInfo.value.nickname)
|
||||
uni.setStorageSync('xc:avatar_url', userInfo.value.avatar_url)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,5 +40,5 @@ export const useUserStore = defineStore('user', () => {
|
||||
return avatarUrl.value
|
||||
}
|
||||
|
||||
return { userInfo, loading, nickname, avatarUrl, fetchUserInfo, updateProfile, uploadAvatar }
|
||||
return { userInfo, nickname, avatarUrl, fetchUserInfo, updateProfile, uploadAvatar }
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user