This commit is contained in:
zzc
2025-03-28 18:28:06 +08:00
commit 939c43f281
206 changed files with 30419 additions and 0 deletions

22
src/store/index.js Normal file
View File

@@ -0,0 +1,22 @@
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 导入所有 vuex 模块自动加入namespaced:true用于解决vuex命名冲突请勿修改。
*/
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const files = require.context('./modules', false, /\.js$/)
const modules = {}
files.keys().forEach((key) => {
modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
})
Object.keys(modules).forEach((key) => {
modules[key]['namespaced'] = true
})
const store = new Vuex.Store({
modules,
})
export default store

View File

@@ -0,0 +1,28 @@
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 异常捕获的状态拦截,请勿修改
*/
const state = () => ({
errorLogs: [],
})
const getters = {
errorLogs: (state) => state.errorLogs,
}
const mutations = {
addErrorLog(state, errorLog) {
state.errorLogs.push(errorLog)
},
clearErrorLog: (state) => {
state.errorLogs.splice(0)
},
}
const actions = {
addErrorLog({ commit }, errorLog) {
commit('addErrorLog', errorLog)
},
clearErrorLog({ commit }) {
commit('clearErrorLog')
},
}
export default { state, getters, mutations, actions }

View File

@@ -0,0 +1,47 @@
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 路由拦截状态管理目前两种模式all模式与intelligence模式其中partialRoutes是菜单暂未使用
*/
import { asyncRoutes, constantRoutes } from '@/router'
import { getRouterList } from '@/api/router'
import { convertRouter, filterAsyncRoutes } from '@/utils/handleRoutes'
const state = () => ({
routes: [],
partialRoutes: [],
})
const getters = {
routes: (state) => state.routes,
partialRoutes: (state) => state.partialRoutes,
}
const mutations = {
setRoutes(state, routes) {
state.routes = constantRoutes.concat(routes)
},
setAllRoutes(state, routes) {
state.routes = constantRoutes.concat(routes)
},
setPartialRoutes(state, routes) {
state.partialRoutes = constantRoutes.concat(routes)
},
}
const actions = {
async setRoutes({ commit }, permissions) {
//开源版只过滤动态路由permissionsadmin不再默认拥有全部权限
const finallyAsyncRoutes = await filterAsyncRoutes([...asyncRoutes], permissions)
commit('setRoutes', finallyAsyncRoutes)
return finallyAsyncRoutes
},
async setAllRoutes({ commit }) {
let { data } = await getRouterList()
data.push({ path: '*', redirect: '/404', hidden: true })
let accessRoutes = convertRouter(data)
commit('setAllRoutes', accessRoutes)
return accessRoutes
},
setPartialRoutes({ commit }, accessRoutes) {
commit('setPartialRoutes', accessRoutes)
return accessRoutes
},
}
export default { state, getters, mutations, actions }

View File

@@ -0,0 +1,88 @@
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 所有全局配置的状态管理,如无必要请勿修改
*/
import defaultSettings from '@/config'
const { tabsBar, logo, layout, header, themeBar, title } = defaultSettings
const theme = JSON.parse(localStorage.getItem('vue-admin-better-theme')) || ''
const state = () => ({
tabsBar: theme.tabsBar || tabsBar,
logo,
collapse: false,
layout: theme.layout || layout,
header: theme.header || header,
device: 'desktop',
themeBar,
title,
})
const getters = {
collapse: (state) => state.collapse,
device: (state) => state.device,
header: (state) => state.header,
layout: (state) => state.layout,
logo: (state) => state.logo,
tabsBar: (state) => state.tabsBar,
themeBar: (state) => state.themeBar,
title: (state) => state.title,
}
const mutations = {
changeLayout: (state, layout) => {
if (layout) state.layout = layout
},
changeHeader: (state, header) => {
if (header) state.header = header
},
changeTabsBar: (state, tabsBar) => {
if (tabsBar) state.tabsBar = tabsBar
},
changeCollapse: (state) => {
state.collapse = !state.collapse
},
foldSideBar: (state) => {
state.collapse = true
},
openSideBar: (state) => {
state.collapse = false
},
toggleDevice: (state, device) => {
state.device = device
},
changeLogo: (state, logo) => {
state.logo = logo
},
changeTitle: (state, title) => {
state.title = title
},
}
const actions = {
changeLayout({ commit }, layout) {
commit('changeLayout', layout)
},
changeHeader({ commit }, header) {
commit('changeHeader', header)
},
changeTabsBar({ commit }, tabsBar) {
commit('changeTabsBar', tabsBar)
},
changeCollapse({ commit }) {
commit('changeCollapse')
},
foldSideBar({ commit }) {
commit('foldSideBar')
},
openSideBar({ commit }) {
commit('openSideBar')
},
toggleDevice({ commit }, device) {
commit('toggleDevice', device)
},
changeLogo({ commit }, logo) {
commit('changeLogo', logo)
},
changeTitle({ commit }, title) {
commit('changeTitle', title)
},
}
export default { state, getters, mutations, actions }

