refactor(projects): 修改了动态侧边栏的生成方法

This commit is contained in:
‘chen.home’ 2022-08-14 14:20:20 +08:00
parent d82e53ee0e
commit c646819b23
9 changed files with 93 additions and 148 deletions

View File

@ -15,58 +15,69 @@ const userInfo = {
token, token,
permissions: [ permissions: [
{ {
label: '主控台', name: 'dashboard',
key: 'dashboard_console', path: '/dashboard',
icon: 'icon-park-outline:alarm', meta: {
}, title: '分析页',
{ requiresAuth: true,
label: '监控页', icon: 'icon-park-outline:analysis',
key: 'dashboard_monitor', },
icon: 'icon-park-outline:anchor',
},
{
label: 'test1',
key: '/test1',
icon: 'icon-park-outline:alarm',
},
{
label: 'test2',
key: '/test2',
icon: 'icon-park-outline:pic',
},
{
label: 'test3',
key: '/test3',
icon: 'icon-park-outline:tool',
},
{
label: '舞,舞,舞',
key: 'dance-dance-dance',
icon: 'icon-park-outline:command',
children: [ children: [
{ {
label: '饮品', name: 'dashboard_console',
key: 'beverage', path: '/dashboard/console',
children: [ meta: {
{ title: '主控台',
label: '威士忌', requiresAuth: true,
key: 'whisky', icon: 'icon-park-outline:alarm',
}, },
],
}, },
{ {
label: '食物', name: 'dashboard_monitor',
key: 'food', path: '/dashboard/monitor',
children: [ meta: {
{ title: '监控页',
label: '三明治', requiresAuth: true,
key: 'sandwich', icon: 'icon-park-outline:anchor',
}, },
], },
],
},
{
name: 'test',
path: '/test',
meta: {
title: '测试专题',
requiresAuth: true,
icon: 'icon-park-outline:ambulance',
},
children: [
{
name: 'test1',
path: '/test/test1',
meta: {
title: '测试专题1',
requiresAuth: true,
icon: 'icon-park-outline:alarm',
},
}, },
{ {
label: '过去增多,未来减少', name: 'test2',
key: 'the-past-increases-the-future-recedes', path: '/test/test2',
meta: {
title: '测试专题2',
requiresAuth: true,
icon: 'icon-park-outline:pic',
},
},
{
name: 'test3',
path: '/test/test3',
meta: {
title: '测试专题3',
requiresAuth: true,
icon: 'icon-park-outline:tool',
},
}, },
], ],
}, },

View File

@ -2,6 +2,7 @@ import type { App } from 'vue';
import { createRouter, createWebHistory, createWebHashHistory, RouteRecordRaw } from 'vue-router'; import { createRouter, createWebHistory, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { setupRouterGuard } from './guard'; import { setupRouterGuard } from './guard';
import { BasicLayout } from '../layouts/index'; import { BasicLayout } from '../layouts/index';
import { constantRoutes } from './routes';
const routes: RouteRecordRaw[] = [ const routes: RouteRecordRaw[] = [
{ {
@ -11,9 +12,9 @@ const routes: RouteRecordRaw[] = [
component: BasicLayout, component: BasicLayout,
children: [ children: [
{ {
path: '/test1', path: '/test/test1',
name: 'test1', name: 'test1',
component: () => import('~/src/views/test/test1.vue'), component: () => import(`@/views/test/test1.vue`),
meta: { meta: {
title: '测试1', title: '测试1',
icon: 'icon-park-outline:game-three', icon: 'icon-park-outline:game-three',
@ -21,9 +22,9 @@ const routes: RouteRecordRaw[] = [
}, },
}, },
{ {
path: '/test2', path: '/test/test2',
name: 'test2', name: 'test2',
component: () => import('~/src/views/test/test2.vue'), component: () => import('@/views/test/test2.vue'),
meta: { meta: {
title: '测试2', title: '测试2',
icon: 'carbon:aperture', icon: 'carbon:aperture',
@ -31,15 +32,16 @@ const routes: RouteRecordRaw[] = [
}, },
}, },
{ {
path: '/test3', path: '/test/test3',
name: 'test3', name: 'test3',
component: () => import('~/src/views/test/test3.vue'), component: () => import('@/views/test/test3.vue'),
meta: { meta: {
title: '测试3', title: '测试3',
icon: 'icon-park-outline:music-list', icon: 'icon-park-outline:music-list',
requiresAuth: true, requiresAuth: true,
}, },
}, },
...constantRoutes,
], ],
}, },
{ {
@ -50,30 +52,6 @@ const routes: RouteRecordRaw[] = [
title: '登录', title: '登录',
}, },
}, },
{
path: '/no-permission',
name: 'no-permission',
component: () => import('@/views/inherit-page/not-permission/index.vue'),
meta: {
title: '无权限',
},
},
{
path: '/service-error',
name: 'service-error',
component: () => import('@/views/inherit-page/service-error/index.vue'),
meta: {
title: '服务器错误',
},
},
{
path: '/:pathMatch(.*)*',
name: '404',
component: () => import('@/views/inherit-page/not-found/index.vue'),
meta: {
title: '错误404',
},
},
]; ];
const { VITE_HASH_ROUTE = 'N', VITE_BASE_URL } = import.meta.env; const { VITE_HASH_ROUTE = 'N', VITE_BASE_URL } = import.meta.env;

View File

@ -1,49 +1,6 @@
import { BasicLayout } from '@/layouts/index'; // import { BasicLayout } from '@/layouts/index';
export const constantRoutes: AppRoute.Route[] = [ export const constantRoutes = [
{
path: '/',
name: 'root',
redirect: '/test1',
component: BasicLayout,
children: [
{
path: '/test1',
name: 'test1',
component: () => import('~/src/views/test/test1.vue'),
meta: {
title: '测试1',
icon: 'icon-park-outline:game-three',
requiresAuth: true,
},
},
{
path: '/test2',
name: 'test2',
component: () => import('~/src/views/test/test2.vue'),
meta: {
title: '测试2',
icon: 'carbon:aperture',
requiresAuth: true,
},
},
{
path: '/test3',
name: 'test3',
component: () => import('~/src/views/test/test3.vue'),
meta: {
title: '测试3',
icon: 'icon-park-outline:music-list',
requiresAuth: true,
},
},
],
},
{
path: '/login',
name: 'login',
component: () => import('@/views/login/index.vue'), // 注意这里要带上 文件后缀.vue
},
{ {
path: '/no-permission', path: '/no-permission',
name: 'no-permission', name: 'no-permission',

View File

@ -1,5 +1,6 @@
import axios from 'axios'; import axios from 'axios';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { getToken } from '@/utils';
/** /**
* @description: axios请求类 * @description: axios请求类
@ -33,8 +34,7 @@ export default class createAxiosInstance {
this.instance.interceptors.request.use( this.instance.interceptors.request.use(
(config: AxiosRequestConfig) => { (config: AxiosRequestConfig) => {
// 一般会请求拦截里面加token // 一般会请求拦截里面加token
// const token = localStorage.getItem('token') as string; config.headers!.Authorization = getToken();
// config.headers!.Authorization = token;
return config; return config;
}, },
(err: any) => Promise.reject(err), (err: any) => Promise.reject(err),

View File

@ -1,5 +1,6 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { renderIcon } from '@/utils/icon'; import { renderIcon } from '@/utils/icon';
import type { MenuOption } from 'naive-ui';
interface RuutesStatus { interface RuutesStatus {
menus: any; menus: any;
@ -15,13 +16,21 @@ export const useRouteStore = defineStore('route-store', {
this.menus = this.transformAuthRoutesToMenus(data); this.menus = this.transformAuthRoutesToMenus(data);
}, },
// 将返回的路由表渲染成侧边栏 // 将返回的路由表渲染成侧边栏
transformAuthRoutesToMenus(data: Auth.UserInfoPermissions[]) { transformAuthRoutesToMenus(data: Auth.UserInfoPermissions[]): MenuOption[] {
return data.map((item) => { return data.map((item) => {
item.icon = renderIcon(item.icon); const target: MenuOption = {
if (item.children) { label: item.meta.title,
this.transformAuthRoutesToMenus(item.children); key: item.path,
};
// 判断有无图标
if (item.meta.icon) {
target.icon = renderIcon(item.meta.icon);
} }
return item; // 判断子元素
if (item.children) {
target.children = this.transformAuthRoutesToMenus(item.children);
}
return target;
}); });
}, },
}, },

View File

@ -29,9 +29,9 @@ declare namespace Auth {
permissions: UserInfoPermissions[]; permissions: UserInfoPermissions[];
} }
interface UserInfoPermissions { interface UserInfoPermissions {
label: string; name: string;
key: string; path: string;
icon: any; meta: AppRoute.RouteMeta;
children: UserInfoPermissions[]; children?: UserInfoPermissions[];
} }
} }

View File

@ -1,5 +1,6 @@
import { setLocal, getLocal, removeLocal } from './storage'; import { setLocal, getLocal, removeLocal } from './storage';
import { EnumStorageKey } from '@/enum'; import { EnumStorageKey } from '@/enum';
const DURATION = 6 * 60 * 60;
/* 获取当前token */ /* 获取当前token */
export function getToken() { export function getToken() {
@ -7,7 +8,7 @@ export function getToken() {
} }
/* 设置token */ /* 设置token */
export function setToken(data: string) { export function setToken(data: string) {
setLocal(EnumStorageKey.token, data); setLocal(EnumStorageKey.token, data, DURATION);
} }
/* 移除token */ /* 移除token */
export function removeToken() { export function removeToken() {

4
src/utils/index.ts Normal file
View File

@ -0,0 +1,4 @@
export * from './auth';
export * from './icon';
export * from './is';
export * from './storage';

View File

@ -1,15 +0,0 @@
import { setLocal, getLocal, removeLocal } from './storage';
const TOKEN_CODE = 'access_token';
const DURATION = 6 * 60 * 60;
export function getToken() {
return getLocal(TOKEN_CODE);
}
export function setToken(token: any) {
setLocal(TOKEN_CODE, token, DURATION);
}
export function removeToken() {
removeLocal(TOKEN_CODE);
}