repair-order-api-test.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. const axios = require('axios');
  2. const colors = require('colors');
  3. // 测试配置
  4. const BASE_URL = 'http://localhost/dev-api';
  5. // 从test/custom-login-script.js中导入登录方法(如果需要的话)
  6. async function getTestToken() {
  7. // 这里可以调用登录获取Token,暂时使用模拟Token进行测试
  8. // 实际使用时需要先登录获取真实Token
  9. return 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...'; // 模拟Token
  10. }
  11. // 通用API测试方法
  12. async function testAPI(endpoint, method = 'GET', data = null, token = null) {
  13. try {
  14. console.log(`\n🧪 测试接口: ${method} ${BASE_URL}${endpoint}`.cyan);
  15. const config = {
  16. method: method.toLowerCase(),
  17. url: `${BASE_URL}${endpoint}`,
  18. headers: {
  19. 'Content-Type': 'application/json;charset=utf-8',
  20. clientid: 'e5cd7e4891bf95d1d19206ce24a7b32e',
  21. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  22. referer: 'http://localhost/login?redirect=%252Findex',
  23. },
  24. timeout: 10000,
  25. };
  26. if (token) {
  27. config.headers['Authorization'] = `Bearer ${token}`;
  28. }
  29. if (data && (method.toUpperCase() === 'POST' || method.toUpperCase() === 'PUT')) {
  30. config.data = data;
  31. } else if (data && method.toUpperCase() === 'GET') {
  32. config.params = data;
  33. }
  34. const response = await axios(config);
  35. console.log(`✅ 状态码: ${response.status}`.green);
  36. console.log(`📊 响应数据: ${JSON.stringify(response.data, null, 2)}`.white);
  37. return {
  38. success: true,
  39. data: response.data,
  40. status: response.status,
  41. };
  42. } catch (error) {
  43. console.log(`❌ 测试失败:`.red);
  44. if (error.response) {
  45. console.log(` 状态码: ${error.response.status}`.red);
  46. console.log(` 错误信息: ${JSON.stringify(error.response.data, null, 2)}`.red);
  47. } else {
  48. console.log(` 错误信息: ${error.message}`.red);
  49. }
  50. return {
  51. success: false,
  52. error: error.response?.data || error.message,
  53. status: error.response?.status,
  54. };
  55. }
  56. }
  57. // 测试维修分类选项接口
  58. async function testCategoryOptions() {
  59. console.log('\n🔧 测试维修分类选项接口'.blue.bold);
  60. console.log('='.repeat(50).blue);
  61. const result = await testAPI('/pm/repairCategory/options');
  62. if (result.success) {
  63. console.log('\n📝 分析结果:'.yellow);
  64. if (result.data && result.data.data) {
  65. console.log(` - 分类数量: ${result.data.data.length}`.yellow);
  66. console.log(` - 数据格式: ${typeof result.data.data}`.yellow);
  67. if (result.data.data.length > 0) {
  68. console.log(` - 首项示例: ${JSON.stringify(result.data.data[0])}`.yellow);
  69. }
  70. }
  71. }
  72. return result;
  73. }
  74. // 测试统计数据接口
  75. async function testStatistics() {
  76. console.log('\n📊 测试报修统计数据接口'.blue.bold);
  77. console.log('='.repeat(50).blue);
  78. const result = await testAPI('/pm/repairOrder/statistics');
  79. if (result.success) {
  80. console.log('\n📝 分析结果:'.yellow);
  81. if (result.data && result.data.data) {
  82. const stats = result.data.data;
  83. console.log(` - 总工单数: ${stats.totalOrders}`.yellow);
  84. console.log(` - 待分派: ${stats.pendingAssign}`.yellow);
  85. console.log(` - 进行中: ${stats.inProgress}`.yellow);
  86. console.log(` - 已完成: ${stats.completed}`.yellow);
  87. console.log(` - 平均处理时间: ${stats.avgProcessTime}小时`.yellow);
  88. console.log(` - 平均评价得分: ${stats.avgEvaluationScore}分`.yellow);
  89. }
  90. }
  91. return result;
  92. }
  93. // 测试维修分类树形接口(对比测试)
  94. async function testCategoryTree() {
  95. console.log('\n🌳 测试维修分类树形接口(对比)'.blue.bold);
  96. console.log('='.repeat(50).blue);
  97. const result = await testAPI('/pm/repairCategory/tree');
  98. if (result.success) {
  99. console.log('\n📝 分析结果:'.yellow);
  100. if (result.data && result.data.data) {
  101. console.log(` - 根分类数量: ${result.data.data.length}`.yellow);
  102. console.log(` - 数据结构: 树形结构`.yellow);
  103. if (result.data.data.length > 0) {
  104. const firstCategory = result.data.data[0];
  105. console.log(` - 首项示例: ${firstCategory.categoryName}`.yellow);
  106. if (firstCategory.children && firstCategory.children.length > 0) {
  107. console.log(` - 子分类数量: ${firstCategory.children.length}`.yellow);
  108. }
  109. }
  110. }
  111. }
  112. return result;
  113. }
  114. // 测试报修工单列表接口(对比测试)
  115. async function testRepairOrderList() {
  116. console.log('\n📋 测试报修工单列表接口(对比)'.blue.bold);
  117. console.log('='.repeat(50).blue);
  118. const result = await testAPI('/pm/repairOrder/list', 'GET', {
  119. pageNum: 1,
  120. pageSize: 10,
  121. });
  122. if (result.success) {
  123. console.log('\n📝 分析结果:'.yellow);
  124. if (result.data) {
  125. console.log(` - 响应格式: ${result.data.msg}`.yellow);
  126. console.log(` - 总数: ${result.data.total || 0}`.yellow);
  127. console.log(
  128. ` - 当前页数据: ${result.data.rows ? result.data.rows.length : 0}`.yellow,
  129. );
  130. }
  131. }
  132. return result;
  133. }
  134. // 主测试函数
  135. async function runTests() {
  136. console.log('🚀 开始测试报修管理相关API接口'.rainbow.bold);
  137. console.log('=' * 60);
  138. const startTime = new Date();
  139. try {
  140. // 测试各个接口
  141. const categoryOptionsResult = await testCategoryOptions();
  142. const statisticsResult = await testStatistics();
  143. const categoryTreeResult = await testCategoryTree();
  144. const orderListResult = await testRepairOrderList();
  145. // 汇总测试结果
  146. console.log('\n📊 测试结果汇总'.rainbow.bold);
  147. console.log('='.repeat(60).rainbow);
  148. const tests = [
  149. { name: '维修分类选项接口', result: categoryOptionsResult },
  150. { name: '统计数据接口', result: statisticsResult },
  151. { name: '维修分类树形接口', result: categoryTreeResult },
  152. { name: '报修工单列表接口', result: orderListResult },
  153. ];
  154. let successCount = 0;
  155. tests.forEach((test) => {
  156. const status = test.result.success ? '✅ 通过' : '❌ 失败';
  157. const statusColor = test.result.success ? 'green' : 'red';
  158. console.log(`${test.name}: ${colors[statusColor](status)}`);
  159. if (test.result.success) successCount++;
  160. });
  161. const endTime = new Date();
  162. const duration = endTime - startTime;
  163. console.log(`\n🎯 测试完成!`.bold);
  164. console.log(` - 成功: ${successCount}/${tests.length}`.green);
  165. console.log(` - 耗时: ${duration}ms`.cyan);
  166. if (successCount === tests.length) {
  167. console.log('\n🎉 所有测试通过!API接口工作正常。'.green.bold);
  168. } else {
  169. console.log('\n⚠️ 部分测试失败,请检查后端服务状态。'.yellow.bold);
  170. }
  171. } catch (error) {
  172. console.error('\n💥 测试执行出错:'.red.bold, error.message);
  173. }
  174. }
  175. // 如果直接运行此脚本
  176. if (require.main === module) {
  177. runTests()
  178. .then(() => {
  179. console.log('\n👋 测试脚本执行完毕'.cyan);
  180. })
  181. .catch((error) => {
  182. console.error('脚本执行失败:', error);
  183. process.exit(1);
  184. });
  185. }
  186. module.exports = {
  187. testAPI,
  188. testCategoryOptions,
  189. testStatistics,
  190. testCategoryTree,
  191. testRepairOrderList,
  192. runTests,
  193. };