1
This commit is contained in:
@@ -1,11 +1,5 @@
|
||||
import { getBreadCrumbList, getHomeRoute } from '../utils/tools'
|
||||
|
||||
// 注意:这里的 paramSetupServer 需要在使用时注入
|
||||
let paramSetupServerInstance = null
|
||||
|
||||
export const setParamSetupServer = (server) => {
|
||||
paramSetupServerInstance = server
|
||||
}
|
||||
import paramSetupServer from '../api/system_high/paramSetupServer'
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
@@ -37,23 +31,20 @@ export default {
|
||||
logoUrl: defaultLogo
|
||||
}
|
||||
|
||||
if (!paramSetupServerInstance) {
|
||||
commit('setSysTitle', formModel)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
let res1 = await paramSetupServerInstance.getOne('sys_title')
|
||||
if (res1.data) {
|
||||
let res1 = await paramSetupServer.getOne('sys_title')
|
||||
if (res1 && res1.data) {
|
||||
formModel.title = res1.data.value
|
||||
document.title = res1.data.value
|
||||
}
|
||||
let res2 = await paramSetupServerInstance.getOne('sys_logo')
|
||||
if (res2.data) {
|
||||
let res2 = await paramSetupServer.getOne('sys_logo')
|
||||
if (res2 && res2.data) {
|
||||
formModel.logoUrl = res2.data.value
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to get sys title:', error)
|
||||
console.warn('获取系统标题失败,使用默认标题:', error || '接口调用失败')
|
||||
// 使用默认标题
|
||||
document.title = formModel.title
|
||||
}
|
||||
|
||||
commit('setSysTitle', formModel)
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import { setToken, getToken } from '../utils/tools'
|
||||
import uiTool from '../utils/uiTool'
|
||||
|
||||
// 注意:这里的 userServer 需要在使用时注入
|
||||
let userServerInstance = null
|
||||
|
||||
export const setUserServer = (server) => {
|
||||
userServerInstance = server
|
||||
}
|
||||
import { defaultMenus, filterMenusByIds } from '../config/menuConfig'
|
||||
import userServer from '../api/system/userServer'
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
@@ -48,43 +43,132 @@ export default {
|
||||
menuList: state => state.menuList
|
||||
},
|
||||
actions: {
|
||||
async setAuthorityMenus({ state, commit }, { Main, ParentView, Page404 }) {
|
||||
if (!userServerInstance) {
|
||||
console.error('userServer not initialized')
|
||||
return
|
||||
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('使用所有默认菜单')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let res = await userServerInstance.authorityMenus()
|
||||
let authorityMenus = res.data
|
||||
commit('setAuthorityMenus', JSON.stringify(authorityMenus))
|
||||
// 如果 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))
|
||||
|
||||
// 生成路由菜单
|
||||
let mainMenu = uiTool.getRoutes(Main, ParentView, Page404)
|
||||
commit('setMenuList', mainMenu.children)
|
||||
console.log('生成的主菜单:', mainMenu)
|
||||
|
||||
if (mainMenu && mainMenu.children) {
|
||||
commit('setMenuList', mainMenu.children)
|
||||
}
|
||||
},
|
||||
async handleLogin({ state, commit, dispatch }, { userFrom, Main, ParentView, Page404 }) {
|
||||
if (!userServerInstance) {
|
||||
throw new Error('userServer not initialized')
|
||||
}
|
||||
debugger
|
||||
let promise = new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
let res = await userServerInstance.login(userFrom)
|
||||
try {
|
||||
let res = await userServer.login(userFrom)
|
||||
console.log('登录接口返回:', res)
|
||||
|
||||
let token = res.data.token
|
||||
let name = res.data.user.name.trim()
|
||||
|
||||
|
||||
commit('setUserName', name)
|
||||
commit('setToken', token)
|
||||
|
||||
await dispatch('setAuthorityMenus', { Main, ParentView, Page404 })
|
||||
resolve(res)
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
// 检查返回数据结构
|
||||
// http.post 返回的是 response.data,即 { code, message, data }
|
||||
if (!res) {
|
||||
throw new Error('登录接口无返回数据')
|
||||
}
|
||||
})
|
||||
|
||||
return promise
|
||||
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
|
||||
})
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
async handleLogOut({ state, commit }, vue) {
|
||||
commit('setToken', '')
|
||||
|
||||
Reference in New Issue
Block a user