前端权限优化

main
qinzhenpen 2 years ago
parent f60644d42e
commit c207b2982a

@ -79,12 +79,11 @@ export default {
mounted() {
const roleid = getroleId();
// roleid4,3
if (roleid == 4) {
this.routesList = dynamicRoutes;
} else if (roleid == 3) {
this.routesList = teacher;
}
console.log("dynamicRoutes", this.limitsRoutes);
// if (roleid == 4) {
this.routesList = dynamicRoutes
// } else if (roleid == 3) {
// this.routesList = teacher;
// }
},
watch: {
limitsRoutes() {
@ -95,12 +94,11 @@ export default {
if (this.limitsRoutes.indexOf(fullPath) == -1) {
item.children.splice(item.children.indexOf(itemChild), 1);
}
console.log("fullPath", fullPath);
});
}
});
console.log("this.routesList", this.routesList);
console.log("dynamicRoutes", this.limitsRoutes);
// console.log("this.routesList", this.routesList);
// console.log("dynamicRoutes", this.limitsRoutes);
},
},
};

@ -18,15 +18,15 @@ router.beforeEach((to, from, next) => {
// }
NProgress.start()
if (getToken()) {
if (to.path === '/login') {
// console.log(getToken(),"getToken()")
// next({ path: '/' })
if (getroleId() === 4) {
router.push({ path: '/teacher' })
} else if (getroleId() === 3) {
router.push({ path: '/' })
}
}
// if (to.path === '/login') {
// // console.log(getToken(),"getToken()")
// // next({ path: '/' })
// // if (getroleId() === 4) {
// // router.push({ path: '/teacher' })
// // } else if (getroleId() === 3) {
// router.push({ path: '/student/index' })
// // }
// }
next()
// to.meta.title && store.dispatch('settings/setTitle', "外汇模拟交易")
/* has token*/

@ -1,10 +1,13 @@
import Vue from 'vue'
import Router from 'vue-router'
import { getRouters } from '@/api/menu'
import { getToken } from '@/utils/auth'
import permission from '@/store/index.js'
Vue.use(Router)
/* Layout */
import Layout from '@/layout'
/**
* Note: 路由配置项
*
@ -179,12 +182,12 @@ export const dynamicRoutes = [{
path: 'case',
component: () => import('@/views/teachingpan/index.vue'),
name: 'teachingPlan',
meta: { title: '实训教案', icon: 'teachingplan', affix: true}
meta: { title: '实训教案', icon: 'teachingplan', affix: true }
}
]
},
{
path: '/quotation',
path: '/student/quotation',
component: Layout,
hidden: false,
permissions: ['system:user:edit'],
@ -193,7 +196,7 @@ export const dynamicRoutes = [{
path: 'lowcore',
component: () => import('@/views/quotation/index.vue'),
name: 'lowcore',
meta: { title: '行情中心', icon: 'market', affix: true}
meta: { title: '行情中心', icon: 'market', affix: true }
}
]
},
@ -266,7 +269,22 @@ export const dynamicRoutes = [{
meta: { title: '修改生成配置', activeMenu: '/tool/gen' }
}
]
}, { path: '*', redirect: '/404'}
}
, { path: '*', redirect: '/404' },
{
path: '',
component: Layout,
redirect: 'ageIssue',
hidden: false,
children: [
{
path: 'teacher',
component: () => import('@/views/index_v1.vue'),
name: 'teacher',
meta: { title: '首页', icon: 'dashboard', affix: true }
}
]
},
]
// 教师端路由
export const teacher = [
@ -296,9 +314,36 @@ Router.prototype.push = function push(location) {
Router.prototype.replace = function push(location) {
return routerReplace.call(this, location).catch(err => err)
}
export default new Router({
const router = new Router({
mode: 'history', // 去掉url中的#
scrollBehavior: () => ({ y: 0 }),
routes: [...dynamicRoutes, ...constantRoutes, ...teacher]
})
});
router.beforeEach((to, from, next) => {
// next();
console.log('to.fullPath',to.fullPath);
console.log('localStorage.getItem("limitsRoutes")', localStorage.getItem("limitsRoutes"));
const limitsRoutes = localStorage.getItem("limitsRoutes") ? localStorage.getItem("limitsRoutes").split(',') : []
if (!constantRoutes.filter(item => item.path == to.fullPath).length && to.fullPath !== '/404') {
if (limitsRoutes.indexOf(to.fullPath) == -1) {
console.log('没有权限')
next({ path: "/login" });
} else {
if (getToken())
{
next();
} else {
next({ path: "/login" });
}
next();
}
}
else {
next();
}
});
export default router

@ -8,7 +8,12 @@
</div>
</div>
<div class="login-mian">
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
<el-form
ref="loginForm"
:model="loginForm"
:rules="loginRules"
class="login-form"
>
<h3 class="title">外汇模拟交易系统</h3>
<el-form-item prop="username">
<el-input
@ -17,7 +22,11 @@
auto-complete="off"
placeholder="账号"
>
<svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
<svg-icon
slot="prefix"
icon-class="user"
class="el-input__icon input-icon"
/>
</el-input>
</el-form-item>
<el-form-item prop="password">
@ -28,23 +37,29 @@
placeholder="密码"
@keyup.enter.native="handleLogin"
>
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
<svg-icon
slot="prefix"
icon-class="password"
class="el-input__icon input-icon"
/>
</el-input>
</el-form-item>
<el-form-item style="width:100%;">
<el-form-item style="width: 100%">
<el-button
:loading="loading"
size="medium"
type="primary"
style="width:100%;"
style="width: 100%"
@click.native.prevent="handleLogin"
>
<span v-if="!loading"> </span>
<span v-else> ...</span>
</el-button>
<div v-if="register" style="float: right;">
<router-link class="link-type" :to="'/register'">立即注册</router-link>
<div v-if="register" style="float: right">
<router-link class="link-type" :to="'/register'"
>立即注册</router-link
>
</div>
</el-form-item>
</el-form>
@ -58,49 +73,52 @@
<script>
// import { getCodeImg } from "@/api/login";
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { getRouters } from "@/api/menu";
import { getroleId } from "@/utils/auth";
export default {
name: 'Login',
name: "Login",
data() {
return {
codeUrl: '',
codeUrl: "",
loginForm: {
username: '',
password: '',
username: "",
password: "",
rememberMe: false,
code: '',
uuid: ''
code: "",
uuid: "",
},
loginRules: {
username: [
{ required: true, trigger: 'blur', message: '请输入您的账号' }
{ required: true, trigger: "blur", message: "请输入您的账号" },
],
password: [
{ required: true, trigger: 'blur', message: '请输入您的密码' }
{ required: true, trigger: "blur", message: "请输入您的密码" },
],
code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
code: [{ required: true, trigger: "change", message: "请输入验证码" }],
},
loading: false,
//
captchaEnabled: true,
//
register: false,
redirect: undefined
}
redirect: undefined,
};
},
watch: {
$route: {
handler: function(route) {
this.redirect = route.query && route.query.redirect
handler: function (route) {
this.redirect = route.query && route.query.redirect;
},
immediate: true
}
immediate: true,
},
},
created() {
Cookies.remove("Admin-Token");
localStorage.removeItem("limitsRoutes");
// this.getCode();
this.getCookie()
this.getCookie();
},
methods: {
getCode() {
@ -113,46 +131,78 @@ export default {
// });
},
getCookie() {
const username = Cookies.get('username')
const password = Cookies.get('password')
const username = Cookies.get("username");
const password = Cookies.get("password");
this.loginForm = {
username: username === undefined ? this.loginForm.username : username,
password: password === undefined ? this.loginForm.password : decrypt(password)
}
password:
password === undefined ? this.loginForm.password : decrypt(password),
};
},
handleLogin() {
this.$refs.loginForm.validate(valid => {
this.$refs.loginForm.validate((valid) => {
if (valid) {
this.loading = true
this.loading = true;
if (this.loginForm.rememberMe) {
Cookies.set('username', this.loginForm.username, { expires: 30 })
Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
Cookies.set("username", this.loginForm.username, { expires: 30 });
Cookies.set("password", encrypt(this.loginForm.password), {
expires: 30,
});
} else {
Cookies.remove('username')
Cookies.remove('password')
Cookies.remove("username");
Cookies.remove("password");
}
const username = Cookies.get('username')
const password = Cookies.get('password')
const username = Cookies.get("username");
const password = Cookies.get("password");
const loginForm = {
username: username === undefined ? this.loginForm.username : username,
passwordEncode: password === undefined ? encrypt(this.loginForm.password) : password
}
this.$store.dispatch('Login', loginForm).then(() => {
console.log('loginForm', loginForm)
this.$router.push({ path: this.redirect || '/' })
}).catch(() => {
console.log('dsdsdsd')
this.loading = false
// if (this.captchaEnabled) {
// this.getCode();
// }
})
username:
username === undefined ? this.loginForm.username : username,
passwordEncode:
password === undefined
? encrypt(this.loginForm.password)
: password,
};
this.$store
.dispatch("Login", loginForm)
.then(() => {
console.log("loginForm", loginForm);
// this.getRouterList();
getRouters().then((res) => {
if (res.data?.length) {
localStorage.setItem("limitsRoutes", res.data);
if (getroleId() == 4) {
this.$router.push({
path: this.redirect || "/student/index",
});
} else {
this.$router.push({ path: this.redirect || "/teacher" });
}
} else {
localStorage.setItem("limitsRoutes",['/teacher']);
if (getroleId() == 4) {
this.$router.push({
path: this.redirect || "/student/index",
});
} else {
this.$router.push({ path: this.redirect || "/teacher" });
}
}
console.log("res111", localStorage.getItem("limitsRoutes"));
});
})
.catch(() => {
console.log("dsdsdsd");
this.loading = false;
// if (this.captchaEnabled) {
// this.getCode();
// }
});
}
})
}
}
}
});
},
},
};
</script>
<style rel="stylesheet/scss" lang="scss">
@ -167,43 +217,42 @@ export default {
background-size: cover;
// transform: translate(10%,10%);
}
.login-header{
.login-header {
position: absolute;
top: 0;
left: 0;
// height: 80px;
background: #3c76df;
width: 100%;
.login-centen{
.login-centen {
width: 50%;
height: 80px;
margin: 0 auto;
.logo{
width: 180px;
height: 80px;
a{
display: block;
width: 177px;
height: 48px;
background: url("../assets/logo/login-logo.png") no-repeat;
background-size: cover;
transform: translate(1%, 30%);;
}
.logo {
width: 180px;
height: 80px;
a {
display: block;
width: 177px;
height: 48px;
background: url("../assets/logo/login-logo.png") no-repeat;
background-size: cover;
transform: translate(1%, 30%);
}
}
}
}
}
.title {
margin: 0px auto 30px auto;
text-align: center;
color: #707070;
}
.login-mian{
.login-mian {
width: 50%;
// height: 100%;
display: flex;
justify-content: end;
align-content: center;
justify-content: end;
align-content: center;
}
.login-form {
border-radius: 6px;

@ -240,6 +240,7 @@ export default {
event.stopPropagation()
}
},
mounted() {
const clipboard = new ClipboardJS('#copyNode', {
text: (trigger) => {

Loading…
Cancel
Save