mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-11-09 13:53:38 +08:00
refactor: permission (#357)
* refactor: permission * chore: update * chore: update * chore: update * chore: update * chore: update * chore: update * chore: update * chore: update * chore: update * fix: 修复`mix`混合模式导航在生产环境左侧菜单一定机率不显示的问题 * chore: update * chore: update * chore: update * chore: update * chore: update * chore: update * chore: update * chore: update
This commit is contained in:
@@ -2,9 +2,7 @@ import { defineStore } from "pinia";
|
||||
import { store } from "/@/store";
|
||||
import { cacheType } from "./types";
|
||||
import { constantMenus } from "/@/router";
|
||||
import { cloneDeep } from "lodash-unified";
|
||||
import { RouteConfigs } from "/@/layout/types";
|
||||
import { ascending, filterTree } from "/@/router/utils";
|
||||
import { ascending, filterTree, filterNoPermissionTree } from "/@/router/utils";
|
||||
|
||||
export const usePermissionStore = defineStore({
|
||||
id: "pure-permission",
|
||||
@@ -13,40 +11,15 @@ export const usePermissionStore = defineStore({
|
||||
constantMenus,
|
||||
// 整体路由生成的菜单(静态、动态)
|
||||
wholeMenus: [],
|
||||
// 深拷贝一个菜单树,与导航菜单不突出
|
||||
menusTree: [],
|
||||
buttonAuth: [],
|
||||
// 缓存页面keepAlive
|
||||
cachePageList: []
|
||||
}),
|
||||
actions: {
|
||||
/** 获取异步路由菜单 */
|
||||
asyncActionRoutes(routes) {
|
||||
if (this.wholeMenus.length > 0) return;
|
||||
this.wholeMenus = filterTree(
|
||||
ascending(this.constantMenus.concat(routes))
|
||||
);
|
||||
|
||||
this.menusTree = cloneDeep(
|
||||
/** 组装整体路由生成的菜单 */
|
||||
handleWholeMenus(routes: any[]) {
|
||||
this.wholeMenus = filterNoPermissionTree(
|
||||
filterTree(ascending(this.constantMenus.concat(routes)))
|
||||
);
|
||||
|
||||
const getButtonAuth = (arrRoutes: Array<RouteConfigs>) => {
|
||||
if (!arrRoutes || !arrRoutes.length) return;
|
||||
arrRoutes.forEach((v: RouteConfigs) => {
|
||||
if (v.meta && v.meta.authority) {
|
||||
this.buttonAuth.push(...v.meta.authority);
|
||||
}
|
||||
if (v.children) {
|
||||
getButtonAuth(v.children);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
getButtonAuth(this.wholeMenus);
|
||||
},
|
||||
async changeSetting(routes) {
|
||||
await this.asyncActionRoutes(routes);
|
||||
},
|
||||
cacheOperate({ mode, name }: cacheType) {
|
||||
switch (mode) {
|
||||
@@ -64,8 +37,6 @@ export const usePermissionStore = defineStore({
|
||||
/** 清空缓存页面 */
|
||||
clearAllCachePage() {
|
||||
this.wholeMenus = [];
|
||||
this.menusTree = [];
|
||||
this.buttonAuth = [];
|
||||
this.cachePageList = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,8 +37,8 @@ export type setType = {
|
||||
};
|
||||
|
||||
export type userType = {
|
||||
token: string;
|
||||
name?: string;
|
||||
username?: string;
|
||||
roles?: Array<string>;
|
||||
verifyCode?: string;
|
||||
currentPage?: number;
|
||||
};
|
||||
|
||||
@@ -1,55 +1,56 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { store } from "/@/store";
|
||||
import { userType } from "./types";
|
||||
import { router } from "/@/router";
|
||||
import { routerArrays } from "/@/layout/types";
|
||||
import { router, resetRouter } from "/@/router";
|
||||
import { storageSession } from "@pureadmin/utils";
|
||||
import { getLogin, refreshToken } from "/@/api/user";
|
||||
import { getToken, setToken, removeToken } from "/@/utils/auth";
|
||||
import { getLogin, refreshTokenApi } from "/@/api/user";
|
||||
import { UserResult, RefreshTokenResult } from "/@/api/user";
|
||||
import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
|
||||
|
||||
const data = getToken();
|
||||
let token = "";
|
||||
let name = "";
|
||||
if (data) {
|
||||
const dataJson = JSON.parse(data);
|
||||
if (dataJson) {
|
||||
token = dataJson?.accessToken;
|
||||
name = dataJson?.name ?? "admin";
|
||||
}
|
||||
}
|
||||
import {
|
||||
type DataInfo,
|
||||
setToken,
|
||||
removeToken,
|
||||
sessionKey
|
||||
} from "/@/utils/auth";
|
||||
|
||||
export const useUserStore = defineStore({
|
||||
id: "pure-user",
|
||||
state: (): userType => ({
|
||||
token,
|
||||
name,
|
||||
username:
|
||||
storageSession.getItem<DataInfo<number>>(sessionKey)?.username ?? "",
|
||||
// 页面级别权限
|
||||
roles: storageSession.getItem<DataInfo<number>>(sessionKey)?.roles ?? [],
|
||||
// 前端生成的验证码(按实际需求替换)
|
||||
verifyCode: "",
|
||||
// 登录显示组件判断 0:登录 1:手机登录 2:二维码登录 3:注册 4:忘记密码,默认0:登录
|
||||
// 判断登录页面显示哪个组件(0:登录(默认)、1:手机登录、2:二维码登录、3:注册、4:忘记密码)
|
||||
currentPage: 0
|
||||
}),
|
||||
actions: {
|
||||
SET_TOKEN(token) {
|
||||
this.token = token;
|
||||
/** 存储用户名 */
|
||||
SET_USERNAME(username: string) {
|
||||
this.username = username;
|
||||
},
|
||||
SET_NAME(name) {
|
||||
this.name = name;
|
||||
/** 存储角色 */
|
||||
SET_ROLES(roles: Array<string>) {
|
||||
this.roles = roles;
|
||||
},
|
||||
SET_VERIFYCODE(verifyCode) {
|
||||
/** 存储前端生成的验证码 */
|
||||
SET_VERIFYCODE(verifyCode: string) {
|
||||
this.verifyCode = verifyCode;
|
||||
},
|
||||
SET_CURRENTPAGE(value) {
|
||||
/** 存储登录页面显示哪个组件 */
|
||||
SET_CURRENTPAGE(value: number) {
|
||||
this.currentPage = value;
|
||||
},
|
||||
/** 登入 */
|
||||
async loginByUsername(data) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
return new Promise<UserResult>((resolve, reject) => {
|
||||
getLogin(data)
|
||||
.then(data => {
|
||||
if (data) {
|
||||
setToken(data);
|
||||
resolve();
|
||||
setToken(data.data);
|
||||
resolve(data);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
@@ -57,23 +58,28 @@ export const useUserStore = defineStore({
|
||||
});
|
||||
});
|
||||
},
|
||||
/** 登出 清空缓存 */
|
||||
/** 前端登出(不调用接口) */
|
||||
logOut() {
|
||||
this.token = "";
|
||||
this.name = "";
|
||||
this.username = "";
|
||||
this.roles = [];
|
||||
removeToken();
|
||||
storageSession.clear();
|
||||
useMultiTagsStoreHook().handleTags("equal", routerArrays);
|
||||
router.push("/login");
|
||||
useMultiTagsStoreHook().handleTags("equal", [...routerArrays]);
|
||||
resetRouter();
|
||||
},
|
||||
/** 刷新token */
|
||||
async refreshToken(data) {
|
||||
removeToken();
|
||||
return refreshToken(data).then(data => {
|
||||
if (data) {
|
||||
setToken(data);
|
||||
return data;
|
||||
}
|
||||
/** 刷新`token` */
|
||||
async handRefreshToken(data) {
|
||||
return new Promise<RefreshTokenResult>((resolve, reject) => {
|
||||
refreshTokenApi(data)
|
||||
.then(data => {
|
||||
if (data) {
|
||||
setToken(data.data);
|
||||
resolve(data);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user