| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- const API = require("../../../lib/API")
- const db = require("../../../plugin/DataBase/db")
- const AccessControl = require("../../../lib/AccessControl")
- const { BaseStdResponse } = require("../../../BaseStdResponse")
- const { enrichLepaoRecordRow } = require("../../../plugin/jkes/formatRecordRow.js")
- class GetRecordDetail extends API {
- constructor() {
- super();
- this.setPath('/Lepao/GetRecordDetail')
- this.setMethod('GET')
- }
- async onRequest(req, res) {
- let {
- uuid,
- session,
- id
- } = req.query
- if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
- return res.json({
- ...BaseStdResponse.MISSING_PARAMETER
- })
- // 检查 session
- if (!await AccessControl.checkSession(uuid, session))
- return res.status(401).json({
- ...BaseStdResponse.ACCESS_DENIED
- })
- const baseWhere = `
- FROM
- lepao_record r
- LEFT JOIN
- lepao_account a
- ON
- r.lepao_account = a.student_num
- LEFT JOIN
- path_data p
- ON
- r.path_id = p.id
- WHERE
- (r.uuid = ? OR (a.create_user IS NOT NULL AND a.create_user = ?)) AND r.id = ?
- `
- let rows
- try {
- const sql = `
- SELECT
- r.time,
- r.result,
- r.state,
- r.lepao_account,
- r.path_id,
- r.path_data AS run_path_data,
- a.name,
- p.data
- ${baseWhere}
- `
- rows = await db.query(sql, [uuid, uuid, id])
- } catch (e) {
- if (!(e?.message || '').includes("Unknown column 'r.path_data'")) throw e
- const sql = `
- SELECT
- r.time,
- r.result,
- r.state,
- r.lepao_account,
- r.path_id,
- r.point_data AS run_path_data,
- a.name,
- p.data
- ${baseWhere}
- `
- rows = await db.query(sql, [uuid, uuid, id])
- }
- if (!rows)
- return res.json({
- ...BaseStdResponse.MISSING_FILE,
- msg: '获取记录数据失败!'
- })
- if(rows.length === 0)
- return res.json({
- ...BaseStdResponse.MISSING_FILE,
- msg: '记录不存在或已被删除!'
- })
- let data = rows[0]
- let pathLine = []
- const rawRunPath = data.run_path_data
- if (rawRunPath != null) {
- let arr = rawRunPath
- if (typeof rawRunPath === 'string') {
- try {
- arr = JSON.parse(rawRunPath)
- } catch {
- arr = null
- }
- }
- if (Array.isArray(arr)) {
- pathLine = arr.map((point) => [point.longitude ?? point.o, point.latitude ?? point.a])
- }
- }
- if (!pathLine.length) {
- const rawPath = data.data
- if (rawPath != null) {
- let arr = rawPath
- if (typeof rawPath === 'string') {
- try {
- arr = JSON.parse(rawPath)
- } catch {
- arr = null
- }
- }
- if (Array.isArray(arr)) {
- pathLine = arr.map((point) => [point.o, point.a])
- }
- }
- }
- data.path_polyline = pathLine
- delete data.data
- delete data.run_path_data
- data.jkes_record = enrichLepaoRecordRow({ result: data.result }).jkes_record
- res.json({
- ...BaseStdResponse.OK,
- data
- })
- }
- }
- module.exports.GetRecordDetail = GetRecordDetail
|