""" Flask应用工厂 """ from flask import Flask from flask_mail import Mail from config.config import Config from config.database import db import re # 初始化邮件服务 mail = Mail() def create_app(config_name='default'): app = Flask(__name__) # 加载配置 app.config.from_object(Config) # 初始化数据库 db.init_app(app) # 初始化邮件服务 mail.init_app(app) # 注册自定义过滤器 register_filters(app) # 注册蓝图 register_blueprints(app) # 创建数据库表 with app.app_context(): try: db.create_all() print("✅ 数据库表创建/同步成功") except Exception as e: print(f"❌ 数据库表创建失败: {str(e)}") return app def register_filters(app): """注册自定义过滤器""" @app.template_filter('nl2br') def nl2br_filter(text): """将换行符转换为HTML
标签""" if not text: return '' # 将换行符替换为
标签 return text.replace('\n', '
') @app.template_filter('truncate_chars') def truncate_chars_filter(text, length=50): """截断字符串""" if not text: return '' if len(text) <= length: return text return text[:length] + '...' def register_blueprints(app): """注册蓝图""" from app.views.main import main_bp from app.views.auth import auth_bp from app.views.user import user_bp from app.views.admin import admin_bp from app.views.product import product_bp from app.views.cart import cart_bp from app.views.address import address_bp from app.views.order import order_bp from app.views.payment import payment_bp app.register_blueprint(main_bp) app.register_blueprint(auth_bp) app.register_blueprint(user_bp) app.register_blueprint(admin_bp) app.register_blueprint(product_bp) app.register_blueprint(cart_bp) app.register_blueprint(address_bp) app.register_blueprint(order_bp) app.register_blueprint(payment_bp) # 修复:正确注册upload蓝图并设置URL前缀 try: from app.views.upload import upload_bp app.register_blueprint(upload_bp, url_prefix='/upload') # 添加URL前缀 print("✅ 上传功能蓝图注册成功") except ImportError as e: print(f"⚠️ 上传功能暂时不可用: {str(e)}") print("✅ 商品管理蓝图注册成功") print("✅ 购物车蓝图注册成功")