refactor(auth): 将认证状态迁移至独立登录 store
This commit is contained in:
@@ -1,60 +1,16 @@
|
||||
import { ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import type { WorkbenchCampus } from '@/api/workbench/types'
|
||||
import type { AuthenticatedUser, LoginResponse } from '@/types/auth'
|
||||
import { clearAuthStorage, readStoredAuthSession, saveAuthSession } from '@/utils/auth-storage'
|
||||
|
||||
const dataScopeLabels: Record<AuthenticatedUser['dataScope'], string> = {
|
||||
ALL: '全院',
|
||||
CAMPUS: '院区',
|
||||
DEPARTMENT: '科室',
|
||||
READ_ONLY_ALL: '全院只读',
|
||||
}
|
||||
export const useAppStore = defineStore('app', () => {
|
||||
const environment = ref('接口联调环境')
|
||||
const campuses = ref<WorkbenchCampus[]>(['本部院区', '东院区', '西院区'])
|
||||
const selectedCampus = ref<WorkbenchCampus>('本部院区')
|
||||
|
||||
function getUserContext(user: AuthenticatedUser | null) {
|
||||
return user ? `${dataScopeLabels[user.dataScope]}数据范围` : '医签通用户'
|
||||
}
|
||||
|
||||
export const useAppStore = defineStore('app', {
|
||||
state: () => {
|
||||
const session = readStoredAuthSession()
|
||||
const user = session?.user ?? null
|
||||
|
||||
return {
|
||||
isLoggedIn: Boolean(session?.token),
|
||||
token: session?.token ?? '',
|
||||
expiresAt: session?.expiresAt ?? '',
|
||||
user,
|
||||
userName: user?.displayName || user?.username || '医护用户',
|
||||
department: getUserContext(user),
|
||||
environment: '接口联调环境',
|
||||
campuses: ['本部院区', '东院区', '西院区'] as WorkbenchCampus[],
|
||||
selectedCampus: '本部院区' as WorkbenchCampus,
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
login(session: LoginResponse) {
|
||||
saveAuthSession(session)
|
||||
this.token = session.token
|
||||
this.expiresAt = session.expiresAt
|
||||
this.user = session.user
|
||||
this.userName = session.user.displayName || session.user.username || '医护用户'
|
||||
this.department = getUserContext(session.user)
|
||||
this.isLoggedIn = true
|
||||
},
|
||||
updateCurrentUser(user: AuthenticatedUser) {
|
||||
this.user = user
|
||||
this.userName = user.displayName || user.username || '医护用户'
|
||||
this.department = getUserContext(user)
|
||||
},
|
||||
logout() {
|
||||
clearAuthStorage()
|
||||
this.isLoggedIn = false
|
||||
this.token = ''
|
||||
this.expiresAt = ''
|
||||
this.user = null
|
||||
this.userName = '医护用户'
|
||||
this.department = getUserContext(null)
|
||||
},
|
||||
},
|
||||
return {
|
||||
environment,
|
||||
campuses,
|
||||
selectedCampus,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import { computed, ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import { getCurrentUser, login as loginRequest, logout as logoutRequest } from '@/api/auth'
|
||||
import type { AuthenticatedUser, LoginRequest, LoginResponse } from '@/types/auth'
|
||||
import { clearAuthStorage, readStoredAuthSession, saveAuthSession } from '@/utils/auth-storage'
|
||||
|
||||
const dataScopeLabels: Record<AuthenticatedUser['dataScope'], string> = {
|
||||
ALL: '全院',
|
||||
CAMPUS: '院区',
|
||||
DEPARTMENT: '科室',
|
||||
READ_ONLY_ALL: '全院只读',
|
||||
}
|
||||
|
||||
function getUserContext(user: AuthenticatedUser | null) {
|
||||
return user ? `${dataScopeLabels[user.dataScope]}数据范围` : '医签通用户'
|
||||
}
|
||||
|
||||
export const useLoginStore = defineStore('login', () => {
|
||||
const storedSession = readStoredAuthSession()
|
||||
const token = ref(storedSession?.token ?? '')
|
||||
const expiresAt = ref(storedSession?.expiresAt ?? '')
|
||||
const user = ref<AuthenticatedUser | null>(storedSession?.user ?? null)
|
||||
|
||||
const isLoggedIn = computed(() => Boolean(token.value))
|
||||
const userName = computed(() => user.value?.displayName || user.value?.username || '医护用户')
|
||||
const department = computed(() => getUserContext(user.value))
|
||||
|
||||
function setSession(session: LoginResponse) {
|
||||
saveAuthSession(session)
|
||||
token.value = session.token
|
||||
expiresAt.value = session.expiresAt
|
||||
user.value = session.user
|
||||
}
|
||||
|
||||
function setCurrentUser(nextUser: AuthenticatedUser) {
|
||||
user.value = nextUser
|
||||
|
||||
if (token.value) {
|
||||
saveAuthSession({
|
||||
token: token.value,
|
||||
expiresAt: expiresAt.value,
|
||||
user: nextUser,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function clearSession() {
|
||||
clearAuthStorage()
|
||||
token.value = ''
|
||||
expiresAt.value = ''
|
||||
user.value = null
|
||||
}
|
||||
|
||||
async function login(payload: LoginRequest) {
|
||||
const session = await loginRequest(payload)
|
||||
setSession(session)
|
||||
return session
|
||||
}
|
||||
|
||||
async function refreshCurrentUser() {
|
||||
const currentUser = await getCurrentUser()
|
||||
setCurrentUser(currentUser)
|
||||
return currentUser
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
try {
|
||||
await logoutRequest()
|
||||
} finally {
|
||||
clearSession()
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
expiresAt,
|
||||
user,
|
||||
isLoggedIn,
|
||||
userName,
|
||||
department,
|
||||
login,
|
||||
refreshCurrentUser,
|
||||
logout,
|
||||
clearSession,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user