Merge remote-tracking branch 'origin/ysj-delivery'
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
|
||||
@@ -64,9 +86,9 @@ export async function getCurrentUser() : Promise<UserProfile | null> {
|
||||
state.userProfile = { username: '', email: '' }
|
||||
state.isLoggedIn = false // 未登录
|
||||
return null
|
||||
} // 查询 ak_users 表补全 profile(权威关联字段:auth_id = auth.uid())
|
||||
} // 查询 ak_users 表补全 profile,通过 auth_id (session 中的 user.id) 进行匹配
|
||||
const res = await supa.from('ak_users').select('*', {}).eq('auth_id', userId).execute()
|
||||
console.log(res)
|
||||
console.log('Profile Load Result:', res)
|
||||
if (res.status >= 200 && res.status < 300 && (res.data != null)) {
|
||||
let user : UTSJSONObject | null = null;
|
||||
const data = res.data as any;
|
||||
@@ -76,9 +98,43 @@ export async function getCurrentUser() : Promise<UserProfile | null> {
|
||||
}
|
||||
} else if (data != null) {
|
||||
user = data as UTSJSONObject;
|
||||
} console.log(user)
|
||||
}
|
||||
|
||||
if (user != null) {
|
||||
const profile : UserProfile = {
|
||||
id: user.getString('id'),
|
||||
username: user.getString('username') ?? "",
|
||||
email: user.getString('email') ?? "",
|
||||
role: user.getString('role'),
|
||||
avatar_url: user.getString('avatar_url')
|
||||
} as UserProfile
|
||||
state.userProfile = profile
|
||||
state.isLoggedIn = true
|
||||
|
||||
// 关键点:将 ak_users 表中的 UUID 存入本地存储,确保过滤时使用的是业务表的 ID
|
||||
if (profile.id != null) {
|
||||
uni.setStorageSync('user_id', profile.id)
|
||||
}
|
||||
return profile
|
||||
}
|
||||
}
|
||||
|
||||
// 如果按 auth_id 没查到,尝试按原逻辑 (id = userId) 查一次作为兼容
|
||||
const resFallback = await supa.from('ak_users').select('*', {}).eq('id', userId).execute()
|
||||
if (resFallback.status >= 200 && resFallback.status < 300 && (resFallback.data != null)) {
|
||||
let user : UTSJSONObject | null = null;
|
||||
const data = resFallback.data as any;
|
||||
if (Array.isArray(data)) {
|
||||
if (data.length > 0) {
|
||||
user = data[0] as UTSJSONObject;
|
||||
}
|
||||
} else if (data != null) {
|
||||
user = data as UTSJSONObject;
|
||||
}
|
||||
|
||||
if (user == null) {
|
||||
console.log('用户资料为空,尝试创建基础资料...') // 如果用户资料为空,尝试创建基础用户资料
|
||||
console.log('用户资料为空,尝试创建基础资料...')
|
||||
// 如果用户资料为空,尝试创建基础用户资料
|
||||
const sessionUser = sessionInfo.user
|
||||
if (sessionUser != null) {
|
||||
const createdProfile = await ensureUserProfile(sessionUser)
|
||||
@@ -99,8 +155,7 @@ export async function getCurrentUser() : Promise<UserProfile | null> {
|
||||
return null
|
||||
}
|
||||
}
|
||||
console.log(user)
|
||||
// 直接用 getString/getNumber,无需兜底属性
|
||||
|
||||
const profile : UserProfile = {
|
||||
id: user.getString('id'),
|
||||
username: user.getString('username') ?? "",
|
||||
@@ -119,6 +174,9 @@ export async function getCurrentUser() : Promise<UserProfile | null> {
|
||||
}
|
||||
state.userProfile = profile
|
||||
state.isLoggedIn = true // 登录成功
|
||||
if (profile.id != null) {
|
||||
uni.setStorageSync('user_id', profile.id)
|
||||
}
|
||||
return profile
|
||||
} else {
|
||||
state.userProfile = { username: '', email: '' }
|
||||
@@ -140,19 +198,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