GetRecordDetail.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const API = require("../../../lib/API")
  2. const db = require("../../../plugin/DataBase/db")
  3. const AccessControl = require("../../../lib/AccessControl")
  4. const { BaseStdResponse } = require("../../../BaseStdResponse")
  5. const { enrichLepaoRecordRow } = require("../../../plugin/jkes/formatRecordRow.js")
  6. class GetRecordDetail extends API {
  7. constructor() {
  8. super();
  9. this.setPath('/Lepao/GetRecordDetail')
  10. this.setMethod('GET')
  11. }
  12. async onRequest(req, res) {
  13. let {
  14. uuid,
  15. session,
  16. id
  17. } = req.query
  18. if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
  19. return res.json({
  20. ...BaseStdResponse.MISSING_PARAMETER
  21. })
  22. // 检查 session
  23. if (!await AccessControl.checkSession(uuid, session))
  24. return res.status(401).json({
  25. ...BaseStdResponse.ACCESS_DENIED
  26. })
  27. const baseWhere = `
  28. FROM
  29. lepao_record r
  30. LEFT JOIN
  31. lepao_account a
  32. ON
  33. r.lepao_account = a.student_num
  34. LEFT JOIN
  35. path_data p
  36. ON
  37. r.path_id = p.id
  38. WHERE
  39. (r.uuid = ? OR (a.create_user IS NOT NULL AND a.create_user = ?)) AND r.id = ?
  40. `
  41. let rows
  42. try {
  43. const sql = `
  44. SELECT
  45. r.time,
  46. r.result,
  47. r.state,
  48. r.lepao_account,
  49. r.path_id,
  50. r.path_data AS run_path_data,
  51. a.name,
  52. p.data
  53. ${baseWhere}
  54. `
  55. rows = await db.query(sql, [uuid, uuid, id])
  56. } catch (e) {
  57. if (!(e?.message || '').includes("Unknown column 'r.path_data'")) throw e
  58. const sql = `
  59. SELECT
  60. r.time,
  61. r.result,
  62. r.state,
  63. r.lepao_account,
  64. r.path_id,
  65. r.point_data AS run_path_data,
  66. a.name,
  67. p.data
  68. ${baseWhere}
  69. `
  70. rows = await db.query(sql, [uuid, uuid, id])
  71. }
  72. if (!rows)
  73. return res.json({
  74. ...BaseStdResponse.MISSING_FILE,
  75. msg: '获取记录数据失败!'
  76. })
  77. if(rows.length === 0)
  78. return res.json({
  79. ...BaseStdResponse.MISSING_FILE,
  80. msg: '记录不存在或已被删除!'
  81. })
  82. let data = rows[0]
  83. let pathLine = []
  84. const rawRunPath = data.run_path_data
  85. if (rawRunPath != null) {
  86. let arr = rawRunPath
  87. if (typeof rawRunPath === 'string') {
  88. try {
  89. arr = JSON.parse(rawRunPath)
  90. } catch {
  91. arr = null
  92. }
  93. }
  94. if (Array.isArray(arr)) {
  95. pathLine = arr.map((point) => [point.longitude ?? point.o, point.latitude ?? point.a])
  96. }
  97. }
  98. if (!pathLine.length) {
  99. const rawPath = data.data
  100. if (rawPath != null) {
  101. let arr = rawPath
  102. if (typeof rawPath === 'string') {
  103. try {
  104. arr = JSON.parse(rawPath)
  105. } catch {
  106. arr = null
  107. }
  108. }
  109. if (Array.isArray(arr)) {
  110. pathLine = arr.map((point) => [point.o, point.a])
  111. }
  112. }
  113. }
  114. data.path_polyline = pathLine
  115. delete data.data
  116. delete data.run_path_data
  117. data.jkes_record = enrichLepaoRecordRow({ result: data.result }).jkes_record
  118. res.json({
  119. ...BaseStdResponse.OK,
  120. data
  121. })
  122. }
  123. }
  124. module.exports.GetRecordDetail = GetRecordDetail