From fe494855639b69413f3d9adbdfbd6be543889635 Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Sun, 18 Dec 2022 15:25:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20pinia=20setup=20store=E7=BB=84?= =?UTF-8?q?=E5=90=88=E5=BC=8F=E5=87=BD=E6=95=B0=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 03faa4898c2633666374b895336da32297d2f54a --- src/api/dict/index.ts | 4 +--- src/permission.ts | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/api/dict/index.ts b/src/api/dict/index.ts index 1e66dc3..3fcb5df 100644 --- a/src/api/dict/index.ts +++ b/src/api/dict/index.ts @@ -78,9 +78,7 @@ export function deleteDictTypes(ids: string) { * * @param typeCode 字典类型编码 */ -export function listDictItemsByTypeCode( - typeCode: string -): AxiosPromise { +export function getDictionaries(typeCode: string): AxiosPromise { return request({ url: '/api/v1/dict/types/' + typeCode + '/items', method: 'get' diff --git a/src/permission.ts b/src/permission.ts index 10a6ccd..669d1a2 100644 --- a/src/permission.ts +++ b/src/permission.ts @@ -1,23 +1,27 @@ import router from '@/router'; -import useStore from '@/store'; +import { RouteRecordRaw } from 'vue-router'; +import { useUserStoreHook } from '@/store/modules/user'; +import { usePermissionStoreHook } from '@/store/modules/permission'; + import NProgress from 'nprogress'; import 'nprogress/nprogress.css'; -NProgress.configure({ showSpinner: false }); // 进度环显示/隐藏 +NProgress.configure({ showSpinner: false }); // 进度条 + +const permissionStore = usePermissionStoreHook(); // 白名单路由 const whiteList = ['/login']; router.beforeEach(async (to, from, next) => { NProgress.start(); - const { user, permission } = useStore(); - const hasToken = user.token; - if (hasToken) { + const userStore = useUserStoreHook(); + if (userStore.token) { // 登录成功,跳转到首页 if (to.path === '/login') { next({ path: '/' }); NProgress.done(); } else { - const hasGetUserInfo = user.roles.length > 0; + const hasGetUserInfo = userStore.roles.length > 0; if (hasGetUserInfo) { if (to.matched.length === 0) { from.name ? next({ name: from.name as any }) : next('/401'); @@ -26,23 +30,23 @@ router.beforeEach(async (to, from, next) => { } } else { try { - await user.getUserInfo(); - const roles = user.roles; - const accessRoutes: any = await permission.generateRoutes(roles); + const { roles } = await userStore.getInfo(); + const accessRoutes: RouteRecordRaw[] = + await permissionStore.generateRoutes(roles); accessRoutes.forEach((route: any) => { router.addRoute(route); }); next({ ...to, replace: true }); } catch (error) { // 移除 token 并跳转登录页 - await user.resetToken(); + await userStore.resetToken(); next(`/login?redirect=${to.path}`); NProgress.done(); } } } } else { - // 未登录可以访问白名单页面(登录页面) + // 未登录可以访问白名单页面 if (whiteList.indexOf(to.path) !== -1) { next(); } else { -- GitLab