|
|
@@ -1,34 +1,49 @@
|
|
|
import { defineStore } from 'pinia'
|
|
|
import { ref, computed } from 'vue'
|
|
|
-import type { User } from '../types'
|
|
|
-import { login, logout } from '../api/auth'
|
|
|
+import type { User, UserRole } from '../types'
|
|
|
+import { login, logout, type LoginMenu } from '../api/auth'
|
|
|
|
|
|
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 || [])
|
|
|
|
|
|
- // Actions
|
|
|
- async function doLogin(username: string, password: string, selectedRole?: 'sales' | 'dispatch' | 'construction') {
|
|
|
- const res = await login({ username, password })
|
|
|
-
|
|
|
- // 后端 roleType: front/dispatch/construction
|
|
|
- // 前端 role: sales/dispatch/construction
|
|
|
- const roleMap: Record<string, 'sales' | 'dispatch' | 'construction'> = {
|
|
|
+ // 后端角色标识映射到前端业务端
|
|
|
+ const backendRoleToFrontend = (roleKey?: string): UserRole => {
|
|
|
+ const map: Record<string, UserRole> = {
|
|
|
front: 'sales',
|
|
|
dispatch: 'dispatch',
|
|
|
construction: 'construction',
|
|
|
}
|
|
|
+ return map[roleKey || ''] || 'sales'
|
|
|
+ }
|
|
|
+
|
|
|
+ // Actions
|
|
|
+ async function doLogin(
|
|
|
+ username: string,
|
|
|
+ password: string,
|
|
|
+ selectedRole?: 'mp-sales' | 'mp-dispatch' | 'mp-construction'
|
|
|
+ ) {
|
|
|
+ const res = await login({ username, password, roleType: selectedRole })
|
|
|
|
|
|
- // 优先使用后端返回的角色
|
|
|
- const backendRole = roleMap[res.roleType]
|
|
|
- const finalRole = backendRole || selectedRole || 'sales'
|
|
|
+ const finalRole = backendRoleToFrontend(res.roleKey)
|
|
|
|
|
|
const userData: User = {
|
|
|
id: String(res.userId),
|
|
|
@@ -45,14 +60,26 @@ export const useAuthStore = defineStore('auth', () => {
|
|
|
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 userData.role
|
|
|
+ return finalRole
|
|
|
}
|
|
|
|
|
|
async function doLogout() {
|
|
|
@@ -63,8 +90,10 @@ export const useAuthStore = defineStore('auth', () => {
|
|
|
}
|
|
|
token.value = ''
|
|
|
user.value = null
|
|
|
+ roleInfo.value = null
|
|
|
uni.removeStorageSync('token')
|
|
|
uni.removeStorageSync('user')
|
|
|
+ uni.removeStorageSync('roleInfo')
|
|
|
}
|
|
|
|
|
|
// 兼容旧代码的别名
|
|
|
@@ -73,19 +102,34 @@ export const useAuthStore = defineStore('auth', () => {
|
|
|
function checkLogin() {
|
|
|
const savedToken = uni.getStorageSync('token')
|
|
|
const savedUser = uni.getStorageSync('user')
|
|
|
+ const savedRoleInfo = uni.getStorageSync('roleInfo')
|
|
|
if (savedToken && savedUser) {
|
|
|
token.value = savedToken
|
|
|
- user.value = JSON.parse(savedUser)
|
|
|
+ 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,
|