lrj
1 天以前 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!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; }
        select { padding: 5px; margin: 5px; min-width: 200px; }
    </style>
</head>
<body>
    <h1>员工角色下拉框测试</h1>
    
    <div>
        <button onclick="checkAuth()">1. 检查认证状态</button>
        <button onclick="testRoleAPI()">2. 测试角色API</button>
        <button onclick="loginAsAdmin()">3. 模拟管理员登录</button>
        <button onclick="testRoleAPIAfterLogin()">4. 登录后测试角色API</button>
    </div>
    
    <div>
        <h3>角色下拉框模拟:</h3>
        <select id="roleSelect">
            <option value="">请选择角色</option>
        </select>
        <button onclick="loadRolesIntoSelect()">加载角色到下拉框</button>
    </div>
    
    <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);
            }
        }
 
        async function testRoleAPIAfterLogin() {
            await testRoleAPI();
        }
 
        async function loadRolesIntoSelect() {
            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();
                const select = document.getElementById('roleSelect');
                
                // 清空现有选项(除了默认选项)
                select.innerHTML = '<option value="">请选择角色</option>';
                
                if (data.errors) {
                    addResult(`<strong>加载角色到下拉框失败:</strong><br>${JSON.stringify(data.errors, null, 2)}`, true);
                } else if (data.data?.activeRoles) {
                    const roles = data.data.activeRoles;
                    roles.forEach(role => {
                        const option = document.createElement('option');
                        option.value = role.id;
                        option.textContent = role.name;
                        select.appendChild(option);
                    });
                    addResult(`<strong>成功加载 ${roles.length} 个角色到下拉框</strong>`);
                } else {
                    addResult(`<strong>没有获取到角色数据</strong>`, true);
                }
            } catch (error) {
                addResult(`<strong>加载角色到下拉框失败:</strong><br>${error.message}`, true);
            }
        }
    </script>
</body>
</html>