feat: 新增运动节拍训练工具套件与子工具页面
新增运动节拍RepClock全套小程序页面、后端API与数据库服务,新增subhub子工具汇总页面,优化全局主页样式与配置,为web静态资源添加版本缓存参数,补充配套调试脚本
This commit is contained in:
@@ -0,0 +1,610 @@
|
||||
const api = require('../../../../api.js')
|
||||
|
||||
const PHASE_ACTION = 'action'
|
||||
const PHASE_REST = 'rest'
|
||||
const PHASE_IDLE = 'idle'
|
||||
|
||||
const BASE_URL_SAFE = function () {
|
||||
try {
|
||||
const a = getApp()
|
||||
if (a && a.globalData && a.globalData.apiBase) return a.globalData.apiBase
|
||||
} catch (e) {}
|
||||
return 'https://traesite.umersoft.com:8975'
|
||||
}
|
||||
|
||||
function clamp(v, a, b) { const x = Number(v) || 0; return Math.max(a, Math.min(b, x)) }
|
||||
function pad2(n) { const x = Math.max(0, Math.floor(Number(n) || 0)); return x < 10 ? '0' + x : String(x) }
|
||||
|
||||
function expandPlan(card) {
|
||||
const steps = Array.isArray(card.steps) ? card.steps : []
|
||||
const mode = card.loopMode || 'sequential_once'
|
||||
const times = mode === 'loop_n_times' ? clamp(card.loopTimes || 1, 1, 999) : 1
|
||||
const plan = []
|
||||
const loopUntil = mode === 'loop_till_stop'
|
||||
const loopCount = loopUntil ? 1 : times
|
||||
for (let round = 1; round <= loopCount; round++) {
|
||||
steps.forEach((step, idx) => {
|
||||
plan.push({
|
||||
round,
|
||||
roundTotal: loopCount,
|
||||
loopUntil,
|
||||
phase: PHASE_ACTION,
|
||||
stepIndex: idx,
|
||||
stepName: step.name || '动作',
|
||||
voiceText: step.voiceText || step.name || '动作',
|
||||
durationSec: clamp(step.durationSec, 1, 3600),
|
||||
restSec: clamp(step.restSec, 0, 3600),
|
||||
})
|
||||
})
|
||||
}
|
||||
return { plan, loopUntil }
|
||||
}
|
||||
|
||||
function sanitizeAnnounceLead(raw) {
|
||||
const list = String(raw || '').split(/[,,\s]+/).map(x => Number(x.trim())).filter(n => Number.isFinite(n) && n > 0 && n <= 30)
|
||||
const s = Array.from(new Set(list)).sort((a, b) => b - a)
|
||||
return s.length ? s : [5, 3, 1]
|
||||
}
|
||||
|
||||
function normalizeAudioUrl(raw) {
|
||||
const url = String(raw || '').trim()
|
||||
if (!url) return ''
|
||||
if (/^https?:\/\//i.test(url)) return url
|
||||
if (url.startsWith('/')) return BASE_URL_SAFE() + url
|
||||
return `${BASE_URL_SAFE()}/${url.replace(/^\/+/, '')}`
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
cardId: '',
|
||||
cardTitle: '运动节拍',
|
||||
started: false,
|
||||
paused: false,
|
||||
phaseLabel: '准备开始',
|
||||
stepSeq: '',
|
||||
remainingText: '00:00',
|
||||
stepName: '',
|
||||
nextName: '',
|
||||
elapsedMin: '00',
|
||||
elapsedSecPad: '00',
|
||||
remainingMin: '00',
|
||||
remainingSecPad: '00',
|
||||
completion: 0,
|
||||
hasCountdown: true,
|
||||
countdownText: '5,3,1',
|
||||
},
|
||||
|
||||
_i: null,
|
||||
|
||||
onLoad(options) {
|
||||
this._i = {
|
||||
card: null,
|
||||
plan: [],
|
||||
loopUntil: false,
|
||||
cursor: 0,
|
||||
phase: PHASE_IDLE,
|
||||
durationMs: 0,
|
||||
phaseStartAt: 0,
|
||||
phaseEndAt: 0,
|
||||
pausedAt: 0,
|
||||
pausedTotalMs: 0,
|
||||
globalStartAt: 0,
|
||||
announcedSet: null,
|
||||
countdownSet: [],
|
||||
voice: 'female_01',
|
||||
sessionId: '',
|
||||
lastTickEmit: 0,
|
||||
lastServerSyncAt: 0,
|
||||
ttsQueue: [],
|
||||
ttsCtx: null,
|
||||
ttsSrc: {},
|
||||
lastAnnouncedText: '',
|
||||
tickTimer: null,
|
||||
hiddenAt: 0,
|
||||
finishing: false,
|
||||
}
|
||||
const cardId = String((options && options.cardId) || '').trim()
|
||||
this.data.cardId = cardId
|
||||
wx.showLoading({ title: '加载卡片…', mask: true })
|
||||
Promise.resolve().then(async () => {
|
||||
if (!cardId) throw new Error('缺少卡片ID')
|
||||
const res = await api.repClock.getCard(cardId)
|
||||
const card = res && res.card ? res.card : null
|
||||
if (!card) throw new Error('卡片不存在')
|
||||
const { plan, loopUntil } = expandPlan(card)
|
||||
if (!plan.length) throw new Error('卡片没有步骤')
|
||||
const i = this._i
|
||||
i.card = card
|
||||
i.plan = plan
|
||||
i.loopUntil = loopUntil
|
||||
i.voice = card.voice || 'female_01'
|
||||
i.announcedSet = new Set()
|
||||
i.countdownSet = sanitizeAnnounceLead(card.announceLeadSeconds || '5,3,1')
|
||||
this._preloadTtsCache(plan, i.voice).catch(() => {})
|
||||
this._ensureAudioCtx()
|
||||
const totalDurationMs = plan.reduce((acc, p) => acc + (p.durationSec + p.restSec) * 1000, 0)
|
||||
const lead = i.countdownSet.length ? i.countdownSet.join(',') : '5,3,1'
|
||||
this.setData({
|
||||
cardTitle: String(card.title || '运动节拍'),
|
||||
countdownText: lead,
|
||||
hasCountdown: !!card.enableCountdownAnnounce !== false,
|
||||
remainingMin: pad2(Math.floor(totalDurationMs / 60000)),
|
||||
remainingSecPad: pad2(Math.floor((totalDurationMs % 60000) / 1000)),
|
||||
})
|
||||
try { wx.hideLoading() } catch (_) {}
|
||||
}).catch(e => {
|
||||
try { wx.hideLoading() } catch (_) {}
|
||||
const msg = e && e.message ? String(e.message) : '加载失败'
|
||||
wx.showToast({ title: msg, icon: 'none', duration: 1500 })
|
||||
setTimeout(() => wx.navigateBack(), 1500)
|
||||
})
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
this._clearTickTimer()
|
||||
this._destroyAudioCtx()
|
||||
if (this._i && this._i.sessionId && !this._i.finishing) {
|
||||
const sid = this._i.sessionId
|
||||
api.repClock.sessionFinish({ sessionId: sid, completed: false, force: true }).catch(() => {})
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
const i = this._i
|
||||
if (!this.data.started || !i.hiddenAt) return
|
||||
const now = Date.now()
|
||||
const hiddenMs = Math.max(0, now - i.hiddenAt)
|
||||
i.hiddenAt = 0
|
||||
if (!this.data.paused) this._applyBgCompensation(hiddenMs)
|
||||
},
|
||||
|
||||
onHide() {
|
||||
this._i.hiddenAt = Date.now()
|
||||
},
|
||||
|
||||
_ensureAudioCtx() {
|
||||
if (this._i.ttsCtx) return
|
||||
try {
|
||||
// iOS 微信 InnerAudioContext 默认 obeyMuteSwitch=true,iPhone 开启静音键会无声;运动播报必须忽略静音键
|
||||
if (wx.setInnerAudioOption) {
|
||||
wx.setInnerAudioOption({ obeyMuteSwitch: false, mixWithOther: true, speakerOn: true })
|
||||
}
|
||||
const ctx = wx.createInnerAudioContext()
|
||||
ctx.src = ''
|
||||
ctx.volume = 1.0
|
||||
ctx.obeyMuteSwitch = false
|
||||
ctx.speakerOn = true
|
||||
ctx.onEnded(() => this._onTtsEnded())
|
||||
ctx.onError((err) => this._onTtsFailed(err))
|
||||
this._i.ttsCtx = ctx
|
||||
} catch (e) {
|
||||
this._i.ttsCtx = null
|
||||
}
|
||||
},
|
||||
_destroyAudioCtx() {
|
||||
try {
|
||||
if (this._i.ttsCtx && this._i.ttsCtx.destroy) this._i.ttsCtx.destroy()
|
||||
} catch (e) {}
|
||||
this._i.ttsCtx = null
|
||||
},
|
||||
|
||||
async _preloadTtsCache(plan, voice) {
|
||||
try {
|
||||
const r = await api.repClock.ttsBatch(this.data.cardId, this._i.card, voice)
|
||||
const map = (r && r.data && Array.isArray(r.data.items)) ? r.data.items : []
|
||||
map.forEach(it => {
|
||||
if (it && it.text) this._i.ttsSrc[String(it.text).trim()] = normalizeAudioUrl(it.url || '')
|
||||
})
|
||||
} catch (e) {}
|
||||
},
|
||||
|
||||
_onTtsEnded() {
|
||||
this._i.lastAnnouncedText = ''
|
||||
this._playNextTtsOrBeep()
|
||||
},
|
||||
_onTtsFailed(err) {
|
||||
this._i.lastAnnouncedText = ''
|
||||
// iOS 上 InnerAudioContext 常见 errCode:10003=格式不支持、10001=系统级失败;用于排查无声问题
|
||||
try { console.error('[rep_clock] TTS 播放失败:', JSON.stringify(err || {})) } catch (e) {}
|
||||
this._fallbackBeep()
|
||||
this._playNextTtsOrBeep()
|
||||
},
|
||||
|
||||
_enqueueTts(text, priority = 0) {
|
||||
if (!text) return
|
||||
const i = this._i
|
||||
i.ttsQueue.push({ text, priority: Number(priority) || 0 })
|
||||
i.ttsQueue.sort((a, b) => (b.priority - a.priority))
|
||||
if (!i.ttsCtx) this._ensureAudioCtx()
|
||||
if (i.ttsCtx && !this._isPlayingNow()) {
|
||||
this._playNextTtsOrBeep()
|
||||
}
|
||||
},
|
||||
|
||||
_isPlayingNow() {
|
||||
try { return this._i.ttsCtx && this._i.ttsCtx.src && this._i.lastAnnouncedText ? true : false } catch (e) { return false }
|
||||
},
|
||||
|
||||
_playNextTtsOrBeep() {
|
||||
const i = this._i
|
||||
if (!i.ttsCtx) { this._fallbackBeep(); return }
|
||||
if (!i.ttsQueue.length) return
|
||||
const item = i.ttsQueue.shift()
|
||||
const text = item.text
|
||||
const url = normalizeAudioUrl(i.ttsSrc[String(text).trim()] || '')
|
||||
if (!url) {
|
||||
this._fallbackBeep()
|
||||
this._vibrate()
|
||||
if (!this._isPlayingNow()) this._playNextTtsOrBeep()
|
||||
return
|
||||
}
|
||||
try {
|
||||
i.lastAnnouncedText = text
|
||||
i.ttsCtx.stop()
|
||||
// encodeURI:iOS 微信对含中文/特殊字符的音频 URL 加载容易静默失败
|
||||
i.ttsCtx.src = encodeURI(url)
|
||||
i.ttsCtx.play()
|
||||
} catch (e) {
|
||||
this._fallbackBeep()
|
||||
this._vibrate()
|
||||
i.lastAnnouncedText = ''
|
||||
this._playNextTtsOrBeep()
|
||||
}
|
||||
},
|
||||
|
||||
_fallbackBeep() {
|
||||
try {
|
||||
if (!this._fallbackBeepCtx) {
|
||||
const ctx = wx.createInnerAudioContext()
|
||||
ctx.volume = 1.0
|
||||
this._fallbackBeepCtx = ctx
|
||||
const list = [{ text: '__beep__', src: '' }]
|
||||
this._fallbackBeepList = list
|
||||
}
|
||||
this._fallbackBeepCtx.stop()
|
||||
// 微信 InnerAudioContext 无 src 时不一定会触发 error,但会静默失败;使用震动兜底
|
||||
this._vibrate()
|
||||
} catch (e) {
|
||||
this._vibrate()
|
||||
}
|
||||
},
|
||||
|
||||
_vibrate() {
|
||||
try { wx.vibrateShort({ type: 'medium' }) } catch (e) {}
|
||||
},
|
||||
|
||||
async onStart() {
|
||||
if (this.data.started) return
|
||||
const i = this._i
|
||||
const now = Date.now()
|
||||
wx.showLoading({ title: '启动中…', mask: true })
|
||||
try {
|
||||
const r = await api.repClock.sessionStart({ cardId: this.data.cardId })
|
||||
if (!r || !r.session || !r.session.id) throw new Error('启动失败')
|
||||
i.sessionId = r.session.id
|
||||
i.globalStartAt = now
|
||||
i.pausedTotalMs = 0
|
||||
i.cursor = 0
|
||||
this._announceStart()
|
||||
this._enterPhase(PHASE_ACTION, now)
|
||||
this._startTickLoop()
|
||||
this.setData({ started: true, paused: false })
|
||||
wx.hideLoading()
|
||||
} catch (e) {
|
||||
wx.hideLoading()
|
||||
const msg = e && e.message ? String(e.message) : ''
|
||||
if (/409|冲突|running session exists/.test(msg)) {
|
||||
const waitSec = e && e.expiresInMs ? Math.ceil(Number(e.expiresInMs) / 1000) : 300
|
||||
wx.showModal({
|
||||
title: '已有运动进行中',
|
||||
content: `检测到另一会话正在进行,${waitSec} 秒后可自动解锁。或选择立即强制抢占(建议先去其他页面结束后再启动)。`,
|
||||
confirmText: '立即强制开始',
|
||||
cancelText: '等待自动解锁',
|
||||
confirmColor: '#e05561',
|
||||
showCancel: true,
|
||||
success: async (mr) => {
|
||||
if (mr.confirm) {
|
||||
try { await api.repClock.resetRunning('') } catch (_) {}
|
||||
wx.showLoading({ title: '启动中…', mask: true })
|
||||
try {
|
||||
const r2 = await api.repClock.sessionStart({ cardId: this.data.cardId })
|
||||
if (!r2 || !r2.session || !r2.session.id) throw new Error('启动失败')
|
||||
i.sessionId = r2.session.id
|
||||
i.globalStartAt = Date.now()
|
||||
i.pausedTotalMs = 0
|
||||
i.cursor = 0
|
||||
this._announceStart()
|
||||
this._enterPhase(PHASE_ACTION, Date.now())
|
||||
this._startTickLoop()
|
||||
this.setData({ started: true, paused: false })
|
||||
wx.hideLoading()
|
||||
} catch (e2) {
|
||||
wx.hideLoading()
|
||||
wx.showModal({ title: '仍无法启动', content: (e2 && e2.message ? e2.message : '未知错误') + ',请稍后重试或重启服务。', showCancel: false, confirmText: '知道了' })
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
} else {
|
||||
wx.showModal({ title: '启动失败', content: msg || '请检查网络或重启服务后重试。', showCancel: false, confirmText: '知道了' })
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
onPause() {
|
||||
if (!this.data.started || this.data.paused) return
|
||||
const now = Date.now()
|
||||
this._i.pausedAt = now
|
||||
this.setData({ paused: true })
|
||||
},
|
||||
onResume() {
|
||||
if (!this.data.started || !this.data.paused) return
|
||||
const now = Date.now()
|
||||
const pMs = Math.max(0, now - (this._i.pausedAt || now))
|
||||
this._i.pausedTotalMs += pMs
|
||||
this._i.phaseStartAt += pMs
|
||||
this._i.phaseEndAt += pMs
|
||||
this._i.pausedAt = 0
|
||||
this.setData({ paused: false })
|
||||
},
|
||||
|
||||
onStepNext() {
|
||||
if (!this.data.started) return
|
||||
const now = Date.now()
|
||||
this._advanceToNextPhase(true, now)
|
||||
},
|
||||
|
||||
async onStop() {
|
||||
if (!this.data.started) return
|
||||
wx.showModal({
|
||||
title: '结束运动?',
|
||||
content: '会保存本次运动记录并返回。',
|
||||
confirmText: '结束',
|
||||
cancelText: '取消',
|
||||
confirmColor: '#e05561',
|
||||
success: async (r) => {
|
||||
if (!r.confirm) return
|
||||
await this._finishSession(false, Date.now())
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
_startTickLoop() {
|
||||
this._clearTickTimer()
|
||||
this._i.tickTimer = setInterval(() => this._tick(), 100)
|
||||
},
|
||||
_clearTickTimer() {
|
||||
if (this._i.tickTimer) { clearInterval(this._i.tickTimer); this._i.tickTimer = null }
|
||||
},
|
||||
|
||||
_applyBgCompensation(hiddenMs) {
|
||||
const i = this._i
|
||||
if (!this.data.started || hiddenMs < 1000) return
|
||||
// 短档:<20秒,直接扣剩余时间
|
||||
if (hiddenMs < 20 * 1000) {
|
||||
i.phaseStartAt += hiddenMs
|
||||
i.phaseEndAt += hiddenMs
|
||||
return
|
||||
}
|
||||
// 中档:20秒~10分钟,快进完整步
|
||||
if (hiddenMs < 10 * 60 * 1000) {
|
||||
let jumpRemain = hiddenMs
|
||||
while (jumpRemain > 2000 && !this._isPlanFinished()) {
|
||||
const remainInPhase = Math.max(0, i.phaseEndAt - Date.now())
|
||||
if (jumpRemain >= remainInPhase) {
|
||||
jumpRemain -= remainInPhase
|
||||
this._advanceToNextPhase(false, Date.now() + remainInPhase)
|
||||
} else {
|
||||
i.phaseStartAt += jumpRemain
|
||||
i.phaseEndAt += jumpRemain
|
||||
jumpRemain = 0
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
// 长档:>10分钟,直接结束
|
||||
this._finishSession(false, Date.now()).catch(() => {})
|
||||
},
|
||||
|
||||
_tick() {
|
||||
const i = this._i
|
||||
if (!this.data.started || this.data.paused) return
|
||||
const now = Date.now()
|
||||
this._renderState(now)
|
||||
this._maybeAnnounceCountdown(now)
|
||||
if (now >= i.phaseEndAt) {
|
||||
this._advanceToNextPhase(false, now)
|
||||
}
|
||||
if (now - (i.lastServerSyncAt || 0) >= 6000) {
|
||||
i.lastServerSyncAt = now
|
||||
this._syncToServer(now).catch(() => {})
|
||||
}
|
||||
},
|
||||
|
||||
_renderState(now) {
|
||||
const i = this._i
|
||||
const phaseRemainMs = Math.max(0, i.phaseEndAt - now)
|
||||
const totalPlanMs = (i.plan || []).reduce((a, p) => a + (p.durationSec + p.restSec) * 1000, 0)
|
||||
const completedBefore = Math.min(i.cursor, i.plan.length) * 1000
|
||||
const finishedBeforeMs = i.plan.slice(0, i.cursor).reduce((a, p) => a + (p.durationSec + p.restSec) * 1000, 0)
|
||||
const phaseTotalMs = i.durationMs
|
||||
const phasePassedMs = Math.max(0, Math.min(phaseTotalMs, phaseTotalMs - phaseRemainMs))
|
||||
const elapsedMs = (now - i.globalStartAt) - i.pausedTotalMs
|
||||
const totalPassedMs = finishedBeforeMs + phasePassedMs
|
||||
const remainMs = Math.max(0, totalPlanMs - totalPassedMs)
|
||||
|
||||
const rMin = Math.floor(remainMs / 60000), rSec = Math.floor((remainMs % 60000) / 1000)
|
||||
const eMin = Math.floor(elapsedMs / 60000), eSec = Math.floor((elapsedMs % 60000) / 1000)
|
||||
const compPct = totalPlanMs <= 0 ? 0 : Math.min(100, Math.round((totalPassedMs / totalPlanMs) * 100))
|
||||
|
||||
const cur = i.plan[i.cursor]
|
||||
const nextIdx = i.cursor + 1
|
||||
const next = nextIdx < i.plan.length ? i.plan[nextIdx] : null
|
||||
let phaseLabel = '准备开始', stepSeq = '', stepName = ''
|
||||
if (cur) {
|
||||
const round = cur.loopUntil ? '循环' : `${cur.round}/${cur.roundTotal}`
|
||||
phaseLabel = i.phase === PHASE_ACTION ? '动作' : '休息'
|
||||
stepSeq = `${round} · 第${cur.stepIndex + 1}步`
|
||||
stepName = i.phase === PHASE_ACTION ? (cur.voiceText || cur.stepName || '') : '休息一下'
|
||||
}
|
||||
|
||||
const phaseSec = Math.max(0, Math.round(phaseRemainMs / 1000))
|
||||
const phaseMin = Math.floor(phaseSec / 60)
|
||||
const phaseSec2 = phaseSec - phaseMin * 60
|
||||
const remainingText = `${String(phaseMin)}:${pad2(phaseSec2)}`
|
||||
|
||||
this.setData({
|
||||
remainingText,
|
||||
phaseLabel,
|
||||
stepSeq,
|
||||
stepName,
|
||||
nextName: next ? (next.phase === PHASE_ACTION ? (next.stepName || '') : '休息') : '',
|
||||
remainingMin: pad2(rMin),
|
||||
remainingSecPad: pad2(rSec),
|
||||
elapsedMin: pad2(eMin),
|
||||
elapsedSecPad: pad2(eSec),
|
||||
completion: compPct,
|
||||
})
|
||||
},
|
||||
|
||||
_enterPhase(phase, nowMs) {
|
||||
const i = this._i
|
||||
const cur = i.plan[i.cursor]
|
||||
if (!cur) return
|
||||
i.phase = phase
|
||||
const sec = phase === PHASE_ACTION ? cur.durationSec : cur.restSec
|
||||
i.durationMs = sec * 1000
|
||||
i.phaseStartAt = nowMs
|
||||
i.phaseEndAt = nowMs + i.durationMs
|
||||
i.announcedSet = new Set()
|
||||
if (sec <= 0 && phase === PHASE_REST) {
|
||||
this._advanceToNextPhase(false, nowMs)
|
||||
return
|
||||
}
|
||||
// 播报:步名/休息。注意文本必须与服务端 ttsBatch 预合成缓存的 key 完全一致,
|
||||
// 否则(如带“第X步:”前缀)命中不了缓存导致无声
|
||||
let speak = ''
|
||||
if (phase === PHASE_ACTION) {
|
||||
speak = cur.voiceText || cur.stepName || '开始'
|
||||
} else {
|
||||
speak = `休息${cur.restSec}秒`
|
||||
}
|
||||
this._enqueueTts(speak, 10)
|
||||
},
|
||||
|
||||
_isPlanFinished() {
|
||||
const i = this._i
|
||||
if (i.loopUntil) return false
|
||||
return i.cursor >= i.plan.length
|
||||
},
|
||||
|
||||
_announceStart() {
|
||||
const title = String((this._i.card && this._i.card.title) || this.data.cardTitle || '').trim()
|
||||
if (!title) return
|
||||
this._enqueueTts(`开始训练,${title}`, 9)
|
||||
},
|
||||
|
||||
// 切换动作前的过渡语:新的一轮(第N轮)播“第N轮”,同一轮内播“下一组”。
|
||||
// 过渡语不内嵌动作名,动作名由进入步骤时单独播报,避免“平板支撑 → 平板支撑”重复。
|
||||
// 缓存里没合成过的文本不播,避免只震动不出声
|
||||
_announceNextStep() {
|
||||
const i = this._i
|
||||
const nxt = i.plan[i.cursor]
|
||||
if (!nxt) return
|
||||
const isNewRound = nxt.round > 1 && Number(nxt.stepIndex) === 0
|
||||
const text = isNewRound ? `第${nxt.round}轮` : '下一组'
|
||||
if (!i.ttsSrc[text]) return
|
||||
this._enqueueTts(text, 11)
|
||||
},
|
||||
|
||||
_advanceToNextPhase(manualSkip, nowMs) {
|
||||
const i = this._i
|
||||
if (i.phase === PHASE_ACTION) {
|
||||
const cur = i.plan[i.cursor]
|
||||
if (cur && cur.restSec > 0) {
|
||||
this._enterPhase(PHASE_REST, nowMs)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 结束当前 rest/action ,推进到下一条 plan 条目
|
||||
i.cursor += 1
|
||||
if (i.loopUntil) {
|
||||
// 循环至停止:如果到达末尾,回到第一条(永不 finish,除非手动停止)
|
||||
if (i.cursor >= i.plan.length) i.cursor = 0
|
||||
this._announceNextStep()
|
||||
this._enterPhase(PHASE_ACTION, nowMs)
|
||||
return
|
||||
}
|
||||
if (i.cursor >= i.plan.length) {
|
||||
this._finishSession(true, nowMs).catch(() => {})
|
||||
return
|
||||
}
|
||||
this._announceNextStep()
|
||||
this._enterPhase(PHASE_ACTION, nowMs)
|
||||
},
|
||||
|
||||
_maybeAnnounceCountdown(now) {
|
||||
const i = this._i
|
||||
if (!this.data.hasCountdown) return
|
||||
if (i.phase !== PHASE_ACTION && i.phase !== PHASE_REST) return
|
||||
const remainSec = Math.round((i.phaseEndAt - now) / 1000)
|
||||
if (remainSec <= 0 || remainSec > 60) return
|
||||
if (!i.countdownSet.includes(remainSec)) return
|
||||
if (i.announcedSet.has(i.cursor + '|' + i.phase + '|' + remainSec)) return
|
||||
i.announcedSet.add(i.cursor + '|' + i.phase + '|' + remainSec)
|
||||
// 最后 3 秒以内,使用更短播报
|
||||
if (remainSec <= 3) {
|
||||
this._enqueueTts(`还有${remainSec}秒`, 5)
|
||||
} else {
|
||||
this._enqueueTts(`还有${remainSec}秒`, 5)
|
||||
}
|
||||
},
|
||||
|
||||
async _syncToServer(nowMs) {
|
||||
const i = this._i
|
||||
if (!i.sessionId || i.finishing) return
|
||||
const cur = i.plan[i.cursor]
|
||||
await api.repClock.sessionSync({
|
||||
sessionId: i.sessionId,
|
||||
currentStepIndex: cur ? cur.stepIndex : -1,
|
||||
currentRound: cur ? cur.round : 1,
|
||||
phase: i.phase,
|
||||
cursor: i.cursor,
|
||||
phaseRemainingMs: Math.max(0, i.phaseEndAt - nowMs),
|
||||
paused: !!this.data.paused,
|
||||
pausedTotalMs: i.pausedTotalMs,
|
||||
})
|
||||
},
|
||||
|
||||
async _finishSession(completed, nowMs) {
|
||||
const i = this._i
|
||||
if (i.finishing) return
|
||||
i.finishing = true
|
||||
this._clearTickTimer()
|
||||
wx.showLoading({ title: '保存记录…', mask: true })
|
||||
try {
|
||||
await api.repClock.sessionSync({
|
||||
sessionId: i.sessionId,
|
||||
currentStepIndex: -1,
|
||||
currentRound: 1,
|
||||
phase: 'finish',
|
||||
cursor: i.cursor,
|
||||
phaseRemainingMs: 0,
|
||||
paused: !!this.data.paused,
|
||||
pausedTotalMs: i.pausedTotalMs,
|
||||
})
|
||||
await api.repClock.sessionFinish({
|
||||
sessionId: i.sessionId,
|
||||
completed: !!completed,
|
||||
force: false,
|
||||
})
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: completed ? '完成!真棒 🎉' : '已保存', icon: 'success' })
|
||||
setTimeout(() => wx.navigateBack(), 900)
|
||||
} catch (e) {
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: '记录已在服务端,返回', icon: 'none' })
|
||||
setTimeout(() => wx.navigateBack(), 900)
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"navigationBarTitleText": "节拍中…",
|
||||
"navigationBarBackgroundColor": "#1a1a2e",
|
||||
"navigationBarTextStyle": "white",
|
||||
"backgroundColor": "#0f0f1a",
|
||||
"disableScroll": true
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<view class="runner-root">
|
||||
<view class="runner-bg"></view>
|
||||
<view class="runner-layout">
|
||||
<view class="runner-topbar">
|
||||
<view class="topbar-title">{{cardTitle}}</view>
|
||||
<view class="topbar-sub muted small">
|
||||
<text wx:if="{{phaseLabel}}">{{phaseLabel}}</text>
|
||||
<text wx:if="{{phaseLabel && stepSeq}}"> · </text>
|
||||
<text wx:if="{{stepSeq}}">{{stepSeq}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="center-clock-wrap">
|
||||
<view class="ring">
|
||||
<view class="ring-left"></view>
|
||||
<view class="ring-right"></view>
|
||||
<view class="ring-mask-left"></view>
|
||||
<view class="ring-mask-right"></view>
|
||||
</view>
|
||||
<view class="clock-inner">
|
||||
<view class="clock-text">{{remainingText}}</view>
|
||||
<view class="clock-sub muted">{{stepName}}</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{nextName}}" class="next-card muted small">
|
||||
下一步:<text class="next-name">{{nextName}}</text>
|
||||
</view>
|
||||
|
||||
<view class="stats-row">
|
||||
<view class="stat">
|
||||
<view class="stat-num">{{elapsedMin}}:{{elapsedSecPad}}</view>
|
||||
<view class="stat-label">已过</view>
|
||||
</view>
|
||||
<view class="stat">
|
||||
<view class="stat-num">{{remainingMin}}:{{remainingSecPad}}</view>
|
||||
<view class="stat-label">剩余</view>
|
||||
</view>
|
||||
<view class="stat">
|
||||
<view class="stat-num">{{completion}}%</view>
|
||||
<view class="stat-label">完成</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="actions">
|
||||
<view wx:if="{{!started}}" class="btn btn-primary btn-block act-btn" bindtap="onStart">▶ 开始</view>
|
||||
<block wx:else>
|
||||
<view wx:if="{{paused}}" class="btn btn-primary btn-block act-btn" bindtap="onResume">▶ 继续</view>
|
||||
<view wx:else class="btn btn-ghost btn-block act-btn" bindtap="onPause">⏸ 暂停</view>
|
||||
<view class="btn btn-ghost btn-block act-btn act-btn-secondary" bindtap="onStepNext">⏭ 下一步</view>
|
||||
<view class="btn btn-danger btn-block act-btn" bindtap="onStop">⏹ 结束</view>
|
||||
</block>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{hasCountdown}}" class="broadcasts muted small">倒计时播报:{{countdownText}}</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,143 @@
|
||||
.runner-root {
|
||||
min-height: 100vh;
|
||||
position: relative;
|
||||
color: #ffffff;
|
||||
overflow-x: hidden;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.runner-bg {
|
||||
position: fixed; inset: 0;
|
||||
background: radial-gradient(circle at 30% 20%, #1f1f3d, #0a0a14 65%);
|
||||
z-index: 0;
|
||||
}
|
||||
.runner-layout {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
padding: 36rpx 28rpx calc(24rpx + env(safe-area-inset-bottom));
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 100vh;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.runner-topbar { margin-bottom: 18rpx; }
|
||||
.topbar-title { font-size: 40rpx; font-weight: 800; letter-spacing: 1rpx; }
|
||||
.topbar-sub { margin-top: 6rpx; }
|
||||
|
||||
.center-clock-wrap {
|
||||
position: relative;
|
||||
width: 468rpx;
|
||||
height: 468rpx;
|
||||
margin: 18rpx auto 0;
|
||||
}
|
||||
.ring {
|
||||
position: absolute; inset: 0;
|
||||
border-radius: 50%;
|
||||
background: linear-gradient(135deg, #6c7ae0, #b06be0);
|
||||
opacity: 0.18;
|
||||
}
|
||||
.ring-left, .ring-right {
|
||||
position: absolute;
|
||||
width: 50%;
|
||||
height: 100%;
|
||||
left: 0; top: 0;
|
||||
border-radius: 280rpx 0 0 280rpx;
|
||||
background: conic-gradient(from 0deg, #6c7ae0 0deg 180deg, rgba(108,122,224,0.15) 180deg 360deg);
|
||||
transform-origin: 100% 50%;
|
||||
}
|
||||
.ring-right {
|
||||
left: 50%;
|
||||
border-radius: 0 280rpx 280rpx 0;
|
||||
background: conic-gradient(from 180deg, #b06be0 180deg 360deg, rgba(176,107,224,0.15) 360deg 360deg);
|
||||
transform-origin: 0% 50%;
|
||||
}
|
||||
.ring-mask-left, .ring-mask-right {
|
||||
position: absolute;
|
||||
width: 50%;
|
||||
height: 100%;
|
||||
background: #0e0e19;
|
||||
z-index: 2;
|
||||
top: 0; left: 0;
|
||||
border-radius: 280rpx 0 0 280rpx;
|
||||
transform-origin: 100% 50%;
|
||||
}
|
||||
.ring-mask-right {
|
||||
left: 50%;
|
||||
border-radius: 0 280rpx 280rpx 0;
|
||||
background: #0e0e19;
|
||||
transform-origin: 0% 50%;
|
||||
}
|
||||
.clock-inner {
|
||||
position: absolute;
|
||||
inset: 48rpx;
|
||||
border-radius: 50%;
|
||||
background: #101025;
|
||||
border: 1rpx solid rgba(255,255,255,0.08);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 3;
|
||||
box-shadow: 0 0 60rpx rgba(108,122,224,0.15), inset 0 0 80rpx rgba(0,0,0,0.6);
|
||||
}
|
||||
.clock-text {
|
||||
font-size: 148rpx;
|
||||
font-weight: 800;
|
||||
letter-spacing: 4rpx;
|
||||
font-variant-numeric: tabular-nums;
|
||||
background: linear-gradient(135deg, #ffffff, #d6dbff);
|
||||
color: transparent;
|
||||
-webkit-background-clip: text;
|
||||
background-clip: text;
|
||||
}
|
||||
.clock-sub { margin-top: 12rpx; font-size: 28rpx; color: #c2c6f4; }
|
||||
|
||||
.next-card {
|
||||
margin: 22rpx auto 0;
|
||||
padding: 14rpx 24rpx;
|
||||
background: rgba(255,255,255,0.04);
|
||||
border: 1rpx solid rgba(255,255,255,0.06);
|
||||
border-radius: 20rpx;
|
||||
text-align: center;
|
||||
}
|
||||
.next-name { color: #ffffff; margin-left: 12rpx; font-weight: 600; }
|
||||
|
||||
.stats-row {
|
||||
margin-top: 26rpx;
|
||||
display: flex;
|
||||
gap: 14rpx;
|
||||
}
|
||||
.stat {
|
||||
flex: 1;
|
||||
padding: 18rpx 8rpx;
|
||||
background: rgba(255,255,255,0.04);
|
||||
border: 1rpx solid rgba(255,255,255,0.06);
|
||||
border-radius: 22rpx;
|
||||
text-align: center;
|
||||
}
|
||||
.stat-num {
|
||||
font-size: 34rpx;
|
||||
font-weight: 700;
|
||||
color: #e9eaff;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.stat-label {
|
||||
margin-top: 6rpx;
|
||||
color: #7a7ea4;
|
||||
font-size: 22rpx;
|
||||
}
|
||||
|
||||
.actions {
|
||||
margin-top: auto;
|
||||
padding: 24rpx 0 8rpx;
|
||||
display: flex;
|
||||
gap: 12rpx;
|
||||
flex-direction: column;
|
||||
}
|
||||
.act-btn { padding: 22rpx 0; font-size: 30rpx; font-weight: 600; border-radius: 20rpx; }
|
||||
.act-btn-secondary { background: rgba(255,255,255,0.04); color: #e2e4ff; border: 1rpx solid rgba(255,255,255,0.08); }
|
||||
|
||||
.broadcasts {
|
||||
margin: 10rpx auto 0;
|
||||
text-align: center;
|
||||
letter-spacing: 0.5rpx;
|
||||
}
|
||||
Reference in New Issue
Block a user