lrj
16 小时以前 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>认证状态检查</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>认证状态检查</h1>
    
    <button onclick="checkAuth()">检查认证状态</button>
    <button onclick="testRoleAPI()">测试角色API</button>
    <button onclick="loginAsAdmin()">模拟管理员登录</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);
        }
 
        function checkAuth() {
            const token = localStorage.getItem('auth_token');
            const userInfo = localStorage.getItem('user_info');
            
            addResult(`
                <strong>认证状态:</strong><br>
                Token: ${token ? '存在' : '不存在'}<br>
                User Info: ${userInfo ? '存在' : '不存在'}<br>
                ${token ? `Token内容: ${token.substring(0, 50)}...` : ''}
            `);
        }
 
        async function testRoleAPI() {
            try {
                const token = localStorage.getItem('auth_token');
                const headers = {
                    'Content-Type': 'application/json'
                };
                
                if (token) {
                    headers['Authorization'] = `Bearer ${token}`;
                }
 
                const response = await fetch('/api/graphql', {
                    method: 'POST',
                    headers,
                    body: JSON.stringify({
                        query: `query GetActiveRoles {
                            activeRoles {
                                id
                                code
                                name
                                description
                                state
                                createTime
                                updateTime
                            }
                        }`
                    })
                });
 
                const data = await response.json();
                
                if (data.errors) {
                    addResult(`<strong>角色API错误:</strong><br>${JSON.stringify(data.errors, null, 2)}`, true);
                } else {
                    addResult(`<strong>角色API成功:</strong><br>角色数量: ${data.data?.activeRoles?.length || 0}<br>数据: <pre>${JSON.stringify(data.data, null, 2)}</pre>`);
                }
            } catch (error) {
                addResult(`<strong>角色API调用失败:</strong><br>${error.message}`, true);
            }
        }
 
        async function loginAsAdmin() {
            try {
                const response = await fetch('/api/graphql', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        query: `mutation Login($phone: String!, $password: String!) {
                            login(phone: $phone, password: $password) {
                                token
                                user {
                                    userId
                                    name
                                    phone
                                    userType
                                }
                            }
                        }`,
                        variables: {
                            phone: "13800000001",
                            password: "123456"
                        }
                    })
                });
 
                const data = await response.json();
                
                if (data.errors) {
                    addResult(`<strong>登录失败:</strong><br>${JSON.stringify(data.errors, null, 2)}`, true);
                } else if (data.data?.login?.token) {
                    localStorage.setItem('auth_token', data.data.login.token);
                    localStorage.setItem('user_info', JSON.stringify(data.data.login.user));
                    addResult(`<strong>登录成功:</strong><br>Token已保存<br>用户信息: ${JSON.stringify(data.data.login.user, null, 2)}`);
                } else {
                    addResult(`<strong>登录响应异常:</strong><br>${JSON.stringify(data, null, 2)}`, true);
                }
            } catch (error) {
                addResult(`<strong>登录请求失败:</strong><br>${error.message}`, true);
            }
        }
    </script>
</body>
</html>