260323 function add report chat and front
This commit is contained in:
1 parent
605317c2b2
commit
5e8ea03b6c
16 files changed
+2351
-100
No files matched your search
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
测试认证头格式脚本
|
||||
用途:测试不同的认证头格式
|
||||
"""
|
||||
import requests
|
||||
import json
|
||||
|
||||
BASE_URL = "http://127.0.0.1:5002"
|
||||
|
||||
def test_auth_header_format():
|
||||
"""测试不同的认证头格式"""
|
||||
|
||||
test_cases = [
|
||||
{
|
||||
"name": "格式1: 直接存储用户对象",
|
||||
"user_data": {"id": 1, "username": "admin", "role": "admin"},
|
||||
"description": "localStorage.setItem('hxops_user', JSON.stringify(user))"
|
||||
},
|
||||
{
|
||||
"name": "格式2: 包装格式 {user, expiry}",
|
||||
"user_data": {"user": {"id": 1, "username": "admin", "role": "admin"}, "expiry": 123456789},
|
||||
"description": "localStorage.setItem('hxops_user', JSON.stringify({user, expiry}))"
|
||||
},
|
||||
]
|
||||
|
||||
for tc in test_cases:
|
||||
print("\n" + "=" * 60)
|
||||
print(f"测试: {tc['name']}")
|
||||
print(f"描述: {tc['description']}")
|
||||
print(f"存储数据: {json.dumps(tc['user_data'], ensure_ascii=False)}")
|
||||
print("-" * 60)
|
||||
|
||||
# 模拟前端 JSON.parse 后的结果
|
||||
user = tc['user_data']
|
||||
|
||||
# 如果是包装格式,提取 user
|
||||
if 'user' in user:
|
||||
user = user['user']
|
||||
|
||||
headers = {
|
||||
'X-Username': user.get('username', ''),
|
||||
'X-Role': user.get('role', ''),
|
||||
}
|
||||
if user.get('id'):
|
||||
headers['X-User-Id'] = str(user['id'])
|
||||
|
||||
print(f"生成的 Headers: {headers}")
|
||||
|
||||
# 测试 API
|
||||
url = f"{BASE_URL}/api/chat/sessions?agent_id=3"
|
||||
response = requests.get(url, headers=headers, timeout=5)
|
||||
print(f"响应状态: {response.status_code}")
|
||||
if response.status_code != 200:
|
||||
print(f"响应内容: {response.text}")
|
||||
|
||||
def test_frontend_storage_format():
|
||||
"""测试前端实际存储格式"""
|
||||
print("\n" + "=" * 60)
|
||||
print("分析前端存储格式")
|
||||
print("=" * 60)
|
||||
|
||||
# 前端代码分析
|
||||
# WorkspaceView.vue 登录成功后:
|
||||
# localStorage.setItem('hxops_user', JSON.stringify({ user, expiry }));
|
||||
# 其中 user 是登录 API 返回的对象: {id, username, role}
|
||||
|
||||
print("前端存储逻辑分析:")
|
||||
print("1. 登录成功后调用: localStorage.setItem('hxops_user', JSON.stringify({ user, expiry }))")
|
||||
print("2. 其中 user 来自登录 API 返回: {id: 1, username: 'admin', role: 'admin'}")
|
||||
print("3. 实际存储格式: {\"user\": {\"id\": 1, \"username\": \"admin\", \"role\": \"admin\"}, \"expiry\": 123456789}")
|
||||
print("\n前端解析逻辑 (useChat.ts):")
|
||||
print("1. 获取: localStorage.getItem('hxops_user')")
|
||||
print("2. 解析: JSON.parse(stored)")
|
||||
print("3. 直接当作用户对象使用,不检查是否有 'user' 键")
|
||||
print("\n问题: 解析时没有处理包装格式,导致 username/role 为 undefined")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_frontend_storage_format()
|
||||
test_auth_header_format()
|
||||
Reference in new issue
Block a user