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);
|