#!/usr/bin/env python3
"""
检查用户管理页面的请求
"""

from playwright.sync_api import sync_playwright
import time

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    context = browser.new_context()
    page = context.new_page()
    
    # 监听网络请求
    def handle_request(request):
        if '/api/' in request.url and not request.url.endswith('.ts'):
            headers = request.headers
            print(f"\n请求: {request.method} {request.url}")
            if 'authorization' in headers:
                print(f"  Authorization: {headers['authorization'][:50]}...")
            else:
                print(f"  ⚠️ 没有Authorization头！")
    
    def handle_response(response):
        if '/api/' in response.url and not response.url.endswith('.ts'):
            print(f"响应: {response.status} {response.url}")
            if response.status == 401:
                print(f"  ⚠️ 401错误！")
    
    page.on('request', handle_request)
    page.on('response', handle_response)
    
    # 登录
    print("登录...")
    page.goto("http://localhost:3002/login")
    page.wait_for_load_state("networkidle")
    page.fill('input[placeholder="请输入用户名"]', 'admin')
    page.fill('input[placeholder="请输入密码"]', 'admin123')
    page.click('button:has-text("登录")')
    
    # 等待Token
    for i in range(10):
        time.sleep(1)
        token = page.evaluate("localStorage.getItem('token')")
        if token:
            print(f"\n登录成功")
            break
    
    # 访问用户管理
    print("\n\n访问用户管理...")
    page.goto("http://localhost:3002/users")
    page.wait_for_load_state("networkidle")
    time.sleep(3)
    
    print(f"\n最终URL: {page.url}")
    token_after = page.evaluate("localStorage.getItem('token')")
    print(f"Token: {'存在' if token_after else '不存在'}")
    
    browser.close()
