refactor: pinia setup store组合式函数写法
Former-commit-id: 03faa4898c2633666374b895336da32297d2f54a
This commit is contained in:
@@ -78,9 +78,7 @@ export function deleteDictTypes(ids: string) {
|
|||||||
*
|
*
|
||||||
* @param typeCode 字典类型编码
|
* @param typeCode 字典类型编码
|
||||||
*/
|
*/
|
||||||
export function listDictItemsByTypeCode(
|
export function getDictionaries(typeCode: string): AxiosPromise<OptionType[]> {
|
||||||
typeCode: string
|
|
||||||
): AxiosPromise<OptionType[]> {
|
|
||||||
return request({
|
return request({
|
||||||
url: '/api/v1/dict/types/' + typeCode + '/items',
|
url: '/api/v1/dict/types/' + typeCode + '/items',
|
||||||
method: 'get'
|
method: 'get'
|
||||||
|
|||||||
@@ -1,23 +1,27 @@
|
|||||||
import router from '@/router';
|
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 from 'nprogress';
|
||||||
import 'nprogress/nprogress.css';
|
import 'nprogress/nprogress.css';
|
||||||
NProgress.configure({ showSpinner: false }); // 进度环显示/隐藏
|
NProgress.configure({ showSpinner: false }); // 进度条
|
||||||
|
|
||||||
|
const permissionStore = usePermissionStoreHook();
|
||||||
|
|
||||||
// 白名单路由
|
// 白名单路由
|
||||||
const whiteList = ['/login'];
|
const whiteList = ['/login'];
|
||||||
|
|
||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
NProgress.start();
|
NProgress.start();
|
||||||
const { user, permission } = useStore();
|
const userStore = useUserStoreHook();
|
||||||
const hasToken = user.token;
|
if (userStore.token) {
|
||||||
if (hasToken) {
|
|
||||||
// 登录成功,跳转到首页
|
// 登录成功,跳转到首页
|
||||||
if (to.path === '/login') {
|
if (to.path === '/login') {
|
||||||
next({ path: '/' });
|
next({ path: '/' });
|
||||||
NProgress.done();
|
NProgress.done();
|
||||||
} else {
|
} else {
|
||||||
const hasGetUserInfo = user.roles.length > 0;
|
const hasGetUserInfo = userStore.roles.length > 0;
|
||||||
if (hasGetUserInfo) {
|
if (hasGetUserInfo) {
|
||||||
if (to.matched.length === 0) {
|
if (to.matched.length === 0) {
|
||||||
from.name ? next({ name: from.name as any }) : next('/401');
|
from.name ? next({ name: from.name as any }) : next('/401');
|
||||||
@@ -26,23 +30,23 @@ router.beforeEach(async (to, from, next) => {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
await user.getUserInfo();
|
const { roles } = await userStore.getInfo();
|
||||||
const roles = user.roles;
|
const accessRoutes: RouteRecordRaw[] =
|
||||||
const accessRoutes: any = await permission.generateRoutes(roles);
|
await permissionStore.generateRoutes(roles);
|
||||||
accessRoutes.forEach((route: any) => {
|
accessRoutes.forEach((route: any) => {
|
||||||
router.addRoute(route);
|
router.addRoute(route);
|
||||||
});
|
});
|
||||||
next({ ...to, replace: true });
|
next({ ...to, replace: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 移除 token 并跳转登录页
|
// 移除 token 并跳转登录页
|
||||||
await user.resetToken();
|
await userStore.resetToken();
|
||||||
next(`/login?redirect=${to.path}`);
|
next(`/login?redirect=${to.path}`);
|
||||||
NProgress.done();
|
NProgress.done();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 未登录可以访问白名单页面(登录页面)
|
// 未登录可以访问白名单页面
|
||||||
if (whiteList.indexOf(to.path) !== -1) {
|
if (whiteList.indexOf(to.path) !== -1) {
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user