tangweijie d7f81893c5 Initial commit: 完整的 Rust 账户管理系统
- 实现账户管理改进设计文档中的所有核心功能
- 三科目余额管理 (个人余额、劳动报酬、冻结余额)
- 交易状态机 (created → pending → bank_submitted → success/failed/timeout → reversed)
- 三键幂等体系 (JZTxId/BankTxId/SourceKey)
- 优先级扣款规则 (先个人后劳动)
- 在途资金管理 (可用→在途→结转/回退)
- 三账对账闭环 (总账 = 银行账 + 在途净额)
- 补偿服务域 (超时检测、重试、死信队列)
- 虚拟银行模拟器用于业务测试
- 完整的集成测试套件 (133 个测试全部通过)
- Docker 容器化部署配置
- 前端 Vue3 + TypeScript 项目结构
2026-01-05 17:56:01 +08:00

76 lines
3.9 KiB
Rust

//! API 层 - HTTP 路由和处理器
mod handlers;
mod state;
pub use state::AppState;
use axum::{
routing::{get, post, put, delete},
Router,
};
use tower_http::cors::{Any, CorsLayer};
use tower_http::trace::TraceLayer;
/// 创建 API 路由
pub fn create_router(state: AppState) -> Router {
let cors = CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any);
Router::new()
// 健康检查
.route("/health", get(handlers::health_check))
// 实体账户 API
.route("/api/v1/physical-accounts", post(handlers::account::create_physical_account))
.route("/api/v1/physical-accounts", get(handlers::account::list_physical_accounts))
.route("/api/v1/physical-accounts/:id", get(handlers::account::get_physical_account))
.route("/api/v1/physical-accounts/:id/freeze", post(handlers::account::freeze_physical_account))
.route("/api/v1/physical-accounts/:id/unfreeze", post(handlers::account::unfreeze_physical_account))
// 虚拟子账户 API
.route("/api/v1/sub-accounts", post(handlers::account::create_sub_account))
.route("/api/v1/sub-accounts/:id", get(handlers::account::get_sub_account))
.route("/api/v1/sub-accounts/:id/balance", get(handlers::account::get_sub_account_balance))
.route("/api/v1/sub-accounts/:id/freeze", post(handlers::account::freeze_sub_account))
.route("/api/v1/sub-accounts/:id/unfreeze", post(handlers::account::unfreeze_sub_account))
.route("/api/v1/sub-accounts/:id/close", post(handlers::account::close_sub_account))
.route("/api/v1/physical-accounts/:id/sub-accounts", get(handlers::account::list_sub_accounts))
// 交易 API
.route("/api/v1/transactions/transfer", post(handlers::transaction::transfer))
.route("/api/v1/transactions/deposit", post(handlers::transaction::deposit))
.route("/api/v1/transactions/withdraw", post(handlers::transaction::withdraw))
.route("/api/v1/transactions/:id", get(handlers::transaction::get_transaction))
.route("/api/v1/transactions", get(handlers::transaction::list_transactions))
// 账务 API
.route("/api/v1/ledger/subjects", get(handlers::ledger::list_subjects))
.route("/api/v1/ledger/entries/:id", get(handlers::ledger::get_entry))
.route("/api/v1/ledger/accounts/:id/entries", get(handlers::ledger::get_account_entries))
// 对账 API
.route("/api/v1/reconciliation/run", post(handlers::reconciliation::run_reconciliation))
.route("/api/v1/reconciliation/batches/:id", get(handlers::reconciliation::get_batch))
.route("/api/v1/reconciliation/batches/:id/items", get(handlers::reconciliation::get_batch_items))
.route("/api/v1/reconciliation/three-account/:account_id", get(handlers::reconciliation::verify_three_account_for_api))
.route("/api/v1/reconciliation/adjustments", post(handlers::reconciliation::create_adjustment))
.route("/api/v1/reconciliation/adjustments/:id/approve", post(handlers::reconciliation::approve_adjustment))
.route("/api/v1/reconciliation/adjustments/:id/reject", post(handlers::reconciliation::reject_adjustment))
.route("/api/v1/reconciliation/adjustments/pending", get(handlers::reconciliation::list_pending_adjustments))
// 积分 API
.route("/api/v1/points/accounts/:sub_account_id", get(handlers::points::get_points_accounts))
.route("/api/v1/points/earn", post(handlers::points::earn_points))
.route("/api/v1/points/spend", post(handlers::points::spend_points))
.route("/api/v1/points/transfer", post(handlers::points::transfer_points))
.route("/api/v1/points/transactions", get(handlers::points::list_transactions))
.layer(TraceLayer::new_for_http())
.layer(cors)
.with_state(state)
}