个股论坛重做
This commit is contained in:
Binary file not shown.
@@ -8,17 +8,17 @@ import uuid
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from flask import Blueprint, request, jsonify, session, g
|
from flask import Blueprint, request, jsonify, session, g, current_app
|
||||||
from elasticsearch import Elasticsearch
|
from elasticsearch import Elasticsearch
|
||||||
from sqlalchemy import create_engine, text
|
from sqlalchemy import text
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# Blueprint 和数据库连接
|
# Blueprint 定义
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
community_bp = Blueprint('community', __name__, url_prefix='/api/community')
|
community_bp = Blueprint('community', __name__, url_prefix='/api/community')
|
||||||
|
|
||||||
# ES 客户端(与 app.py 共享配置)
|
# ES 客户端
|
||||||
es_client = Elasticsearch(
|
es_client = Elasticsearch(
|
||||||
hosts=["http://222.128.1.157:19200"],
|
hosts=["http://222.128.1.157:19200"],
|
||||||
request_timeout=30,
|
request_timeout=30,
|
||||||
@@ -26,9 +26,20 @@ es_client = Elasticsearch(
|
|||||||
retry_on_timeout=True
|
retry_on_timeout=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# MySQL 连接(与 app.py 共享配置)
|
# 注意:数据库连接从 app.py 的 db 对象获取,不再独立创建
|
||||||
DATABASE_URL = "mysql+pymysql://root:wangzhe66@222.128.1.157:3307/stock_analysis?charset=utf8mb4"
|
# engine 变量会在 Blueprint 注册后通过 app context 获取
|
||||||
engine = create_engine(DATABASE_URL, pool_pre_ping=True, pool_recycle=3600)
|
|
||||||
|
engine = None # 将在第一次请求时初始化
|
||||||
|
|
||||||
|
def get_db_engine():
|
||||||
|
"""获取数据库引擎(延迟初始化)"""
|
||||||
|
global engine
|
||||||
|
if engine is None:
|
||||||
|
from flask import current_app
|
||||||
|
# 从 app.py 的 SQLAlchemy 实例获取 engine
|
||||||
|
from app import db
|
||||||
|
engine = db.engine
|
||||||
|
return engine
|
||||||
|
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@@ -92,7 +103,7 @@ def get_channels():
|
|||||||
返回格式:[{ id, name, icon, channels: [...] }, ...]
|
返回格式:[{ id, name, icon, channels: [...] }, ...]
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
with engine.connect() as conn:
|
with get_db_engine().connect() as conn:
|
||||||
# 查询分类
|
# 查询分类
|
||||||
categories_sql = text("""
|
categories_sql = text("""
|
||||||
SELECT id, name, icon, position, is_collapsible, is_system
|
SELECT id, name, icon, position, is_collapsible, is_system
|
||||||
@@ -163,7 +174,7 @@ def get_channels():
|
|||||||
def get_channel(channel_id):
|
def get_channel(channel_id):
|
||||||
"""获取单个频道详情"""
|
"""获取单个频道详情"""
|
||||||
try:
|
try:
|
||||||
with engine.connect() as conn:
|
with get_db_engine().connect() as conn:
|
||||||
sql = text("""
|
sql = text("""
|
||||||
SELECT
|
SELECT
|
||||||
c.*, cc.concept_name, cc.stock_count, cc.is_hot
|
c.*, cc.concept_name, cc.stock_count, cc.is_hot
|
||||||
@@ -204,7 +215,7 @@ def subscribe_channel(channel_id):
|
|||||||
user = g.current_user
|
user = g.current_user
|
||||||
subscription_id = generate_id()
|
subscription_id = generate_id()
|
||||||
|
|
||||||
with engine.connect() as conn:
|
with get_db_engine().connect() as conn:
|
||||||
# 检查是否已订阅
|
# 检查是否已订阅
|
||||||
check_sql = text("""
|
check_sql = text("""
|
||||||
SELECT id FROM community_subscriptions
|
SELECT id FROM community_subscriptions
|
||||||
@@ -251,7 +262,7 @@ def unsubscribe_channel(channel_id):
|
|||||||
try:
|
try:
|
||||||
user = g.current_user
|
user = g.current_user
|
||||||
|
|
||||||
with engine.connect() as conn:
|
with get_db_engine().connect() as conn:
|
||||||
# 删除订阅
|
# 删除订阅
|
||||||
delete_sql = text("""
|
delete_sql = text("""
|
||||||
DELETE FROM community_subscriptions
|
DELETE FROM community_subscriptions
|
||||||
@@ -329,7 +340,7 @@ def send_message(channel_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 更新频道最后消息时间和消息数
|
# 更新频道最后消息时间和消息数
|
||||||
with engine.connect() as conn:
|
with get_db_engine().connect() as conn:
|
||||||
update_sql = text("""
|
update_sql = text("""
|
||||||
UPDATE community_channels
|
UPDATE community_channels
|
||||||
SET message_count = message_count + 1,
|
SET message_count = message_count + 1,
|
||||||
@@ -503,7 +514,7 @@ def create_post(channel_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 更新频道消息数
|
# 更新频道消息数
|
||||||
with engine.connect() as conn:
|
with get_db_engine().connect() as conn:
|
||||||
update_sql = text("""
|
update_sql = text("""
|
||||||
UPDATE community_channels
|
UPDATE community_channels
|
||||||
SET message_count = message_count + 1,
|
SET message_count = message_count + 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user