同步修改页面逻辑
This commit is contained in:
@@ -86,9 +86,9 @@ export async function getCurrentUser() : Promise<UserProfile | null> {
|
||||
state.userProfile = { username: '', email: '' }
|
||||
state.isLoggedIn = false // 未登录
|
||||
return null
|
||||
} // 查询 ak_users 表补全 profile
|
||||
const res = await supa.from('ak_users').select('*', {}).eq('id', userId).execute()
|
||||
console.log(res)
|
||||
} // 查询 ak_users 表补全 profile,通过 auth_id (session 中的 user.id) 进行匹配
|
||||
const res = await supa.from('ak_users').select('*', {}).eq('auth_id', userId).execute()
|
||||
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;
|
||||
@@ -98,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)
|
||||
@@ -121,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') ?? "",
|
||||
@@ -141,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: '' }
|
||||
|
||||
Reference in New Issue
Block a user