诊断面板和 console 识别摘要改为编译时设置 VITE_POSE_DIAG=1 才开启(与远程日志 VITE_POSE_LOG_URL 一致), 开发版平时不再显示,需要真机排查时再打开。
52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
/**
|
||
* 开发版真机排查(默认全部关闭,编译时用环境变量打开,发行版始终不生效):
|
||
* - VITE_POSE_DIAG=1:跟练页显示识别诊断面板,console 每 2 秒输出识别摘要
|
||
* - VITE_POSE_LOG_URL=http://电脑局域网IP:端口/log:把识别数据批量发到电脑上的日志服务,边练边看。
|
||
* 地址不写进代码;需要项目关闭合法域名校验(manifest 里 urlCheck: false)
|
||
*/
|
||
const LOG_URL: string = ((import.meta as any).env?.VITE_POSE_LOG_URL as string | undefined) || ''
|
||
const FLUSH_MS = 1000
|
||
/** 断网时最多攒这么多条,超出丢最旧的 */
|
||
const MAX_QUEUE = 600
|
||
|
||
const IS_DEV = process.env.NODE_ENV === 'development'
|
||
|
||
export const remoteLogEnabled = Boolean(LOG_URL) && IS_DEV
|
||
/** 识别诊断面板和 console 摘要 */
|
||
export const poseDiagEnabled = IS_DEV && Boolean((import.meta as any).env?.VITE_POSE_DIAG)
|
||
|
||
let queue: Record<string, unknown>[] = []
|
||
let timer: ReturnType<typeof setTimeout> | null = null
|
||
let session = ''
|
||
let failWarned = false
|
||
|
||
/** 开始一次新的跟练:之后的日志带上同一个会话编号 */
|
||
export function rlogSession() {
|
||
session = String(Date.now())
|
||
}
|
||
|
||
export function rlog(type: string, data: Record<string, unknown> = {}) {
|
||
if (!remoteLogEnabled) return
|
||
queue.push({ t: Date.now(), type, ...data })
|
||
if (queue.length > MAX_QUEUE) queue = queue.slice(-MAX_QUEUE)
|
||
if (!timer) timer = setTimeout(flush, FLUSH_MS)
|
||
}
|
||
|
||
function flush() {
|
||
timer = null
|
||
if (!queue.length) return
|
||
const entries = queue
|
||
queue = []
|
||
uni.request({
|
||
url: LOG_URL,
|
||
method: 'POST',
|
||
data: { session, entries },
|
||
timeout: 3000,
|
||
fail: (e) => {
|
||
if (failWarned) return
|
||
failWarned = true
|
||
console.warn('[跟练日志] 发送失败,检查手机和电脑是否在同一网络、日志服务是否启动', LOG_URL, e)
|
||
},
|
||
})
|
||
}
|