lrj
18 小时以前 7ad9c3c93f0cc103347ae2e2429e0122fb512e24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>角色API调试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .result { margin: 10px 0; padding: 10px; border: 1px solid #ccc; }
        .error { background-color: #ffe6e6; }
        .success { background-color: #e6ffe6; }
        button { padding: 10px 20px; margin: 5px; }
    </style>
</head>
<body>
    <h1>角色API调试</h1>
    
    <button onclick="testWithoutAuth()">测试无认证API调用</button>
    <button onclick="testWithAuth()">测试带认证API调用</button>
    <button onclick="checkLocalStorage()">检查本地存储</button>
    
    <div id="results"></div>
 
    <script>
        function addResult(content, isError = false) {
            const div = document.createElement('div');
            div.className = `result ${isError ? 'error' : 'success'}`;
            div.innerHTML = content;
            document.getElementById('results').appendChild(div);
        }
 
        async function testWithoutAuth() {
            try {
                const response = await fetch('/api/graphql', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        query: `query GetActiveRoles {
                            activeRoles {
                                id
                                code
                                name
                                description
                                state
                                createTime
                                updateTime
                            }
                        }`
                    })
                });
 
                const data = await response.json();
                addResult(`<strong>无认证API调用结果:</strong><br>状态: ${response.status}<br>数据: <pre>${JSON.stringify(data, null, 2)}</pre>`);
            } catch (error) {
                addResult(`<strong>无认证API调用失败:</strong><br>${error.message}`, true);
            }
        }
 
        async function testWithAuth() {
            try {
                const token = localStorage.getItem('token');
                const response = await fetch('/api/graphql', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': token ? `Bearer ${token}` : ''
                    },
                    body: JSON.stringify({
                        query: `query GetActiveRoles {
                            activeRoles {
                                id
                                code
                                name
                                description
                                state
                                createTime
                                updateTime
                            }
                        }`
                    })
                });
 
                const data = await response.json();
                addResult(`<strong>带认证API调用结果:</strong><br>状态: ${response.status}<br>Token: ${token ? '存在' : '不存在'}<br>数据: <pre>${JSON.stringify(data, null, 2)}</pre>`);
            } catch (error) {
                addResult(`<strong>带认证API调用失败:</strong><br>${error.message}`, true);
            }
        }
 
        function checkLocalStorage() {
            const token = localStorage.getItem('token');
            const user = localStorage.getItem('user');
            addResult(`<strong>本地存储检查:</strong><br>Token: ${token || '无'}<br>User: ${user || '无'}`);
        }
    </script>
</body>
</html>