This commit is contained in:
2026-08-10 12:24:18 +08:00
parent 79435fe474
commit 88788dc473
7 changed files with 30 additions and 26 deletions
+13 -19
View File
@@ -6,11 +6,10 @@ import { ApiError } from '@/api/http'
import {
FRONTEND_TO_MERCHANT_PERMISSION_CODES,
TENANT_PERMISSION_GROUPS,
getMerchantPermissionResources,
getCurrentMerchantPermissionCodes,
getMerchantPersonalProfile,
getTenantProfile,
updateMerchantPersonalProfile,
type MerchantPermissionResource,
} from '@/modules/tenant-management/public'
import { clearSessionTokens, getAccessToken, getRefreshToken, persistTemporaryAccessToken, persistTokenPair } from '@/api/session'
import { decodeMerchantToken, type MerchantJwtPayload } from '@/modules/auth/utils/jwt'
@@ -41,15 +40,8 @@ const emptyProfile: ClientAccountProfile = {
tenantName: '',
}
function effectiveBackendPermissions(payload: MerchantJwtPayload, resources?: MerchantPermissionResource[]) {
const jwtPermissions = new Set(payload.permissions ?? [])
if (!resources) return jwtPermissions
const activeResources = new Set(resources.filter((item) => item.status === 'ACTIVE').map((item) => item.permissionCode))
return new Set([...jwtPermissions].filter((permission) => activeResources.has(permission)))
}
function mapPermissions(payload: MerchantJwtPayload, resources?: MerchantPermissionResource[]) {
const backendPermissions = effectiveBackendPermissions(payload, resources)
function mapPermissions(backendPermissionCodes: string[] = []) {
const backendPermissions = new Set(backendPermissionCodes)
const result = new Set<string>(['dashboard:view', 'account:profile:view'])
for (const permission of backendPermissions) {
if (frontendPermissionCodes.has(permission)) result.add(permission)
@@ -64,7 +56,7 @@ function currentUser(payload: MerchantJwtPayload, tenantName?: string): CurrentU
name: payload.username,
account: payload.username,
tenantName: tenantName || `企业租户 ${payload.tenant_id}`,
roles: payload.roles?.length ? [...payload.roles] : ['企业成员'],
roles: [],
}
}
@@ -80,14 +72,14 @@ export const useAuthStore = defineStore('client-auth', () => {
const permissionCodes = ref<string[]>([])
const sessionRestored = ref(false)
function applyToken(accessToken: string, resources?: MerchantPermissionResource[]) {
function applyToken(accessToken: string) {
const decoded = decodeMerchantToken(accessToken)
payload.value = decoded
token.value = accessToken
user.value = currentUser(decoded, user.value?.tenantName)
profile.value.username = decoded.username
profile.value.realName = decoded.username
permissionCodes.value = mapPermissions(decoded, resources)
permissionCodes.value = mapPermissions()
}
function clearSession() {
@@ -131,12 +123,14 @@ export const useAuthStore = defineStore('client-auth', () => {
async function loadSessionContext() {
if (!payload.value) return
const [resources, tenant, personalProfile] = await Promise.all([
getMerchantPermissionResources(),
const currentPermissionCodes = await getCurrentMerchantPermissionCodes()
const currentAccessToken = getAccessToken()
if (currentAccessToken && currentAccessToken !== token.value) applyToken(currentAccessToken)
permissionCodes.value = mapPermissions(currentPermissionCodes)
const [tenant, personalProfile] = await Promise.all([
getTenantProfile(),
getMerchantPersonalProfile(),
])
permissionCodes.value = mapPermissions(payload.value, resources)
user.value = currentUser(payload.value, tenant.tenantName)
profile.value = {
...profile.value,
@@ -174,7 +168,7 @@ export const useAuthStore = defineStore('client-auth', () => {
payload.value = decoded
user.value = currentUser(decoded)
profile.value = { ...emptyProfile, username: decoded.username, realName: decoded.username }
permissionCodes.value = mapPermissions(decoded)
permissionCodes.value = mapPermissions()
sessionRestored.value = false
try {
await loadSessionContext()
@@ -196,7 +190,7 @@ export const useAuthStore = defineStore('client-auth', () => {
payload.value = decoded
user.value = currentUser(decoded)
profile.value = { ...emptyProfile, username: decoded.username, realName: decoded.username }
permissionCodes.value = mapPermissions(decoded)
permissionCodes.value = mapPermissions()
sessionRestored.value = false
try {
await loadSessionContext()