zwc007-update-progress-test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. const axios = require('axios');
  2. const colors = require('colors');
  3. // 测试配置
  4. const CONFIG = {
  5. baseURL: 'http://localhost/dev-api',
  6. username: 'zwc007',
  7. password: 'admin123', // 需要根据实际密码调整
  8. timeout: 10000,
  9. };
  10. /**
  11. * 获取Token的通用函数
  12. */
  13. async function getToken(username, password) {
  14. const { getToken } = require('./quick-token-getter');
  15. return await getToken(username, password);
  16. }
  17. /**
  18. * 测试API调用的通用函数
  19. */
  20. async function testAPI(endpoint, data, options = {}) {
  21. try {
  22. const response = await axios({
  23. method: options.method || 'GET',
  24. url: `${CONFIG.baseURL}${endpoint}`,
  25. data: data,
  26. headers: {
  27. 'Content-Type': 'application/json;charset=utf-8',
  28. clientid: 'e5cd7e4891bf95d1d19206ce24a7b32e',
  29. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  30. referer: 'http://localhost/login?redirect=%252Findex',
  31. Authorization: options.token ? `Bearer ${options.token}` : undefined,
  32. ...options.headers,
  33. },
  34. timeout: CONFIG.timeout,
  35. });
  36. return {
  37. success: true,
  38. data: response.data,
  39. status: response.status,
  40. };
  41. } catch (error) {
  42. return {
  43. success: false,
  44. error: error.response?.data || error.message,
  45. status: error.response?.status,
  46. };
  47. }
  48. }
  49. /**
  50. * 测试zwc007更新进度按钮权限
  51. */
  52. async function testZwc007UpdateProgressPermission() {
  53. console.log('\n' + '='.repeat(80));
  54. console.log(colors.cyan.bold(' zwc007更新进度按钮权限测试'));
  55. console.log('='.repeat(80));
  56. console.log(colors.yellow(`测试时间: ${new Date().toLocaleString()}`));
  57. console.log(colors.yellow(`测试用户: ${CONFIG.username}`));
  58. console.log('='.repeat(80));
  59. const results = {
  60. login: null,
  61. userInfo: null,
  62. orderList: null,
  63. updateProgressPermission: null,
  64. assignedOrders: [],
  65. testResults: [],
  66. };
  67. try {
  68. // 步骤1:登录获取Token
  69. console.log(colors.blue('\n【步骤1】zwc007用户登录测试'));
  70. console.log('----------------------------------------');
  71. try {
  72. const token = await getToken(CONFIG.username, CONFIG.password);
  73. results.login = { success: true, token: token.substring(0, 50) + '...' };
  74. console.log(colors.green('✓ 登录成功'));
  75. console.log(`Token: ${token.substring(0, 50)}...`);
  76. } catch (error) {
  77. results.login = { success: false, error: error.message };
  78. console.log(colors.red('✗ 登录失败: ' + error.message));
  79. return results;
  80. }
  81. // 步骤2:获取用户信息,检查角色和权限
  82. console.log(colors.blue('\n【步骤2】用户信息和权限检查'));
  83. console.log('----------------------------------------');
  84. const userInfoResult = await testAPI('/system/user/getInfo', null, {
  85. method: 'GET',
  86. token:
  87. results.login.token.split('...')[0] +
  88. (await getToken(CONFIG.username, CONFIG.password)).substring(50),
  89. });
  90. if (userInfoResult.success) {
  91. const userInfo = userInfoResult.data.data || userInfoResult.data;
  92. results.userInfo = {
  93. userId: userInfo.user?.userId,
  94. userName: userInfo.user?.userName,
  95. nickName: userInfo.user?.nickName,
  96. roles: userInfo.roles?.map((r) => r.roleKey) || [],
  97. permissions: userInfo.permissions || [],
  98. };
  99. console.log(colors.green('✓ 用户信息获取成功'));
  100. console.log(`用户ID: ${results.userInfo.userId}`);
  101. console.log(`用户名: ${results.userInfo.userName}`);
  102. console.log(`昵称: ${results.userInfo.nickName}`);
  103. console.log(`角色: ${results.userInfo.roles.join(', ')}`);
  104. // 检查维修人员角色
  105. const hasMaintenanceRole = results.userInfo.roles.includes('maintenance_worker');
  106. console.log(
  107. `维修人员角色: ${
  108. hasMaintenanceRole ? colors.green('✓ 已配置') : colors.red('✗ 缺少')
  109. }`,
  110. );
  111. // 检查更新进度权限
  112. const hasUpdateProgressPermission = results.userInfo.permissions.includes(
  113. 'pm:repairOrder:updateProgress',
  114. );
  115. console.log(
  116. `更新进度权限: ${
  117. hasUpdateProgressPermission ? colors.green('✓ 已配置') : colors.red('✗ 缺少')
  118. }`,
  119. );
  120. results.updateProgressPermission = hasUpdateProgressPermission;
  121. } else {
  122. console.log(colors.red('✗ 用户信息获取失败: ' + JSON.stringify(userInfoResult.error)));
  123. }
  124. // 步骤3:获取工单列表,检查数据权限过滤
  125. console.log(colors.blue('\n【步骤3】工单列表和数据权限检查'));
  126. console.log('----------------------------------------');
  127. const orderListResult = await testAPI('/pm/repairOrder/list', null, {
  128. method: 'GET',
  129. token: await getToken(CONFIG.username, CONFIG.password),
  130. });
  131. if (orderListResult.success) {
  132. const orderData = orderListResult.data;
  133. const orders = orderData.rows || orderData.data || [];
  134. results.orderList = {
  135. total: orderData.total || orders.length,
  136. count: orders.length,
  137. orders: orders,
  138. };
  139. console.log(colors.green('✓ 工单列表获取成功'));
  140. console.log(`可见工单总数: ${results.orderList.total}`);
  141. console.log(`当前页工单数: ${results.orderList.count}`);
  142. // 分析工单状态和负责人
  143. const statusAnalysis = {};
  144. const assigneeAnalysis = {};
  145. let myAssignedOrders = 0;
  146. let myAssignedStatus2Orders = 0;
  147. orders.forEach((order) => {
  148. // 状态统计
  149. const statusText = getStatusText(order.orderStatus);
  150. statusAnalysis[statusText] = (statusAnalysis[statusText] || 0) + 1;
  151. // 负责人统计
  152. if (order.assigneeId) {
  153. const assigneeInfo = `${order.assigneeName || '未知'}(${order.assigneeId})`;
  154. assigneeAnalysis[assigneeInfo] = (assigneeAnalysis[assigneeInfo] || 0) + 1;
  155. // 检查是否为zwc007负责的工单
  156. if (order.assigneeId === results.userInfo?.userId) {
  157. myAssignedOrders++;
  158. if (order.orderStatus === '2' || order.orderStatus === 2) {
  159. myAssignedStatus2Orders++;
  160. results.assignedOrders.push({
  161. id: order.id,
  162. orderNo: order.orderNo,
  163. title: order.title,
  164. orderStatus: order.orderStatus,
  165. shouldShowUpdateButton: true,
  166. });
  167. }
  168. }
  169. }
  170. });
  171. console.log('\n工单状态分布:');
  172. Object.entries(statusAnalysis).forEach(([status, count]) => {
  173. console.log(` ${status}: ${count}个`);
  174. });
  175. console.log('\n负责人分布:');
  176. Object.entries(assigneeAnalysis).forEach(([assignee, count]) => {
  177. console.log(` ${assignee}: ${count}个`);
  178. });
  179. console.log(`\n${CONFIG.username}负责的工单: ${myAssignedOrders}个`);
  180. console.log(
  181. `${CONFIG.username}负责的已分派工单: ${myAssignedStatus2Orders}个 ${
  182. myAssignedStatus2Orders > 0
  183. ? colors.green('(应显示更新进度按钮)')
  184. : colors.red('(无法显示更新进度按钮)')
  185. }`,
  186. );
  187. } else {
  188. console.log(colors.red('✗ 工单列表获取失败: ' + JSON.stringify(orderListResult.error)));
  189. }
  190. // 步骤4:测试更新进度API权限
  191. console.log(colors.blue('\n【步骤4】更新进度API权限测试'));
  192. console.log('----------------------------------------');
  193. if (results.assignedOrders.length > 0) {
  194. const testOrder = results.assignedOrders[0];
  195. console.log(`测试工单: ${testOrder.orderNo} - ${testOrder.title}`);
  196. // 尝试调用更新进度API
  197. const progressData = {
  198. orderId: testOrder.id,
  199. progressType: '5',
  200. progressStatus: '测试进度状态',
  201. progressDetail: '测试进度更新 - 验证权限配置',
  202. locationInfo: '测试位置',
  203. remark: '权限测试备注',
  204. };
  205. const updateResult = await testAPI('/pm/repairOrder/updateProgress', progressData, {
  206. method: 'POST',
  207. token: await getToken(CONFIG.username, CONFIG.password),
  208. });
  209. if (updateResult.success) {
  210. console.log(colors.green('✓ 更新进度API调用成功'));
  211. console.log('响应数据:', JSON.stringify(updateResult.data, null, 2));
  212. results.testResults.push({
  213. api: 'updateProgress',
  214. success: true,
  215. orderNo: testOrder.orderNo,
  216. });
  217. } else {
  218. console.log(colors.red('✗ 更新进度API调用失败'));
  219. console.log('错误信息:', JSON.stringify(updateResult.error, null, 2));
  220. results.testResults.push({
  221. api: 'updateProgress',
  222. success: false,
  223. error: updateResult.error,
  224. orderNo: testOrder.orderNo,
  225. });
  226. }
  227. } else {
  228. console.log(colors.yellow('⚠ 没有找到zwc007负责的已分派工单,无法测试更新进度API'));
  229. results.testResults.push({
  230. api: 'updateProgress',
  231. success: false,
  232. error: '没有可测试的工单',
  233. });
  234. }
  235. // 步骤5:生成测试报告
  236. console.log(colors.blue('\n【步骤5】测试结果总结'));
  237. console.log('----------------------------------------');
  238. const hasRole = results.userInfo?.roles.includes('maintenance_worker');
  239. const hasPermission = results.updateProgressPermission;
  240. const hasAssignedOrders = results.assignedOrders.length > 0;
  241. const apiCallSuccess = results.testResults.some(
  242. (r) => r.api === 'updateProgress' && r.success,
  243. );
  244. console.log(`维修人员角色: ${hasRole ? colors.green('✓') : colors.red('✗')}`);
  245. console.log(`更新进度权限: ${hasPermission ? colors.green('✓') : colors.red('✗')}`);
  246. console.log(
  247. `负责的已分派工单: ${hasAssignedOrders ? colors.green('✓') : colors.red('✗')} (${
  248. results.assignedOrders.length
  249. }个)`,
  250. );
  251. console.log(`API调用权限: ${apiCallSuccess ? colors.green('✓') : colors.red('✗')}`);
  252. const allConditionsMet = hasRole && hasPermission && hasAssignedOrders;
  253. console.log(
  254. `\n更新进度按钮应该显示: ${allConditionsMet ? colors.green('是') : colors.red('否')}`,
  255. );
  256. if (!allConditionsMet) {
  257. console.log(colors.yellow('\n问题解决建议:'));
  258. if (!hasRole) console.log(colors.yellow('- 需要为zwc007分配maintenance_worker角色'));
  259. if (!hasPermission)
  260. console.log(
  261. colors.yellow('- 需要为维修人员角色分配pm:repairOrder:updateProgress权限'),
  262. );
  263. if (!hasAssignedOrders)
  264. console.log(colors.yellow('- 需要创建状态为"已分派"且zwc007为负责人的工单'));
  265. console.log(
  266. colors.yellow('- 执行 test/zwc007-update-progress-diagnosis.sql 脚本进行修复'),
  267. );
  268. }
  269. } catch (error) {
  270. console.log(colors.red('测试过程中发生错误: ' + error.message));
  271. results.error = error.message;
  272. }
  273. console.log('\n' + '='.repeat(80));
  274. console.log(colors.cyan.bold(' 测试完成'));
  275. console.log('='.repeat(80));
  276. return results;
  277. }
  278. /**
  279. * 获取状态文本
  280. */
  281. function getStatusText(status) {
  282. const statusMap = {
  283. 1: '待分派',
  284. 2: '已分派',
  285. 3: '进行中',
  286. 4: '已完成',
  287. 5: '已评价',
  288. 6: '已归档',
  289. 9: '已取消',
  290. };
  291. return statusMap[status] || `状态${status}`;
  292. }
  293. // 执行测试
  294. if (require.main === module) {
  295. testZwc007UpdateProgressPermission()
  296. .then((results) => {
  297. console.log('\n测试结果已保存');
  298. })
  299. .catch((error) => {
  300. console.error(colors.red('测试执行失败:'), error);
  301. });
  302. }
  303. module.exports = {
  304. testZwc007UpdateProgressPermission,
  305. };