Files
wx_pbzc/utils/cloud.js
T
lc 1d55df72b3 feat: v1.5 — plan editor, voice prompts, UI polish
Major features:
- Training plan editor: edit preset days/duration per plan (in-place
  override via custom_plans storage; preset ids preserved so existing
  records stay valid)
- Voice prompts at 4 fixed points during training (halfway / 30s /
  10s / done) via Tencent Cloud TTS (cloudfunctions/tts + utils/voice.js)
- Free-training button: switched from outline (transparent) to ghost
  variant (theme-tinted) for visual weight

Bug fixes:
- 4 functional pages: SVG data URIs failed to render in WeChat because
  '\#' in colors was parsed as a data-URI fragment delimiter. encode the
  whole SVG via encodeURIComponent in utils/icons.js build().
- Old records (saved before id field was added) could not be deleted
  (data-id was empty, triggered defensive guard). Backfill stable
  legacy-<month>-<index>-<duration> ids in getRecords() and persist.
- settings.js plan-row editor button event was bubbling up to the row's
  bindtap (which also fired onSelectPlan). Wrapped in catch:tap.

UI:
- Settings page: 3 hardcoded plans replaced with dynamic buildPlansList
  that overlays custom_plans on top of presets
- Plan editor: bottom-sheet modal in settings page (regular view, not
  ui-modal — WeChat custom component root element drops position:fixed
  in this runtime)
- Free-training button: rounded pill style, full-width, 24rpx gap from
  primary action; bottom sheet uses max-height: 88vh + internal scroll
- Version bumped to v1.5, last updated 2026-06-10

Removed:
- Daily reminder section (dailyReminder / reminderTime) — replaced by
  the 4 voice prompts which cover the same user need without requiring
  long-term scheduling that WeChat mini-programs can't actually do

Misc:
- utils/plan.js refactored to formula-driven: presets declare
  totalDays/startTarget/increment/cycleDays, days[] is generated. Same
  formula applies to custom plans.
- Timer _remind() guards each prompt on minimum duration so short
  free-mode sessions don't fire 'last30' at the start
- Cloud storage cloud_plans field added to data push payload; restored
  on first install via _restoreFromCloud
- .gitignore added for local AI tool caches (.reasonix/, reasonix.toml,
  .codegraph/daemon.pid)
2026-06-10 17:23:13 +08:00

121 lines
3.0 KiB
JavaScript

const DB_COLLECTION = 'plank_data'
const ENV_ID = 'cloudbase-d1g56kl2q8f4f7d8a'
let _db = null
let _pushTimer = null
let _enabled = false
let _docId = null // cache doc id after first successful query
const getDb = () => {
if (!_enabled) return null
if (!_db) {
try { _db = wx.cloud.database({ env: ENV_ID }) } catch (e) { _enabled = false }
}
return _db
}
const init = () => {
try {
wx.cloud.init({ env: ENV_ID, traceUser: true })
_enabled = true
} catch (e) {
_enabled = false
}
}
const pullAll = async () => {
const db = getDb()
if (!db) return null
try {
// First try cached doc id
if (_docId) {
try {
const res = await db.collection(DB_COLLECTION).doc(_docId).get()
if (res && res.data) return res.data
} catch (e) {
_docId = null // doc deleted or inaccessible
}
}
// Query: rely on security rules to scope to current user
const res = await db.collection(DB_COLLECTION).limit(1).get()
if (res && res.data && res.data.length > 0) {
_docId = res.data[0]._id
return res.data[0]
}
return null
} catch (e) {
return null
}
}
const pushAll = () => {
if (!_enabled) return
if (_pushTimer) clearTimeout(_pushTimer)
_pushTimer = setTimeout(() => _doPush(), 2000)
}
const _doPush = async () => {
const db = getDb()
if (!db) return
try {
const storage = require('./storage')
const themeMod = require('./theme')
const data = {
records: storage.getRecords(),
settings: storage.getSettings(),
streak: storage.getStreak(),
customPlans: storage.getCustomPlans(),
themeId: themeMod.getCurrentTheme().id,
updatedAt: db.serverDate()
}
// Use cached doc id if available
if (_docId) {
try {
await db.collection(DB_COLLECTION).doc(_docId).update({ data })
return
} catch (e) {
_docId = null // doc may have been deleted
}
}
// Query for existing doc (relies on security rules to scope to current user)
const existing = await db.collection(DB_COLLECTION).limit(1).get()
if (existing && existing.data && existing.data.length > 0) {
_docId = existing.data[0]._id
await db.collection(DB_COLLECTION).doc(_docId).update({ data })
} else {
const res = await db.collection(DB_COLLECTION).add({ data })
if (res && res._id) _docId = res._id
}
} catch (e) {
// Silently retry on next write
}
}
const clearAll = async () => {
const db = getDb()
if (!db) return
try {
if (_docId) {
try {
await db.collection(DB_COLLECTION).doc(_docId).remove()
_docId = null
return
} catch (e) {
_docId = null
}
}
const existing = await db.collection(DB_COLLECTION).limit(1).get()
if (existing && existing.data && existing.data.length > 0) {
await db.collection(DB_COLLECTION).doc(existing.data[0]._id).remove()
}
_docId = null
} catch (e) {
// Nothing to clear
}
}
module.exports = { init, pullAll, pushAll, clearAll, get enabled() { return _enabled } }