| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import { defineStore } from 'pinia'
- import { ref, computed } from 'vue'
- import type { User, UserRole } from '../types'
- import { login, logout } from '../api/auth'
- import type { LoginMenu } from '../api/auth'
- import { getTeamSimpleList } from '../api/team'
- export const useAuthStore = defineStore('auth', () => {
- // State
- const token = ref<string>('')
- const user = ref<User | null>(null)
- const roleInfo = ref<{
- roleId?: number
- roleName?: string
- roleKey?: string
- homePath?: string
- menus?: LoginMenu[]
- permissions?: string[]
- endpoints?: string[]
- } | null>(null)
- // Getters
- const isLoggedIn = computed(() => !!token.value)
- const userName = computed(() => user.value?.name || '')
- const userRole = computed(() => user.value?.role || '')
- const userInfo = computed(() => user.value)
- const homePath = computed(() => roleInfo.value?.homePath || '')
- const userEndpoints = computed(() => roleInfo.value?.endpoints || [])
- // 由账号绑定的可登录端点推导所属业务端
- const endpointToRole = (endpoints?: string[]): UserRole | null => {
- const map: Record<string, UserRole> = {
- 'mp-sales': 'sales',
- 'mp-dispatch': 'dispatch',
- 'mp-construction': 'construction',
- }
- for (const code of endpoints || []) {
- if (map[code]) return map[code]
- }
- return null
- }
- // Actions
- async function doLogin(
- username: string,
- password: string,
- selectedRole?: 'mp-sales' | 'mp-dispatch' | 'mp-construction'
- ) {
- const res = await login({ username, password, roleType: selectedRole })
- let teamName = ''
- try {
- const teams = await getTeamSimpleList()
- const team = teams.find((t) => t.value === res.teamId)
- teamName = team?.label || ''
- } catch {
- teamName = ''
- }
- const finalRole = endpointToRole(res.endpoints)
- if (!finalRole) {
- uni.showToast({ title: '当前账号未配置可登录端点,无法登录', icon: 'none' })
- throw new Error('当前账号未配置可登录端点')
- }
- const userData: User = {
- id: String(res.userId),
- name: res.nickName || res.userName,
- phone: res.phonenumber || res.phone || '',
- avatar: res.avatar,
- role: finalRole,
- department: res.deptName || res.dept?.deptName || '',
- deptId: res.deptId || res.dept?.deptId,
- employeeNo: res.staffCode || res.employeeNo || '',
- email: res.email || '',
- hireDate: res.entryDate || res.hireDate || '',
- team: teamName || res.teamName || res.team || '',
- teamId: res.teamId,
- postNames: res.postNames || '',
- dutyStatus: res.dutyStatus || '',
- skillLevel: res.skillLevel || '',
- certifications: res.certifications || '',
- tags: res.tags || '',
- skillTags: res.skillTags || '',
- currentTaskId: res.currentTaskId,
- status: 'active',
- }
- const roleData = {
- roleId: res.roleId,
- roleName: res.roleName,
- roleKey: res.roleKey,
- homePath: res.homePath,
- menus: res.menus,
- permissions: res.permissions,
- endpoints: res.endpoints,
- }
- user.value = userData
- roleInfo.value = roleData
- token.value = res.token
- // 保存到本地存储
- uni.setStorageSync('token', res.token)
- uni.setStorageSync('user', JSON.stringify(userData))
- uni.setStorageSync('roleInfo', JSON.stringify(roleData))
- return finalRole
- }
- async function doLogout() {
- try {
- await logout()
- } catch {
- // 忽略登出失败
- }
- token.value = ''
- user.value = null
- roleInfo.value = null
- uni.removeStorageSync('token')
- uni.removeStorageSync('user')
- uni.removeStorageSync('roleInfo')
- }
- // 兼容旧代码的别名
- const logout = doLogout
- function checkLogin() {
- const savedToken = uni.getStorageSync('token')
- const savedUser = uni.getStorageSync('user')
- const savedRoleInfo = uni.getStorageSync('roleInfo')
- if (savedToken && savedUser) {
- token.value = savedToken
- try {
- user.value = JSON.parse(savedUser)
- } catch {
- user.value = null
- }
- }
- if (savedRoleInfo) {
- try {
- roleInfo.value = JSON.parse(savedRoleInfo)
- } catch {
- roleInfo.value = null
- }
- }
- }
- return {
- token,
- user,
- roleInfo,
- isLoggedIn,
- userName,
- userRole,
- userInfo,
- homePath,
- userEndpoints,
- doLogin,
- doLogout,
- logout,
- checkLogin,
- }
- })
|