feat: 修复清除数据、语音缓存、排行榜限制,新增项目配置文件
- 修复 cloud.clearAll 静默失败导致重启后数据恢复 - 清除数据弹窗改用自定义 ui-modal 替代 wx.showModal - TTS 语音合成增加 fileID 持久化缓存,重启不再重新合成 - 排行榜限制前15名,maxRank 参数化到 config.js - 新增 config.js 统一管理版本号/更新日期/开发者/排行榜限制 - progress-ring 消除 getSystemInfoSync 弃用警告 - voice.js 增加 InnerAudioContext 错误监听 - storage/util 完善用户资料、打卡日期精度、记录日志
This commit is contained in:
+43
-10
@@ -2,12 +2,15 @@
|
||||
* Voice prompt utility for the timer.
|
||||
*
|
||||
* 4 fixed, polished prompts live in `cloudfunctions/tts`. We call that
|
||||
* cloud function with a key, get back an audio URL (cached in cloud
|
||||
* storage after first synthesis), and play it via createInnerAudioContext.
|
||||
* cloud function with a key, get back an audio URL and permanent fileID,
|
||||
* and play it via createInnerAudioContext.
|
||||
*
|
||||
* The in-memory `_urlCache` avoids hitting the cloud function more than
|
||||
* once per prompt per session. The audio context is a singleton so
|
||||
* overlapping prompts interrupt each other cleanly.
|
||||
* Two-level caching so TTS synthesis happens at most once per prompt,
|
||||
* ever (not once per app launch):
|
||||
* 1. In-memory `_urlCache` — avoids cloud calls within a single session
|
||||
* 2. Persistent `_fileIDCache` in wx storage — survives app restarts;
|
||||
* we pass the permanent fileID back to the cloud function so it can
|
||||
* call getTempFileURL directly instead of re-synthesizing.
|
||||
*
|
||||
* If the cloud function is unconfigured (no TTS credentials) and returns
|
||||
* `success: false`, we fall back to a silent no-op — better than spamming
|
||||
@@ -21,8 +24,24 @@ const PROMPTS = {
|
||||
complete: '太棒了!今天的目标已完成,继续加油!'
|
||||
}
|
||||
|
||||
const FILEID_CACHE_KEY = 'tts_fileid_cache'
|
||||
|
||||
let _ctx = null
|
||||
const _urlCache = {} // { promptKey: audioUrl }
|
||||
const _urlCache = {} // { promptKey: audioUrl } — per-session, volatile
|
||||
let _fileIDCache = null // { promptKey: fileID } — persisted, lazy-loaded
|
||||
|
||||
const _loadFileIDCache = () => {
|
||||
if (_fileIDCache) return
|
||||
try {
|
||||
_fileIDCache = wx.getStorageSync(FILEID_CACHE_KEY) || {}
|
||||
} catch (e) {
|
||||
_fileIDCache = {}
|
||||
}
|
||||
}
|
||||
|
||||
const _saveFileIDCache = () => {
|
||||
try { wx.setStorageSync(FILEID_CACHE_KEY, _fileIDCache) } catch (e) {}
|
||||
}
|
||||
|
||||
const _getCtx = () => {
|
||||
if (!_ctx) {
|
||||
@@ -30,23 +49,37 @@ const _getCtx = () => {
|
||||
// Play even when the device is in silent mode — we want the user to
|
||||
// actually hear training cues during a session.
|
||||
_ctx.obeyMuteSwitch = false
|
||||
_ctx.onError((err) => {
|
||||
console.log('[voice] audio error:', err.errCode, err.errMsg)
|
||||
})
|
||||
}
|
||||
return _ctx
|
||||
}
|
||||
|
||||
const _fetchUrl = async (promptKey) => {
|
||||
// 1. In-memory cache: same-session instant return
|
||||
if (_urlCache[promptKey]) return _urlCache[promptKey]
|
||||
|
||||
// 2. Persistent fileID cache: pass to cloud function for instant getTempFileURL
|
||||
_loadFileIDCache()
|
||||
const cachedFileID = _fileIDCache[promptKey] || null
|
||||
|
||||
try {
|
||||
const res = await wx.cloud.callFunction({
|
||||
name: 'tts',
|
||||
data: { promptKey },
|
||||
// Default WeChat cloud timeout is 3s, which is exactly the TTS
|
||||
// first-call budget (synth + upload + tempURL). Bump to 30s for
|
||||
// cold starts; cached calls return in <500ms.
|
||||
data: { promptKey, fileID: cachedFileID },
|
||||
// Timeout: 30s for first synthesis (cold start), <1s for cached
|
||||
config: { timeout: 30000 }
|
||||
})
|
||||
if (res && res.result && res.result.success && res.result.audioUrl) {
|
||||
_urlCache[promptKey] = res.result.audioUrl
|
||||
|
||||
// Persist the fileID if we got a new one (first synthesis or
|
||||
// re-synthesis after cache invalidation).
|
||||
if (res.result.fileID && res.result.fileID !== cachedFileID) {
|
||||
_fileIDCache[promptKey] = res.result.fileID
|
||||
_saveFileIDCache()
|
||||
}
|
||||
return res.result.audioUrl
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user