提交登录限制

This commit is contained in:
闵宪瑞 2025-02-07 16:18:08 +08:00
parent 5d762b82a5
commit 4bdb545c0c
3 changed files with 11 additions and 42 deletions

View File

@ -51,12 +51,12 @@ const state = reactive({
const login = reactive({ username: 'admin', password: 'admin', captcha: '', uuid: '',role: 0,})
const onLogin = () => {
state.loading = true
if (login.role) {
if (login.role == 1) {
console.log("管理员")
loginAdmin(login).then(response => {
state.loading = false
ElMessage.success('登录成功')
userStore().isLogin = true
userStore().adminIsLogin = true
userStore().adminToken = response.data.token
router.push('/admin')
}).catch(() => {
@ -66,10 +66,9 @@ const onLogin = () => {
} else {
loginFront(login)
ElMessage.success("登录成功~")
router.push('/front')
router.push('/')
}
}
/**
* 获取验证码
*/
@ -92,7 +91,6 @@ onMounted(() => {
height: 100vh;
background: #FFFFFF;
}
.module {
width: 100%;
display: flex;
@ -100,12 +98,10 @@ onMounted(() => {
justify-content: space-between;
height: 100vh;
}
.module_img {
width: 60%;
height: auto;
}
.module_r {
width: 40%;
background: #e5efee;
@ -113,7 +109,6 @@ onMounted(() => {
display: flex;
align-items: center;
justify-content: center;
.module_mian {
width: 65%;
background: #FFFFFF;
@ -122,21 +117,18 @@ onMounted(() => {
overflow: hidden;
padding-top: 40px;
padding-bottom: 40px;
.module_title {
font-size: 18px;
font-weight: 500;
text-align: center;
color: #333333;
}
.module_desc {
font-size: 12px;
text-align: center;
color: #a7a7a7;
margin-bottom: 20px;
}
.module_m {
margin: 0 auto;
width: 80%;
@ -148,7 +140,6 @@ onMounted(() => {
color: #333333;
margin-bottom: 5px;
}
.module_input {
width: 96%;
height: 40px;
@ -158,12 +149,10 @@ onMounted(() => {
border-radius: 5px;
font-size: 12px;
}
.module_code {
width: 96%;
display: flex;
align-items: center;
.module_code_input {
width: 60%;
height: 40px;
@ -173,7 +162,6 @@ onMounted(() => {
padding-left: 2%;
padding-right: 2%;
}
.module_code_img {
width: 130px;
height: 40px;
@ -183,11 +171,9 @@ onMounted(() => {
}
}
}
.module_radio input {
margin-right: 5px;
}
.forgetpwd {
margin: 0 auto;
width: 80%;
@ -196,7 +182,6 @@ onMounted(() => {
margin-top: 10px;
cursor: pointer;
}
.module_button {
margin: 0 auto;
display: block;
@ -209,7 +194,6 @@ onMounted(() => {
font-weight: 500;
cursor: pointer;
}
.module_button:active {
opacity: 0.4;
}

View File

@ -1,7 +1,6 @@
import { setupLayouts } from 'virtual:meta-layouts'
import { createRouter, createWebHistory } from 'vue-router'
import { routes as fileRoutes } from 'vue-router/auto-routes'
import { userInfoAdmin } from '~/api/user/adminUserApi'
declare module 'vue-router' {}
// 重定向 BASE_URL
@ -14,36 +13,21 @@ export const router = createRouter({
})
// 路由拦截
router.beforeEach((to, from, next) => {
if (to.fullPath.includes("/front")){
console.log(to.fullPath)
console.log(to)
console.log(to.fullPath)
}
// 管理员全部限制
else if (to.fullPath.includes("/admin")){
console.log("管理员认证~")
// 判断有没有登录
if (userStore().adminToken === null || userStore().adminToken === undefined ||userStore().adminToken == ""){
//判断有没有登录
if (!userStore().adminIsLogin){
ElMessage.warning("认证失败~")
next('/login');
}else {
// 获取用户信息
userInfoAdmin().then(response => {
userStore().adminUserInfo = response.data
})
}
}
// 在这里编写拦截逻辑
// 例如,检查用户是否认证
const isLogin = userStore().isLogin
const authList=["/"]
if (authList.includes(to.fullPath) && !isLogin) {
// 如果用户未认证,则跳转到登录页面
next('/login');
} else {
// 如果用户已认证,或者不需要认证,则继续
next();
}
});
export default router

View File

@ -3,7 +3,8 @@ import { defineStore } from 'pinia'
export default defineStore('userStore', {
state() {
return {
isLogin: true,
adminIsLogin: false,
isLogin: false,
adminToken: "",
frontToken: "",
adminUserInfo:{},