GetRecordDetail.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 AdminGetRecordDetail extends API {
  7. constructor() {
  8. super();
  9. this.setPath('/Admin/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. // 检查权限
  28. let permission = await AccessControl.getPermission(uuid)
  29. if (!permission.includes("admin") && !permission.includes("server"))
  30. return res.json({
  31. ...BaseStdResponse.PERMISSION_DENIED
  32. })
  33. const baseWhere = `
  34. FROM
  35. lepao_record r
  36. LEFT JOIN
  37. lepao_account a
  38. ON
  39. r.lepao_account = a.student_num
  40. LEFT JOIN
  41. path_data p
  42. ON
  43. r.path_id = p.id
  44. WHERE
  45. r.id = ?
  46. `
  47. let rows
  48. try {
  49. const sql = `
  50. SELECT
  51. r.time,
  52. r.result,
  53. r.state,
  54. r.lepao_account,
  55. r.path_id,
  56. r.path_data AS run_path_data,
  57. a.name,
  58. p.data,
  59. p.run_zone_name,
  60. p.run_zone_name AS path_run_zone_name,
  61. NULL AS path_distance
  62. ${baseWhere}
  63. `
  64. rows = await db.query(sql, [id])
  65. } catch (e) {
  66. if (!(e?.message || '').includes("Unknown column 'r.path_data'")) throw e
  67. const sql = `
  68. SELECT
  69. r.time,
  70. r.result,
  71. r.state,
  72. r.lepao_account,
  73. r.path_id,
  74. r.point_data AS run_path_data,
  75. a.name,
  76. p.data,
  77. p.run_zone_name,
  78. p.run_zone_name AS path_run_zone_name,
  79. NULL AS path_distance
  80. ${baseWhere}
  81. `
  82. rows = await db.query(sql, [id])
  83. }
  84. if (!rows)
  85. return res.json({
  86. ...BaseStdResponse.MISSING_FILE,
  87. msg: '获取记录数据失败!'
  88. })
  89. if(rows.length === 0)
  90. return res.json({
  91. ...BaseStdResponse.MISSING_FILE,
  92. msg: '记录不存在或已被删除!'
  93. })
  94. let data = rows[0]
  95. let pathLine = []
  96. const rawRunPath = data.run_path_data
  97. if (rawRunPath != null) {
  98. let arr = rawRunPath
  99. if (typeof rawRunPath === 'string') {
  100. try {
  101. arr = JSON.parse(rawRunPath)
  102. } catch {
  103. arr = null
  104. }
  105. }
  106. if (Array.isArray(arr)) {
  107. pathLine = arr.map((point) => [point.longitude ?? point.o, point.latitude ?? point.a])
  108. }
  109. }
  110. if (!pathLine.length) {
  111. const rawPath = data.data
  112. if (rawPath != null) {
  113. let arr = rawPath
  114. if (typeof rawPath === 'string') {
  115. try {
  116. arr = JSON.parse(rawPath)
  117. } catch {
  118. arr = null
  119. }
  120. }
  121. if (Array.isArray(arr)) {
  122. pathLine = arr.map((point) => [point.o, point.a])
  123. }
  124. }
  125. }
  126. data.path_polyline = pathLine
  127. delete data.data
  128. delete data.run_path_data
  129. data.jkes_record = enrichLepaoRecordRow({ result: data.result }).jkes_record
  130. res.json({
  131. ...BaseStdResponse.OK,
  132. data
  133. })
  134. }
  135. }
  136. module.exports.AdminGetRecordDetail = AdminGetRecordDetail