refactor: trim smoke home API usage
This commit is contained in:
+16
-43
@@ -60,18 +60,14 @@
|
||||
├─────────────────────────────────────────────────────────┤
|
||||
│ 0ms ─ 页面骨架屏渲染 │
|
||||
│ │ │
|
||||
│ ├──── 并行请求 ──────────────────────────────────── │
|
||||
│ ├──── 串行守卫 + 单接口数据 ──────────────────────── │
|
||||
│ │ ├── /profile (检查用户状态) │
|
||||
│ │ ├── /dashboard (核心数据) │
|
||||
│ │ └── /next_smoke_time (建议时间) │
|
||||
│ │ └── /home (首页核心数据) │
|
||||
│ │ │
|
||||
│ 200ms ─ 核心数据返回,渲染计时器+统计卡片 │
|
||||
│ │ │
|
||||
│ 300ms ─ 首屏渲染完成 │
|
||||
│ │ │
|
||||
│ │ ┌── 延迟加载 ────────────────────────────── │
|
||||
│ │ └── /ai/advice (AI提示卡片) │
|
||||
│ │ │
|
||||
│ 500ms ─ 完整页面渲染 │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
```
|
||||
@@ -79,29 +75,14 @@
|
||||
### 3.2 缓存策略
|
||||
|
||||
```javascript
|
||||
// stores/dashboard.js
|
||||
import { defineStore } from 'pinia'
|
||||
// 首页使用 /smoke/home 单接口返回当前屏所需字段。
|
||||
// 页面级刷新由 onShow 触发,避免维护额外 dashboard store 和重复请求。
|
||||
const homeData = ref(null)
|
||||
|
||||
export const useDashboardStore = defineStore('dashboard', {
|
||||
state: () => ({
|
||||
todayCount: 0,
|
||||
minutesSinceLast: 0,
|
||||
weekly: [],
|
||||
nextSmokeTime: null,
|
||||
lastFetchTime: 0,
|
||||
cacheExpiry: 30 * 1000
|
||||
}),
|
||||
|
||||
actions: {
|
||||
async fetchDashboard(forceRefresh = false) {
|
||||
const now = Date.now()
|
||||
if (!forceRefresh && now - this.lastFetchTime < this.cacheExpiry) {
|
||||
return
|
||||
}
|
||||
// 发起请求...
|
||||
}
|
||||
}
|
||||
})
|
||||
async function fetchRecordHomeData() {
|
||||
const res = await api.getHome()
|
||||
homeData.value = res.data || {}
|
||||
}
|
||||
```
|
||||
|
||||
### 3.3 计时器优化
|
||||
@@ -240,35 +221,27 @@ export const request = {
|
||||
```javascript
|
||||
// pages/index/index.vue
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { useDashboardStore } from '@/stores/dashboard'
|
||||
import * as api from '@/api/smoke'
|
||||
import * as api from '@/api'
|
||||
|
||||
const loading = ref(true)
|
||||
const dashboardStore = useDashboardStore()
|
||||
const homeData = ref(null)
|
||||
|
||||
async function initPage() {
|
||||
loading.value = true
|
||||
|
||||
try {
|
||||
const [profileRes, dashboardRes, nextTimeRes] = await Promise.all([
|
||||
api.getProfile(),
|
||||
api.getDashboard(),
|
||||
api.getNextSmokeTime()
|
||||
])
|
||||
|
||||
if (!profileRes.data.exists || !profileRes.data.is_completed) {
|
||||
const profileRes = await api.getSmokeProfile()
|
||||
if (!profileRes.exists || !profileRes.is_completed) {
|
||||
uni.redirectTo({ url: '/pages/onboarding/index' })
|
||||
return
|
||||
}
|
||||
|
||||
dashboardStore.setDashboard(dashboardRes.data)
|
||||
dashboardStore.setNextSmokeTime(nextTimeRes.data)
|
||||
|
||||
const homeRes = await api.getHome()
|
||||
homeData.value = homeRes.data || {}
|
||||
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
setTimeout(loadAiAdvice, 300)
|
||||
}
|
||||
|
||||
onMounted(initPage)
|
||||
|
||||
Reference in New Issue
Block a user