2026-09-09 22:05:00 +08:00
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
import { ref, computed } from 'vue'
|
|
|
|
|
import { post } from '@/api/client'
|
|
|
|
|
|
|
|
|
|
interface OperatorInfo {
|
|
|
|
|
operatorId: string
|
|
|
|
|
name: string
|
|
|
|
|
username: string
|
|
|
|
|
permissions: string[]
|
|
|
|
|
organizationId: string | null
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 16:06:11 +08:00
|
|
|
const previewRolePermissions: Record<string, string[]> = {
|
|
|
|
|
reviewer: ['review.view', 'user.view'],
|
|
|
|
|
counselor: ['counseling.service.view', 'referral.manage'],
|
|
|
|
|
station: ['service_location.view', 'service_appointment.view'],
|
|
|
|
|
operator: ['review.view', 'user.view', 'organization.manage', 'permission.manage', 'activity.review_registration', 'risk.report.view', 'risk.report.process', 'analytics.view', 'banner.view', 'content.view', 'service_location.view', 'service_appointment.view', 'service_program.manage', 'counseling.service.view', 'referral.manage'],
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-09 22:05:00 +08:00
|
|
|
export const useAuthStore = defineStore('auth', () => {
|
2026-09-10 16:06:11 +08:00
|
|
|
const previewParams = import.meta.env.DEV ? new URLSearchParams(window.location.search) : null
|
|
|
|
|
const previewRole = previewParams?.get('preview') || ''
|
|
|
|
|
const previewOperator: OperatorInfo | null = previewRolePermissions[previewRole]
|
|
|
|
|
? { operatorId: 'ui-preview', name: '界面验收员', username: 'preview', permissions: previewRolePermissions[previewRole], organizationId: null }
|
|
|
|
|
: null
|
|
|
|
|
const token = ref<string>(previewOperator ? 'ui-preview-only' : (localStorage.getItem('accessToken') || ''))
|
2026-09-09 22:05:00 +08:00
|
|
|
const operator = ref<OperatorInfo | null>(
|
2026-09-10 16:06:11 +08:00
|
|
|
previewOperator || JSON.parse(localStorage.getItem('operator') || 'null')
|
2026-09-09 22:05:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const isLoggedIn = computed(() => !!token.value)
|
|
|
|
|
const permissions = computed(() => operator.value?.permissions || [])
|
|
|
|
|
|
|
|
|
|
async function login(username: string, password: string) {
|
|
|
|
|
const res: any = await post('/auth/operator/login', { username, password })
|
|
|
|
|
if (res.success) {
|
2026-09-10 10:58:43 +08:00
|
|
|
token.value = res.data.accessToken
|
2026-09-09 22:05:00 +08:00
|
|
|
operator.value = {
|
|
|
|
|
operatorId: res.data.operatorId,
|
|
|
|
|
name: res.data.name,
|
|
|
|
|
username: res.data.username,
|
|
|
|
|
permissions: res.data.permissions || [],
|
2026-09-10 10:58:43 +08:00
|
|
|
organizationId: res.data.organizationId ?? null,
|
2026-09-09 22:05:00 +08:00
|
|
|
}
|
2026-09-10 10:58:43 +08:00
|
|
|
localStorage.setItem('accessToken', res.data.accessToken)
|
2026-09-09 22:05:00 +08:00
|
|
|
localStorage.setItem('operator', JSON.stringify(operator.value))
|
|
|
|
|
return res.data
|
|
|
|
|
}
|
|
|
|
|
throw new Error(res.message || '登录失败')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function logout() {
|
|
|
|
|
token.value = ''
|
|
|
|
|
operator.value = null
|
|
|
|
|
localStorage.removeItem('accessToken')
|
|
|
|
|
localStorage.removeItem('operator')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { token, operator, isLoggedIn, permissions, login, logout }
|
|
|
|
|
})
|