const axios = require('axios'); const fs = require('fs'); const path = require('path'); // 读取服务器端口 function getServerPort() { try { const portFile = path.join(__dirname, 'data', 'server-port.json'); if (fs.existsSync(portFile)) { const portData = JSON.parse(fs.readFileSync(portFile, 'utf8')); return portData.port || 3001; } } catch (error) { console.log('读取端口文件失败:', error.message); } return 3001; } async function testFullCheckout() { const port = getServerPort(); const baseURL = `http://localhost:${port}/api`; console.log('开始测试完整结账流程...'); console.log('服务器地址:', baseURL); try { // 1. 登录获取用户信息 console.log('\n1. 尝试登录...'); const loginResponse = await axios.post(`${baseURL}/cashiers/login`, { username: 'cashier1', password: 'cashier123' }); console.log('登录成功:', loginResponse.data); const userInfo = loginResponse.data; // 2. 创建订单 console.log('\n2. 创建订单...'); const orderData = { items: [ { product_id: 1, quantity: 2 } ], payment_method: 'cash', cashier_id: userInfo.id, paid_amount: 20, discount_amount: 0 }; console.log('订单数据:', JSON.stringify(orderData, null, 2)); const orderResponse = await axios.post(`${baseURL}/orders`, orderData); console.log('订单创建成功:', orderResponse.data); console.log('\n测试完成!'); } catch (error) { console.error('\n测试失败:'); if (error.response) { console.error('响应状态:', error.response.status); console.error('响应数据:', error.response.data); } else if (error.request) { console.error('请求失败:', error.message); } else { console.error('错误:', error.message); } } } testFullCheckout();