优化get_my_published_apps
This commit is contained in:
parent
3784da1eb7
commit
26925ee793
@ -1036,111 +1036,45 @@ async def get_my_published_apps(
|
|||||||
sort_by: Optional[str] = None
|
sort_by: Optional[str] = None
|
||||||
):
|
):
|
||||||
"""获取当前用户已发布的应用列表,支持搜索、分页和排序"""
|
"""获取当前用户已发布的应用列表,支持搜索、分页和排序"""
|
||||||
try:
|
session_cookie = request.cookies.get('sid')
|
||||||
# 获取当前登录用户
|
if not session_cookie:
|
||||||
session_cookie = request.cookies.get('sid')
|
raise HTTPException(status_code=401, detail="未提供认证信息")
|
||||||
if not session_cookie:
|
|
||||||
raise HTTPException(status_code=401, detail="未提供认证信息")
|
url = f"{get_jingrow_cloud_url()}/api/action/jcloud.api.local_app.get_my_local_app_list"
|
||||||
|
|
||||||
user = get_logged_user(session_cookie)
|
# 构建参数
|
||||||
if not user:
|
params = {
|
||||||
raise HTTPException(status_code=401, detail="认证失败")
|
'order_by': sort_by or "app_name asc",
|
||||||
|
'limit_start': (page - 1) * page_size,
|
||||||
# 调用云端API的get_my_local_app_list函数
|
'limit_page_length': page_size
|
||||||
url = f"{get_jingrow_cloud_url()}/api/action/jcloud.api.local_app.get_my_local_app_list"
|
}
|
||||||
|
|
||||||
# 构建过滤条件
|
if search:
|
||||||
filters = {}
|
params['filters'] = json.dumps({"title": ["like", f"%{search}%"]}, ensure_ascii=False)
|
||||||
if search:
|
|
||||||
filters["title"] = ["like", f"%{search}%"]
|
# 获取总数
|
||||||
|
total_params = params.copy()
|
||||||
# 构建请求参数
|
total_params['limit_start'] = 0
|
||||||
params = {}
|
total_params['limit_page_length'] = 0
|
||||||
if filters:
|
|
||||||
params['filters'] = json.dumps(filters, ensure_ascii=False)
|
headers = get_jingrow_cloud_api_headers()
|
||||||
|
headers['Cookie'] = f'sid={session_cookie}'
|
||||||
# 排序参数
|
|
||||||
if sort_by:
|
total_response = requests.get(url, params=total_params, headers=headers, timeout=20)
|
||||||
params['order_by'] = sort_by
|
total_response.raise_for_status()
|
||||||
else:
|
total_count = len(total_response.json().get('message', []))
|
||||||
params['order_by'] = "app_name asc" # 默认排序
|
|
||||||
|
# 获取分页数据
|
||||||
# 分页参数
|
response = requests.get(url, params=params, headers=headers, timeout=20)
|
||||||
limit_start = (page - 1) * page_size
|
response.raise_for_status()
|
||||||
params['limit_start'] = limit_start
|
apps = response.json().get('message', [])
|
||||||
params['limit_page_length'] = page_size
|
|
||||||
|
return {
|
||||||
headers = get_jingrow_cloud_api_headers()
|
"items": apps,
|
||||||
# 添加session cookie以获取用户的团队信息
|
"total": total_count,
|
||||||
headers['Cookie'] = f'sid={session_cookie}'
|
"page": page,
|
||||||
|
"page_size": page_size
|
||||||
# 1. 先获取总数(不分页)
|
}
|
||||||
total_params = params.copy()
|
|
||||||
total_params['limit_start'] = 0
|
|
||||||
total_params['limit_page_length'] = 0 # 不限制数量,获取所有数据来计算总数
|
|
||||||
|
|
||||||
total_response = requests.get(url, params=total_params, headers=headers, timeout=20)
|
|
||||||
|
|
||||||
total_count = 0
|
|
||||||
if total_response.status_code == 200:
|
|
||||||
total_data = total_response.json()
|
|
||||||
# get_my_local_app_list返回的是列表,不是message格式
|
|
||||||
if isinstance(total_data, dict) and 'message' in total_data:
|
|
||||||
total_count = len(total_data.get('message', []))
|
|
||||||
elif isinstance(total_data, list):
|
|
||||||
total_count = len(total_data)
|
|
||||||
elif isinstance(total_data, dict) and total_data.get('success') is False:
|
|
||||||
# 如果没有团队信息,返回空列表
|
|
||||||
return {
|
|
||||||
"items": [],
|
|
||||||
"total": 0,
|
|
||||||
"page": page,
|
|
||||||
"page_size": page_size
|
|
||||||
}
|
|
||||||
|
|
||||||
# 2. 获取分页数据
|
|
||||||
response = requests.get(url, params=params, headers=headers, timeout=20)
|
|
||||||
|
|
||||||
if response.status_code == 200:
|
|
||||||
data = response.json()
|
|
||||||
|
|
||||||
# 处理不同的响应格式
|
|
||||||
if isinstance(data, dict) and 'message' in data:
|
|
||||||
apps = data.get('message', [])
|
|
||||||
elif isinstance(data, list):
|
|
||||||
apps = data
|
|
||||||
elif isinstance(data, dict) and data.get('success') is False:
|
|
||||||
# 如果没有团队信息,返回空列表
|
|
||||||
apps = []
|
|
||||||
else:
|
|
||||||
apps = []
|
|
||||||
|
|
||||||
# 如果没有获取到总数,使用当前返回的数据长度
|
|
||||||
if total_count == 0 and apps:
|
|
||||||
total_count = len(apps)
|
|
||||||
|
|
||||||
# 返回分页格式的数据
|
|
||||||
return {
|
|
||||||
"items": apps,
|
|
||||||
"total": total_count,
|
|
||||||
"page": page,
|
|
||||||
"page_size": page_size
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
error_detail = "获取已发布应用数据失败"
|
|
||||||
if response.headers.get('content-type', '').startswith('application/json'):
|
|
||||||
try:
|
|
||||||
error_data = response.json()
|
|
||||||
error_detail = error_data.get('detail') or error_data.get('message') or error_detail
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
raise HTTPException(status_code=response.status_code, detail=error_detail)
|
|
||||||
|
|
||||||
except HTTPException:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"获取已发布应用数据失败: {str(e)}")
|
|
||||||
raise HTTPException(status_code=500, detail=f"获取已发布应用数据失败: {str(e)}")
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/jingrow/upload-image")
|
@router.post("/jingrow/upload-image")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user