205 lines
6.3 KiB
JavaScript
205 lines
6.3 KiB
JavaScript
import { setToken, getToken } from '../utils/tools'
|
||
import uiTool from '../utils/uiTool'
|
||
import { defaultMenus, filterMenusByIds } from '../config/menuConfig'
|
||
import userServer from '../api/system/userServer'
|
||
|
||
export default {
|
||
namespaced: true,
|
||
state: {
|
||
userName: '',
|
||
avatorImgPath: '',
|
||
token: getToken(),
|
||
authorityMenus: [],
|
||
menuList: localStorage.getItem('menuList') ? JSON.parse(localStorage.getItem('menuList')) : []
|
||
},
|
||
mutations: {
|
||
setAvator(state, avatorPath) {
|
||
state.avatorImgPath = avatorPath
|
||
},
|
||
setUserName(state, userName) {
|
||
state.userName = userName
|
||
localStorage.userName = state.userName
|
||
},
|
||
setToken(state, token) {
|
||
state.token = token
|
||
setToken(token)
|
||
},
|
||
setAuthorityMenus(state, menus) {
|
||
state.authorityMenus = menus
|
||
localStorage.authorityMenus = menus
|
||
},
|
||
setMenuList(state, menus) {
|
||
state.menuList = menus
|
||
localStorage.setItem('menuList', JSON.stringify(menus))
|
||
}
|
||
},
|
||
getters: {
|
||
avatorImgPath: state => state.avatorImgPath,
|
||
userName(state) {
|
||
if (!state.userName) {
|
||
state.userName = localStorage.userName
|
||
}
|
||
return state.userName
|
||
},
|
||
menuList: state => state.menuList
|
||
},
|
||
actions: {
|
||
async setAuthorityMenus({ state, commit }, { Main, ParentView, Page404, authorityMenus, menuIds }) {
|
||
// 如果传入了 authorityMenus,直接使用;否则从接口获取
|
||
let menus = authorityMenus
|
||
|
||
if (!menus) {
|
||
try {
|
||
let res = await userServer.authorityMenus()
|
||
console.log('获取权限菜单返回:', res)
|
||
|
||
// res 的结构是 { code, message, data }
|
||
if (res && res.code === 0 && res.data) {
|
||
menus = res.data
|
||
}
|
||
} catch (error) {
|
||
console.error('获取权限菜单失败:', error)
|
||
console.warn('将使用默认菜单配置')
|
||
|
||
// 如果接口失败,使用默认菜单配置
|
||
// 如果有 menuIds,根据 ID 过滤菜单;否则使用所有默认菜单
|
||
if (menuIds && menuIds.length > 0) {
|
||
menus = filterMenusByIds(menuIds, defaultMenus)
|
||
console.log('根据菜单 IDs 过滤后的菜单:', menus)
|
||
} else {
|
||
menus = defaultMenus
|
||
console.log('使用所有默认菜单')
|
||
}
|
||
}
|
||
}
|
||
|
||
// 如果 menus 是字符串,先解析
|
||
if (typeof menus === 'string') {
|
||
try {
|
||
menus = JSON.parse(menus)
|
||
} catch (e) {
|
||
console.error('解析权限菜单失败:', e)
|
||
menus = defaultMenus
|
||
}
|
||
}
|
||
|
||
// 如果 menus 仍然为空或不是数组,使用默认菜单
|
||
if (!menus || !Array.isArray(menus)) {
|
||
console.warn('菜单数据无效,使用默认菜单')
|
||
menus = defaultMenus
|
||
}
|
||
|
||
console.log('最终处理的权限菜单:', menus)
|
||
|
||
// 保存权限菜单
|
||
commit('setAuthorityMenus', JSON.stringify(menus))
|
||
|
||
// 生成路由菜单(传递 HomePage 组件)
|
||
// 从框架导出的组件中获取 HomePage
|
||
const HomePage = window.framework && window.framework.HomePage ? window.framework.HomePage : null
|
||
let mainMenu = uiTool.getRoutes(Main, ParentView, Page404, HomePage)
|
||
console.log('生成的主菜单:', mainMenu)
|
||
|
||
if (mainMenu && mainMenu.children) {
|
||
commit('setMenuList', mainMenu.children)
|
||
|
||
// 动态添加路由(重要!解决登录后点击菜单空白的问题)
|
||
if (window.rootVue && window.rootVue.$router) {
|
||
const router = window.rootVue.$router
|
||
const routes = router.options.routes
|
||
|
||
// 查找并移除旧的主路由
|
||
const mainRouteIndex = routes.findIndex(r => r.path === '/')
|
||
if (mainRouteIndex !== -1) {
|
||
routes.splice(mainRouteIndex, 1)
|
||
}
|
||
|
||
// 添加新的主路由
|
||
router.addRoute(mainMenu)
|
||
console.log('动态路由已添加,redirect 设置为:', mainMenu.redirect)
|
||
}
|
||
}
|
||
},
|
||
async handleLogin({ state, commit, dispatch }, { userFrom, Main, ParentView, Page404 }) {
|
||
try {
|
||
let res = await userServer.login(userFrom)
|
||
console.log('登录接口返回:', res)
|
||
|
||
// 检查返回数据结构
|
||
// http.post 返回的是 response.data,即 { code, message, data }
|
||
if (!res) {
|
||
throw new Error('登录接口无返回数据')
|
||
}
|
||
|
||
if (res.code !== 0) {
|
||
throw new Error(res.message || '登录失败')
|
||
}
|
||
|
||
if (!res.data) {
|
||
throw new Error('登录接口返回数据格式错误')
|
||
}
|
||
|
||
// 实际数据在 res.data 中
|
||
let token = res.data.token
|
||
let user = res.data.user
|
||
let authorityMenusIds = res.data.authorityMenus
|
||
|
||
if (!token) {
|
||
throw new Error('未获取到 token')
|
||
}
|
||
|
||
if (!user || !user.name) {
|
||
throw new Error('未获取到用户信息')
|
||
}
|
||
|
||
let name = user.name.trim()
|
||
|
||
commit('setUserName', name)
|
||
commit('setToken', token)
|
||
|
||
// 登录接口返回的 authorityMenus 是菜单 ID 数组字符串
|
||
console.log('登录返回的菜单 IDs:', authorityMenusIds)
|
||
|
||
// 解析菜单 IDs
|
||
let menuIds = []
|
||
if (authorityMenusIds) {
|
||
try {
|
||
if (typeof authorityMenusIds === 'string') {
|
||
menuIds = JSON.parse(authorityMenusIds)
|
||
} else if (Array.isArray(authorityMenusIds)) {
|
||
menuIds = authorityMenusIds
|
||
}
|
||
} catch (e) {
|
||
console.error('解析菜单 IDs 失败:', e)
|
||
}
|
||
}
|
||
|
||
// 调用 authorityMenus 接口获取完整菜单数据
|
||
// 如果接口失败,会使用默认菜单配置和 menuIds 进行过滤
|
||
await dispatch('setAuthorityMenus', {
|
||
Main,
|
||
ParentView,
|
||
Page404,
|
||
menuIds
|
||
})
|
||
|
||
// 登录成功后获取系统标题
|
||
await dispatch('app/getSysTitle', {}, { root: true })
|
||
|
||
return res
|
||
} catch (error) {
|
||
console.error('登录失败:', error)
|
||
throw error
|
||
}
|
||
},
|
||
async handleLogOut({ state, commit }, vue) {
|
||
commit('setToken', '')
|
||
commit('setAuthorityMenus', '[]')
|
||
commit('setMenuList', [])
|
||
localStorage.removeItem('menuList')
|
||
window.location.reload()
|
||
}
|
||
}
|
||
}
|
||
|