Skip to content

Commit

Permalink
Merge pull request #128 from sj-distributor/enhance-user-list-tree
Browse files Browse the repository at this point in the history
Enhance user list tree
  • Loading branch information
chenmeiling12 authored Nov 13, 2024
2 parents a72874f + 7284ef1 commit 6d38814
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 53 deletions.
46 changes: 42 additions & 4 deletions web/src/pages/login/hook.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { useDebounceFn } from "ahooks";
import { message } from "antd";
import { App } from "antd";
import { useState } from "react";
import { useNavigate } from "react-router-dom";

import { useAuth } from "@/hooks/use-auth";
import { Login } from "@/services/api/login";
import { IUserInfo } from "@/services/dtos/login";
import { GetCurrentAccountPermission } from "@/services/api/user-permission";
import { FrontRolePermissionEnum } from "../user/user-permissions/user-newpermissions/props";

export const useAction = () => {
const { signIn } = useAuth();

const { message } = App.useApp();

const navigate = useNavigate();

const [userInfo, setUserInfo] = useState<IUserInfo>({
Expand All @@ -26,16 +30,26 @@ export const useAction = () => {
}));
};

const hanldeNoPermission = () => {
message.error("您没有访问後台权限");

localStorage.removeItem(
(window as any).appsettings?.tokenKey ?? "tokenKey"
);

localStorage.removeItem((window as any).appsettings?.userNameKey);
};

