3532d0111b
循环训练(circuit)
- 新增 utils/circuitTimer.js 状态机: 工作->休息->...->完成, stop() 返回有效撑总秒(不含休息)
- 首页新增「循环训练·分组练习」入口 + stepper 配置弹窗
- 配置本地持久化 circuit_config(每组时长/组数/休息/每周目标), 下次自动预填
- 记录以 planId:'circuit' + mode:'circuit' 落库, duration 语义不变 -> 排行榜/统计/每日计划进度零改动
- 记录页展示「循环 N组xMs·休Rs」
计时器顶部循环进度面板
- 组进度点阵按组数等分铺满(flex:1 1 0, 去掉 max-width 封顶), 当前组以增高而非加宽强调
- 大号「X/Y 组」+ 四态阶段徽章(撑住/休息中/准备开始/已暂停) + 累计秒数副行
- 状态灯用纯 CSS 圆点(SVG 图标颜色烤死在 data URI 里, CSS 改不动)
- 超过 15 组自动降级为线性进度条; 面板不设固定高度, 规避真机大字体档裁字
修复: 循环训练全程无语音
- 根因: circuit 的 onTick 走 _circuitCue(只有震动), 从未调用 _remind
- 接入整场进度播报, 基准为 sets x holdPerSet(而非每组), 避免 4 组播 4 次「已完成一半」
- 新增 restStart/nextSet/lastSet 三条不含数字的词条, 客户端与 tts 云函数词表同步
- 修复必然撞车的时序: 偶数组时 halfway 与休息切换同一 tick, 加 2.6s 优先窗口让位
- 防吵闸门: 每组 <10s 不播过场语音, 休息 <5s 不播; 预载按模式取词条
训练期间屏幕常亮
- wx.setKeepScreenOn: onStart 开 / onUnload 关 / onShow 在 isRunning 时重新武装
- 切后台再回来该标志会被系统清掉, 故必须重新武装, 否则后半程丢语音与震动
- 低基础库与 Android 省电模式静默降级, 不弹 toast
修复: 首页打卡卡片左侧两个竖条
- 根因: ui-card 宿主节点无 display 声明为 inline, 页面侧加的 border 被打断成两个零宽行盒碎片
- ui-card 加 :host{display:block}, margin-bottom 从内部 view 上提到宿主
- 高亮由 border 改为 box-shadow spread 描边(不占布局, 无跳动) + 补 border-radius
- 连带修复失效的交错入场: nth-child 跨组件边界恒匹配 1, 改为 index 属性驱动内联 animation-delay, 14 处调用点补序号
注意: tts 云函数需重新部署, 否则新增的 3 条词条返回 Unknown prompt key(静默降级不报错)
131 lines
5.6 KiB
JavaScript
131 lines
5.6 KiB
JavaScript
// Cloud function: synthesize the 4 fixed training-voice prompts.
|
|
//
|
|
// Caching strategy: the client stores the permanent `fileID` returned by
|
|
// the first successful synthesis in local storage. On subsequent calls it
|
|
// passes `fileID` back to us; we call `getTempFileURL` with it to get a
|
|
// fresh temporary URL instantly (no synthesis, no upload). If the cached
|
|
// fileID is stale (file deleted from cloud storage), we fall through and
|
|
// re-synthesize, returning the new fileID so the client can update its cache.
|
|
//
|
|
// Configuration (云开发控制台 → 云函数 → tts → 函数配置 → 环境变量):
|
|
// TTS_SECRET_ID — Tencent Cloud API key id
|
|
// TTS_SECRET_KEY — Tencent Cloud API key
|
|
// TTS_REGION — (optional) defaults to ap-guangzhou
|
|
//
|
|
// NOTE: env var names cannot start with SCF_ / QCLOUD_ / TENCENTCLOUD_ —
|
|
// those prefixes are reserved by Tencent Cloud SCF. Use the TTS_ prefix.
|
|
|
|
const cloud = require('wx-server-sdk')
|
|
const { cachedFileId } = require('./cache')
|
|
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
|
|
|
// Polished, fixed prompts.
|
|
const PROMPTS = {
|
|
start: '训练已开始',
|
|
halfway: '已完成一半啦,坚持就是胜利!',
|
|
last30: '最后30秒,保持呼吸,稳住姿势!',
|
|
last10: '最后10秒,再加把劲!',
|
|
goal: '目标达成,继续挑战!',
|
|
complete: '太棒了!今天的目标已完成,继续加油!',
|
|
// 循环训练(circuit)阶段切换专用。必须与 utils/voice.js 的 PROMPTS 保持一致,
|
|
// 否则客户端能请求、云端返回 Unknown prompt key(会静默降级为不播)。
|
|
restStart: '休息一下,调整呼吸',
|
|
nextSet: '下一组,准备开始',
|
|
lastSet: '最后一组,全力冲刺!'
|
|
}
|
|
|
|
const CACHE_DIR = 'tts-cache'
|
|
|
|
const _synthesize = async (text, promptKey, opts) => {
|
|
let TtsClient
|
|
try {
|
|
TtsClient = require('tencentcloud-sdk-nodejs').tts.v20190823.Client
|
|
} catch (e) {
|
|
throw new Error('tencentcloud-sdk-nodejs not installed. Run `npm i tencentcloud-sdk-nodejs` in this cloud function directory.')
|
|
}
|
|
|
|
const secretId = process.env.TTS_SECRET_ID
|
|
const secretKey = process.env.TTS_SECRET_KEY
|
|
if (!secretId || !secretKey) {
|
|
throw new Error('TTS_SECRET_ID / TTS_SECRET_KEY not configured.')
|
|
}
|
|
|
|
const client = new TtsClient({
|
|
credential: { secretId, secretKey },
|
|
region: process.env.TTS_REGION || 'ap-guangzhou'
|
|
})
|
|
|
|
const res = await client.TextToVoice({
|
|
Text: text,
|
|
SessionId: `${promptKey}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`,
|
|
VoiceType: (opts && opts.voiceType) || 101001, // 智瑜,温柔女声
|
|
Codec: 'mp3',
|
|
SampleRate: 16000,
|
|
Speed: (opts && opts.speed != null) ? opts.speed : 0,
|
|
Volume: (opts && opts.volume != null) ? opts.volume : 5
|
|
})
|
|
return Buffer.from(res.Audio, 'base64')
|
|
}
|
|
|
|
const _upload = async (key, buffer, voiceType, volume, speed) => {
|
|
// cloudPath 含 volume/speed,避免不同音量/语速的音频互相覆盖(缓存碰撞)
|
|
const cloudPath = `${CACHE_DIR}/${key}-${voiceType || 'default'}-v${volume != null ? volume : 0}-s${speed != null ? speed : 0}.mp3`
|
|
// uploadFile returns the canonical fileID (e.g. "cloud://env.xxx/...").
|
|
// We must use THAT — not the relative cloudPath we passed in — when
|
|
// calling getTempFileURL. A self-constructed fileID is invalid.
|
|
const res = await cloud.uploadFile({ cloudPath, fileContent: buffer })
|
|
return res.fileID
|
|
}
|
|
|
|
exports.main = async (event) => {
|
|
const { promptKey, fileID, voiceType, volume, speed } = event || {}
|
|
const text = PROMPTS[promptKey]
|
|
if (!text) {
|
|
return { success: false, error: `Unknown prompt key: ${promptKey}` }
|
|
}
|
|
|
|
// If the client passed a cached fileID, try to get a fresh temp URL
|
|
// from it — this is the fast path (no synthesis, no upload).
|
|
const db = cloud.database()
|
|
const cacheKey = `${promptKey}-${voiceType || 'default'}-v${volume != null ? volume : 0}-s${speed != null ? speed : 0}`
|
|
|
|
// 解析 fileID:优先 client 传入,否则查服务端 tts_cache,防 client 传 null 强制重合成(刷 TTS 账单)
|
|
let resolvedFileID = fileID || null
|
|
// 校验 client 传的 fileID 必须属于 tts-cache 目录,防伪造 cloud:// 换取他人私有文件
|
|
if (resolvedFileID && !resolvedFileID.includes(CACHE_DIR)) resolvedFileID = null
|
|
if (!resolvedFileID) {
|
|
try {
|
|
const r = await db.collection('tts_cache').doc(cacheKey).get()
|
|
const cached = cachedFileId(r.data)
|
|
if (cached) resolvedFileID = cached
|
|
} catch (e) {} // 集合/文档不存在,走合成
|
|
}
|
|
if (resolvedFileID) {
|
|
try {
|
|
const urlRes = await cloud.getTempFileURL({ fileList: [resolvedFileID] })
|
|
const url = urlRes.fileList[0].tempFileURL
|
|
if (url) {
|
|
return { success: true, audioUrl: url, fileID: resolvedFileID, text, cached: true }
|
|
}
|
|
} catch (e) {
|
|
// fileID stale or file deleted — fall through to re-synthesize
|
|
}
|
|
}
|
|
|
|
// Slow path: synthesize + upload + return fresh fileID for caching
|
|
try {
|
|
const buffer = await _synthesize(text, promptKey, { voiceType, volume, speed })
|
|
const newFileID = await _upload(promptKey, buffer, voiceType, volume, speed)
|
|
const urlRes = await cloud.getTempFileURL({ fileList: [newFileID] })
|
|
const url = urlRes.fileList[0].tempFileURL
|
|
if (!url) {
|
|
return { success: false, error: 'getTempFileURL returned empty URL', text }
|
|
}
|
|
// 写服务端 tts_cache,下次相同参数直接命中,不重复合成
|
|
try { await db.collection('tts_cache').doc(cacheKey).set({ data: { fileID: newFileID, updatedAt: db.serverDate() } }) } catch (e) {}
|
|
return { success: true, audioUrl: url, fileID: newFileID, text, cached: false }
|
|
} catch (e) {
|
|
return { success: false, error: e.message, text }
|
|
}
|
|
}
|