Codex Assistant
23 小时以前 58d9f460b2f8c34430285115e2557d18333c5cab
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
const axios = require('axios');
 
const BASE_URL = 'http://localhost:8080';
 
// 登录函数
async function login() {
    try {
        const loginData = {
            phone: '17898163888',
            password: '123456'
        };
 
        console.log('正在登录...');
        const response = await axios.post(`${BASE_URL}/api/auth/web-login`, loginData);
        
        if (response.status === 200 && response.data.success) {
            console.log('登录成功');
            return response.data.data.token;
        } else {
            console.error('登录失败:', response.data);
            return null;
        }
    } catch (error) {
        console.error('登录请求失败:', error.message);
        return null;
    }
}
 
// 查询当前用户信息
async function getCurrentUserInfo(token) {
    try {
        const query = `
            query {
                currentUser {
                    id
                    name
                    phone
                }
            }
        `;
 
        const response = await axios.post(`${BASE_URL}/api/graphql`, {
            query: query
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
 
        console.log('当前用户信息:', JSON.stringify(response.data, null, 2));
        return response.data.data.currentUser;
    } catch (error) {
        console.error('查询当前用户失败:', error.message);
        return null;
    }
}
 
// 查询所有员工
async function getAllEmployees(token) {
    try {
        const query = `
            query {
                employees {
                    id
                    name
                    phone
                    roleId
                    description
                }
            }
        `;
 
        const response = await axios.post(`${BASE_URL}/api/graphql`, {
            query: query
        }, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        });
 
        console.log('所有员工信息:', JSON.stringify(response.data, null, 2));
        return response.data.data.employees;
    } catch (error) {
        console.error('查询员工失败:', error.message);
        return null;
    }
}
 
// 主函数
async function main() {
    console.log('=== 检查当前用户是否已经是员工 ===');
    
    // 1. 登录
    const token = await login();
    if (!token) {
        console.error('登录失败,退出');
        return;
    }
 
    // 2. 获取当前用户信息
    const currentUser = await getCurrentUserInfo(token);
    if (!currentUser) {
        console.error('获取当前用户信息失败');
        return;
    }
 
    // 3. 获取所有员工信息
    const employees = await getAllEmployees(token);
    if (!employees) {
        console.error('获取员工信息失败');
        return;
    }
 
    // 4. 检查当前用户是否已经是员工
    const currentUserPhone = currentUser.phone;
    const isEmployee = employees.some(emp => emp.phone === currentUserPhone);
    
    console.log('\n=== 检查结果 ===');
    console.log(`当前用户手机号: ${currentUserPhone}`);
    console.log(`是否已经是员工: ${isEmployee}`);
    
    if (isEmployee) {
        const employeeInfo = employees.find(emp => emp.phone === currentUserPhone);
        console.log('员工信息:', employeeInfo);
        console.log('\n这就是为什么创建新员工失败的原因:当前登录用户已经是员工了!');
    } else {
        console.log('当前用户不是员工,可以创建新员工记录');
    }
}
 
main().catch(console.error);