首次提交:初始化项目代码
This commit is contained in:
+136
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* 本地工具类(无需接口的功能)
|
||||
*/
|
||||
|
||||
// ===================== 本地缓存清理(核心实现) =====================
|
||||
/**
|
||||
* 清理小程序缓存
|
||||
* @returns {Promise<{size: string, success: boolean}>}
|
||||
*/
|
||||
export function clearCache() {
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
// 1. 清理本地存储(除了token,避免清理后直接退出)
|
||||
const token = uni.getStorageSync('user_token') // 保留登录态
|
||||
uni.clearStorageSync()
|
||||
if (token) uni.setStorageSync('user_token', token) // 恢复token
|
||||
|
||||
// 2. 清理临时文件(如图片、视频缓存)
|
||||
uni.getFileSystemManager().rmdir({
|
||||
dirPath: `${wx.env.USER_DATA_PATH}/tmp`, // 微信小程序临时目录
|
||||
recursive: true,
|
||||
fail: () => {} // 目录不存在时忽略
|
||||
})
|
||||
|
||||
// 3. 计算清理的缓存大小(模拟,真实场景可通过getStorageInfo获取)
|
||||
uni.getStorageInfo({
|
||||
success: (res) => {
|
||||
const clearSize = (res.currentSize || 0).toFixed(2)
|
||||
uni.showToast({ title: `清理完成,释放${clearSize}KB`, icon: 'success' })
|
||||
resolve({
|
||||
success: true,
|
||||
size: `${clearSize}KB`
|
||||
})
|
||||
},
|
||||
fail: () => {
|
||||
uni.showToast({ title: '缓存清理成功', icon: 'success' })
|
||||
resolve({ success: true, size: '未知' })
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('清理缓存失败:', err)
|
||||
uni.showToast({ title: '缓存清理失败', icon: 'none' })
|
||||
resolve({ success: false, size: '0KB' })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ===================== 偏好设置管理(适配后端接口) =====================
|
||||
// 偏好设置本地存储键名
|
||||
const PREFERENCE_KEY = 'user_preferences'
|
||||
|
||||
/**
|
||||
* 默认偏好设置(后端易适配的核心配置)
|
||||
*/
|
||||
const DEFAULT_PREFERENCES = {
|
||||
theme: 'light', // 主题:light(浅色)/dark(深色)/auto(跟随系统)
|
||||
listStyle: 'card', // 列表样式:card(卡片)/list(列表)
|
||||
budgetWarn: true, // 预算超限提醒:true/false
|
||||
autoSync: true, // 自动同步(后端接口适配:true-后台自动同步/false-手动同步)
|
||||
remindTime: '08:00', // 待办提醒时间:HH:mm
|
||||
monthBudget: 5000, // 月预算(数字,后端用于超限提醒)
|
||||
currency: 'CNY' // 货币单位(后端统一适配)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取偏好设置(本地优先,无则返回默认值)
|
||||
*/
|
||||
export function getPreferences() {
|
||||
try {
|
||||
const localPrefs = uni.getStorageSync(PREFERENCE_KEY)
|
||||
if (localPrefs && typeof localPrefs === 'object') {
|
||||
// 合并默认值(避免新增配置项缺失)
|
||||
return { ...DEFAULT_PREFERENCES, ...localPrefs }
|
||||
}
|
||||
return { ...DEFAULT_PREFERENCES }
|
||||
} catch (err) {
|
||||
console.error('获取偏好设置失败:', err)
|
||||
return { ...DEFAULT_PREFERENCES }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存偏好设置(本地+可选同步到后端)
|
||||
* @param {Object} prefs - 要修改的偏好设置
|
||||
* @param {boolean} [syncToServer=true] - 是否同步到后端
|
||||
* @returns {Promise<Object>} 最新的偏好设置
|
||||
*/
|
||||
export async function savePreferences(prefs, syncToServer = true) {
|
||||
try {
|
||||
// 1. 合并并保存到本地
|
||||
const currentPrefs = getPreferences()
|
||||
const newPrefs = { ...currentPrefs, ...prefs }
|
||||
uni.setStorageSync(PREFERENCE_KEY, newPrefs)
|
||||
|
||||
// 2. 同步到后端(可选)
|
||||
if (syncToServer) {
|
||||
try {
|
||||
// 导入request(避免循环依赖)
|
||||
const request = (await import('./request.js')).default
|
||||
await request.post('/settings/save', { preferences: newPrefs })
|
||||
} catch (err) {
|
||||
console.warn('偏好设置同步后端失败:', err)
|
||||
// 同步失败不影响本地保存
|
||||
}
|
||||
}
|
||||
|
||||
uni.showToast({ title: '设置保存成功', icon: 'success' })
|
||||
return newPrefs
|
||||
} catch (err) {
|
||||
console.error('保存偏好设置失败:', err)
|
||||
uni.showToast({ title: '设置保存失败', icon: 'none' })
|
||||
return getPreferences()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置偏好设置为默认值
|
||||
*/
|
||||
export function resetPreferences() {
|
||||
uni.setStorageSync(PREFERENCE_KEY, DEFAULT_PREFERENCES)
|
||||
uni.showToast({ title: '已恢复默认设置', icon: 'success' })
|
||||
return { ...DEFAULT_PREFERENCES }
|
||||
}
|
||||
|
||||
// ===================== 其他本地实用功能 =====================
|
||||
/**
|
||||
* 获取小程序版本信息
|
||||
*/
|
||||
export function getAppVersion() {
|
||||
const systemInfo = uni.getSystemInfoSync()
|
||||
return {
|
||||
version: systemInfo.version || '未知',
|
||||
platform: systemInfo.platform || '未知',
|
||||
appName: '备忘录' // 替换为你的小程序名称
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user