feat(auth): 接入 MEDISIGN 登录接口
- 新增认证 API、类型与本地会话存储 - 登录页改为真实账号密码登录并移除验证码 - 请求拦截器改用 X-Token 并处理 401 跳转 - 添加 Vite /api 开发代理配置
This commit is contained in:
@@ -1,23 +1,60 @@
|
||||
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: '全院只读',
|
||||
}
|
||||
|
||||
function getUserContext(user: AuthenticatedUser | null) {
|
||||
return user ? `${dataScopeLabels[user.dataScope]}数据范围` : '医签通用户'
|
||||
}
|
||||
|
||||
export const useAppStore = defineStore('app', {
|
||||
state: () => ({
|
||||
isLoggedIn: false,
|
||||
userName: '张文静',
|
||||
department: '医务管理 · 主任医师',
|
||||
environment: '原型演示环境',
|
||||
campuses: ['本部院区', '东院区', '西院区'] as WorkbenchCampus[],
|
||||
selectedCampus: '本部院区' as WorkbenchCampus,
|
||||
}),
|
||||
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(userName: string) {
|
||||
this.userName = userName || '张文静'
|
||||
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)
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user