refactor: 代码优化

This commit is contained in:
郝先瑞
2022-03-22 00:08:06 +08:00
parent 88ee9c7d3a
commit de3d0ef8d8
5 changed files with 15 additions and 18 deletions

View File

@@ -1,11 +1,11 @@
import {PermissionState} from "@/types";
import {RouteRecordRaw} from 'vue-router'
import {constantRoutes} from '@/router'
import {listRoutes} from "@/api/system/menu";
import {defineStore} from "pinia";
import { PermissionState } from "@/types";
import { RouteRecordRaw } from 'vue-router'
import { defineStore } from "pinia";
import { constantRoutes } from '@/router'
import { listRoutes } from "@/api/system/menu";
const modules = import.meta.glob("../../views/**/**.vue");
export const Layout = () => import( '@/layout/index.vue')
export const Layout = () => import('@/layout/index.vue')
const hasPermission = (roles: string[], route: RouteRecordRaw) => {
if (route.meta && route.meta.roles) {
@@ -24,7 +24,7 @@ const hasPermission = (roles: string[], route: RouteRecordRaw) => {
export const filterAsyncRoutes = (routes: RouteRecordRaw[], roles: string[]) => {
const res: RouteRecordRaw[] = []
routes.forEach(route => {
const tmp = {...route} as any
const tmp = { ...route } as any
if (hasPermission(roles, tmp)) {
if (tmp.component == 'Layout') {
tmp.component = Layout
@@ -47,7 +47,7 @@ export const filterAsyncRoutes = (routes: RouteRecordRaw[], roles: string[]) =>
}
const usePermissionStore = defineStore({
const usePermissionStore = defineStore({
id: "permission",
state: (): PermissionState => ({
routes: [],
@@ -73,6 +73,4 @@ export const filterAsyncRoutes = (routes: RouteRecordRaw[], roles: string[]) =>
}
})
export default usePermissionStore;

View File

@@ -1,9 +1,9 @@
import { defineStore } from "pinia";
import { LoginFormData, UserState } from "@/types";
import { localStorage } from "@/utils/storage";
import { resetRouter } from "@/router";
import { login, logout } from "@/api/login";
import { getUserInfo } from "@/api/system/user";
import { resetRouter } from "@/router";
const useUserStore = defineStore({
id: "user",
@@ -53,8 +53,7 @@ const useUserStore = defineStore({
*/
getUserInfo() {
return new Promise(((resolve, reject) => {
getUserInfo().then(response => {
const { data } = response
getUserInfo().then(({data}) => {
if (!data) {
return reject('Verification failed, please Login again.')
}