const axios = require('axios'); const { getToken } = require('./quick-token-getter'); // 配置信息 const config = { baseURL: 'http://localhost/dev-api', timeout: 10000, headers: { 'Content-Type': 'application/json;charset=utf-8', 'clientid': 'e5cd7e4891bf95d1d19206ce24a7b32e', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', 'referer': 'http://localhost/login?redirect=%252Findex' } }; /** * 测试维修人员接口 */ async function testMaintenanceWorkersAPI() { console.log('🚀 开始测试维修人员接口...\n'); try { // 1. 获取Token console.log('1️⃣ 正在获取Token...'); const token = await getToken(); console.log(`✅ Token获取成功: ${token.substring(0, 20)}...\n`); // 2. 设置认证头 config.headers['Authorization'] = `Bearer ${token}`; // 3. 调用维修人员接口 console.log('2️⃣ 正在调用维修人员接口...'); const response = await axios.get('/pm/repairOrder/maintenance-workers', config); // 4. 检查响应格式 console.log('3️⃣ 接口响应检查:'); console.log(`状态码: ${response.status}`); console.log(`响应格式: ${JSON.stringify(response.data, null, 2)}\n`); // 5. 验证响应数据 if (response.data.code === 200) { console.log('✅ 接口调用成功!'); const workers = response.data.data; if (Array.isArray(workers)) { console.log(`📊 维修人员数量: ${workers.length}个`); if (workers.length > 0) { console.log('\n👷 维修人员列表:'); workers.forEach((worker, index) => { console.log(`${index + 1}. ${worker.label} (ID: ${worker.value}) - ${worker.userName || '无用户名'} - ${worker.deptName || '无部门'}`); }); } else { console.log('⚠️ 没有找到维修人员,请确认:'); console.log(' - 是否已创建"维修人员"角色'); console.log(' - 是否已为用户分配"维修人员"角色'); console.log(' - 用户状态是否正常'); } } else { console.log('❌ 响应数据格式错误,期望数组格式'); } } else { console.log(`❌ 接口调用失败: ${response.data.msg}`); } } catch (error) { console.error('❌ 测试失败:', error.response?.data || error.message); // 详细错误信息 if (error.response) { console.log('\n📋 错误详情:'); console.log(`状态码: ${error.response.status}`); console.log(`错误信息: ${JSON.stringify(error.response.data, null, 2)}`); } } } /** * 验证维修人员权限配置 */ async function validateMaintenanceRoleConfig() { console.log('\n🔍 验证维修人员权限配置...'); try { const token = await getToken(); config.headers['Authorization'] = `Bearer ${token}`; // 检查维修人员角色 console.log('1️⃣ 检查"维修人员"角色...'); // 这里可以添加更多验证逻辑 console.log('✅ 权限配置验证完成'); } catch (error) { console.error('❌ 权限配置验证失败:', error.message); } } // 执行测试 async function runTests() { console.log('='.repeat(50)); console.log('📋 维修人员接口测试'); console.log('='.repeat(50)); await testMaintenanceWorkersAPI(); await validateMaintenanceRoleConfig(); console.log('\n' + '='.repeat(50)); console.log('✅ 测试完成'); console.log('='.repeat(50)); } // 如果直接运行此文件 if (require.main === module) { runTests().catch(console.error); } module.exports = { testMaintenanceWorkersAPI, validateMaintenanceRoleConfig };