完成距离预校验
This commit is contained in:
@@ -26,7 +26,11 @@ export type AkUserProfile = {
|
||||
|
||||
/**
|
||||
* 加载当前登录用户的业务账号信息
|
||||
* 从 Supabase Auth 获取 authUserId,然后查询 ak_users 表获取业务用户信息
|
||||
* 查询顺序:
|
||||
* 1. auth_id = session.user.id
|
||||
* 2. id = uni.getStorageSync('user_id')
|
||||
* 3. email = 当前登录邮箱
|
||||
* 4. 最后才失败
|
||||
*/
|
||||
export async function loadCurrentAkUser(): Promise<AkUserProfile> {
|
||||
// 1. 获取当前 Supabase Auth 用户
|
||||
@@ -41,17 +45,68 @@ export async function loadCurrentAkUser(): Promise<AkUserProfile> {
|
||||
}
|
||||
|
||||
// 2. 查询 ak_users 表,通过 auth_id 匹配
|
||||
const { data: profiles, error } = await supa
|
||||
console.log('[akUserMapping] 尝试通过 auth_id 查询:', authUserId)
|
||||
let { data: profiles, error } = await supa
|
||||
.from('ak_users')
|
||||
.select('id, auth_id, username, email, role, user_type, status')
|
||||
.eq('auth_id', authUserId)
|
||||
.single()
|
||||
|
||||
if (error == null && profiles != null) {
|
||||
console.log('[akUserMapping] 通过 auth_id 查询成功')
|
||||
} else {
|
||||
console.warn('[akUserMapping] auth_id 查询失败:', error?.message ?? '未知错误')
|
||||
|
||||
// 3. 尝试通过 user_id 缓存查询
|
||||
const cachedUserId = uni.getStorageSync('user_id')
|
||||
if (cachedUserId != null && String(cachedUserId).length > 0) {
|
||||
console.log('[akUserMapping] 尝试通过 user_id 缓存查询:', cachedUserId)
|
||||
const { data: profilesById, error: errorById } = await supa
|
||||
.from('ak_users')
|
||||
.select('id, auth_id, username, email, role, user_type, status')
|
||||
.eq('id', cachedUserId)
|
||||
.single()
|
||||
|
||||
if (errorById == null && profilesById != null) {
|
||||
profiles = profilesById
|
||||
error = null
|
||||
console.log('[akUserMapping] 通过 user_id 缓存查询成功')
|
||||
} else {
|
||||
console.warn('[akUserMapping] user_id 缓存查询失败:', errorById?.message ?? '未知错误')
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 尝试通过 session 中的 email 查询
|
||||
if (profiles == null) {
|
||||
const userEmail = session.user.getString('email') ?? ''
|
||||
if (userEmail != '') {
|
||||
console.log('[akUserMapping] 尝试通过 email 查询:', userEmail)
|
||||
const { data: profilesByEmail, error: errorByEmail } = await supa
|
||||
.from('ak_users')
|
||||
.select('id, auth_id, username, email, role, user_type, status')
|
||||
.eq('email', userEmail)
|
||||
.single()
|
||||
|
||||
if (errorByEmail == null && profilesByEmail != null) {
|
||||
profiles = profilesByEmail
|
||||
error = null
|
||||
console.log('[akUserMapping] 通过 email 查询成功')
|
||||
} else {
|
||||
console.warn('[akUserMapping] email 查询失败:', errorByEmail?.message ?? '未知错误')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (error != null || profiles == null) {
|
||||
console.error('[akUserMapping] ❌ 所有查询方式都失败')
|
||||
console.error('[akUserMapping] authUserId:', authUserId)
|
||||
console.error('[akUserMapping] user_id 缓存:', uni.getStorageSync('user_id'))
|
||||
console.error('[akUserMapping] session email:', session.user?.getString('email'))
|
||||
throw new Error('未找到当前用户的业务账号,请联系管理员绑定 ak_users.auth_id')
|
||||
}
|
||||
|
||||
// 3. 存储到本地
|
||||
// 5. 存储到本地
|
||||
const profile: AkUserProfile = {
|
||||
id: profiles.getString('id') ?? '',
|
||||
auth_id: profiles.getString('auth_id') ?? '',
|
||||
@@ -78,18 +133,192 @@ export async function loadCurrentAkUser(): Promise<AkUserProfile> {
|
||||
|
||||
/**
|
||||
* 获取当前业务用户 ID(ak_users.id)
|
||||
* 优先从本地缓存读取,如果缓存不存在则自动加载
|
||||
* 优先级:
|
||||
* 1. uni.getStorageSync('ak_user_id')
|
||||
* 2. uni.getStorageSync('current_ak_user').id
|
||||
* 3. uni.getStorageSync('user_info').id
|
||||
* 4. uni.getStorageSync('userInfo').id
|
||||
* 5. uni.getStorageSync('user').id
|
||||
* 6. uni.getStorageSync('uid')
|
||||
* 7. uni.getStorageSync('user_id')
|
||||
* 8. uni.getStorageSync('auth_user_id')
|
||||
* 9. 最后再请求 ak_users 表或 RPC resolve_ak_user_id
|
||||
*/
|
||||
export async function getCurrentAkUserId(): Promise<string> {
|
||||
// 1. 尝试从缓存读取
|
||||
// 1. 尝试从 ak_user_id 缓存读取
|
||||
const cachedAkUserId = uni.getStorageSync(AK_USER_ID_KEY)
|
||||
if (cachedAkUserId != null && String(cachedAkUserId).length > 0) {
|
||||
return String(cachedAkUserId)
|
||||
const result = String(cachedAkUserId)
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 从 ak_user_id 缓存读取:', result)
|
||||
return result
|
||||
}
|
||||
|
||||
// 2. 缓存不存在,自动加载
|
||||
const profile = await loadCurrentAkUser()
|
||||
return profile.id
|
||||
// 2. 尝试从 current_ak_user 缓存读取
|
||||
const cachedProfile = uni.getStorageSync(AK_USER_PROFILE_KEY)
|
||||
if (cachedProfile != null && String(cachedProfile).length > 0) {
|
||||
try {
|
||||
const profile = JSON.parse(cachedProfile) as AkUserProfile
|
||||
if (profile.id != null && profile.id !== '') {
|
||||
const result = profile.id
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 从 current_ak_user 缓存读取:', result)
|
||||
return result
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: 解析 current_ak_user 失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 尝试从 user_info 缓存读取
|
||||
try {
|
||||
const cachedUserInfo = uni.getStorageSync('user_info')
|
||||
if (cachedUserInfo != null && String(cachedUserInfo).length > 0) {
|
||||
try {
|
||||
const userInfo = JSON.parse(cachedUserInfo) as any
|
||||
if (userInfo != null && userInfo.id != null && String(userInfo.id).length > 0) {
|
||||
const result = String(userInfo.id)
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 从 user_info 缓存读取:', result)
|
||||
return result
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: 解析 user_info 失败:', e)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: user_info 读取失败:', e)
|
||||
}
|
||||
|
||||
// 4. 尝试从 userInfo 缓存读取
|
||||
try {
|
||||
const cachedUserInfo = uni.getStorageSync('userInfo')
|
||||
if (cachedUserInfo != null && String(cachedUserInfo).length > 0) {
|
||||
try {
|
||||
const userInfo = JSON.parse(cachedUserInfo) as any
|
||||
if (userInfo != null && userInfo.id != null && String(userInfo.id).length > 0) {
|
||||
const result = String(userInfo.id)
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 从 userInfo 缓存读取:', result)
|
||||
return result
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: 解析 userInfo 失败:', e)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: userInfo 读取失败:', e)
|
||||
}
|
||||
|
||||
// 5. 尝试从 user 缓存读取
|
||||
try {
|
||||
const cachedUser = uni.getStorageSync('user')
|
||||
if (cachedUser != null && String(cachedUser).length > 0) {
|
||||
try {
|
||||
const user = JSON.parse(cachedUser) as any
|
||||
if (user != null && user.id != null && String(user.id).length > 0) {
|
||||
const result = String(user.id)
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 从 user 缓存读取:', result)
|
||||
return result
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: 解析 user 失败:', e)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: user 读取失败:', e)
|
||||
}
|
||||
|
||||
// 6. 尝试从 uid 缓存读取
|
||||
const cachedUid = uni.getStorageSync('uid')
|
||||
if (cachedUid != null && String(cachedUid).length > 0) {
|
||||
const result = String(cachedUid)
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 从 uid 缓存读取:', result)
|
||||
return result
|
||||
}
|
||||
|
||||
// 7. 尝试从 user_id 缓存读取
|
||||
const cachedUserId = uni.getStorageSync('user_id')
|
||||
if (cachedUserId != null && String(cachedUserId).length > 0) {
|
||||
const result = String(cachedUserId)
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 从 user_id 缓存读取:', result)
|
||||
return result
|
||||
}
|
||||
|
||||
// 8. 尝试从 auth_user_id 缓存读取(可能是 Supabase Auth ID,需要转换)
|
||||
const cachedAuthUserId = uni.getStorageSync(AUTH_USER_ID_KEY)
|
||||
if (cachedAuthUserId != null && String(cachedAuthUserId).length > 0) {
|
||||
const authId = String(cachedAuthUserId)
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 从 auth_user_id 缓存读取到 Auth ID:', authId)
|
||||
|
||||
// 禁止直接返回 auth_user_id,必须通过 RPC 或 ak_users 表转换
|
||||
console.log('[akUserMapping] 尝试通过 RPC resolve_ak_user_id 转换 Auth ID')
|
||||
try {
|
||||
const rpcParams = { p_auth_user_id: authId } as UTSJSONObject
|
||||
const { data: resolvedData, error: resolveError } = await supa
|
||||
.rpc('resolve_ak_user_id', rpcParams)
|
||||
|
||||
if (resolveError == null && resolvedData != null) {
|
||||
const akUserId = String(resolvedData)
|
||||
if (akUserId != '' && akUserId != 'null') {
|
||||
console.log('[akUserMapping] RPC 转换成功,akUserId:', akUserId)
|
||||
// 缓存 akUserId
|
||||
uni.setStorageSync(AK_USER_ID_KEY, akUserId)
|
||||
return akUserId
|
||||
}
|
||||
}
|
||||
console.warn('[akUserMapping] RPC 转换失败:', resolveError?.message ?? '未知错误')
|
||||
} catch (rpcErr) {
|
||||
console.warn('[akUserMapping] RPC 转换异常:', rpcErr)
|
||||
}
|
||||
|
||||
// 如果 RPC 失败,尝试通过 ak_users 表查询
|
||||
console.log('[akUserMapping] 尝试通过 ak_users 表查询 auth_id 映射')
|
||||
const { data: profiles, error } = await supa
|
||||
.from('ak_users')
|
||||
.select('id')
|
||||
.eq('auth_id', authId)
|
||||
.single()
|
||||
|
||||
if (error == null && profiles != null) {
|
||||
const akUserId = profiles.getString('id') ?? ''
|
||||
if (akUserId != '') {
|
||||
console.log('[akUserMapping] ak_users 表查询成功,akUserId:', akUserId)
|
||||
uni.setStorageSync(AK_USER_ID_KEY, akUserId)
|
||||
return akUserId
|
||||
}
|
||||
}
|
||||
console.warn('[akUserMapping] ak_users 表查询也失败')
|
||||
}
|
||||
|
||||
// 9. 最后尝试从 Supabase session 查询 ak_users
|
||||
try {
|
||||
const session = supa.getSession()
|
||||
if (session != null && session.user != null) {
|
||||
const authUserId = session.user.getString('id') ?? ''
|
||||
if (authUserId != '') {
|
||||
console.log('[akUserMapping] getCurrentAkUserId: 尝试从 Supabase session 查询 ak_users, authUserId:', authUserId)
|
||||
const { data: profiles, error } = await supa
|
||||
.from('ak_users')
|
||||
.select('id')
|
||||
.eq('auth_id', authUserId)
|
||||
.single()
|
||||
|
||||
if (error == null && profiles != null) {
|
||||
const akUserId = profiles.getString('id') ?? ''
|
||||
if (akUserId != '') {
|
||||
console.log('[akUserMapping] getCurrentAkUserId: Supabase 查询成功:', akUserId)
|
||||
// 缓存结果
|
||||
uni.setStorageSync(AK_USER_ID_KEY, akUserId)
|
||||
return akUserId
|
||||
}
|
||||
}
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: Supabase 查询未找到 ak_users 记录')
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: Supabase 查询失败:', e)
|
||||
}
|
||||
|
||||
// 10. 全部失败,返回空字符串(不 throw)
|
||||
console.warn('[akUserMapping] getCurrentAkUserId: 所有降级策略失败,返回空字符串')
|
||||
return ''
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user