Files
2025-11-28 15:18:10 +08:00

654 lines
35 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package router
import (
"fmt"
"dianshang/internal/config"
"dianshang/internal/handler"
"dianshang/internal/middleware"
"dianshang/internal/repository"
"dianshang/internal/service"
"dianshang/pkg/logger"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// Setup 设置路由
func Setup(db *gorm.DB, cfg *config.Config) *gin.Engine {
r := gin.New()
// 添加中间件
r.Use(middleware.CORSMiddleware())
r.Use(middleware.RequestIDMiddleware()) // 请求ID中间件
r.Use(middleware.UserContextMiddleware()) // 用户上下文中间件
r.Use(middleware.EnhancedLoggerMiddleware()) // 增强日志中间件
r.Use(middleware.OperationLogMiddleware()) // 操作日志中间件
r.Use(gin.Recovery())
// 初始化repositories
userRepo := repository.NewUserRepository(db)
productRepo := repository.NewProductRepository(db)
orderRepo := repository.NewOrderRepository(db)
bannerRepo := repository.NewBannerRepository(db)
couponRepo := repository.NewCouponRepository(db)
pointsRepo := repository.NewPointsRepository(db)
refundRepo := repository.NewRefundRepository(db)
commentRepo := repository.NewCommentRepository(db)
platformRepo := repository.NewPlatformRepository(db)
liveStreamRepo := repository.NewLiveStreamRepository(db)
// 初始化services
userService := service.NewUserService(db)
productService := service.NewProductService(productRepo, userRepo)
orderService := service.NewOrderService(orderRepo, productRepo, userRepo, couponRepo)
cartService := service.NewCartService(orderRepo, productRepo, userRepo)
bannerService := service.NewBannerService(bannerRepo)
couponService := service.NewCouponService(couponRepo)
pointsService := service.NewPointsService(pointsRepo, db)
afterSaleService := service.NewAfterSaleService(db)
adminService := service.NewAdminService(db)
roleService := service.NewRoleService(db)
logService := service.NewLogService(db)
commentService := service.NewCommentService(commentRepo, orderRepo, productRepo)
platformService := service.NewPlatformService(platformRepo, productRepo)
liveStreamService := service.NewLiveStreamService(liveStreamRepo)
// 初始化微信支付服务 - 使用官方SDK
var wechatPayService *service.WeChatPayService
if cfg.WeChatPay.AppID != "" && cfg.WeChatPay.MchID != "" {
var err error
wechatPayService, err = service.NewWeChatPayService(&cfg.WeChatPay, orderRepo, nil)
if err != nil {
logger.Errorf("初始化微信支付服务失败: %v", err)
// 在开发环境下,允许服务继续运行
if cfg.Server.Mode != "production" {
logger.Warn("微信支付服务初始化失败,但在非生产环境下继续运行")
} else {
panic(fmt.Sprintf("微信支付服务初始化失败: %v", err))
}
} else {
logger.Info("微信支付服务初始化成功")
}
} else {
logger.Warn("微信支付配置不完整,跳过微信支付服务初始化")
}
// 初始化退款服务
refundService := service.NewRefundService(refundRepo, orderRepo, wechatPayService)
// 初始化handlers
healthHandler := handler.NewHealthHandler(db)
frontendHandler := handler.NewFrontendHandler(productService, userService, orderService, cartService, bannerService, couponService)
businessHandler := handler.NewBusinessHandler()
pointsHandler := handler.NewPointsHandler(pointsService)
uploadHandler := handler.NewUploadHandler(cfg)
cartHandler := handler.NewCartHandler(cartService)
refundHandler := handler.NewRefundHandler(refundService)
commentHandler := handler.NewCommentHandler(commentService)
// 初始化支付处理器
var paymentHandler *handler.PaymentHandler
if wechatPayService != nil {
paymentHandler = handler.NewPaymentHandler(orderService, wechatPayService, userService)
}
// 健康检查路由(无需认证)
r.GET("/health", healthHandler.Health)
r.GET("/ping", healthHandler.Ping)
// 静态资源服务
r.Static("/static", "./static")
r.StaticFile("/favicon.ico", "./static/assets/favicon.ico")
// 测试页面路由
r.StaticFile("/test_payment_fixed.html", "./test_payment_fixed.html")
r.StaticFile("/test_payment_success.html", "./test_payment_success.html")
// 微信回调路由(不在 /api/v1 下,直接挂在根路径)
r.POST("/api/refunds/callback", refundHandler.RefundCallback) // 微信退款回调
// 根路径处理(用于域名验证)
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "电商小程序API服务",
"version": "v1.0.0",
"status": "running",
})
})
// API路由组
api := r.Group("/api/v1")
{
// 商家信息相关路由
api.GET("/business/time", businessHandler.GetBusinessTime) // 获取商家营业时间
// 前端专用接口
api.GET("/home", frontendHandler.GetHomeData) // 获取首页数据
api.GET("/frontend/products/recommend", frontendHandler.GetProductsRecommend) // 获取推荐商品
api.GET("/frontend/products/hot", frontendHandler.GetProductsHot) // 获取热门商品
api.GET("/frontend/products/:spuId/detail", frontendHandler.GetProductDetail) // 获取商品详情
api.GET("/usercenter", middleware.AuthMiddleware(), frontendHandler.GetUserCenter) // 获取用户中心
api.GET("/cart/data", middleware.AuthMiddleware(), frontendHandler.GetCartData) // 获取购物车数据
api.GET("/orders/list", middleware.AuthMiddleware(), frontendHandler.GetOrderList) // 获取订单列表
api.GET("/addresses", middleware.AuthMiddleware(), frontendHandler.GetAddressList) // 获取地址列表
// 用户相关路由
userHandler := handler.NewUserHandler(db, cfg)
userRoutes := api.Group("/users")
{
userRoutes.POST("/login", userHandler.Login) // 用户登录(兼容旧版本)
userRoutes.POST("/wechat-login", userHandler.WeChatLogin) // 微信登录
userRoutes.POST("/email-login", userHandler.EmailLogin) // 邮箱登录Web端
userRoutes.POST("/email-register", userHandler.EmailRegister) // 邮箱注册Web端
userRoutes.GET("/wechat-session", middleware.AuthMiddleware(), userHandler.GetWeChatSession) // 获取微信会话
userRoutes.POST("/register", userHandler.Register) // 用户注册
userRoutes.GET("/profile", middleware.AuthMiddleware(), userHandler.GetProfile) // 获取用户信息
userRoutes.PUT("/profile", middleware.AuthMiddleware(), userHandler.UpdateProfile) // 更新用户信息
// 用户地址管理
addressRoutes := userRoutes.Group("/addresses", middleware.AuthMiddleware())
{
addressRoutes.GET("", userHandler.GetAddresses) // 获取地址列表
addressRoutes.POST("", userHandler.CreateAddress) // 创建地址
addressRoutes.PUT("/:id", userHandler.UpdateAddress) // 更新地址
addressRoutes.DELETE("/:id", userHandler.DeleteAddress) // 删除地址
addressRoutes.PUT("/:id/default", userHandler.SetDefaultAddress) // 设置默认地址
}
// 用户收藏管理
favoriteRoutes := userRoutes.Group("/favorites", middleware.AuthMiddleware())
{
favoriteRoutes.GET("", userHandler.GetFavorites) // 获取收藏列表
}
}
// 商品收藏相关路由
productFavoriteRoutes := api.Group("/products", middleware.AuthMiddleware())
{
productFavoriteRoutes.POST("/:id/favorite", userHandler.AddToFavorite) // 添加收藏
productFavoriteRoutes.DELETE("/:id/favorite", userHandler.RemoveFromFavorite) // 取消收藏
productFavoriteRoutes.GET("/:id/favorite/status", userHandler.GetFavoriteStatus) // 获取收藏状态
}
// 轮播图相关路由
bannerHandler := handler.NewBannerHandler(bannerService)
api.GET("/banners", bannerHandler.GetBanners) // 获取轮播图
// 直播投流源相关路由(前台)
liveStreamHandler := handler.NewLiveStreamHandler(liveStreamService)
api.GET("/livestreams", liveStreamHandler.GetActiveLiveStreams) // 获取启用的投流源
api.POST("/livestreams/:id/view", liveStreamHandler.IncrementViewCount) // 增加观看次数
// 优惠券相关路由
couponHandler := handler.NewCouponHandler(couponService)
couponRoutes := api.Group("/coupons")
{
couponRoutes.GET("", middleware.OptionalAuthMiddleware(), couponHandler.GetAvailableCoupons) // 获取可用优惠券(可选认证)
couponRoutes.GET("/user", middleware.AuthMiddleware(), couponHandler.GetUserCoupons) // 获取用户优惠券
couponRoutes.POST("/:id/receive", middleware.AuthMiddleware(), couponHandler.ReceiveCoupon) // 领取优惠券
couponRoutes.PUT("/:id/use", middleware.AuthMiddleware(), couponHandler.UseCoupon) // 使用优惠券
couponRoutes.GET("/order/available", middleware.AuthMiddleware(), couponHandler.GetAvailableCouponsForOrder) // 获取订单可用优惠券
couponRoutes.GET("/:id/validate", middleware.AuthMiddleware(), couponHandler.ValidateCoupon) // 验证优惠券
}
// 积分相关路由
pointsRoutes := api.Group("/points").Use(middleware.AuthMiddleware())
{
pointsRoutes.GET("", pointsHandler.GetUserPoints)
pointsRoutes.GET("/overview", pointsHandler.GetPointsOverview)
pointsRoutes.GET("/history", pointsHandler.GetPointsHistory)
pointsRoutes.GET("/rules", pointsHandler.GetPointsRules)
pointsRoutes.GET("/exchange", pointsHandler.GetPointsExchangeList)
pointsRoutes.POST("/exchange/:id", pointsHandler.ExchangePoints)
pointsRoutes.GET("/exchange/records", pointsHandler.GetUserExchangeRecords)
pointsRoutes.POST("/daily-checkin", pointsHandler.DailyCheckin)
}
// 商品相关路由
productHandler := handler.NewProductHandler(productService)
productRoutes := api.Group("/products")
{
productRoutes.GET("", productHandler.GetProductList) // 获取商品列表
productRoutes.GET("/:id", productHandler.GetProductDetail) // 获取商品详情
productRoutes.GET("/:id/skus", productHandler.GetProductSKUs) // 获取商品SKU
productRoutes.GET("/categories", productHandler.GetCategories) // 获取分类列表
productRoutes.GET("/tags", productHandler.GetProductTags) // 获取商品标签
productRoutes.GET("/stores", productHandler.GetStores) // 获取店铺列表
productRoutes.GET("/search", productHandler.SearchProducts) // 搜索商品
productRoutes.GET("/hot", productHandler.GetHotProducts) // 获取热门商品
productRoutes.GET("/recommend", productHandler.GetRecommendProducts) // 获取推荐商品
productRoutes.GET("/:id/reviews", productHandler.GetProductReviews) // 获取商品评价
productRoutes.GET("/:id/reviews/count", productHandler.GetProductReviewCount) // 获取商品评价统计
}
// 文件上传相关路由
uploadRoutes := api.Group("/upload", middleware.AuthMiddleware())
{
uploadRoutes.POST("/image", uploadHandler.UploadImage) // 上传单个图片
uploadRoutes.POST("/images", uploadHandler.BatchUploadImages) // 批量上传图片
uploadRoutes.POST("/file", uploadHandler.UploadFile) // 上传通用文件
}
// 购物车相关路由
cartGroup := api.Group("/cart", middleware.AuthMiddleware())
{
cartGroup.GET("", cartHandler.GetCart) // 获取购物车
cartGroup.POST("", cartHandler.AddToCart) // 添加到购物车
cartGroup.PUT("/:product_id", cartHandler.UpdateCartItem) // 更新购物车项
cartGroup.DELETE("/:product_id", cartHandler.RemoveFromCart) // 移除购物车项
cartGroup.DELETE("", cartHandler.ClearCart) // 清空购物车
cartGroup.GET("/count", cartHandler.GetCartCount) // 获取购物车商品数量
cartGroup.PUT("/:product_id/select", cartHandler.SelectCartItem) // 选择/取消选择购物车项
cartGroup.PUT("/select-all", cartHandler.SelectAllCartItems) // 全选/取消全选购物车
// 新增的购物车功能
cartGroup.POST("/batch", cartHandler.BatchAddToCart) // 批量添加到购物车
cartGroup.DELETE("/batch", cartHandler.BatchRemoveFromCart) // 批量从购物车移除
cartGroup.PUT("/batch", cartHandler.BatchUpdateCartItems) // 批量更新购物车项
cartGroup.GET("/details", cartHandler.GetCartWithDetails) // 获取购物车详细信息
cartGroup.GET("/validate", cartHandler.ValidateCartItems) // 验证购物车商品有效性
cartGroup.DELETE("/invalid", cartHandler.CleanInvalidCartItems) // 清理无效的购物车项
cartGroup.GET("/summary", cartHandler.GetCartSummary) // 获取购物车摘要信息
cartGroup.POST("/merge", cartHandler.MergeCart) // 合并购物车
}
// 支付相关路由
if paymentHandler != nil {
paymentRoutes := api.Group("/payment")
{
// 需要认证的支付路由
authPaymentRoutes := paymentRoutes.Group("", middleware.AuthMiddleware())
{
authPaymentRoutes.POST("/create", paymentHandler.CreatePayment) // 创建支付订单
authPaymentRoutes.GET("/query/:orderId", paymentHandler.QueryPayment) // 查询支付状态
authPaymentRoutes.POST("/cancel/:orderId", paymentHandler.CancelPayment) // 取消支付
}
// 微信支付回调(无需认证)
paymentRoutes.POST("/notify", paymentHandler.PaymentNotify) // 支付回调通知
// 测试接口(开发环境使用)
paymentRoutes.GET("/test/success", paymentHandler.TestPaymentSuccess) // 测试支付成功处理
}
}
// 订单相关路由
orderHandler := handler.NewOrderHandler(orderService, wechatPayService)
orderSettleHandler := handler.NewOrderSettleHandler(orderService, productService, userService)
orderRoutes := api.Group("/orders", middleware.AuthMiddleware())
{
orderRoutes.GET("", orderHandler.GetUserOrders) // 获取订单列表
orderRoutes.GET("/:id", orderHandler.GetOrderDetail) // 获取订单详情
orderRoutes.POST("", orderHandler.CreateOrder) // 创建订单
orderRoutes.POST("/settle", orderSettleHandler.SettleOrder) // 订单结算
orderRoutes.POST("/merge", orderHandler.MergeOrders) // 合并订单
orderRoutes.PUT("/:id/pay", orderHandler.PayOrder) // 支付订单
orderRoutes.GET("/:id/payment/status", orderHandler.GetPaymentStatus) // 获取支付状态
orderRoutes.PUT("/:id/cancel", orderHandler.CancelOrder) // 取消订单
orderRoutes.PUT("/:id/remind-ship", orderHandler.RemindShip) // 提醒发货
orderRoutes.PUT("/:id/receive", orderHandler.ConfirmReceive) // 确认收货
orderRoutes.PUT("/:id/refund", orderHandler.RefundOrder) // 申请退款
}
// 售后相关路由
afterSaleHandler := handler.NewAfterSaleHandler(afterSaleService)
afterSaleRoutes := api.Group("/after-sales", middleware.AuthMiddleware())
{
afterSaleRoutes.GET("", afterSaleHandler.GetAfterSaleList) // 获取售后列表
afterSaleRoutes.GET("/:id", afterSaleHandler.GetAfterSaleDetail) // 获取售后详情
afterSaleRoutes.POST("", afterSaleHandler.CreateAfterSale) // 创建售后申请
}
// 退款相关路由
refundRoutes := api.Group("/refunds", middleware.AuthMiddleware())
{
refundRoutes.POST("", refundHandler.CreateRefund) // 创建退款申请
refundRoutes.GET("/order/:order_id", refundHandler.GetRefundsByOrder) // 获取订单的退款记录
refundRoutes.GET("/user", refundHandler.GetUserRefunds) // 获取用户的退款记录
refundRoutes.GET("/:id", refundHandler.GetRefundDetail) // 获取退款详情
refundRoutes.POST("/:id/query", refundHandler.QueryRefundStatus) // 查询退款状态
}
// 评论相关路由
commentRoutes := api.Group("/comments")
{
// 公开路由(无需认证)
commentRoutes.GET("/high-rating", commentHandler.GetHighRatingComments) // 获取高分评论(首页展示)
commentRoutes.GET("/products/:product_id", commentHandler.GetProductComments) // 获取商品评论列表
commentRoutes.GET("/products/:product_id/stats", commentHandler.GetCommentStats) // 获取商品评论统计
commentRoutes.GET("/:id", commentHandler.GetCommentDetail) // 获取评论详情
// 需要认证的路由
authCommentRoutes := commentRoutes.Group("", middleware.AuthMiddleware())
{
authCommentRoutes.POST("", commentHandler.CreateComment) // 创建评论
authCommentRoutes.GET("/user", commentHandler.GetUserComments) // 获取用户评论列表
authCommentRoutes.POST("/:id/like", commentHandler.LikeComment) // 点赞评论
authCommentRoutes.DELETE("/:id/like", commentHandler.UnlikeComment) // 取消点赞评论
authCommentRoutes.POST("/replies", commentHandler.CreateReply) // 创建评论回复
authCommentRoutes.GET("/uncommented-items", commentHandler.GetUncommentedOrderItems) // 获取未评论的订单项
}
}
}
// 管理员登录路由(不需要认证)
adminHandler := handler.NewAdminHandler(adminService)
adminProductHandler := handler.NewAdminProductHandler(productService)
adminOrderHandler := handler.NewAdminOrderHandler(orderService)
adminRefundHandler := handler.NewAdminRefundHandler(refundService, orderService)
r.POST("/admin/api/v1/login", adminHandler.Login) // 管理员登录
// 管理员路由组
admin := r.Group("/admin/api/v1", middleware.AdminAuthMiddleware())
{
// 认证相关路由
admin.POST("/logout", adminHandler.Logout) // 管理员退出登录
// 管理员管理相关路由
admin.POST("/admins", adminHandler.CreateAdmin) // 创建管理员
admin.GET("/admins", adminHandler.GetAdminList) // 获取管理员列表
admin.GET("/admins/:id", adminHandler.GetAdminDetail) // 获取管理员详情
admin.PUT("/admins/:id", adminHandler.UpdateAdmin) // 更新管理员
admin.DELETE("/admins/:id", adminHandler.DeleteAdmin) // 删除管理员
admin.PUT("/admins/:id/password", adminHandler.ChangePassword) // 修改密码
admin.GET("/profile", adminHandler.GetProfile) // 获取个人信息
admin.PUT("/profile", adminHandler.UpdateProfile) // 更新个人信息
// 商品管理
productGroup := admin.Group("/products")
{
productGroup.GET("", adminProductHandler.GetProductList)
productGroup.GET("/:id", adminProductHandler.GetProductDetail)
productGroup.POST("", adminProductHandler.CreateProduct)
productGroup.PUT("/:id", adminProductHandler.UpdateProduct)
productGroup.DELETE("/:id", adminProductHandler.DeleteProduct)
productGroup.PUT("/:id/stock", adminProductHandler.UpdateStock)
productGroup.PUT("/batch/status", adminProductHandler.BatchUpdateStatus)
productGroup.GET("/:id/skus", adminProductHandler.GetProductSKUs)
productGroup.GET("/:id/reviews", adminProductHandler.GetProductReviews)
// 新增的商品管理API
productGroup.PUT("/batch/status-new", adminProductHandler.BatchUpdateProductStatus)
productGroup.PUT("/batch/price", adminProductHandler.BatchUpdateProductPrice)
productGroup.DELETE("/batch", adminProductHandler.BatchDeleteProducts)
// SKU管理
productGroup.POST("/skus", adminProductHandler.CreateProductSKU)
productGroup.PUT("/skus/:id", adminProductHandler.UpdateProductSKU)
productGroup.DELETE("/skus/:id", adminProductHandler.DeleteProductSKU)
// 图片管理
productGroup.GET("/:id/images", adminProductHandler.GetProductImages)
productGroup.POST("/images", adminProductHandler.CreateProductImage)
productGroup.PUT("/images/:id/sort", adminProductHandler.UpdateProductImageSort)
productGroup.DELETE("/images/:id", adminProductHandler.DeleteProductImage)
// 规格管理
productGroup.GET("/:id/specs", adminProductHandler.GetProductSpecs)
productGroup.POST("/specs", adminProductHandler.CreateProductSpec)
productGroup.PUT("/specs/:id", adminProductHandler.UpdateProductSpec)
productGroup.DELETE("/specs/:id", adminProductHandler.DeleteProductSpec)
// 标签管理
productGroup.POST("/tags", adminProductHandler.CreateProductTag)
productGroup.PUT("/tags/:id", adminProductHandler.UpdateProductTag)
productGroup.DELETE("/tags/:id", adminProductHandler.DeleteProductTag)
productGroup.PUT("/:id/tags", adminProductHandler.AssignTagsToProduct)
// 库存管理
productGroup.GET("/low-stock", adminProductHandler.GetLowStockProducts)
productGroup.GET("/inventory/statistics", adminProductHandler.GetInventoryStatistics)
productGroup.PUT("/:id/sync-stock", adminProductHandler.SyncProductStock)
productGroup.PUT("/sync-all-stock", adminProductHandler.SyncAllProductsStock)
// 导入导出
productGroup.GET("/export", adminProductHandler.ExportProducts)
productGroup.POST("/import", adminProductHandler.ImportProducts)
// 划线价管理
productGroup.PUT("/:id/orig-price", adminProductHandler.UpdateProductOrigPrice) // 更新单个商品划线价
productGroup.PUT("/batch/orig-price", adminProductHandler.BatchUpdateOrigPrice) // 批量更新SPU划线价
productGroup.PUT("/skus/batch/orig-price", adminProductHandler.BatchUpdateSKUOrigPrice) // 批量更新SKU划线价
}
// 分类管理
categoryAdmin := admin.Group("/categories")
{
categoryAdmin.GET("", adminProductHandler.GetCategories) // 获取分类列表
categoryAdmin.POST("", adminProductHandler.CreateCategory) // 创建分类
categoryAdmin.PUT("/:id", adminProductHandler.UpdateCategory) // 更新分类
categoryAdmin.DELETE("/:id", adminProductHandler.DeleteCategory) // 删除分类
}
// 平台管理
platformHandler := handler.NewPlatformHandler(platformService)
platformAdmin := admin.Group("/platforms")
{
platformAdmin.GET("", platformHandler.GetPlatforms) // 获取平台列表
platformAdmin.GET("/all/active", platformHandler.GetAllActivePlatforms) // 获取所有启用平台(用于下拉选择)
platformAdmin.GET("/:id", platformHandler.GetPlatform) // 获取平台详情
platformAdmin.POST("", platformHandler.CreatePlatform) // 创建平台
platformAdmin.PUT("/:id", platformHandler.UpdatePlatform) // 更新平台
platformAdmin.DELETE("/:id", platformHandler.DeletePlatform) // 删除平台
}
// 店铺管理
admin.GET("/stores", adminProductHandler.GetStores) // 获取店铺列表
// 订单管理
adminOrderGroup := admin.Group("/orders")
{
adminOrderGroup.GET("", adminOrderHandler.GetOrderList)
adminOrderGroup.GET("/:id", adminOrderHandler.GetOrderDetail)
adminOrderGroup.PUT("/:id/status", adminOrderHandler.UpdateOrderStatus)
adminOrderGroup.POST("/:id/ship", adminOrderHandler.ShipOrder)
adminOrderGroup.POST("/:id/refund", adminOrderHandler.RefundOrder)
adminOrderGroup.POST("/:id/cancel", adminOrderHandler.CancelOrder)
adminOrderGroup.POST("/batch", adminOrderHandler.BatchProcessOrders)
adminOrderGroup.GET("/export", adminOrderHandler.ExportOrders)
// 新增的订单管理API
adminOrderGroup.GET("/logistics/:orderNo", adminOrderHandler.GetLogisticsInfo)
adminOrderGroup.PUT("/:id/status-new", adminOrderHandler.UpdateOrderStatusNew)
adminOrderGroup.POST("/batch-status", adminOrderHandler.BatchUpdateOrderStatus)
adminOrderGroup.POST("/:id/process-refund", adminOrderHandler.ProcessRefund)
adminOrderGroup.GET("/statistics", adminOrderHandler.GetOrderStatistics)
adminOrderGroup.GET("/statistics/daily", adminOrderHandler.GetDailyOrderStatistics)
adminOrderGroup.GET("/date-range", adminOrderHandler.GetOrdersByDateRange)
adminOrderGroup.GET("/export-excel", adminOrderHandler.ExportOrdersToExcel)
adminOrderGroup.GET("/trends", adminOrderHandler.GetOrderTrends)
adminOrderGroup.GET("/pending-count", adminOrderHandler.GetPendingOrdersCount)
adminOrderGroup.GET("/summary", adminOrderHandler.GetOrderSummary)
}
// 退款管理
adminRefundGroup := admin.Group("/refunds")
{
adminRefundGroup.GET("", adminRefundHandler.GetAllRefunds) // 获取所有退款记录
adminRefundGroup.GET("/pending", adminRefundHandler.GetPendingRefunds) // 获取待处理退款
adminRefundGroup.GET("/:id", adminRefundHandler.GetRefundDetail) // 获取退款详情
adminRefundGroup.POST("/:id/process", adminRefundHandler.ProcessRefund) // 处理退款申请
adminRefundGroup.POST("/:id/reject", adminRefundHandler.RejectRefund) // 拒绝退款申请
adminRefundGroup.GET("/:id/logs", adminRefundHandler.GetRefundLogs) // 获取退款日志
adminRefundGroup.POST("/:id/query", adminRefundHandler.QueryRefundStatus) // 查询退款状态
adminRefundGroup.GET("/statistics", adminRefundHandler.GetRefundStatistics) // 获取退款统计
adminRefundGroup.POST("/sync-status", refundHandler.SyncRefundAndOrderStatus) // 同步退款状态和订单状态
}
// 用户管理
adminUserHandler := handler.NewAdminUserHandler(userService, orderService)
userAdmin := admin.Group("/users")
{
userAdmin.GET("", adminUserHandler.GetUserList) // 获取用户列表
userAdmin.GET("/:id", adminUserHandler.GetUserDetail) // 获取用户详情
userAdmin.PUT("/:id/status", adminUserHandler.UpdateUserStatus) // 更新用户状态
userAdmin.PUT("/batch/status", adminUserHandler.BatchUpdateUserStatus) // 批量更新用户状态
userAdmin.PUT("/:id/profile", adminUserHandler.UpdateUserProfile) // 更新用户资料
userAdmin.GET("/:id/addresses", adminUserHandler.GetUserAddresses) // 获取用户地址
userAdmin.GET("/:id/favorites", adminUserHandler.GetUserFavorites) // 获取用户收藏
userAdmin.GET("/:id/orders", adminUserHandler.GetUserOrders) // 获取用户订单
userAdmin.GET("/:id/order-stats", adminUserHandler.GetUserOrderStatistics) // 获取用户订单统计
userAdmin.GET("/:id/level", adminUserHandler.GetUserLevelInfo) // 获取用户等级信息
userAdmin.PUT("/:id/level", adminUserHandler.UpdateUserLevel) // 更新用户等级
userAdmin.PUT("/:id/password", adminUserHandler.ResetUserPassword) // 重置用户密码
userAdmin.DELETE("/:id", adminUserHandler.DeleteUser) // 删除用户
userAdmin.DELETE("/batch", adminUserHandler.BatchDeleteUsers) // 批量删除用户
userAdmin.GET("/export", adminUserHandler.ExportUsers) // 导出用户数据
userAdmin.GET("/:id/login-logs", adminUserHandler.GetUserLoginLogs) // 获取用户登录日志
}
// 评论管理
commentAdmin := admin.Group("/comments")
{
commentAdmin.GET("", commentHandler.GetCommentListForAdmin) // 获取评论列表
commentAdmin.GET("/products/:product_id/stats", commentHandler.GetCommentStats) // 获取商品评论统计
commentAdmin.GET("/:id", commentHandler.GetCommentDetail) // 获取评论详情
commentAdmin.PUT("/:id/status", commentHandler.UpdateCommentStatus) // 更新评论状态
commentAdmin.DELETE("/:id", commentHandler.DeleteComment) // 删除评论
commentAdmin.POST("/replies", commentHandler.CreateAdminReply) // 管理员回复评论
commentAdmin.POST("/:id/reply", commentHandler.CreateAdminReplyForComment) // 管理员回复指定评论
}
// 轮播图管理
adminBannerHandler := handler.NewAdminBannerHandler(bannerService)
bannerAdmin := admin.Group("/banners")
{
bannerAdmin.GET("", adminBannerHandler.GetBannerList) // 获取轮播图列表
bannerAdmin.GET("/:id", adminBannerHandler.GetBannerDetail) // 获取轮播图详情
bannerAdmin.POST("", adminBannerHandler.CreateBanner) // 创建轮播图
bannerAdmin.PUT("/:id", adminBannerHandler.UpdateBanner) // 更新轮播图
bannerAdmin.DELETE("/:id", adminBannerHandler.DeleteBanner) // 删除轮播图
bannerAdmin.DELETE("/batch", adminBannerHandler.BatchDeleteBanners) // 批量删除轮播图
bannerAdmin.PUT("/:id/status", adminBannerHandler.UpdateBannerStatus) // 更新轮播图状态
bannerAdmin.PUT("/batch/status", adminBannerHandler.BatchUpdateBannerStatus) // 批量更新轮播图状态
bannerAdmin.PUT("/:id/sort", adminBannerHandler.UpdateBannerSort) // 更新轮播图排序
bannerAdmin.PUT("/batch/sort", adminBannerHandler.BatchUpdateBannerSort) // 批量更新轮播图排序
bannerAdmin.GET("/date-range", adminBannerHandler.GetBannersByDateRange) // 按日期范围获取轮播图
bannerAdmin.GET("/status/:status", adminBannerHandler.GetBannersByStatus) // 按状态获取轮播图
bannerAdmin.GET("/statistics", adminBannerHandler.GetBannerStatistics) // 获取轮播图统计
bannerAdmin.POST("/clean-expired", adminBannerHandler.CleanExpiredBanners) // 清理过期轮播图
}
// 直播投流源管理
adminLiveStreamHandler := handler.NewLiveStreamHandler(liveStreamService)
liveStreamAdmin := admin.Group("/livestreams")
{
liveStreamAdmin.GET("", adminLiveStreamHandler.GetLiveStreamList) // 获取投流源列表
liveStreamAdmin.GET("/:id", adminLiveStreamHandler.GetLiveStreamDetail) // 获取投流源详情
liveStreamAdmin.POST("", adminLiveStreamHandler.CreateLiveStream) // 创建投流源
liveStreamAdmin.PUT("/:id", adminLiveStreamHandler.UpdateLiveStream) // 更新投流源
liveStreamAdmin.DELETE("/:id", adminLiveStreamHandler.DeleteLiveStream) // 删除投流源
liveStreamAdmin.DELETE("/batch", adminLiveStreamHandler.BatchDeleteLiveStreams) // 批量删除投流源
liveStreamAdmin.PUT("/:id/status", adminLiveStreamHandler.UpdateLiveStreamStatus) // 更新投流源状态
}
// 文件上传管理(管理员专用)
uploadAdmin := admin.Group("/upload")
{
uploadAdmin.POST("/image", uploadHandler.UploadImage) // 上传单个图片
uploadAdmin.POST("/images", uploadHandler.BatchUploadImages) // 批量上传图片
uploadAdmin.POST("/file", uploadHandler.UploadFile) // 上传通用文件
}
// 积分管理
pointsAdmin := admin.Group("/points")
{
pointsAdmin.POST("/users/:id/add", pointsHandler.AddPoints) // 给用户增加积分
pointsAdmin.POST("/users/:id/deduct", pointsHandler.DeductPoints) // 扣除用户积分
}
// 优惠券管理
adminCouponHandler := handler.NewAdminCouponHandler(couponService)
couponAdmin := admin.Group("/coupons")
{
couponAdmin.GET("", adminCouponHandler.GetCouponList) // 获取优惠券列表
couponAdmin.GET("/:id", adminCouponHandler.GetCouponDetail) // 获取优惠券详情
couponAdmin.POST("", adminCouponHandler.CreateCoupon) // 创建优惠券
couponAdmin.PUT("/:id", adminCouponHandler.UpdateCoupon) // 更新优惠券
couponAdmin.DELETE("/:id", adminCouponHandler.DeleteCoupon) // 删除优惠券
couponAdmin.PUT("/:id/status", adminCouponHandler.UpdateCouponStatus) // 更新优惠券状态
couponAdmin.DELETE("/batch", adminCouponHandler.BatchDeleteCoupons) // 批量删除优惠券
couponAdmin.GET("/statistics", adminCouponHandler.GetCouponStatistics) // 获取优惠券统计
couponAdmin.GET("/user-coupons", adminCouponHandler.GetUserCouponList) // 获取用户优惠券列表
couponAdmin.POST("/distribute", adminCouponHandler.DistributeCoupon) // 发放优惠券
couponAdmin.GET("/distribute/history", adminCouponHandler.GetDistributeHistory) // 获取发放历史
}
// 角色权限管理
adminRoleHandler := handler.NewAdminRoleHandler(db, roleService)
roleAdmin := admin.Group("/roles")
{
roleAdmin.GET("", adminRoleHandler.GetRoles) // 获取角色列表
roleAdmin.GET("/:id", adminRoleHandler.GetRole) // 获取角色详情
roleAdmin.POST("", adminRoleHandler.CreateRole) // 创建角色
roleAdmin.PUT("/:id", adminRoleHandler.UpdateRole) // 更新角色
roleAdmin.DELETE("/:id", adminRoleHandler.DeleteRole) // 删除角色
roleAdmin.GET("/:id/permissions", adminRoleHandler.GetRolePermissions) // 获取角色权限
roleAdmin.PUT("/:id/permissions", adminRoleHandler.AssignPermissionsToRole) // 分配权限给角色
}
// 权限管理
permissionAdmin := admin.Group("/permissions")
{
permissionAdmin.GET("", adminRoleHandler.GetPermissions) // 获取权限列表
permissionAdmin.POST("", adminRoleHandler.CreatePermission) // 创建权限
permissionAdmin.PUT("/:id", adminRoleHandler.UpdatePermission) // 更新权限
permissionAdmin.DELETE("/:id", adminRoleHandler.DeletePermission) // 删除权限
}
// 用户角色管理
userRoleAdmin := admin.Group("/user-roles")
{
userRoleAdmin.GET("/:id/roles", adminRoleHandler.GetUserRoles) // 获取用户角色
userRoleAdmin.PUT("/:id/roles", adminRoleHandler.AssignRolesToUser) // 分配角色给用户
userRoleAdmin.GET("/:id/permissions", adminRoleHandler.GetUserPermissions) // 获取用户权限
}
// 系统初始化
systemAdmin := admin.Group("/system")
{
systemAdmin.POST("/init-roles", adminRoleHandler.InitializeDefaultRoles) // 初始化默认角色权限
}
// 日志管理
adminLogHandler := handler.NewAdminLogHandler(db, logService)
logAdmin := admin.Group("/logs")
{
logAdmin.GET("/login", adminLogHandler.GetLoginLogs) // 获取登录日志
logAdmin.GET("/operation", adminLogHandler.GetOperationLogs) // 获取操作日志
logAdmin.GET("/login/stats", adminLogHandler.GetLoginStatistics) // 获取登录统计
logAdmin.GET("/operation/stats", adminLogHandler.GetOperationStatistics) // 获取操作统计
logAdmin.GET("/summary", adminLogHandler.GetLogSummary) // 获取日志概览
logAdmin.GET("/:type/:id", adminLogHandler.GetLogDetail) // 获取日志详情
logAdmin.GET("/export", adminLogHandler.ExportLogs) // 导出日志
logAdmin.DELETE("/clean", adminLogHandler.CleanOldLogs) // 清理旧日志
}
// 数据统计
adminStatsHandler := handler.NewAdminStatsHandler(orderService, userService, productService, refundService)
statsAdmin := admin.Group("/stats")
{
statsAdmin.GET("/dashboard", adminStatsHandler.GetDashboardStats) // 获取仪表盘统计
statsAdmin.GET("/orders", adminOrderHandler.GetOrderStatistics) // 获取订单统计
statsAdmin.GET("/daily", adminOrderHandler.GetDailyOrderStatistics) // 获取每日订单统计
statsAdmin.GET("/users", adminUserHandler.GetUserStatistics) // 获取用户统计
statsAdmin.GET("/order-trend", adminStatsHandler.GetOrderTrend) // 获取订单趋势
statsAdmin.GET("/user-trend", adminStatsHandler.GetUserTrend) // 获取用户增长趋势
statsAdmin.GET("/sales-ranking", adminStatsHandler.GetSalesRanking) // 获取销售排行
statsAdmin.GET("/region", adminStatsHandler.GetRegionStats) // 获取地区统计
statsAdmin.GET("/payment", adminStatsHandler.GetPaymentStats) // 获取支付方式统计
statsAdmin.GET("/refund", adminStatsHandler.GetRefundStats) // 获取退款统计
statsAdmin.GET("/realtime", adminStatsHandler.GetRealTimeStats) // 获取实时统计
// 新增的用户统计API
statsAdmin.GET("/user-growth-trend", adminStatsHandler.GetUserGrowthTrend) // 获取用户增长趋势
statsAdmin.GET("/user-activity-analysis", adminStatsHandler.GetUserActivityAnalysis) // 获取用户活跃度分析
statsAdmin.GET("/user-retention-rate", adminStatsHandler.GetUserRetentionRate) // 获取用户留存率
statsAdmin.GET("/user-level-distribution", adminStatsHandler.GetUserLevelDistribution) // 获取用户等级分布
statsAdmin.GET("/user-geographic-distribution", adminStatsHandler.GetUserGeographicDistribution) // 获取用户地理分布
statsAdmin.GET("/user-age-distribution", adminStatsHandler.GetUserAgeDistribution) // 获取用户年龄分布
statsAdmin.GET("/user-engagement-metrics", adminStatsHandler.GetUserEngagementMetrics) // 获取用户参与度指标
}
}
return r
}