|
|
|
@ -265,6 +265,17 @@ class QuestionResponse(BaseModel): |
|
|
|
|
@router.post("/get_question", summary="获取开启的前4个问题") |
|
|
|
|
async def get_question(request: Request, req: AllScenicFlowRequest): |
|
|
|
|
|
|
|
|
|
# Redis 缓存查询 |
|
|
|
|
cache_key = "quick_questions" |
|
|
|
|
try: |
|
|
|
|
redis_client = request.app.state.redis_client |
|
|
|
|
cached = await redis_client.get(cache_key) |
|
|
|
|
if cached: |
|
|
|
|
# 缓存命中,直接返回缓存数据 |
|
|
|
|
return json.loads(cached) |
|
|
|
|
except Exception as e: |
|
|
|
|
print(f"[Redis] 查询缓存失败: {e}") |
|
|
|
|
|
|
|
|
|
# 验签逻辑 |
|
|
|
|
if not req.sign: |
|
|
|
|
raise HTTPException(status_code=401, detail="缺少签名参数") |
|
|
|
@ -275,11 +286,20 @@ async def get_question(request: Request, req: AllScenicFlowRequest): |
|
|
|
|
try: |
|
|
|
|
# 查询状态为正常(0)的问题,按order_num正序排序,取前4条 |
|
|
|
|
questions = await QuickQuestion.filter(status="0").order_by("order_num").limit(4).values("title","subtitle","logo","label") |
|
|
|
|
return { |
|
|
|
|
|
|
|
|
|
result = { |
|
|
|
|
"code": 200, |
|
|
|
|
"message": "查询成功", |
|
|
|
|
"data": questions |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
# 将结果存入Redis缓存,过期时间10分钟 |
|
|
|
|
try: |
|
|
|
|
await redis_client.setex(cache_key, 600, json.dumps(result)) |
|
|
|
|
except Exception as e: |
|
|
|
|
print(f"[Redis] 写缓存失败: {e}") |
|
|
|
|
|
|
|
|
|
return result |
|
|
|
|
except Exception as e: |
|
|
|
|
print(f"查询快捷问题失败: {e}") |
|
|
|
|
return { |
|
|
|
@ -301,6 +321,17 @@ class HotQuestionResponse(BaseModel): |
|
|
|
|
|
|
|
|
|
@router.post("/get_hot_questions", summary="获取热门问题top10") |
|
|
|
|
async def get_hot_questions(request: Request, req: AllScenicFlowRequest): |
|
|
|
|
# Redis 缓存查询 |
|
|
|
|
cache_key = "hot_questions" |
|
|
|
|
try: |
|
|
|
|
redis_client = request.app.state.redis_client |
|
|
|
|
cached = await redis_client.get(cache_key) |
|
|
|
|
if cached: |
|
|
|
|
# 缓存命中,直接返回缓存数据 |
|
|
|
|
return json.loads(cached) |
|
|
|
|
except Exception as e: |
|
|
|
|
print(f"[Redis] 查询缓存失败: {e}") |
|
|
|
|
|
|
|
|
|
# 验签逻辑 |
|
|
|
|
if not req.sign: |
|
|
|
|
raise HTTPException(status_code=401, detail="缺少签名参数") |
|
|
|
@ -323,11 +354,19 @@ async def get_hot_questions(request: Request, req: AllScenicFlowRequest): |
|
|
|
|
for q in hot_questions: |
|
|
|
|
q["update_time"] = q["update_time"].strftime("%Y-%m-%d %H:%M:%S") |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
result = { |
|
|
|
|
"code": 200, |
|
|
|
|
"message": "查询成功", |
|
|
|
|
"data": hot_questions |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
# 将结果存入Redis缓存,过期时间10分钟 |
|
|
|
|
try: |
|
|
|
|
await redis_client.setex(cache_key, 600, json.dumps(result)) |
|
|
|
|
except Exception as e: |
|
|
|
|
print(f"[Redis] 写缓存失败: {e}") |
|
|
|
|
|
|
|
|
|
return result |
|
|
|
|
except Exception as e: |
|
|
|
|
print(f"查询热门问题失败: {e}") |
|
|
|
|
return { |
|
|
|
|