| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- const axios = require('axios');
- const colors = require('colors');
- // 测试配置
- const CONFIG = {
- baseURL: 'http://localhost/dev-api',
- 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,
- };
- }
- }
- /**
- * 测试zwc007更新进度按钮权限
- */
- async function testZwc007UpdateProgressPermission() {
- console.log('\n' + '='.repeat(80));
- console.log(colors.cyan.bold(' zwc007更新进度按钮权限测试'));
- console.log('='.repeat(80));
- console.log(colors.yellow(`测试时间: ${new Date().toLocaleString()}`));
- console.log(colors.yellow(`测试用户: ${CONFIG.username}`));
- console.log('='.repeat(80));
- const results = {
- login: null,
- userInfo: null,
- orderList: null,
- updateProgressPermission: null,
- assignedOrders: [],
- testResults: [],
- };
- try {
- // 步骤1:登录获取Token
- console.log(colors.blue('\n【步骤1】zwc007用户登录测试'));
- 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:
- results.login.token.split('...')[0] +
- (await getToken(CONFIG.username, CONFIG.password)).substring(50),
- });
- 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}`);
- console.log(`角色: ${results.userInfo.roles.join(', ')}`);
- // 检查维修人员角色
- const hasMaintenanceRole = results.userInfo.roles.includes('maintenance_worker');
- console.log(
- `维修人员角色: ${
- hasMaintenanceRole ? colors.green('✓ 已配置') : colors.red('✗ 缺少')
- }`,
- );
- // 检查更新进度权限
- const hasUpdateProgressPermission = results.userInfo.permissions.includes(
- 'pm:repairOrder:updateProgress',
- );
- console.log(
- `更新进度权限: ${
- hasUpdateProgressPermission ? colors.green('✓ 已配置') : colors.red('✗ 缺少')
- }`,
- );
- results.updateProgressPermission = hasUpdateProgressPermission;
- } else {
- console.log(colors.red('✗ 用户信息获取失败: ' + JSON.stringify(userInfoResult.error)));
- }
- // 步骤3:获取工单列表,检查数据权限过滤
- console.log(colors.blue('\n【步骤3】工单列表和数据权限检查'));
- 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 || [];
- results.orderList = {
- total: orderData.total || orders.length,
- count: orders.length,
- orders: orders,
- };
- console.log(colors.green('✓ 工单列表获取成功'));
- console.log(`可见工单总数: ${results.orderList.total}`);
- console.log(`当前页工单数: ${results.orderList.count}`);
- // 分析工单状态和负责人
- const statusAnalysis = {};
- const assigneeAnalysis = {};
- let myAssignedOrders = 0;
- let myAssignedStatus2Orders = 0;
- orders.forEach((order) => {
- // 状态统计
- const statusText = getStatusText(order.orderStatus);
- statusAnalysis[statusText] = (statusAnalysis[statusText] || 0) + 1;
- // 负责人统计
- if (order.assigneeId) {
- const assigneeInfo = `${order.assigneeName || '未知'}(${order.assigneeId})`;
- assigneeAnalysis[assigneeInfo] = (assigneeAnalysis[assigneeInfo] || 0) + 1;
- // 检查是否为zwc007负责的工单
- if (order.assigneeId === results.userInfo?.userId) {
- myAssignedOrders++;
- if (order.orderStatus === '2' || order.orderStatus === 2) {
- myAssignedStatus2Orders++;
- results.assignedOrders.push({
- id: order.id,
- orderNo: order.orderNo,
- title: order.title,
- orderStatus: order.orderStatus,
- shouldShowUpdateButton: true,
- });
- }
- }
- }
- });
- console.log('\n工单状态分布:');
- Object.entries(statusAnalysis).forEach(([status, count]) => {
- console.log(` ${status}: ${count}个`);
- });
- console.log('\n负责人分布:');
- Object.entries(assigneeAnalysis).forEach(([assignee, count]) => {
- console.log(` ${assignee}: ${count}个`);
- });
- console.log(`\n${CONFIG.username}负责的工单: ${myAssignedOrders}个`);
- console.log(
- `${CONFIG.username}负责的已分派工单: ${myAssignedStatus2Orders}个 ${
- myAssignedStatus2Orders > 0
- ? colors.green('(应显示更新进度按钮)')
- : colors.red('(无法显示更新进度按钮)')
- }`,
- );
- } else {
- console.log(colors.red('✗ 工单列表获取失败: ' + JSON.stringify(orderListResult.error)));
- }
- // 步骤4:测试更新进度API权限
- console.log(colors.blue('\n【步骤4】更新进度API权限测试'));
- console.log('----------------------------------------');
- if (results.assignedOrders.length > 0) {
- const testOrder = results.assignedOrders[0];
- console.log(`测试工单: ${testOrder.orderNo} - ${testOrder.title}`);
- // 尝试调用更新进度API
- const progressData = {
- orderId: testOrder.id,
- progressType: '5',
- progressStatus: '测试进度状态',
- progressDetail: '测试进度更新 - 验证权限配置',
- locationInfo: '测试位置',
- remark: '权限测试备注',
- };
- const updateResult = await testAPI('/pm/repairOrder/updateProgress', progressData, {
- method: 'POST',
- token: await getToken(CONFIG.username, CONFIG.password),
- });
- if (updateResult.success) {
- console.log(colors.green('✓ 更新进度API调用成功'));
- console.log('响应数据:', JSON.stringify(updateResult.data, null, 2));
- results.testResults.push({
- api: 'updateProgress',
- success: true,
- orderNo: testOrder.orderNo,
- });
- } else {
- console.log(colors.red('✗ 更新进度API调用失败'));
- console.log('错误信息:', JSON.stringify(updateResult.error, null, 2));
- results.testResults.push({
- api: 'updateProgress',
- success: false,
- error: updateResult.error,
- orderNo: testOrder.orderNo,
- });
- }
- } else {
- console.log(colors.yellow('⚠ 没有找到zwc007负责的已分派工单,无法测试更新进度API'));
- results.testResults.push({
- api: 'updateProgress',
- success: false,
- error: '没有可测试的工单',
- });
- }
- // 步骤5:生成测试报告
- console.log(colors.blue('\n【步骤5】测试结果总结'));
- console.log('----------------------------------------');
- const hasRole = results.userInfo?.roles.includes('maintenance_worker');
- const hasPermission = results.updateProgressPermission;
- const hasAssignedOrders = results.assignedOrders.length > 0;
- const apiCallSuccess = results.testResults.some(
- (r) => r.api === 'updateProgress' && r.success,
- );
- console.log(`维修人员角色: ${hasRole ? colors.green('✓') : colors.red('✗')}`);
- console.log(`更新进度权限: ${hasPermission ? colors.green('✓') : colors.red('✗')}`);
- console.log(
- `负责的已分派工单: ${hasAssignedOrders ? colors.green('✓') : colors.red('✗')} (${
- results.assignedOrders.length
- }个)`,
- );
- console.log(`API调用权限: ${apiCallSuccess ? colors.green('✓') : colors.red('✗')}`);
- const allConditionsMet = hasRole && hasPermission && hasAssignedOrders;
- console.log(
- `\n更新进度按钮应该显示: ${allConditionsMet ? colors.green('是') : colors.red('否')}`,
- );
- if (!allConditionsMet) {
- console.log(colors.yellow('\n问题解决建议:'));
- if (!hasRole) console.log(colors.yellow('- 需要为zwc007分配maintenance_worker角色'));
- if (!hasPermission)
- console.log(
- colors.yellow('- 需要为维修人员角色分配pm:repairOrder:updateProgress权限'),
- );
- if (!hasAssignedOrders)
- console.log(colors.yellow('- 需要创建状态为"已分派"且zwc007为负责人的工单'));
- console.log(
- colors.yellow('- 执行 test/zwc007-update-progress-diagnosis.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) {
- testZwc007UpdateProgressPermission()
- .then((results) => {
- console.log('\n测试结果已保存');
- })
- .catch((error) => {
- console.error(colors.red('测试执行失败:'), error);
- });
- }
- module.exports = {
- testZwc007UpdateProgressPermission,
- };
|