router文件夹下,index.js 配置
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: Home,
meta: {
requireAuth: true
},
children: [
// 学生数据
{
path: '/home/studentData',
name: 'studentData',
meta: {
requireAuth: true
},
component: () => import('../views/studentData/studentData.vue')
},
// 老师数据
{
path: '/home/teacherData',
name: 'teacherData',
meta: {
requireAuth: true
},
component: () => import('../views/teacherData/teacherData.vue')
},
]
},
{
path: '/login',
name: 'login',
component: () => import('../views/login/login.vue'),
// 子路由
children: [
]
}
]
// 多次点击同一个导航时会报错,所以添加了这段代码
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
routes
})
// 别的地方会用到 router,所以将它导出去
export default router
login.vue(当前是纯静态页面,稍后添加接口与方法)
<template>
<div class="login-box">
<div class="form-con">
<h2>Vue+element ui 管理系统实战</h2>
<div class="label">
<el-input placeholder="请输入账号" v-model="userName">
<template slot="prepend"><i class="el-icon-user"></i></template>
</el-input>
</div>
<div class="label">
<el-input placeholder="请输入密码" v-model="password" show-password @keyup.enter.native="loginFn()">
<template slot="prepend"><i class="el-icon-lock"></i></template>
</el-input>
</div>
<div class="label">
<el-button class="login-btn" type="primary" @click="loginFn()"
>登 录</el-button
>
</div>
</div>
</div>
</template>
<script>
export default {
name: "login",
data() {
return {
userName: "",
password: "",
verify: "",
};
},
};
</script>
<style lang="scss" scope>
.login-box {
width: 100%;
height: 100%;
display: flex;
text-align: center;
background: url("~@/assets/4.jpg") no-repeat;
background-size: 100% 100%;
.form-con {
h2 {
color: #ffffff;
// color: $colorRed;
}
width: 360px;
height: 420px;
margin: 150px auto auto auto;
.label {
margin: 40px 0;
.login-btn {
width: 100%;
}
}
}
}
</style>
3.1 service.js 配置
import axios from 'axios'
import vue from '../main.js'
// 从本地获取token
function getTokenByLocal() {
let token = sessionStorage.getItem('token');
return token;
}
const service = axios.create({
baseURL: '/sys',
// withCredentials: true,
timeout: 5000,
})
// 请求拦截
service.interceptors.request.use(
config => {
if (getTokenByLocal()) {
// 在此可以设置所有接口headers头部
config.headers['token'] = getTokenByLocal();
}else{
// window.location.href="/login";
}
return config
},
error => {
return Promise.reject(error)
}
)
// 响应拦截
service.interceptors.response.use(
response => {
let res = response.data;
// console.log(res);
// 状态码处理
if (res.code == '200') {
// location.href = "home/login";
}
// 如果为 -101 代表用户未登录
if(res.code == '-101'){
vue.$router.push('/login');
}
return Promise.resolve(res);
},
error => {
return Promise.reject(error)
}
)
export default service;
3.2 common.js 配置
// 将service.js引入进来
import service from './service.js'
// post请求 80% 耦合度低 复用性高
export function requestOfPost(url, data){
return service.post(url, data);
}
3.3 api.js 配置
import {
requestOfPost} from './common.js'
export function postRequest(url, data){
return new Promise((resolve, reject) => {
requestOfPost(url, data).then(res => resolve(res))
.catch(error => reject(error))
})
}
3.4 url.j配置
const url = {
// 登录
login: '/login',
// 学生列表
getClassmates: '/getClassmates'
}
export default url;
module.exports = {
devServer: {
compress: false,
open: true,
proxy: {
'/sys': {
// 代理地址
target: 'http://api.gebilaowang.com',
// websocket (一般用于即时通讯,游戏,这里不需要,所以不开)
ws: false,
// 是否允许跨域
changeOrigin: true,
// 重写
pathRewite: {
'/sys': '/'
}
}
}
}
}
login.vue 代码添加
<script>
// 此为全局定义的过渡方法
import {
loadingShow } from "../../common/js/common.js";
import url from "../../request/url.js";
import {
postRequest } from "../../request/api.js";
export default {
name: "login",
props: {
msg: String,
},
data() {
return {
userName: "",
password: "",
verify: "",
};
},
methods: {
// 登录方法
loginFn() {
if (!this.userName) {
this.msgFn("warning", "请输入账号名");
return;
} else if (!this.password) {
this.msgFn("warning", "请输入密码");
return;
} else {
// 加载动画
loadingShow();
let data = {
userName: this.userName,
passWord: this.password
};
postRequest(url.login, data).then(
(res) => {
// 动画隐藏
loadingHide();
if (res.code == 500) {
this.msgFn("error", res.msg);
return;
}
// token 存入sessionStorage
sessionStorage.setItem("token", res.token);
// 页面跳转
setTimeout(() => {
this.$router.push("/home/studentData");
}, 500);
},
(error) => {
console.log(error);
}
);
}
},
// 弹窗
msgFn(type, text) {
this.$message({
message: text,
type: type,
});
},
},
created() {
},
};
</script>
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import {
Button,
Container,
Header,
Aside,
Main,
Footer,
Input,
Loading,
Message,
Menu,
Submenu,
MenuItem,
MenuItemGroup,
Dropdown,
DropdownMenu,
Table,
TableColumn,
DropdownItem,
Form,
FormItem,
Select,
Option,
OptionGroup,
DatePicker,
Pagination,
MessageBox,
Popover,
Tag,
Switch,
Dialog
} from 'element-ui';
Vue.use(global)
Vue.use(Button)
Vue.use(Container)
Vue.use(Header)
Vue.use(Aside)
Vue.use(Main)
Vue.use(Footer)
Vue.use(Input)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(MenuItemGroup)
Vue.use(Dropdown)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Select)
Vue.use(Option)
Vue.use(OptionGroup)
Vue.use(DatePicker)
Vue.use(Pagination)
Vue.use(Popover)
Vue.use(Dialog);
Vue.use(Tag)
Vue.use(Switch)
Vue.use(Loading.directive);
// 添加全局方法
Vue.prototype.$loading = Loading.service;
Vue.prototype.$message = Message;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$msgbox = MessageBox;
Vue.config.productionTip = false;
let vue = new Vue({
router,
render: h => h(App)
}).$mount('#app')
export default vue;
router.beforeEach((to, from, next) => {
// 以token为例
let token = sessionStorage.getItem('token');
// 需要验证之后才能进入 requireAuth
if (to.meta.requireAuth) {
if (token) {
next();
// 顺利进入
} else {
// 跳入到指定页面
next({
path: '/login'
})
}
} else {
// 顺利进入
next();
}
})
import vue from '../../main.js'
// 遮罩层控制
export function loadingShow(close){
const loadingFade = vue.$loading({
lock: true,
text: 'Loading',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
})
if(close){
loadingFade.close();
}
}
<template>
<div class="container-big">
<el-container class="container">
<el-aside width="220px">
<el-menu class="menu" :default-active="index">
<el-submenu index="1">
<template slot="title"><i class="el-icon-s-home"></i>首页</template>
<el-menu-item index="1-1" @click="toRoute('/home/studentData', '学生数据')"
>学生数据</el-menu-item
>
</el-submenu>
<el-submenu index="2">
<template slot="title"
><i class="el-icon-s-cooperation"></i>教务处 / 管理</template
>
<el-submenu index="2-2">
<template slot="title">角色管理</template>
<el-menu-item
index="2-2-1"
@click="toRoute('/home/teacherData', '老师管理')"
>
老师管理</el-menu-item
>
<el-menu-item
index="2-2-2"
@click="toRoute()"
>
老师审核</el-menu-item
>
</el-submenu>
<el-submenu index="2-3">
<template slot="title">其它管理</template>
<el-menu-item
index="2-3-1"
@click="
toRoute()
"
>
教务管理</el-menu-item
>
<el-menu-item
index="2-3-3"
@click="
toRoute(
'/home/shopList',
' / 运营管理 / 店铺管理 / 店铺列表',
'店铺列表',
'2-3-3'
)
"
>
扫厕所管理</el-menu-item
>
</el-submenu>
</el-submenu>
</el-menu>
</el-aside>
<el-container>
<el-header style="text-align: right; font-size: 14px">
<div class="opration">
<el-dropdown>
<i class="el-icon-setting" style="margin-right: 15px"> 操作 </i>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item @click.native="logOut()"
>退出登录</el-dropdown-item
>
</el-dropdown-menu>
</el-dropdown>
<span>{
{
userName }}</span>
</div>
<div class="router-con">
<div>
<span>xxx公司运营管理系统</span>
</div>
<h2>{
{
pageName}}</h2>
</div>
</el-header>
<el-main>
<router-view></router-view>
</el-main>
</el-container>
</el-container>
</div>
</template>
<script>
export default {
name: "home",
data() {
return {
userName: "",
index: "1-1",
pageName: ''
};
},
created() {
// 可以在此取权限相关数据
},
methods: {
// 跳转各个页面
toRoute(url, name) {
this.$router.push(url);
this.pageName = name;
},
// 退出登录
logOut() {
// 跳转至登录页面
this.$router.push("/login");
// 一般需要在此清除用户的一些缓存数据
},
},
};
</script>
<style lang="scss" scope>
.logo {
overflow: hidden;
padding: 10px 0px 10px 20px;
cursor: pointer;
img {
float: left;
margin-top: 10px;
}
p {
float: left;
margin: 12px 0 0 12px;
color: #fff;
font-weight: 600;
font-size: 20px;
vertical-align: middle;
animation: fade-in;
animation-duration: 0.3s;
}
}
.el-submenu .el-menu-item {
text-align: left;
height: 40px;
line-height: 40px;
// margin-left: 10px;
}
.menu {
.el-submenu {
.el-submenu__title {
padding-left: 30px;
}
.el-menu {
.el-submenu__title {
padding-left: 50px !important;
}
}
}
}
.el-header {
color: #333;
border-bottom: 1px solid #d3d3d3;
background: #fff;
padding: 0;
box-shadow: darkgrey 5px 0 5px 1px; //边框阴影
.opration {
height: 60px;
line-height: 60px;
padding: 0 20px;
cursor: pointer;
}
.router-con {
height: 60px;
// width: 100%;
padding: 20px;
border-bottom: 1px solid #d3d3d3;
text-align: left;
div {
margin-bottom: 10px;
}
}
}
.container-big {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
.el-main {
margin-top: 100px;
background: #f2f2f2;
}
}
.el-aside {
color: #333;
height: 100%;
background: #001529;
overflow-y: auto;
overflow-x: hidden;
.el-menu {
border: none;
background: #001529;
li {
.el-submenu__title {
color: #f5f5f5;
text-align: left;
}
.el-submenu__title:hover {
color: #333;
}
ul {
li {
color: #dcdcdc;
.el-menu-item-group__title {
// color: #dcdcdc;
}
}
li.is-active {
color: #409eff;
}
li:hover {
color: #333;
}
}
}
}
}
</style>