This commit is contained in:
2023-01-03 09:33:30 +08:00
commit bd4d0e897b
190 changed files with 48628 additions and 0 deletions

12
src/store/getters.js Normal file
View File

@@ -0,0 +1,12 @@
const getters = {
topModuleList: (state) => state.menu.topModuleList,
leftMenuList: (state) => state.menu.leftMenuList,
activeModule: (state) => state.menu.activeModule,
activeMenu: (state) => state.menu.activeMenu,
token: (state) => state.user.token,
visitedViews: (state) => state.tagsView.visitedViews,
cachedViews: (state) => state.tagsView.cachedViews,
username: (state) => state.user.username,
roles: (state) => state.user.roles
}
export default getters

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

@@ -0,0 +1,24 @@
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
Vue.use(Vuex)
const modulesFiles = require.context('./modules', true, /\.js$/)
// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
// set './app.js' => 'app'
const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
const value = modulesFiles(modulePath)
modules[moduleName] = value.default
return modules
}, {})
const store = new Vuex.Store({
modules,
getters
})
export default store

80
src/store/modules/menu.js Normal file
View File

@@ -0,0 +1,80 @@
import { routes } from '@/router'
const state = {
topModuleList:
sessionStorage.getItem('topModuleList') &&
sessionStorage.getItem('topModuleList') !== 'undefined'
? JSON.parse(sessionStorage.getItem('topModuleList'))
: [],
leftMenuList:
sessionStorage.getItem('leftMenuList') &&
sessionStorage.getItem('leftMenuList') !== 'undefined'
? JSON.parse(sessionStorage.getItem('leftMenuList'))
: [],
activeModule: sessionStorage.getItem('activeModule'), // 顶部菜单模块
activeMenu: sessionStorage.getItem('activeMenu') // 左侧菜单
}
const mutations = {
SET_TOPMODULE: (state, menuModule) => {
state.topModuleList = menuModule
sessionStorage.setItem('topModuleList', JSON.stringify(menuModule))
},
SET_LEFTMENU: (state, menu) => {
state.leftMenuList = menu
sessionStorage.setItem('leftMenuList', JSON.stringify(menu))
},
SET_ACTIVEMODULE: (state, activeModule) => {
state.activeModule = activeModule
sessionStorage.setItem('activeModule', activeModule)
},
SET_ACTIVEMENU: (state, activeMenu) => {
state.activeMenu =
activeMenu || (state.leftMenuList[0] ? state.leftMenuList[0].name : '')
sessionStorage.setItem('activeMenu', state.activeMenu)
}
}
const actions = {
// 获取顶部菜单模块
getTopModuleList({ commit }) {
let temp = []
routes &&
routes.map((item) => {
if (item.hidden) {
return true
}
let obj = {}
obj.name = item.name
obj.label = item.meta.title
temp.push(obj)
})
commit('SET_TOPMODULE', temp)
},
// 设置菜单模块
setActiveModule({ commit, dispatch }, activeBox) {
const { activeModule } = activeBox
dispatch('getLeftMenuList', activeBox)
commit('SET_ACTIVEMODULE', activeModule)
},
// 获取左侧菜单
getLeftMenuList({ commit, dispatch }, activeBox) {
const { activeModule, activeMenu } = activeBox
let temp = []
for (let i = 0; i < routes.length; i++) {
if (activeModule === routes[i].name) {
temp = routes[i].children
}
}
commit('SET_LEFTMENU', temp)
dispatch('setActiveMenu', activeMenu)
},
setActiveMenu({ commit }, activeMenu) {
commit('SET_ACTIVEMENU', activeMenu)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}

View File

@@ -0,0 +1,76 @@
const state = {
visitedViews: [],
cachedViews: []
}
const mutations = {
ADD_VISITED_VIEW: (state, view) => {
if (state.visitedViews.some((v) => v.path === view.path)) return
state.visitedViews.push(
Object.assign({}, view, {
title: view.meta.title || 'no-name'
})
)
},
ADD_CACHED_VIEW: (state, view) => {
if (state.cachedViews.includes(view.name)) return
if (!view.meta.noCache) {
state.cachedViews.push(view.name)
}
},
DEL_VISITED_VIEW: (state, view) => {
for (const [i, v] of state.visitedViews.entries()) {
if (v.path === view.path) {
state.visitedViews.splice(i, 1)
break
}
}
},
DEL_CACHED_VIEW: (state, view) => {
const index = state.cachedViews.indexOf(view.name)
index > -1 && state.cachedViews.splice(index, 1)
}
}
const actions = {
addView({ dispatch }, view) {
dispatch('addVisitedView', view)
dispatch('addCachedView', view)
},
addVisitedView({ commit }, view) {
commit('ADD_VISITED_VIEW', view)
},
addCachedView({ commit }, view) {
commit('ADD_CACHED_VIEW', view)
},
delView({ dispatch, state }, view) {
return new Promise((resolve) => {
dispatch('delVisitedView', view)
dispatch('delCachedView', view)
resolve({
visitedViews: [...state.visitedViews],
cachedViews: [...state.cachedViews]
})
})
},
delVisitedView({ commit, state }, view) {
return new Promise((resolve) => {
commit('DEL_VISITED_VIEW', view)
resolve([...state.visitedViews])
})
},
delCachedView({ commit, state }, view) {
return new Promise((resolve) => {
commit('DEL_CACHED_VIEW', view)
resolve([...state.cachedViews])
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}

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

@@ -0,0 +1,121 @@
import { login, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import Cookies from 'js-cookie'
const state = {
token: getToken(),
name: '',
avatar: '',
introduction: '',
roles: JSON.parse(localStorage.getItem('roles')),
menus: [],
username: Cookies.get('username') || ''
}
const mutations = {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_USERNAME: (state, username) => {
state.username = username
},
SET_INTRODUCTION: (state, introduction) => {
state.introduction = introduction
},
SET_NAME: (state, name) => {
state.name = name
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
},
SET_MENUS: (state, menus) => {
state.menus = menus
},
SET_ROLES: (state, roles) => {
state.roles = roles
}
}
const actions = {
// user login
login({ commit }, userInfo) {
const { mobile, password } = userInfo
return new Promise((resolve, reject) => {
login({ mobile: mobile.trim(), password: password })
.then((res) => {
console.log(res)
if (res.code === 0) {
const { token, name } = res.data
commit('SET_TOKEN', token)
commit('SET_USERNAME', name)
Cookies.set('username', name)
setToken(token)
resolve()
} else {
reject(res)
}
})
.catch((error) => {
reject(error)
})
})
},
// get user info
getInfo({ commit }) {
return new Promise((resolve, reject) => {
getUserInfo()
.then((res) => {
console.log(res)
const { data } = res
if (!data) {
reject('获取用户信息失败,重新登录')
}
// const { roleName, name, avatar, introduction, menus } = data
const { role } = data
// // roles must be a non-empty array
// if (!menus || menus.length <= 0) {
// reject('getInfo: menus must be a non-null array!')
// }
commit('SET_ROLES', role || '')
localStorage.setItem('roles', JSON.stringify(role))
// commit('SET_NAME', name)
// commit('SET_AVATAR', avatar)
// commit('SET_MENUS', menus)
// commit('SET_INTRODUCTION', introduction)
resolve(data)
})
.catch((error) => {
reject(error)
})
})
},
// user logout
logout() {
return new Promise((resolve) => {
removeToken()
// dispatch('tagsView/delAllViews', null, { root: true })
// commit('SET_TOKEN', '')
// commit('SET_ROLES', [])
// commit('SET_MENUS', [])
// commit('SET_USERNAME')
// Cookies.set('username', '')
// localStorage.clear()
resolve()
})
}
// remove token
// resetToken({ commit }) {
// return new Promise((resolve) => {
// commit('SET_TOKEN', '')
// commit('SET_ROLES', [])
// removeToken()
// resolve()
// })
// }
}
export default {
namespaced: true,
state,
mutations,
actions
}