View File

@@ -0,0 +1,23 @@
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 代码生成机状态管理
*/
const state = () => ({
srcCode: '',
})
const getters = {
srcTableCode: (state) => state.srcCode,
}
const mutations = {
setTableCode(state, srcCode) {
state.srcCode = srcCode
},
}
const actions = {
setTableCode({ commit }, srcCode) {
commit('setTableCode', srcCode)
},
}
export default { state, getters, mutations, actions }

View File

@@ -0,0 +1,110 @@
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description tabsBar多标签页逻辑前期借鉴了很多开源项目发现都有个共同的特点很繁琐并不符合框架设计的初衷后来在github用户hipi的启发下完成了重构请勿修改
*/
const state = () => ({
visitedRoutes: [],
})
const getters = {
visitedRoutes: (state) => state.visitedRoutes,
}
const mutations = {
addVisitedRoute(state, route) {
let target = state.visitedRoutes.find((item) => item.path === route.path)
if (target) {
if (route.fullPath !== target.fullPath) Object.assign(target, route)
return
}
state.visitedRoutes.push(Object.assign({}, route))
},
delVisitedRoute(state, route) {
state.visitedRoutes.forEach((item, index) => {
if (item.path === route.path) state.visitedRoutes.splice(index, 1)
})
},
delOthersVisitedRoute(state, route) {
state.visitedRoutes = state.visitedRoutes.filter((item) => item.meta.affix || item.path === route.path)
},
delLeftVisitedRoute(state, route) {
let index = state.visitedRoutes.length
state.visitedRoutes = state.visitedRoutes.filter((item) => {
if (item.name === route.name) index = state.visitedRoutes.indexOf(item)
return item.meta.affix || index <= state.visitedRoutes.indexOf(item)
})
},
delRightVisitedRoute(state, route) {
let index = state.visitedRoutes.length
state.visitedRoutes = state.visitedRoutes.filter((item) => {
if (item.name === route.name) index = state.visitedRoutes.indexOf(item)
return item.meta.affix || index >= state.visitedRoutes.indexOf(item)
})
},
delAllVisitedRoutes(state) {
state.visitedRoutes = state.visitedRoutes.filter((item) => item.meta.affix)
},
updateVisitedRoute(state, route) {
state.visitedRoutes.forEach((item) => {
if (item.path === route.path) item = Object.assign(item, route)
})
},
}
const actions = {
addVisitedRoute({ commit }, route) {
commit('addVisitedRoute', route)
},
async delRoute({ dispatch, state }, route) {
await dispatch('delVisitedRoute', route)
return {
visitedRoutes: [...state.visitedRoutes],
}
},
delVisitedRoute({ commit, state }, route) {
commit('delVisitedRoute', route)
return [...state.visitedRoutes]
},
async delOthersRoutes({ dispatch, state }, route) {
await dispatch('delOthersVisitedRoute', route)
return {
visitedRoutes: [...state.visitedRoutes],
}
},
async delLeftRoutes({ dispatch, state }, route) {
await dispatch('delLeftVisitedRoute', route)
return {
visitedRoutes: [...state.visitedRoutes],
}
},
async delRightRoutes({ dispatch, state }, route) {
await dispatch('delRightVisitedRoute', route)
return {
visitedRoutes: [...state.visitedRoutes],
}
},
delOthersVisitedRoute({ commit, state }, route) {
commit('delOthersVisitedRoute', route)
return [...state.visitedRoutes]
},
delLeftVisitedRoute({ commit, state }, route) {
commit('delLeftVisitedRoute', route)
return [...state.visitedRoutes]
},
delRightVisitedRoute({ commit, state }, route) {
commit('delRightVisitedRoute', route)
return [...state.visitedRoutes]
},
async delAllRoutes({ dispatch, state }, route) {
await dispatch('delAllVisitedRoutes', route)
return {
visitedRoutes: [...state.visitedRoutes],
}
},
delAllVisitedRoutes({ commit, state }) {
commit('delAllVisitedRoutes')
return [...state.visitedRoutes]
},
updateVisitedRoute({ commit }, route) {
commit('updateVisitedRoute', route)
},
}
export default { state, getters, mutations, actions }

