Skip to content

Commit

Permalink
fix: 优化权限路由
Browse files Browse the repository at this point in the history
  • Loading branch information
ckvv committed Dec 17, 2021
1 parent 8a70a96 commit cbb2e61
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/assets/style/index.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import "./variable.scss";
@import "./mixin.scss";
@use "./variable.scss";
@use "./mixin.scss";

@tailwind base;
@tailwind components;
Expand Down
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import App from '@/App.vue';
import router from '@/router';
import { userAPI } from '@/api';
import { checkRes } from '@/utils/helpers';
import '@/assets/style/index.scss';

async function getUserInfo() {
const userRes = await userAPI.info();
Expand Down
10 changes: 8 additions & 2 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRouter, createWebHashHistory } from 'vue-router';
import Home from '@/views/Home.vue';
import Sign from '@/views/Sign.vue';
import About from '@/views/About.vue';
import { ROLE } from '@/constant';

const ADMIN = [ROLE.ADMIN];
Expand All @@ -18,6 +19,11 @@ const routes = [
name: 'Sign',
component: Sign,
},
{
path: '/about',
name: 'About',
component: About,
},
{
path: '/chat',
name: 'Chat',
Expand All @@ -42,14 +48,14 @@ const router = createRouter({
});

router.beforeEach(async (to, from, next) => {
const { meta } = to;
const userInfo = router.$user;
if (to.name !== 'Sign' && !userInfo) {
if ((meta && meta.role) && !userInfo) {
next({
name: 'Sign',
});
}
if (userInfo) {
const { meta } = to;
if (meta && meta.role) {
if (meta.role.indexOf(userInfo.role) === -1) {
router.push({
Expand Down
11 changes: 11 additions & 0 deletions src/views/About.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div class="about">
About Page
</div>
</template>

<script>
export default {
name: 'About',
};
</script>
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default defineConfig({
css: {
preprocessorOptions: {
scss: {
additionalData: '@use "@/assets/style/index.scss" as *;',
additionalData: '@use "@/assets/style/mixin.scss" as *;\n@use "@/assets/style/variable.scss" as *;',
},
},
},
Expand Down

0 comments on commit cbb2e61

Please sign in to comment.