const onLogin = () => {
setLoginLoading(true);

if (
userInfo.userName.trim().length !== 0 &&
userInfo.password.trim().length !== 0
) {
Login(userInfo)
.then((res) => {
if (res) {
message.success("登录成功");
localStorage.setItem(
(window as any).appsettings?.tokenKey ?? "tokenKey",
res
Expand All @@ -45,9 +59,33 @@ export const useAction = () => {
userInfo.userName
);

navigate("/user/list");
GetCurrentAccountPermission()
.then((response) => {
if (
response.rolePermissionData.some((item) =>
item.permissions.some(
(permission) =>
permission.name ===
FrontRolePermissionEnum.CanSwitchCameraAiBackEnd
)
)
) {
message.success("登录成功");

navigate("/");

signIn(res);
signIn(
localStorage.getItem(
(window as any).appsettings?.tokenKey ?? "tokenKey"
) || ""
);
} else {
hanldeNoPermission();
}
})
.catch(() => {
hanldeNoPermission();
});
}
})
.catch(() => {
Expand Down
49 changes: 48 additions & 1 deletion web/src/pages/main/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
import { Outlet } from "react-router-dom";
import { useEffect, useState } from "react";
import { GetCurrentAccountPermission } from "@/services/api/user-permission";
import { Spin } from "antd";
import { FrontRolePermissionEnum } from "../user/user-permissions/user-newpermissions/props";

export const Container = () => {
const [permission, setPermission] = useState<{
isGetPermission: boolean;
hasSwitchCameraAiBackEnd: boolean;
}>({
isGetPermission: false,
hasSwitchCameraAiBackEnd: false,
});

useEffect(() => {
GetCurrentAccountPermission()
.then((response) => {
if (!response) return;

setPermission((prevState) => ({
...prevState,
isGetPermission: true,
hasSwitchCameraAiBackEnd: response.rolePermissionData.some((item) =>
item.permissions.some(
(permission) =>
permission.name ===
FrontRolePermissionEnum.CanSwitchCameraAiBackEnd
)
),
}));
})
.catch(() => {
setPermission({
isGetPermission: true,
hasSwitchCameraAiBackEnd: false,
});
});
}, []);

return (
<div className="pt-6 px-6 flex-1 flex flex-col bg-[#F6F8FC] overflow-hidden">
<Outlet />
{permission.isGetPermission ? (
permission.hasSwitchCameraAiBackEnd ? (
<Outlet />
) : (
<div className="flex justify-center font-semibold">暂无权限</div>
)
) : (
<div className="w-full h-screen flex items-center justify-center">
<Spin />
</div>
)}
</div>
);
};
31 changes: 24 additions & 7 deletions web/src/pages/user/user-lilst/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export const useAction = () => {
PageSize: 20,
});

const [disableTreeStaffId, setDisableTreeStaffId] = useState<string[]>([]);

const handelConfirmDeleteUsers = () => {
if (deleteUserKeys.length < 1) return;

Expand All @@ -73,12 +75,16 @@ export const useAction = () => {
deleteUserFun(data as string & string[])
.then(() => {
setIsRemoveUser(false);

handelGetUserList({
PageIndex: 1,
PageSize: userListData.PageSize,
Keyword: filterKeyword,
Status: userListData.Status,
});

getAllUserList();

message.success(t(KEYS.REMOVE_USER_OK, source));
})
.catch((err) => {
Expand All @@ -104,6 +110,8 @@ export const useAction = () => {
Status: userListData.Status,
});

getAllUserList();

loading = false;
} catch (err) {
loading = false;
Expand Down Expand Up @@ -143,6 +151,21 @@ export const useAction = () => {
},
});

const getAllUserList = () => {
GetUserList({
PageIndex: 1,
PageSize: 2147483647,
}).then((res) => {
setDisableTreeStaffId(
(res?.userProfiles ?? []).map((item) => item.staffId)
);
});
};

useEffect(() => {
getAllUserList();
}, []);

useEffect(() => {
handelGetUserList({
PageIndex: 1,
Expand All @@ -152,13 +175,6 @@ export const useAction = () => {
});
}, [filterKeyword]);

useEffect(() => {
handelGetUserList({
PageIndex: userListData.PageIndex,
PageSize: userListData.PageSize,
});
}, []);

const handelGetUserList = (data: IGetUserListRequest) => {
setUserListData((prev) => ({
...prev,
Expand Down Expand Up @@ -224,5 +240,6 @@ export const useAction = () => {
language,
handelGetUserList,
filterKeyword,
disableTreeStaffId,
};
};
4 changes: 4 additions & 0 deletions web/src/pages/user/user-lilst/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IUserDataItem, UserStatus } from "@/services/dtos/user";
import { TransferTree } from "../user-permissions/tranfer-tree";
import { BackGroundRolePermissionEnum } from "../user-permissions/user-newpermissions/props";
import { useAction } from "./hook";
import { TreeTypeEnum } from "@/services/dtos/tree";

export const UserList = () => {
const {
Expand Down Expand Up @@ -51,6 +52,7 @@ export const UserList = () => {
language,
handelGetUserList,
filterKeyword,
disableTreeStaffId,
} = useAction();

const columns: TableProps<IUserDataItem>["columns"] = [
Expand Down Expand Up @@ -324,6 +326,8 @@ export const UserList = () => {
setIsModelOpen={setIsAddUser}
handelGetSelectedUsers={handelGetSelectedUsers}
staffIdSource={0}
disableTreeStaffId={disableTreeStaffId}
type={TreeTypeEnum.UserList}
/>

<CustomModal
Expand Down
29 changes: 20 additions & 9 deletions web/src/pages/user/user-permissions/distribute/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export const useAction = () => {
const [userByRoleIdAllData, setUserByRoleIdAllData] =
useState<IUserByRoleIdResponse>(initialUserByRoleIdData);

const [disableTreeStaffId, setDisableTreeStaffId] = useState<string[]>([]);

const filterKeyword = useDebounce(searchValue, { wait: 500 });

const onSelectedAllRow = (selected: boolean) => {
Expand Down Expand Up @@ -98,6 +100,8 @@ export const useAction = () => {
KeyWord: filterKeyword,
RoleId: id,
});

getAllRolesUsersList();
})
.catch((error) => message.error((error as Error).message));
};
Expand All @@ -124,6 +128,8 @@ export const useAction = () => {
RoleId: id,
});

getAllRolesUsersList();

loading = false;
} catch (err) {
loading = false;
Expand All @@ -145,13 +151,22 @@ export const useAction = () => {
.finally(() => setIsTableLoading(false));
};

const getAllRolesUsersList = (prams: IRoleIdRequestParams) => {
GetRolesUserByRoleId(prams)
const getAllRolesUsersList = () => {
GetRolesUserByRoleId({
PageIndex: 1,
PageSize: 2147483647,
RoleId: id,
})
.then((res) => {
setDisableTreeStaffId(
(res?.roleUsers ?? []).map((item) => String(item.userId))
);

if (res) setUserByRoleIdAllData(res ?? initialUserByRoleIdData);
})
.catch(() => {
navigate("/user/permissions");

message.error("獲取全部用戶數據失敗");
});
};
Expand All @@ -166,13 +181,8 @@ export const useAction = () => {
}, [pageDto.pageIndex, pageDto.pageSize, filterKeyword]);

useEffect(() => {
getAllRolesUsersList({
PageIndex: 1,
PageSize: 2147483647,
KeyWord: filterKeyword,
RoleId: id,
});
}, [filterKeyword]);
getAllRolesUsersList();
}, []);

useEffect(() => {
const newSelectedRowKeys = selectedRows.map((x) => x.id);
Expand All @@ -184,6 +194,7 @@ export const useAction = () => {
t,
source,
isDeletePermissions,
disableTreeStaffId,
setIsDeletePermissions,
isBatchDeleteUser,
setIsBatchDeleteUser,
Expand Down
8 changes: 4 additions & 4 deletions web/src/pages/user/user-permissions/distribute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dayjs from "dayjs";
import { Trans } from "react-i18next";

import KEYS from "@/i18n/language/keys/user-permissions-keys";
import { HierarchyStaffIdSourceEnum } from "@/services/dtos/tree";
import { HierarchyStaffIdSourceEnum, TreeTypeEnum } from "@/services/dtos/tree";
import { IUserByRoleIdData } from "@/services/dtos/user-permission";

import { OperateConfirmModal } from "../operate-confirm";
Expand All @@ -23,6 +23,7 @@ export const UserDistribute = () => {
const {
t,
source,
disableTreeStaffId,
isDeletePermissions,
setIsDeletePermissions,
isBatchDeleteUser,
Expand Down Expand Up @@ -209,9 +210,8 @@ export const UserDistribute = () => {
setIsModelOpen={setIsAddNewUser}
handelGetSelectedUsers={handelGetSelectedUsers}
staffIdSource={HierarchyStaffIdSourceEnum.IntegerStaffId}
disabledKeys={userByRoleIdData.roleUsers?.map((item) =>
String(item.userId)
)}
type={TreeTypeEnum.UserPermission}
disableTreeStaffId={disableTreeStaffId}
/>
</div>
);
Expand Down
Loading

0 comments on commit 6d38814

Please sign in to comment.