| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- const axios = require('axios');
- const colors = require('colors');
- // 测试配置
- const CONFIG = {
- baseURL: 'http://localhost/dev-api',
- orderNo: 'RO20250126008',
- username: 'zwc007',
- password: 'admin123', // 需要根据实际密码调整
- timeout: 10000,
- };
- /**
- * 获取Token的通用函数
- */
- async function getToken(username, password) {
- const { getToken } = require('./quick-token-getter');
- return await getToken(username, password);
- }
- /**
- * 测试API调用的通用函数
- */
- async function testAPI(endpoint, data, options = {}) {
- try {
- const response = await axios({
- method: options.method || 'GET',
- url: `${CONFIG.baseURL}${endpoint}`,
- data: data,
- 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',
- Authorization: options.token ? `Bearer ${options.token}` : undefined,
- ...options.headers,
- },
- timeout: CONFIG.timeout,
- });
- return {
- success: true,
- data: response.data,
- status: response.status,
- };
- } catch (error) {
- return {
- success: false,
- error: error.response?.data || error.message,
- status: error.response?.status,
- };
- }
- }
- /**
- * 测试工单RO20250126008的更新进度按钮
- */
- async function testRO20250126008UpdateProgressButton() {
- console.log('\n' + '='.repeat(80));
- console.log(colors.cyan.bold(' 工单RO20250126008更新进度按钮测试'));
- console.log('='.repeat(80));
- console.log(colors.yellow(`测试时间: ${new Date().toLocaleString()}`));
- console.log(colors.yellow(`目标工单: ${CONFIG.orderNo}`));
- console.log(colors.yellow(`测试用户: ${CONFIG.username}`));
- console.log('='.repeat(80));
- const results = {
- login: null,
- userInfo: null,
- targetOrder: null,
- permissionCheck: null,
- buttonDisplayConditions: null,
- apiTest: null,
- };
- try {
- // 步骤1:用户登录
- console.log(colors.blue('\n【步骤1】用户登录验证'));
- console.log('----------------------------------------');
- try {
- const token = await getToken(CONFIG.username, CONFIG.password);
- results.login = { success: true, token: token.substring(0, 50) + '...' };
- console.log(colors.green('✓ 登录成功'));
- console.log(`Token: ${token.substring(0, 50)}...`);
- } catch (error) {
- results.login = { success: false, error: error.message };
- console.log(colors.red('✗ 登录失败: ' + error.message));
- return results;
- }
- // 步骤2:获取用户信息和权限
- console.log(colors.blue('\n【步骤2】用户权限检查'));
- console.log('----------------------------------------');
- const userInfoResult = await testAPI('/system/user/getInfo', null, {
- method: 'GET',
- token: await getToken(CONFIG.username, CONFIG.password),
- });
- if (userInfoResult.success) {
- const userInfo = userInfoResult.data.data || userInfoResult.data;
- results.userInfo = {
- userId: userInfo.user?.userId,
- userName: userInfo.user?.userName,
- nickName: userInfo.user?.nickName,
- roles: userInfo.roles?.map((r) => r.roleKey) || [],
- permissions: userInfo.permissions || [],
- };
- console.log(colors.green('✓ 用户信息获取成功'));
- console.log(`用户ID: ${results.userInfo.userId}`);
- console.log(`用户名: ${results.userInfo.userName}`);
- console.log(`昵称: ${results.userInfo.nickName}`);
- // 检查关键权限
- const hasMaintenanceRole = results.userInfo.roles.includes('maintenance_worker');
- const hasUpdateProgressPerm = results.userInfo.permissions.includes(
- 'pm:repairOrder:updateProgress',
- );
- console.log(
- `维修人员角色: ${
- hasMaintenanceRole ? colors.green('✓ 已配置') : colors.red('✗ 缺少')
- }`,
- );
- console.log(
- `更新进度权限: ${
- hasUpdateProgressPerm ? colors.green('✓ 已配置') : colors.red('✗ 缺少')
- }`,
- );
- results.permissionCheck = {
- hasMaintenanceRole,
- hasUpdateProgressPerm,
- };
- } else {
- console.log(colors.red('✗ 用户信息获取失败: ' + JSON.stringify(userInfoResult.error)));
- }
- // 步骤3:查找目标工单
- console.log(colors.blue('\n【步骤3】查找目标工单RO20250126008'));
- console.log('----------------------------------------');
- const orderListResult = await testAPI('/pm/repairOrder/list', null, {
- method: 'GET',
- token: await getToken(CONFIG.username, CONFIG.password),
- });
- if (orderListResult.success) {
- const orderData = orderListResult.data;
- const orders = orderData.rows || orderData.data || [];
- console.log(colors.green('✓ 工单列表获取成功'));
- console.log(`可见工单总数: ${orderData.total || orders.length}`);
- // 查找目标工单
- const targetOrder = orders.find((order) => order.orderNo === CONFIG.orderNo);
- if (targetOrder) {
- results.targetOrder = {
- id: targetOrder.id,
- orderNo: targetOrder.orderNo,
- title: targetOrder.title,
- orderStatus: targetOrder.orderStatus,
- assigneeId: targetOrder.assigneeId,
- assigneeName: targetOrder.assigneeName,
- repairLocation: targetOrder.repairLocation,
- createTime: targetOrder.createTime,
- };
- console.log(colors.green(`✓ 找到目标工单: ${CONFIG.orderNo}`));
- console.log(`工单标题: ${targetOrder.title}`);
- console.log(`工单状态: ${getStatusText(targetOrder.orderStatus)}`);
- console.log(
- `维修负责人: ${targetOrder.assigneeName || '未分派'} (ID: ${
- targetOrder.assigneeId || 'null'
- })`,
- );
- console.log(`报修地点: ${targetOrder.repairLocation}`);
- // 检查按钮显示条件
- const isStatus2 = targetOrder.orderStatus === '2' || targetOrder.orderStatus === 2;
- const isAssignedToZwc007 = targetOrder.assigneeId === results.userInfo?.userId;
- console.log('\n按钮显示条件检查:');
- console.log(
- `工单状态为已分派(2): ${
- isStatus2 ? colors.green('✓ 满足') : colors.red('✗ 不满足')
- } (当前: ${targetOrder.orderStatus})`,
- );
- console.log(
- `zwc007是负责人: ${
- isAssignedToZwc007 ? colors.green('✓ 满足') : colors.red('✗ 不满足')
- } (负责人ID: ${targetOrder.assigneeId})`,
- );
- console.log(
- `维修人员角色: ${
- results.permissionCheck?.hasMaintenanceRole
- ? colors.green('✓ 满足')
- : colors.red('✗ 不满足')
- }`,
- );
- console.log(
- `更新进度权限: ${
- results.permissionCheck?.hasUpdateProgressPerm
- ? colors.green('✓ 满足')
- : colors.red('✗ 不满足')
- }`,
- );
- const allConditionsMet =
- isStatus2 &&
- isAssignedToZwc007 &&
- results.permissionCheck?.hasMaintenanceRole &&
- results.permissionCheck?.hasUpdateProgressPerm;
- console.log(
- `\n更新进度按钮应该显示: ${
- allConditionsMet ? colors.green('是 🎉') : colors.red('否 ❌')
- }`,
- );
- results.buttonDisplayConditions = {
- isStatus2,
- isAssignedToZwc007,
- allConditionsMet,
- };
- } else {
- console.log(colors.red(`✗ 未找到工单${CONFIG.orderNo}`));
- console.log(colors.yellow('可能原因:'));
- console.log(colors.yellow('1. 工单不存在'));
- console.log(colors.yellow('2. zwc007没有查看该工单的权限'));
- console.log(colors.yellow('3. 数据权限过滤导致工单不可见'));
- results.targetOrder = { found: false };
- }
- } else {
- console.log(colors.red('✗ 工单列表获取失败: ' + JSON.stringify(orderListResult.error)));
- }
- // 步骤4:测试更新进度API(如果工单存在且条件满足)
- console.log(colors.blue('\n【步骤4】更新进度API测试'));
- console.log('----------------------------------------');
- if (results.targetOrder?.found !== false && results.targetOrder?.id) {
- console.log(`测试工单: ${results.targetOrder.orderNo} (ID: ${results.targetOrder.id})`);
- // 构造测试用的进度数据
- const progressData = {
- orderId: results.targetOrder.id,
- progressType: '5', // 维修进度
- progressStatus: '测试进度状态',
- progressDetail: `工单${
- CONFIG.orderNo
- }进度更新测试 - ${new Date().toLocaleString()}`,
- progressTime: new Date().toISOString().slice(0, 19).replace('T', ' '),
- locationInfo: results.targetOrder.repairLocation || '测试位置',
- estimatedDuration: 60,
- nextStep: '继续测试验证',
- remark: 'API权限验证测试',
- };
- const apiResult = await testAPI('/pm/repairOrder/updateProgress', progressData, {
- method: 'POST',
- token: await getToken(CONFIG.username, CONFIG.password),
- });
- if (apiResult.success) {
- console.log(colors.green('✓ 更新进度API调用成功'));
- console.log('响应数据:', JSON.stringify(apiResult.data, null, 2));
- results.apiTest = {
- success: true,
- response: apiResult.data,
- };
- } else {
- console.log(colors.red('✗ 更新进度API调用失败'));
- console.log('错误信息:', JSON.stringify(apiResult.error, null, 2));
- results.apiTest = {
- success: false,
- error: apiResult.error,
- };
- }
- } else {
- console.log(colors.yellow('⚠ 跳过API测试:工单不存在或条件不满足'));
- results.apiTest = { skipped: true, reason: '工单不存在或条件不满足' };
- }
- // 步骤5:生成测试总结
- console.log(colors.blue('\n【步骤5】测试结果总结'));
- console.log('----------------------------------------');
- console.log(colors.cyan('测试结果汇总:'));
- console.log(
- `1. 用户登录: ${
- results.login?.success ? colors.green('✓ 成功') : colors.red('✗ 失败')
- }`,
- );
- console.log(
- `2. 权限配置: ${
- results.permissionCheck?.hasMaintenanceRole &&
- results.permissionCheck?.hasUpdateProgressPerm
- ? colors.green('✓ 完整')
- : colors.red('✗ 缺失')
- }`,
- );
- console.log(
- `3. 目标工单: ${
- results.targetOrder?.found !== false
- ? colors.green('✓ 找到')
- : colors.red('✗ 未找到')
- }`,
- );
- console.log(
- `4. 按钮显示: ${
- results.buttonDisplayConditions?.allConditionsMet
- ? colors.green('✓ 应显示')
- : colors.red('✗ 不显示')
- }`,
- );
- console.log(
- `5. API权限: ${
- results.apiTest?.success
- ? colors.green('✓ 正常')
- : results.apiTest?.skipped
- ? colors.yellow('⚠ 跳过')
- : colors.red('✗ 失败')
- }`,
- );
- // 提供修复建议
- if (!results.buttonDisplayConditions?.allConditionsMet) {
- console.log(colors.yellow('\n修复建议:'));
- if (!results.permissionCheck?.hasMaintenanceRole) {
- console.log(colors.yellow('- 需要为zwc007分配maintenance_worker角色'));
- }
- if (!results.permissionCheck?.hasUpdateProgressPerm) {
- console.log(
- colors.yellow('- 需要为维修人员角色分配pm:repairOrder:updateProgress权限'),
- );
- }
- if (!results.buttonDisplayConditions?.isStatus2) {
- console.log(colors.yellow(`- 需要将工单${CONFIG.orderNo}状态设置为已分派(2)`));
- }
- if (!results.buttonDisplayConditions?.isAssignedToZwc007) {
- console.log(colors.yellow(`- 需要将工单${CONFIG.orderNo}分派给zwc007`));
- }
- console.log(
- colors.yellow('- 执行 test/RO20250126008-update-progress-fix.sql 脚本进行修复'),
- );
- }
- } catch (error) {
- console.log(colors.red('测试过程中发生错误: ' + error.message));
- results.error = error.message;
- }
- console.log('\n' + '='.repeat(80));
- console.log(colors.cyan.bold(' 测试完成'));
- console.log('='.repeat(80));
- return results;
- }
- /**
- * 获取状态文本
- */
- function getStatusText(status) {
- const statusMap = {
- 1: '待分派',
- 2: '已分派',
- 3: '进行中',
- 4: '已完成',
- 5: '已评价',
- 6: '已归档',
- 9: '已取消',
- };
- return statusMap[status] || `状态${status}`;
- }
- // 执行测试
- if (require.main === module) {
- testRO20250126008UpdateProgressButton()
- .then((results) => {
- console.log('\n测试结果已保存');
- // 输出最终结论
- const success =
- results.buttonDisplayConditions?.allConditionsMet && results.apiTest?.success;
- if (success) {
- console.log(
- colors.green.bold(
- '\n🎉 测试通过!工单RO20250126008的更新进度按钮应该可以正常显示和使用。',
- ),
- );
- } else {
- console.log(colors.red.bold('\n❌ 测试未通过!需要执行修复脚本解决问题。'));
- }
- })
- .catch((error) => {
- console.error(colors.red('测试执行失败:'), error);
- });
- }
- module.exports = {
- testRO20250126008UpdateProgressButton,
- };
|