gin-vue-admin 2.0代码重构

This commit is contained in:
QM303176530
2020-04-04 21:39:07 +08:00
parent 5ed41ddd8c
commit 1b9adea284
202 changed files with 1240 additions and 1299 deletions
+22
View File
@@ -0,0 +1,22 @@
import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist'
import { user } from "@/store/module/user"
import { router } from "@/store/module/router"
Vue.use(Vuex)
const vuexLocal = new VuexPersistence({
storage: window.localStorage,
modules: ['user']
})
export const store = new Vuex.Store({
modules: {
user,
router
},
plugins: [vuexLocal.plugin]
})
+58
View File
@@ -0,0 +1,58 @@
import { asyncRouterHandle } from '@/utils/asyncRouter';
import { asyncMenu } from '@/api/menu'
const formatRouter = (routes) => {
routes && routes.map(item => {
item.meta.hidden = item.hidden
if (item.children.length > 0) {
formatRouter(item.children)
}
})
}
export const router = {
namespaced: true,
state: {
asyncRouters: []
},
mutations: {
// 设置动态路由
setAsyncRouter(state, asyncRouters) {
state.asyncRouters = asyncRouters
}
},
actions: {
// 从后台获取动态路由
async SetAsyncRouter({ commit }) {
const baseRouter = [{
path: '/layout',
name: 'layout',
component: "view/layout/index.vue",
meta: {
title: "底层layout"
},
children: []
}]
const asyncRouterRes = await asyncMenu()
const asyncRouter = asyncRouterRes.data.menus
formatRouter(asyncRouter)
baseRouter[0].children = asyncRouter
baseRouter.push({
path: '*',
redirect: '/404'
})
asyncRouterHandle(baseRouter)
commit('setAsyncRouter', baseRouter)
return true
}
},
getters: {
// 获取动态路由
asyncRouters(state) {
return state.asyncRouters
}
}
}
+75
View File
@@ -0,0 +1,75 @@
import { login } from '@/api/user'
import { jsonInBlacklist } from '@/api/jwt'
import router from '@/router/index'
export const user = {
namespaced: true,
state: {
userInfo: {
uuid: "",
nickName: "",
headerImg: "",
authority: "",
},
token: "",
expiresAt: ""
},
mutations: {
setUserInfo(state, userInfo) {
// 这里的 `state` 对象是模块的局部状态
state.userInfo = userInfo
},
setToken(state, token) {
// 这里的 `state` 对象是模块的局部状态
state.token = token
},
setExpiresAt(state, expiresAt) {
// 这里的 `state` 对象是模块的局部状态
state.expiresAt = expiresAt
},
LoginOut(state) {
state.userInfo = {}
state.token = ""
state.expiresAt = ""
router.push({ name: 'login', replace: true })
window.location.reload()
},
ResetUserInfo(state, userInfo = {}) {
state.userInfo = {...state.userInfo,
...userInfo
}
}
},
actions: {
async LoginIn({ commit }, loginInfo) {
const res = await login(loginInfo)
commit('setUserInfo', res.data.user)
commit('setToken', res.data.token)
commit('setExpiresAt', res.data.expiresAt)
if (res.success) {
const redirect = router.history.current.query.redirect
if (redirect) {
router.push({ path: redirect })
} else {
router.push({ path: '/layout/dashbord' })
}
}
},
async LoginOut({ commit }) {
const res = await jsonInBlacklist()
if (res.success) {
commit("LoginOut")
}
}
},
getters: {
userInfo(state) {
return state.userInfo
},
token(state) {
return state.token
},
expiresAt(state) {
return state.expiresAt
}
}
}