149 lines
3.6 KiB
JavaScript
149 lines
3.6 KiB
JavaScript
|
|
/**
|
|||
|
|
* 本地存储工具
|
|||
|
|
* storage key 设计:
|
|||
|
|
* draw_learning_progress - 学习进度
|
|||
|
|
* draw_recent_history - 最近学习记录
|
|||
|
|
* draw_favorite_courses - 收藏课程
|
|||
|
|
* draw_works_records - 作品上传记录
|
|||
|
|
* draw_user_settings - 用户设置
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
const KEYS = {
|
|||
|
|
PROGRESS: 'draw_learning_progress',
|
|||
|
|
HISTORY: 'draw_recent_history',
|
|||
|
|
FAVORITE: 'draw_favorite_courses',
|
|||
|
|
WORKS: 'draw_works_records',
|
|||
|
|
SETTINGS: 'draw_user_settings',
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// --------- 学习进度 ---------
|
|||
|
|
// 格式:{ [courseId]: { currentStep: 0, completed: false, updatedAt: timestamp } }
|
|||
|
|
function getProgress() {
|
|||
|
|
return wx.getStorageSync(KEYS.PROGRESS) || {}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function saveProgress(courseId, stepIndex, completed = false) {
|
|||
|
|
const progress = getProgress()
|
|||
|
|
progress[courseId] = {
|
|||
|
|
currentStep: stepIndex,
|
|||
|
|
completed,
|
|||
|
|
updatedAt: Date.now()
|
|||
|
|
}
|
|||
|
|
wx.setStorageSync(KEYS.PROGRESS, progress)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function getCourseProgress(courseId) {
|
|||
|
|
const progress = getProgress()
|
|||
|
|
return progress[courseId] || { currentStep: 0, completed: false }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function getCompletedCount() {
|
|||
|
|
const progress = getProgress()
|
|||
|
|
return Object.values(progress).filter(p => p.completed).length
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// --------- 最近学习记录 ---------
|
|||
|
|
// 格式:[{ courseId, courseTitle, coverEmoji, coverColor, stepIndex, timestamp }]
|
|||
|
|
function getRecentHistory() {
|
|||
|
|
return wx.getStorageSync(KEYS.HISTORY) || []
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function addRecentHistory(courseInfo) {
|
|||
|
|
let history = getRecentHistory()
|
|||
|
|
// 去重
|
|||
|
|
history = history.filter(h => h.courseId !== courseInfo.courseId)
|
|||
|
|
history.unshift({ ...courseInfo, timestamp: Date.now() })
|
|||
|
|
// 最多保留10条
|
|||
|
|
history = history.slice(0, 10)
|
|||
|
|
wx.setStorageSync(KEYS.HISTORY, history)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// --------- 收藏课程 ---------
|
|||
|
|
// 格式:[courseId]
|
|||
|
|
function getFavorites() {
|
|||
|
|
return wx.getStorageSync(KEYS.FAVORITE) || []
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function toggleFavorite(courseId) {
|
|||
|
|
let favorites = getFavorites()
|
|||
|
|
const idx = favorites.indexOf(courseId)
|
|||
|
|
if (idx >= 0) {
|
|||
|
|
favorites.splice(idx, 1)
|
|||
|
|
} else {
|
|||
|
|
favorites.unshift(courseId)
|
|||
|
|
}
|
|||
|
|
wx.setStorageSync(KEYS.FAVORITE, favorites)
|
|||
|
|
return idx < 0 // true=已收藏
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function isFavorite(courseId) {
|
|||
|
|
return getFavorites().includes(courseId)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// --------- 作品记录 ---------
|
|||
|
|
// 格式:[{ id, courseId, courseTitle, imagePath, savedAt }]
|
|||
|
|
function getWorksRecords() {
|
|||
|
|
return wx.getStorageSync(KEYS.WORKS) || []
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function addWorkRecord(record) {
|
|||
|
|
let records = getWorksRecords()
|
|||
|
|
records.unshift({ ...record, savedAt: Date.now(), id: Date.now().toString() })
|
|||
|
|
wx.setStorageSync(KEYS.WORKS, records)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function getCompletedPracticeCount() {
|
|||
|
|
return getWorksRecords().length
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// --------- 用户设置 ---------
|
|||
|
|
function getSettings() {
|
|||
|
|
return wx.getStorageSync(KEYS.SETTINGS) || {
|
|||
|
|
reminderEnabled: false,
|
|||
|
|
reminderTime: '08:00',
|
|||
|
|
nickname: '学画的你',
|
|||
|
|
avatar: '',
|
|||
|
|
joinDays: 0,
|
|||
|
|
joinDate: null
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function saveSettings(settings) {
|
|||
|
|
const current = getSettings()
|
|||
|
|
const merged = { ...current, ...settings }
|
|||
|
|
// 计算学习天数
|
|||
|
|
if (!merged.joinDate) {
|
|||
|
|
merged.joinDate = Date.now()
|
|||
|
|
merged.joinDays = 1
|
|||
|
|
} else {
|
|||
|
|
merged.joinDays = Math.max(1, Math.ceil((Date.now() - merged.joinDate) / 86400000))
|
|||
|
|
}
|
|||
|
|
wx.setStorageSync(KEYS.SETTINGS, merged)
|
|||
|
|
return merged
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function initSettings() {
|
|||
|
|
const settings = getSettings()
|
|||
|
|
if (!settings.joinDate) {
|
|||
|
|
saveSettings({})
|
|||
|
|
}
|
|||
|
|
return getSettings()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
module.exports = {
|
|||
|
|
saveProgress,
|
|||
|
|
getCourseProgress,
|
|||
|
|
getCompletedCount,
|
|||
|
|
getRecentHistory,
|
|||
|
|
addRecentHistory,
|
|||
|
|
getFavorites,
|
|||
|
|
toggleFavorite,
|
|||
|
|
isFavorite,
|
|||
|
|
getWorksRecords,
|
|||
|
|
addWorkRecord,
|
|||
|
|
getCompletedPracticeCount,
|
|||
|
|
getSettings,
|
|||
|
|
saveSettings,
|
|||
|
|
initSettings
|
|||
|
|
}
|