vue-admin-template,连接自己后台,二次开发必看
vue-admin-template
PanJiaChen/vue-admin-template: 基于 Vue.js 和 Element UI 的后台管理系统模板,支持多语言、主题和布局切换。该项目提供了一个完整的后台管理系统模板,可以方便地实现后台管理系统的快速搭建和定制,同时支持多种数据源和插件扩展。
项目地址:https://gitcode.com/gh_mirrors/vu/vue-admin-template
免费下载资源
·
第一步:找到 .env.development文件做如下修改
# just a flag
ENV = 'development'
# base api
VUE_APP_BASE_API = 'api'
第二步:找到 :vue.config.js,配置跨域,关闭mock,必须做!!!
target换成自己的后端地址
//配置跨域
proxy: {
'/api': {
target: 'http://118.31.228.113:5730/',
ws: true,
changeOrigin: true,//允许跨域
pathRewrite: {
'^/api': '' //请求的时候使用这个api就可以
}
}
},
//这行把mock禁用
// before: require('./mock/mock-server.js')
第三步:在这个文件里面,把响应状态码20000改成200(前后端自己约定)
注意:做完这些其实就可以登录了,修改接口在api文件里面,如果要修改登录接口和获取信息接口,
接下来做前端权限管理,路由过滤:
首先在store下面的modules下面新建文件permission.js
复制到里面
import { asyncRoutes, constantRoutes } from '@/router'
/**
* Use meta.role to determine if the current user has permission
* @param roles
* @param route
*/
function hasPermission(roles, route) {
if (route.meta && route.meta.role) {
return roles.some(role => route.meta.role.includes(role))
} else {
return true
}
}
/**
* Filter asynchronous routing tables by recursion
* @param routes asyncRoutes
* @param roles
*/
export function filterAsyncRoutes(routes, roles) {
const res = []
routes.forEach(route => {
const tmp = { ...route }
if (hasPermission(roles, tmp)) {
if (tmp.children) {
tmp.children = filterAsyncRoutes(tmp.children, roles)
}
res.push(tmp)
}
})
return res;
}
const state = {
routes: [],
addRoutes: []
}
const mutations = {
SET_ROUTES: (state, routes) => {
state.addRoutes = routes
state.routes = constantRoutes.concat(routes)
}
}
const actions = {
generateRoutes({ commit }, roles) {
return new Promise(resolve => {
let accessedRoutes
if (roles.includes('ADMIN')) {
//路由是否有admin,有直接全部显示
accessedRoutes = asyncRoutes || []
} else {
//accessedRoutes这个就是当前角色可见的动态路由
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
然后修改store下面的index.js和getters.js
index.js:
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'
//这里
import permission from './modules/permission'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
app,
settings,
user,
//这里
permission
},
getters
})
export default store
getters.js:
const getters = {
sidebar: state => state.app.sidebar,
device: state => state.app.device,
token: state => state.user.token,
avatar: state => state.user.avatar,
name: state => state.user.name,
permission_routes: state=>state.permission.routes,
}
export default getters
然后找到和main平级的 permission.js
修改为如下,就修改了tey{}里面的内容
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
NProgress.configure({ showSpinner: false }) // NProgress Configuration
const whiteList = ['/login'] // no redirect whitelist
router.beforeEach(async (to, from, next) => {
// start progress bar
NProgress.start()
// set page title
document.title = getPageTitle(to.meta.title)
// determine whether the user has logged in
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' })
NProgress.done()
} else {
const hasGetUserInfo = store.getters.name
if (hasGetUserInfo) {
next()
} else {
try {
// get user info
//从请求中获取到角色
const val = await store.dispatch('user/getInfo')
console.log(val.roles)
// 把当前登录用户信息放到sessionStorage
sessionStorage.setItem("roles", JSON.stringify(val) );
//这里从permission/generateRoutes拿到路由
const accessRoutes = await store.dispatch('permission/generateRoutes', val.roles)
next()
//刷新路由
router.options.routes = store.getters.permission_routes
// dynamically add accessible routes
router.addRoutes(accessRoutes)
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
router.afterEach(() => {
// finish progress bar
NProgress.done()
})
然后就是添加异步路由了,,router下面的index.js添加asyncRoutes ,和constantRoutes平级
export const asyncRoutes = [
{
path: 'external-link',
component: Layout,
children: [
{
path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
//重点:role
meta: { title: 'External Link', icon: 'link', role: ['admin'] },
}
]
},
]
这就大功告成了,不懂得可以留言,一 一回复
GitHub 加速计划 / vu / vue-admin-template
19.83 K
7.39 K
下载
PanJiaChen/vue-admin-template: 基于 Vue.js 和 Element UI 的后台管理系统模板,支持多语言、主题和布局切换。该项目提供了一个完整的后台管理系统模板,可以方便地实现后台管理系统的快速搭建和定制,同时支持多种数据源和插件扩展。
最近提交(Master分支:2 个月前 )
4c18a3f4 - 2 年前
714ded11 - 4 年前
更多推荐
已为社区贡献4条内容
所有评论(0)