初步完成merchant和admin不同role的展示内容逻辑

This commit is contained in:
2026-03-12 10:18:04 +08:00
parent c90b549b89
commit f19dd093bf
13 changed files with 853 additions and 110 deletions

View File

@@ -175,65 +175,54 @@ const isLoading = ref<boolean>(false)
* 【核心函数】:登录成功后,多条件校验是否为商家角色
* 优先级: session_uid (auth_id) -> id -> normalized email
*/
const checkMerchantAccess = async (uid: string, rawEmail: string) : Promise<string | null> => {
const email = rawEmail.trim().toLowerCase()
console.log(`🔍 开始校验商家端角色 -> UID: ${uid}, Email: ${email}`)
const checkAdminOrMerchantAccess = async (uid: string, rawEmail: string) : Promise<UTSJSONObject | null> => {
const email = rawEmail.trim().toLowerCase()
console.log(`🔍 开始校验后台或商家端角色 -> UID: ${uid}, Email: ${email}`)
try {
// 1. 尝试按 auth_id 查询
let res = await supa.from('ak_users').select('id, role').eq('auth_id', uid).execute()
let dataArray = res.data
if (Array.isArray(dataArray) && dataArray.length > 0) {
const obj = dataArray[0] as UTSJSONObject
const role = obj.getString('role')
const id = obj.getString('id')
console.log('✅ 按 auth_id 匹配成功role:', role)
if (role === 'merchant' && id != null) return id
return null
}
const parseRoleData = (dataArray: any | null): UTSJSONObject | null => {
if (Array.isArray(dataArray) && dataArray.length > 0) {
const obj = dataArray[0] as UTSJSONObject
const role = obj.getString('role')
const id = obj.getString('id')
console.log('✅ 匹配成功role:', role)
if ((role === 'merchant' || role === 'admin') && id != null) {
return { id, role } as UTSJSONObject
}
}
return null
}
// 2. 尝试按 id 查询 (兼容老数据)
res = await supa.from('ak_users').select('id, role').eq('id', uid).execute()
dataArray = res.data
if (Array.isArray(dataArray) && dataArray.length > 0) {
const obj = dataArray[0] as UTSJSONObject
const role = obj.getString('role')
const id = obj.getString('id')
console.log('✅ 按 id 匹配成功role:', role)
if (role === 'merchant' && id != null) return id
return null
}
try {
// 1. 尝试按 auth_id 查询
let res = await supa.from('ak_users').select('id, role').eq('auth_id', uid).execute()
let parsed = parseRoleData(res.data)
if (parsed != null) return parsed
// 3. 尝试按 email 兜底查询
if (email !== '') {
res = await supa.from('ak_users').select('id, role').eq('email', email).execute()
dataArray = res.data
if (Array.isArray(dataArray) && dataArray.length > 0) {
// 如果按邮箱查出来多条,可能存在脏数据,只取第一条并记录日志
if (dataArray.length > 1) {
console.error('⚠️ 警告: 按 email 查到多条 ak_users 记录取第一条校验。Email:', email)
}
const obj = dataArray[0] as UTSJSONObject
const role = obj.getString('role')
const id = obj.getString('id')
console.log('✅ 按 email 匹配成功role:', role)
if (role === 'merchant' && id != null) return id
return null
}
}
// 2. 尝试按 id 查询 (兼容老数据)
res = await supa.from('ak_users').select('id, role').eq('id', uid).execute()
parsed = parseRoleData(res.data)
if (parsed != null) return parsed
console.error('❌ 未能在 ak_users 中找到该用户的任何记录')
// 查无此人,跑出自定义错误以与普通系统报错区分
throw new Error('NOT_REGISTERED')
} catch (e) {
console.error('❌ 查询角色过程异常:', e)
if (e instanceof Error && e.message === 'NOT_REGISTERED') {
throw new Error('您还没有注册商家端账户,快去注册一个')
}
// 真实的查询异常/RLS异常抛出防止误会为"未注册"
throw new Error('商家身份校验失败,请联系管理员检查用户数据')
}
// 3. 尝试按 email 兜底查询
if (email !== '') {
res = await supa.from('ak_users').select('id, role').eq('email', email).execute()
const dataArray = res.data
if (Array.isArray(dataArray) && dataArray.length > 1) {
console.error('⚠️ 警告: 按 email 查到多条 ak_users 记录取第一条校验。Email:', email)
}
parsed = parseRoleData(dataArray)
if (parsed != null) return parsed
}
console.error('❌ 未能在 ak_users 中找到该用户的有效角色记录')
throw new Error('NOT_REGISTERED')
} catch (e) {
console.error('❌ 查询角色过程异常:', e)
if (e instanceof Error && e.message === 'NOT_REGISTERED') {
throw new Error('该账户无后台或商家端权限,请联系管理员核对')
}
throw new Error('后台身份校验失败,请联系管理员检查用户数据')
}
}
const codeDisabled = ref<boolean>(false)
@@ -400,15 +389,22 @@ const handleLogin = async () => {
const sessionUser = result.user
let sessionUid = sessionUser?.getString('id') ?? ''
const merchantId = await checkMerchantAccess(sessionUid, account.value)
if (merchantId == null) {
await supa.signOut()
logout()
throw new Error('您还没有注册商家端账户,快去注册一个')
}
const accessData = await checkAdminOrMerchantAccess(sessionUid, account.value)
if (accessData == null) {
await supa.signOut()
logout()
throw new Error('该账户无后台或商家端权限')
}
// 存入商家ID
uni.setStorageSync('merchant_id', merchantId)
const currRole = accessData.getString('role')
const currId = accessData.getString('id')
uni.setStorageSync('adminRole', currRole)
if (currRole === 'merchant') {
uni.setStorageSync('merchant_id', currId)
} else {
uni.removeStorageSync('merchant_id')
}
} else {
uni.showToast({ title: '手机号密码登录功能开发中', icon: 'none' })
return