feat: 增加存储上限提示功能

This commit is contained in:
2026-08-28 15:55:46 +08:00
parent 338ba726a0
commit 196251dfc9
4 changed files with 20 additions and 16 deletions

View File

@@ -10,9 +10,9 @@
</view> </view>
<view class="flex flex-col items-center"> <view class="flex flex-col items-center">
<wd-text :text="`${profileStore.storageTotal} / 1000`"></wd-text> <wd-text :text="`${profileStore.storageTotal} / ${profileStore.storageMax}`"></wd-text>
<wd-progress <wd-progress
:percentage="Number((profileStore.storageTotal / 1000 * 100).toFixed(2))" :percentage="Number((profileStore.storageTotal / profileStore.storageMax * 100).toFixed(2))"
:color="['#00c740', '#ffb300', '#e2231a']"/> :color="['#00c740', '#ffb300', '#e2231a']"/>
</view> </view>

View File

@@ -48,7 +48,7 @@ import { getOverview, getPlan, getProfile } from '@/utils/profile'
import { useProfileStore } from '@/store/profile' import { useProfileStore } from '@/store/profile'
import { useAppStore } from "@/store/app"; import { useAppStore } from "@/store/app";
import type { IProfile } from "@/type/profile"; import type { IProfile } from "@/type/profile";
import { countRecordsByMonth, getTotalCount } from "@/utils/record"; import { countRecordsByMonth, getMaxRecord, getTotalCount } from "@/utils/record";
const profileStore = useProfileStore() const profileStore = useProfileStore()
const appStore = useAppStore() const appStore = useAppStore()
@@ -87,6 +87,7 @@ function openPlanPopup() {
function openStoragePopup() { function openStoragePopup() {
profileStore.storageStats = countRecordsByMonth() profileStore.storageStats = countRecordsByMonth()
profileStore.storageTotal = getTotalCount() profileStore.storageTotal = getTotalCount()
profileStore.storageMax = getMaxRecord()
profileStore.showStoragePopup = true profileStore.showStoragePopup = true
} }
</script> </script>

View File

@@ -15,6 +15,7 @@ export const useProfileStore = defineStore('profile', {
} as IProfile, } as IProfile,
storageStats: [] as IStats[], storageStats: [] as IStats[],
storageTotal: 0, storageTotal: 0,
storageMax: 1000,
overviewList: [] as string[], overviewList: [] as string[],
planList: [] as string[], planList: [] as string[],
isRefresh: false, isRefresh: false,

View File

@@ -2,9 +2,13 @@ import { getProfile } from '@/utils/profile'
import type { IGroupHealthRecord, IHealthRecord, IRecordMeta, IRecordStats } from "@/type/record"; import type { IGroupHealthRecord, IHealthRecord, IRecordMeta, IRecordStats } from "@/type/record";
import type { IStats } from "@/type"; import type { IStats } from "@/type";
const MONTH_KEEP = 12 const MAX_RECORD = 1000;
const RECORD_PREFIX = 'health_' const RECORD_PREFIX = 'health_'
export function getMaxRecord() {
return MAX_RECORD
}
function getMonthKey(timestamp: number) { function getMonthKey(timestamp: number) {
return `${RECORD_PREFIX}${getMonthStr(timestamp)}` return `${RECORD_PREFIX}${getMonthStr(timestamp)}`
} }
@@ -26,18 +30,6 @@ export function getTimeStr(timestamp: number) {
return d.toTimeString().slice(0, 5) return d.toTimeString().slice(0, 5)
} }
function cleanOldMonths() {
const cutoff = new Date()
cutoff.setMonth(cutoff.getMonth() - MONTH_KEEP)
const cutoffKey = `${RECORD_PREFIX}${cutoff.getFullYear()}-${String(cutoff.getMonth() + 1).padStart(2, '0')}`
const keys = wx.getStorageInfoSync().keys
keys.forEach((key: string) => {
if (key.startsWith(RECORD_PREFIX) && key < cutoffKey) {
wx.removeStorageSync(key)
}
})
}
/** /**
* 新增记录 * 新增记录
* @param type 类型 * @param type 类型
@@ -45,6 +37,16 @@ function cleanOldMonths() {
* @param timestamp 时间戳 * @param timestamp 时间戳
*/ */
function addRecord(type: string, data: Record<string, any>, timestamp: number) { function addRecord(type: string, data: Record<string, any>, timestamp: number) {
if (getTotalCount() >= MAX_RECORD) {
wx.showModal({
title: '提示',
content: `本地记录已超过 ${MAX_RECORD} 条,请先删除部分记录后再继续操作`,
confirmText: '我知道了',
showCancel: false
})
return
}
// 键为月份 例如 health_2026-08 // 键为月份 例如 health_2026-08
const key = getMonthKey(timestamp) const key = getMonthKey(timestamp)
const all = wx.getStorageSync(key) || {} const all = wx.getStorageSync(key) || {}