数据持久化和添加文档

This commit is contained in:
not-like-juvenile
2026-02-03 21:03:45 +08:00
parent 768140fda3
commit 3a0936c12e
5 changed files with 89 additions and 3 deletions

View File

@@ -186,3 +186,23 @@ uni-app-x 项目结构
**修复验证**: ✅ 完成 **修复验证**: ✅ 完成
**系统状态**: 🟢 就绪 **系统状态**: 🟢 就绪
**可部署**: ✅ 是 **可部署**: ✅ 是
---
## 2026年2月3日 修复追加
### 现象一:登录后不跳转或直接进入管理端
- **问题**: 用户报告登录成功后无反应,或者刷新后由于 `IS_TEST_MODE` 开启,系统停留在默认的 `pages/mall/admin/homePage/index`
- **修复**: 修改 `pages/user/login.uvue`,强制在登录成功后跳转到商城首页 `/pages/mall/consumer/index`,确保引导流程正确。
### 现象二:数据分析页面编译错误
- **问题**: `pages/mall/analytics/profile.uvue` 报错 `Unexpected token`
- **修复**: 补全了代码中缺失的闭合花括号 `}`
### 现象三H5 刷新后登录态/数据丢失
- **问题**: 刷新页面导致内存状态清空Supabase Session 回复慢导致用户 ID 丢失,页面显示“今日数据”全为 0。
- **修复**:
-`utils/store.uts` 中通过 `uni.setStorageSync` 实现 `user_id` 的本地持久化。
- 增强 `getCurrentUserId` 逻辑,支持本地存储兜底。
- 优化 `admin/admin` 模拟账号的持久化支持。

View File

@@ -0,0 +1,37 @@
# 问题修复汇总报告 (2026-02-03)
## 1. 登录后无法进入首页的问题
**现象**:用户登录成功后页面无跳转,或者直接跳到了管理/分析后台。
**原因**
- `pages.json``pages/mall/admin/homePage/index` 排在首位,导致默认入口偏向管理端。
- `ak/config.uts` 中的 `IS_TEST_MODE` 开启时,登录页和启动页禁用了自动重定向。
- 登录逻辑中缺少对 `setIsLoggedIn` 的调用,导致全局状态未激活。
**修复方案**
- 修改 `pages/user/login.uvue`,无论是否在测试模式,登录成功后显式执行 `uni.switchTab` 跳转至 `/pages/mall/consumer/index`
- 补全了 `utils/store.uts` 中相关状态管理函数的导入。
## 2. 分析页面 (Profile) 报错
**现象**:编译时提示 `Unexpected token`,页面无法打开。
**原因**
- `pages/mall/analytics/profile.uvue` 中的 `loadReportCounts` 函数缺少一个闭合花括号 `}`,导致语法解析异常。
**修复方案**
-`pages/mall/analytics/profile.uvue` 中补全了完整的代码结构。
## 3. H5 页面刷新后数据消失 (0数据)
**现象**:在配送端或用户中心,刷新页面后“今日统计”或“用户信息”变为 0 或空。
**原因**
- 在 H5 环境下刷新会导致内存中的变量Vue Reactive State重置。
- 如果 Supabase Session 恢复较慢,或者使用了模拟账号(如 `admin`),会导致系统判定用户未登录,从而查询不到 ID。
**修复方案**
- **增强持久化**:在 `utils/store.uts``getCurrentUserId` 函数中加入了本地存储LocalStorage兜底。
- **登录保存**:在登录成功瞬间,将 `user_id` 写入 `uni.setStorageSync('user_id', ...)`
- **模拟账号优化**:针对 `admin` 账号在刷新后不会因为没有远程 Session 而被踢出。
---
## 待核对事项
- **首页顺序**:如果需要默认进入移动全商城而非管理后台,请调整 `pages.json` 的数组顺序。
- **订单数据**:目前“附近订单”在日志中显示有数据,但若在界面显示不全,请检查 `ml_delivery_tasks` 表中的 `status` 是否符合筛选条件。

View File

@@ -405,6 +405,7 @@ import { getCurrentUserId, getCurrentUser } from '@/utils/store.uts'
console.log('loadAvailableOrders: query result=', res) console.log('loadAvailableOrders: query result=', res)
if (res && Array.isArray(res.data)) { if (res && Array.isArray(res.data)) {
const fetched = (res.data as Array<any>).map((r:any) => this._transformTask(r)) const fetched = (res.data as Array<any>).map((r:any) => this._transformTask(r))
console.log('loadAvailableOrders: transformed count=', fetched.length)
// 再次检查 currentTask避免并发情况下短暂展示可接单 // 再次检查 currentTask避免并发情况下短暂展示可接单
if (this.currentTask) { if (this.currentTask) {
this.availableOrders = [] this.availableOrders = []

View File

@@ -280,6 +280,7 @@ const handleLogin = async () => {
class_id: '' class_id: ''
} as UserProfile } as UserProfile
setUserProfile(adminProfile) setUserProfile(adminProfile)
uni.setStorageSync('user_id', 'admin')
uni.showToast({ title: '管理员登录成功', icon: 'success' }) uni.showToast({ title: '管理员登录成功', icon: 'success' })
setTimeout(() => { setTimeout(() => {

View File

@@ -54,7 +54,29 @@ export async function getCurrentUser() : Promise<UserProfile | null> {
} catch (_) {} } catch (_) {}
const sessionInfo = supa.getSession() const sessionInfo = supa.getSession()
// 如果没有 session 但 state 中已有用户信息(可能是 admin 这种 mock 账号),不再强制清空
if (sessionInfo.user == null) { if (sessionInfo.user == null) {
const existingId = state.userProfile?.id
if (existingId == 'admin') {
state.isLoggedIn = true
return state.userProfile as UserProfile
}
// 检查本地持久化是否有 admin 标记
const cachedId = uni.getStorageSync('user_id') as string | null
if (cachedId == 'admin') {
const adminProfile: UserProfile = {
id: 'admin',
username: 'Admin',
email: 'admin@mall.com',
role: 'admin'
}
state.userProfile = adminProfile
state.isLoggedIn = true
return adminProfile
}
state.userProfile = { username: '', email: '' } state.userProfile = { username: '', email: '' }
state.isLoggedIn = false // 未登录 state.isLoggedIn = false // 未登录
return null return null
@@ -140,19 +162,24 @@ export function getCurrentUserId() : string {
const profile = state.userProfile const profile = state.userProfile
if (profile != null && profile.id != null) { if (profile != null && profile.id != null) {
const profileId = profile.id const profileId = profile.id
if (profileId != null) { if (profileId != null && profileId !== "") {
return profileId return profileId
} }
} }
} catch (e) { } } catch (e) { }
try { try {
const session = supa.getSession() const session = supa.getSession()
if (session != null) { if (session != null && session.user != null) {
const curuser = session.user const curuser = session.user
const userId = curuser?.getString('id') const userId = curuser?.getString('id')
if (userId != null) return userId if (userId != null && userId !== "") return userId
} }
} catch (e) { } } catch (e) { }
// 新增:从本地存储获取兜底,解决 H5 刷新后 state 丢失且 session 尚未恢复的问题
try {
const cachedId = uni.getStorageSync('user_id') as string | null
if (cachedId != null && cachedId !== "") return cachedId
} catch (e) { }
return '' return ''
} }