feat: 初始化收支管理系统
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
// 分类相关 API 接口:分类的增删改查
|
||||
import request from './request'
|
||||
|
||||
/** 获取分类列表,可按类型筛选 */
|
||||
export function getCategoryList(type?: string) {
|
||||
return request.get('/category/list', { params: type ? { type } : {} })
|
||||
}
|
||||
|
||||
/** 创建新分类 */
|
||||
export function createCategory(data: { name: string; type: string; icon?: string; sortOrder?: number }) {
|
||||
return request.post('/category', data)
|
||||
}
|
||||
|
||||
/** 更新指定分类 */
|
||||
export function updateCategory(id: number, data: { name: string; type: string; icon?: string; sortOrder?: number }) {
|
||||
return request.put(`/category/${id}`, data)
|
||||
}
|
||||
|
||||
/** 删除指定分类 */
|
||||
export function deleteCategory(id: number) {
|
||||
return request.delete(`/category/${id}`)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// HTTP 请求封装模块:基于 axios 创建请求实例,统一处理请求拦截和响应拦截
|
||||
import axios from 'axios'
|
||||
import { useUserStore } from '../store/user'
|
||||
import router from '../router'
|
||||
|
||||
// 创建 axios 实例,设置基础路径和超时时间
|
||||
const request = axios.create({
|
||||
baseURL: '/api', // API 基础路径
|
||||
timeout: 10000, // 请求超时时间(毫秒)
|
||||
})
|
||||
|
||||
// 请求拦截器:自动携带 Authorization 请求头
|
||||
request.interceptors.request.use((config) => {
|
||||
const userStore = useUserStore()
|
||||
if (userStore.token) {
|
||||
config.headers.Authorization = `Bearer ${userStore.token}` // 附加 JWT token
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
// 响应拦截器:统一处理响应数据和错误
|
||||
request.interceptors.response.use(
|
||||
(response) => {
|
||||
const data = response.data
|
||||
// 如果业务状态码不是 200,视为请求失败
|
||||
if (data.code !== 200) {
|
||||
return Promise.reject(new Error(data.message || '请求失败'))
|
||||
}
|
||||
return data
|
||||
},
|
||||
(error) => {
|
||||
// 401 未授权:自动退出登录并跳转到登录页
|
||||
if (error.response?.status === 401) {
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
router.push('/login')
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default request
|
||||
@@ -0,0 +1,17 @@
|
||||
// 统计数据相关 API 接口:月度统计、分类统计、趋势统计
|
||||
import request from './request'
|
||||
|
||||
/** 获取指定年月的收支汇总统计(总收入、总支出、结余、交易笔数) */
|
||||
export function getMonthlyStatistics(params: { year: number; month: number }) {
|
||||
return request.get('/statistics/monthly', { params })
|
||||
}
|
||||
|
||||
/** 获取指定年月的分类收支统计(用于饼图等) */
|
||||
export function getCategoryStatistics(params: { year: number; month: number }) {
|
||||
return request.get('/statistics/category', { params })
|
||||
}
|
||||
|
||||
/** 获取指定年月的每日收支趋势数据(用于折线图/柱状图) */
|
||||
export function getTrendStatistics(params: { year: number; month: number }) {
|
||||
return request.get('/statistics/trend', { params })
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
// 收支记录相关 API 接口:交易记录的增删改查(分页)
|
||||
import request from './request'
|
||||
import axios from 'axios'
|
||||
import { useUserStore } from '../store/user'
|
||||
|
||||
/** 分页获取交易记录列表,支持按类型、分类、支付方式、时间范围、计入统计多选筛选 */
|
||||
export function getTransactionPage(params: {
|
||||
page?: number
|
||||
size?: number
|
||||
types?: string[]
|
||||
categoryIds?: number[]
|
||||
paymentMethods?: string[]
|
||||
startTime?: string
|
||||
endTime?: string
|
||||
includeInStats?: boolean
|
||||
}) {
|
||||
return request.get('/transaction/page', { params })
|
||||
}
|
||||
|
||||
/** 创建新的交易记录 */
|
||||
export function createTransaction(data: {
|
||||
categoryId: number
|
||||
type: string
|
||||
amount: number
|
||||
paymentMethod?: string
|
||||
remark?: string
|
||||
transactionTime: string
|
||||
includeInStats?: boolean
|
||||
}) {
|
||||
return request.post('/transaction', data)
|
||||
}
|
||||
|
||||
/** 更新指定交易记录 */
|
||||
export function updateTransaction(id: number, data: {
|
||||
categoryId: number
|
||||
type: string
|
||||
amount: number
|
||||
paymentMethod?: string
|
||||
remark?: string
|
||||
transactionTime: string
|
||||
includeInStats?: boolean
|
||||
}) {
|
||||
return request.put(`/transaction/${id}`, data)
|
||||
}
|
||||
|
||||
/** 删除指定交易记录 */
|
||||
export function deleteTransaction(id: number) {
|
||||
return request.delete(`/transaction/${id}`)
|
||||
}
|
||||
|
||||
/** 切换指定交易记录的"计入统计"标记 */
|
||||
export function toggleTransactionStats(id: number, includeInStats: boolean) {
|
||||
return request.put(`/transaction/${id}/stats`, { includeInStats })
|
||||
}
|
||||
|
||||
/** 导出交易记录为 Excel 文件,支持按条件筛选 */
|
||||
export function exportTransaction(params: {
|
||||
types?: string[]
|
||||
categoryIds?: number[]
|
||||
paymentMethods?: string[]
|
||||
startTime?: string
|
||||
endTime?: string
|
||||
includeInStats?: boolean
|
||||
}) {
|
||||
const userStore = useUserStore()
|
||||
return axios.get('/api/transaction/export', {
|
||||
params,
|
||||
paramsSerializer: {
|
||||
indexes: null,
|
||||
},
|
||||
responseType: 'blob',
|
||||
headers: {
|
||||
Authorization: `Bearer ${userStore.token}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 用户相关 API 接口:登录、注册、获取用户信息
|
||||
import request from './request'
|
||||
|
||||
/** 用户登录 */
|
||||
export function login(data: { username: string; password: string }) {
|
||||
return request.post('/user/login', data)
|
||||
}
|
||||
|
||||
/** 用户注册 */
|
||||
export function register(data: { username: string; password: string; nickname?: string }) {
|
||||
return request.post('/user/register', data)
|
||||
}
|
||||
|
||||
/** 获取当前登录用户的信息 */
|
||||
export function getUserInfo() {
|
||||
return request.get('/user/info')
|
||||
}
|
||||
Reference in New Issue
Block a user