verify-home-stats.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const http = require('http')
  2. const BASE_URL = 'http://localhost:8080/api'
  3. const TOKENS = {
  4. sales: 'mock-token-mp-21',
  5. dispatch: 'mock-token-mp-13',
  6. construction: 'mock-token-mp-15',
  7. }
  8. function formatDate(date) {
  9. const y = date.getFullYear()
  10. const m = String(date.getMonth() + 1).padStart(2, '0')
  11. const d = String(date.getDate()).padStart(2, '0')
  12. return `${y}-${m}-${d}`
  13. }
  14. const today = formatDate(new Date())
  15. const tomorrowDate = new Date()
  16. tomorrowDate.setDate(tomorrowDate.getDate() + 1)
  17. const tomorrow = formatDate(tomorrowDate)
  18. function request(method, path, token, data) {
  19. return new Promise((resolve, reject) => {
  20. const options = {
  21. hostname: 'localhost',
  22. port: 8080,
  23. path: '/api' + path,
  24. method,
  25. headers: {
  26. 'Content-Type': 'application/json',
  27. 'Authorization': `Bearer ${token}`,
  28. },
  29. }
  30. const req = http.request(options, (res) => {
  31. let body = ''
  32. res.on('data', (chunk) => (body += chunk))
  33. res.on('end', () => {
  34. try {
  35. resolve(JSON.parse(body))
  36. } catch (e) {
  37. resolve(body)
  38. }
  39. })
  40. })
  41. req.on('error', reject)
  42. if (data) req.write(JSON.stringify(data))
  43. req.end()
  44. })
  45. }
  46. function countByStatus(tasks, statuses) {
  47. return tasks.filter(t => statuses.includes(t.status)).length
  48. }
  49. function countByDate(tasks, date) {
  50. return tasks.filter(t => {
  51. if (t.planDate === date) return true
  52. if (t.planDate && t.planEndDate && date >= t.planDate && date <= t.planEndDate) return true
  53. if (t.scheduleDate === date) return true
  54. if (t.serviceDate === date) return true
  55. return false
  56. }).length
  57. }
  58. async function main() {
  59. // 销售端:项目总数、待确认(auditing)、进行中(in_progress)
  60. const projectRes = await request('GET', '/project/list?pageNum=1&pageSize=1', TOKENS.sales)
  61. const salesTaskRes = await request('GET', '/task/list?pageNum=1&pageSize=100', TOKENS.sales)
  62. const salesTasks = salesTaskRes.data?.rows || []
  63. console.log('=== 销售端首页 ===')
  64. console.log('本月项目:', projectRes.data?.total || 0)
  65. console.log('待确认 (status=auditing):', countByStatus(salesTasks, ['auditing']))
  66. console.log('进行中 (status=in_progress):', countByStatus(salesTasks, ['in_progress']))
  67. console.log('任务状态分布:', salesTasks.reduce((acc, t) => {
  68. acc[t.status] = (acc[t.status] || 0) + 1
  69. return acc
  70. }, {}))
  71. // 调度端:今日任务、待审核(auditing)、待安排(approved)
  72. const dispatchMyRes = await request('GET', `/task/my?planDate=${today}`, TOKENS.dispatch)
  73. const dispatchTasks = dispatchMyRes.data || dispatchMyRes || []
  74. console.log('\n=== 调度端首页 ===')
  75. console.log('今日任务 (planDate/scheduleDate/serviceDate = today):', countByDate(dispatchTasks, today), '/', dispatchTasks.length)
  76. console.log('待审核 (status=auditing):', countByStatus(dispatchTasks, ['auditing']))
  77. console.log('待安排 (status=approved):', countByStatus(dispatchTasks, ['approved']))
  78. console.log('任务状态分布:', dispatchTasks.reduce((acc, t) => {
  79. acc[t.status] = (acc[t.status] || 0) + 1
  80. return acc
  81. }, {}))
  82. // 施工端:已完成(completed)、进行中(in_progress)、待确认(assigned)
  83. const constructionMyRes = await request('GET', '/task/my', TOKENS.construction)
  84. const constructionTasks = constructionMyRes.data || constructionMyRes || []
  85. console.log('\n=== 施工端首页 ===')
  86. console.log('已完成 (status=completed):', countByStatus(constructionTasks, ['completed']))
  87. console.log('进行中 (status=in_progress):', countByStatus(constructionTasks, ['in_progress']))
  88. console.log('待确认 (status=assigned):', countByStatus(constructionTasks, ['assigned']))
  89. console.log('任务状态分布:', constructionTasks.reduce((acc, t) => {
  90. acc[t.status] = (acc[t.status] || 0) + 1
  91. return acc
  92. }, {}))
  93. }
  94. main().catch(err => {
  95. console.error('验证失败:', err)
  96. process.exit(1)
  97. })