from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.api.endpoints import wechat,user, address, community, station, order, coupon, community_building, upload, merchant, merchant_product, merchant_order, point, config, merchant_category, log, account,merchant_pay_order, message, bank_card, withdraw, mp, point_product, point_product_order, coupon_activity, dashboard, wecom, feedback, timeperiod, community_timeperiod, order_additional_fee, ai, community_set, community_set_mapping, community_profit_sharing, partner from app.models.database import Base, engine from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse from app.core.response import error_response from fastapi import HTTPException from app.middleware.request_logger import RequestLoggerMiddleware from app.core.response import CustomJSONResponse import logging from app.core.config import settings from app.core.wecombot import WecomBot from app.api.endpoints import wecom from app.api.endpoints import feedback from starlette.middleware.sessions import SessionMiddleware import os # 创建数据库表 Base.metadata.create_all(bind=engine) app = FastAPI( title="Beefast 蜂快到家", description="API 文档", version="1.0.0", docs_url="/docs" if settings.DEBUG else None ) app.default_response_class = CustomJSONResponse # 配置 CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 添加请求日志中间件 app.add_middleware(RequestLoggerMiddleware) app.add_middleware(SessionMiddleware, secret_key=settings.SECRET_KEY) # 添加用户路由 app.include_router(ai.router, prefix="/api/ai", tags=["AI服务"]) app.include_router(dashboard.router, prefix="/api/dashboard", tags=["仪表盘"]) app.include_router(wechat.router,prefix="/api/wechat",tags=["微信"]) app.include_router(mp.router, prefix="/api/mp", tags=["微信公众号"]) app.include_router(wecom.router, prefix="/api/wecom", tags=["企业微信"]) app.include_router(partner.router, prefix="/api/partner", tags=["合伙人"]) app.include_router(user.router, prefix="/api/user", tags=["用户"]) app.include_router(bank_card.router, prefix="/api/bank-cards", tags=["用户银行卡"]) app.include_router(withdraw.router, prefix="/api/withdraw", tags=["提现"]) app.include_router(point.router, prefix="/api/point", tags=["用户积分"]) app.include_router(point_product.router, prefix="/api/point-products", tags=["积分商品"]) app.include_router(point_product_order.router, prefix="/api/point-product-orders", tags=["积分商品订单"]) app.include_router(account.router, prefix="/api/account", tags=["账户"]) app.include_router(address.router, prefix="/api/address", tags=["配送地址"]) app.include_router(community.router, prefix="/api/community", tags=["社区"]) app.include_router(community_set.router, prefix="/api/community-sets", tags=["社区集合"]) app.include_router(community_set_mapping.router, prefix="/api/community-set-mappings", tags=["社区集合映射"]) app.include_router(community_profit_sharing.router, prefix="/api/community-profit-sharings", tags=["社区分润"]) app.include_router(timeperiod.router, prefix="/api/time-periods", tags=["配送时段"]) app.include_router(community_timeperiod.router, prefix="/api/community-time-periods", tags=["社区配送时段"]) app.include_router(community_building.router, prefix="/api/community/building", tags=["社区楼栋"]) app.include_router(station.router, prefix="/api/station", tags=["驿站"]) app.include_router(order.router, prefix="/api/order", tags=["订单"]) app.include_router(order_additional_fee.router, prefix="/api/order-additional-fee", tags=["订单加价"]) app.include_router(coupon.router, prefix="/api/coupon", tags=["抵扣券"]) app.include_router(coupon_activity.router, prefix="/api/coupon-activities", tags=["抵扣券活动"]) app.include_router(merchant.router, prefix="/api/merchant", tags=["商家"]) app.include_router(merchant_category.router, prefix="/api/merchant-categories", tags=["商家分类"]) app.include_router(merchant_product.router, prefix="/api/merchant/product", tags=["商家产品"]) app.include_router(merchant_order.router, prefix="/api/merchant/order", tags=["商家订单"]) app.include_router(merchant_pay_order.router,prefix="/api/merchant-pay",tags=["商家在线买单"]) app.include_router(message.router, prefix="/api/message", tags=["消息中心"]) app.include_router(upload.router, prefix="/api/upload", tags=["文件上传"]) app.include_router(config.router, prefix="/api/config", tags=["系统配置"]) app.include_router(log.router, prefix="/api/logs", tags=["系统日志"]) app.include_router(feedback.router, prefix="/api/feedback", tags=["反馈"]) @app.get("/") async def root(): return {"message": "欢迎使用 Beefast 蜂快到家 API"} @app.exception_handler(Exception) async def exception_handler(request, exc): env = "测试环境" if settings.DEBUG else "生产环境" logging.exception(f"【{env}】API异常: {str(exc)}") # 发送企业微信消息 wecom_bot = WecomBot(settings.URL_WECOMBOT_SYS_EXCEPTION) env = os.getenv("APP_ENV", "dev").lower() exception_log = f"""{env}环境**API异常** **请求信息** > 请求方法:{request.method} > 请求URL:{request.url} > 请求体:{request.body} **异常信息** {str(exc)} """ await wecom_bot.send_markdown(exception_log) return CustomJSONResponse( status_code=500, content={ "code": 500, "message": str(exc) } )