107 lines
4.6 KiB
Python
107 lines
4.6 KiB
Python
import codecs
|
||
import re
|
||
|
||
path = r'd:\骅锋\mall\pages\user\login.uvue'
|
||
with codecs.open(path, 'r', 'utf-8') as f:
|
||
text = f.read()
|
||
|
||
# Replace the checkMerchantAccess function and related logic
|
||
|
||
old_func_start = "const checkMerchantAccess = async (uid: string, rawEmail: string) : Promise<string | null> => {"
|
||
old_func_end_marker = "const codeDisabled = ref<boolean>(false)"
|
||
|
||
func_start_idx = text.find(old_func_start)
|
||
func_end_idx = text.find(old_func_end_marker)
|
||
|
||
if func_start_idx != -1 and func_end_idx != -1:
|
||
new_func = '''const checkAdminOrMerchantAccess = async (uid: string, rawEmail: string) : Promise<UTSJSONObject | null> => {
|
||
const email = rawEmail.trim().toLowerCase()
|
||
console.log(🔍 开始校验后台或商家端角色 -> UID: , Email: )
|
||
|
||
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
|
||
}
|
||
|
||
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
|
||
|
||
// 2. 尝试按 id 查询 (兼容老数据)
|
||
res = await supa.from('ak_users').select('id, role').eq('id', uid).execute()
|
||
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()
|
||
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('后台身份校验失败,请联系管理员检查用户数据')
|
||
}
|
||
}
|
||
|
||
'''
|
||
|
||
text = text[:func_start_idx] + new_func + text[func_end_idx:]
|
||
|
||
# Replace the usage in handleLogin
|
||
old_usage = ''' const merchantId = await checkMerchantAccess(sessionUid, account.value)
|
||
if (merchantId == null) {
|
||
await supa.signOut()
|
||
logout()
|
||
throw new Error('您还没有注册商家端账户,快去注册一个')
|
||
}
|
||
|
||
// 存入商家ID
|
||
uni.setStorageSync('merchant_id', merchantId)'''
|
||
|
||
new_usage = ''' const accessData = await checkAdminOrMerchantAccess(sessionUid, account.value)
|
||
if (accessData == null) {
|
||
await supa.signOut()
|
||
logout()
|
||
throw new Error('该账户无后台或商家端权限')
|
||
}
|
||
|
||
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')
|
||
}'''
|
||
|
||
if 'checkMerchantAccess' in text:
|
||
print("WARNING: checkMerchantAccess still found after replace, let's substitute directly")
|
||
text = text.replace(old_usage, new_usage)
|
||
|
||
with codecs.open(path, 'w', 'utf-8') as f:
|
||
f.write(text)
|
||
|
||
print("Updated login.uvue successfully.")
|