数据持久化和添加文档
This commit is contained in:
@@ -54,7 +54,29 @@ export async function getCurrentUser() : Promise<UserProfile | null> {
|
||||
} catch (_) {}
|
||||
|
||||
const sessionInfo = supa.getSession()
|
||||
|
||||
// 如果没有 session 但 state 中已有用户信息(可能是 admin 这种 mock 账号),不再强制清空
|
||||
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.isLoggedIn = false // 未登录
|
||||
return null
|
||||
@@ -140,19 +162,24 @@ export function getCurrentUserId() : string {
|
||||
const profile = state.userProfile
|
||||
if (profile != null && profile.id != null) {
|
||||
const profileId = profile.id
|
||||
if (profileId != null) {
|
||||
if (profileId != null && profileId !== "") {
|
||||
return profileId
|
||||
}
|
||||
}
|
||||
} catch (e) { }
|
||||
try {
|
||||
const session = supa.getSession()
|
||||
if (session != null) {
|
||||
if (session != null && session.user != null) {
|
||||
const curuser = session.user
|
||||
const userId = curuser?.getString('id')
|
||||
if (userId != null) return userId
|
||||
if (userId != null && userId !== "") return userId
|
||||
}
|
||||
} catch (e) { }
|
||||
// 新增:从本地存储获取兜底,解决 H5 刷新后 state 丢失且 session 尚未恢复的问题
|
||||
try {
|
||||
const cachedId = uni.getStorageSync('user_id') as string | null
|
||||
if (cachedId != null && cachedId !== "") return cachedId
|
||||
} catch (e) { }
|
||||
return ''
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user