feat(tenant): 按租户隔离用户身份与后台权限

This commit is contained in:
yml
2026-11-15 15:37:23 +08:00
parent ca57f604bf
commit abf0fce89b
43 changed files with 1572 additions and 196 deletions
@@ -12,6 +12,7 @@ export interface AdminRole {
export interface AdminUser {
id: number
tenant_id: number
username: string
nickname: string
status: UserStatus
+1
View File
@@ -5,6 +5,7 @@ import { getTrackCode } from '@/shared/utils/trackCode'
export interface AuthUser {
id: number
tenant_id: number
phone: string
nickname: string
avatar_url: string
+8 -1
View File
@@ -12,6 +12,7 @@ const userKeys = {
refreshToken: 'refresh_token',
profile: [
'user_id',
'user_tenant_id',
'phone',
'nickname',
'avatar_url',
@@ -24,7 +25,13 @@ const userKeys = {
const adminKeys = {
accessToken: 'admin_access_token',
refreshToken: 'admin_refresh_token',
profile: ['admin_id', 'admin_username', 'admin_support_status', 'admin_password_must_change'],
profile: [
'admin_id',
'admin_tenant_id',
'admin_username',
'admin_support_status',
'admin_password_must_change',
],
}
function keysFor(scope: AuthScope) {
+15
View File
@@ -7,12 +7,14 @@ import {
type AdminUser,
} from '@/features/admin/api/adminAuth'
import { clearAuthStorage } from '@/shared/utils/authStorage'
import { useDistributionSiteStore } from '@/stores/distributionSite'
export const useAdminSessionStore = defineStore('adminSession', {
state: () => ({
token: '',
refreshToken: '',
adminId: Number(localStorage.getItem('admin_id') || 0),
tenantId: Number(localStorage.getItem('admin_tenant_id') || 0),
username: localStorage.getItem('admin_username') || '',
nickname: '',
supportStatus: (localStorage.getItem('admin_support_status') || 'offline') as
@@ -42,6 +44,11 @@ export const useAdminSessionStore = defineStore('adminSession', {
return result
},
async loadMe() {
const site = useDistributionSiteStore()
if (site.tenantId > 0 && this.tenantId > 0 && site.tenantId !== this.tenantId) {
this.logout()
throw new Error('admin tenant session mismatch')
}
const admin = await fetchAdminMe()
this.applyAdmin(admin)
return admin
@@ -50,6 +57,7 @@ export const useAdminSessionStore = defineStore('adminSession', {
this.token = ''
this.refreshToken = ''
this.adminId = 0
this.tenantId = 0
this.username = ''
this.nickname = ''
this.supportStatus = 'offline'
@@ -64,6 +72,7 @@ export const useAdminSessionStore = defineStore('adminSession', {
this.token = ''
this.refreshToken = ''
this.adminId = Number(localStorage.getItem('admin_id') || 0)
this.tenantId = Number(localStorage.getItem('admin_tenant_id') || 0)
this.username = localStorage.getItem('admin_username') || ''
this.supportStatus = (localStorage.getItem('admin_support_status') || 'offline') as
| 'online'
@@ -78,6 +87,7 @@ export const useAdminSessionStore = defineStore('adminSession', {
},
applyAdmin(admin: AdminUser) {
this.adminId = admin.id
this.tenantId = Number(admin.tenant_id || 0)
this.username = admin.username
this.nickname = admin.nickname
this.supportStatus = admin.support_status || 'offline'
@@ -85,6 +95,11 @@ export const useAdminSessionStore = defineStore('adminSession', {
this.roles = admin.roles || []
this.permissions = admin.permissions || []
localStorage.setItem('admin_id', String(admin.id))
if (this.tenantId > 0) {
localStorage.setItem('admin_tenant_id', String(this.tenantId))
} else {
localStorage.removeItem('admin_tenant_id')
}
localStorage.setItem('admin_username', admin.username)
localStorage.setItem('admin_support_status', admin.support_status || 'offline')
localStorage.setItem('admin_password_must_change', String(admin.password_must_change))
+20
View File
@@ -14,6 +14,7 @@ import {
setAuthTokens,
} from '@/shared/utils/authStorage'
import { loginWithPassword, registerWithPassword } from '@/features/auth/api/auth'
import { useDistributionSiteStore } from '@/stores/distributionSite'
function renterGrowthLevelName(level: string) {
const map: Record<string, string> = {
@@ -30,6 +31,7 @@ export const useSessionStore = defineStore('session', {
token: getAccessToken('user'),
refreshToken: getRefreshToken('user'),
userId: Number(localStorage.getItem('user_id') || 0),
tenantId: Number(localStorage.getItem('user_tenant_id') || 0),
phone: localStorage.getItem('phone') || '',
nickname: localStorage.getItem('nickname') || '',
avatarUrl: localStorage.getItem('avatar_url') || '',
@@ -65,6 +67,11 @@ export const useSessionStore = defineStore('session', {
return result
},
async loadMe() {
const site = useDistributionSiteStore()
if (site.tenantId > 0 && this.tenantId > 0 && site.tenantId !== this.tenantId) {
this.clearSession()
throw new Error('tenant session mismatch')
}
if (this._loadingMe) {
// 如果正在加载,等待完成
await new Promise(resolve => {
@@ -98,9 +105,15 @@ export const useSessionStore = defineStore('session', {
} catch {
// 本地退出不依赖网络成功,避免用户无法离开当前账号。
}
this.token = ''
this.refreshToken = ''
this.clearSession()
},
clearSession() {
this.token = ''
this.refreshToken = ''
this.userId = 0
this.tenantId = 0
this.phone = ''
this.nickname = ''
this.avatarUrl = ''
@@ -115,6 +128,7 @@ export const useSessionStore = defineStore('session', {
this.token = getAccessToken('user')
this.refreshToken = getRefreshToken('user')
this.userId = Number(localStorage.getItem('user_id') || 0)
this.tenantId = Number(localStorage.getItem('user_tenant_id') || 0)
this.phone = localStorage.getItem('phone') || ''
this.nickname = localStorage.getItem('nickname') || ''
this.avatarUrl = localStorage.getItem('avatar_url') || ''
@@ -133,11 +147,17 @@ export const useSessionStore = defineStore('session', {
refresh_token: refreshToken,
})
localStorage.setItem('user_id', String(user.id))
this.tenantId = Number(user.tenant_id || 0)
localStorage.setItem('user_tenant_id', String(this.tenantId))
this.applyUser(user)
},
applyUser(user: AuthUser) {
this.userId = user.id
localStorage.setItem('user_id', String(user.id))
if (user.tenant_id > 0) {
this.tenantId = Number(user.tenant_id)
localStorage.setItem('user_tenant_id', String(this.tenantId))
}
this.phone = user.phone
localStorage.setItem('phone', user.phone)
this.nickname = user.nickname