Просмотр исходного кода

前端获取当前用户机构信息,包括一对多机构的信息

kenny 4 лет назад
Родитель
Сommit
419c43abdf

+ 4 - 0
Vben2/src/api/sys/admin.ts

@@ -41,6 +41,7 @@ enum Api {
   AddOrg = '/sysOrg/add',
   DeleteOrg = '/sysOrg/delete',
   UpdateOrg = '/sysOrg/update',
+  UserOwnOrgInfoList = '/sysOrg/userOwnOrgInfo',
 
   // 职位接口
   PosList = '/sysPos/list',
@@ -220,6 +221,9 @@ export const addOrg = (params: any) => defHttp.post({ url: Api.AddOrg, params })
 export const deleteOrg = (id: number) => defHttp.post({ url: Api.DeleteOrg, params: { id } });
 // 更新机构
 export const updateOrg = (params: any) => defHttp.post({ url: Api.UpdateOrg, params });
+// 获取用户拥有机构信息列表
+export const userOwnOrgInfoList = (params: any) =>
+  defHttp.get<any>({ url: Api.UserOwnOrgInfoList + '/' + params });
 
 ////////// 职位管理接口 //////////
 // 获取职位列表

+ 4 - 0
Vben2/src/api/sys/model/userModel.ts

@@ -35,4 +35,8 @@ export interface GetUserInfoModel {
   avatar: string;
   // 介绍
   desc?: string;
+  // 机构id
+  orgId: string | number;
+  //机构名称
+  orgName: string;
 }

+ 5 - 0
Vben2/src/router/guard/permissionGuard.ts

@@ -29,6 +29,11 @@ export function createPermissionGuard(router: Router) {
       return;
     }
 
+    //缓存当前用户部门信息
+    if (userStore.orgInfo.length === 0 && userStore.userInfo) {
+      const userid = Number(userStore.getUserInfo.userId);
+      await userStore.getOrgInfoAction(userid);
+    }
     //缓存所有常量数据
     if (userStore.constSelectorWithOptions.length === 0) {
       await userStore.getAllConstSelectorWithOptionsAction();

+ 24 - 2
Vben2/src/store/modules/user.ts

@@ -1,4 +1,4 @@
-import type { UserInfo } from '/#/store';
+import type { UserInfo, OrgInfo } from '/#/store';
 import type { ErrorMessageMode } from '/#/axios';
 import { defineStore } from 'pinia';
 import { store } from '/@/store';
@@ -8,7 +8,7 @@ import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
 import { getAuthCache, setAuthCache } from '/@/utils/auth';
 import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel';
 import { doLogout, getUserInfo, loginApi } from '/@/api/sys/user';
-import { getAllConstSelectorWithOptions } from '/@/api/sys/admin';
+import { getAllConstSelectorWithOptions, userOwnOrgInfoList } from '/@/api/sys/admin';
 import { useI18n } from '/@/hooks/web/useI18n';
 import { useMessage } from '/@/hooks/web/useMessage';
 import { router } from '/@/router';
@@ -20,6 +20,7 @@ import { h } from 'vue';
 
 interface UserState {
   userInfo: Nullable<UserInfo>;
+  orgInfo: OrgInfo[];
   token?: string;
   roleList: RoleEnum[];
   sessionTimeout?: boolean;
@@ -32,6 +33,8 @@ export const useUserStore = defineStore({
   state: (): UserState => ({
     // user info
     userInfo: null,
+    // org info
+    orgInfo: [],
     // token
     token: undefined,
     // roleList
@@ -62,6 +65,9 @@ export const useUserStore = defineStore({
     getAllConstSelectorWithOptions(): any[] {
       return this.constSelectorWithOptions || getAllConstSelectorWithOptions();
     },
+    getOrgInfo(): OrgInfo[] {
+      return this.orgInfo || [];
+    },
   },
   actions: {
     setToken(info: string | undefined) {
@@ -180,6 +186,22 @@ export const useUserStore = defineStore({
       this.constSelectorWithOptions = data;
       // setAuthCache(USER_INFO_KEY, data);
     },
+    async getOrgInfoAction(userid: number) {
+      const data = await userOwnOrgInfoList(userid);
+
+      let obj: OrgInfo;
+      const list: OrgInfo[] = [];
+      for (let i = 0; i < data.length; i++) {
+        obj = {
+          orgId: data[i].id,
+          code: data[i].code,
+          name: data[i].name,
+          status: data[i].status,
+        };
+        list.push(obj);
+      }
+      this.orgInfo = list;
+    },
   },
 });