90
src/store/modules/user.js Normal file
View File

@@ -0,0 +1,90 @@
/**
* @author https://github.com/zxwk1998/vue-admin-better 不想保留author可删除
* @description 登录、获取用户信息、退出登录、清除accessToken逻辑不建议修改
*/
import Vue from 'vue'
import { getUserInfo, login, logout } from '@/api/user'
import { getAccessToken, removeAccessToken, setAccessToken } from '@/utils/accessToken'
import { resetRouter } from '@/router'
import { title, tokenName } from '@/config'
const state = () => ({
accessToken: getAccessToken(),
username: '',
avatar: '',
permissions: [],
})
const getters = {
accessToken: (state) => state.accessToken,
username: (state) => state.username,
avatar: (state) => state.avatar,
permissions: (state) => state.permissions,
}
const mutations = {
setAccessToken(state, accessToken) {
state.accessToken = accessToken
setAccessToken(accessToken)
},
setUsername(state, username) {
state.username = username
},
setAvatar(state, avatar) {
state.avatar = avatar
},
setPermissions(state, permissions) {
state.permissions = permissions
},
}
const actions = {
setPermissions({ commit }, permissions) {
commit('setPermissions', permissions)
},
async login({ commit }, userInfo) {
const { data } = await login(userInfo)
if (data?.code) {
Vue.prototype.$baseMessage(data?.msg, 'error')
return
}
const accessToken = data[tokenName]
if (accessToken) {
commit('setAccessToken', accessToken)
const hour = new Date().getHours()
const thisTime = hour < 8 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 18 ? '下午好' : '晚上好'
Vue.prototype.$baseNotify(`欢迎登录${title}`, `${thisTime}`)
} else {
Vue.prototype.$baseMessage(`登录接口异常,未正确返回${tokenName}...`, 'error')
}
},
async getUserInfo({ commit, state, dispatch }) {
const { data } = await getUserInfo(state.accessToken)
if (!data) {
Vue.prototype.$baseMessage('验证失败,请重新登录...', 'error')
return false
}
let { permissions, username, avatar, appIcon, appName } = data
if (permissions && username && Array.isArray(permissions)) {
commit('setPermissions', permissions)
commit('setUsername', username)
commit('setAvatar', avatar)
dispatch('settings/changeLogo', appIcon, { root: true })
dispatch('settings/changeTitle', appName, { root: true })
return permissions
} else {
Vue.prototype.$baseMessage('用户信息接口异常', 'error')
return false
}
},
async logout({ dispatch }) {
await logout(state.accessToken)
await dispatch('resetAccessToken')
await resetRouter()
},
resetAccessToken({ commit }) {
commit('setPermissions', [])
commit('setAccessToken', '')
removeAccessToken()
},
}
export default { state, getters, mutations, actions }