diff --git a/src/compenents/tree-category/index.tsx b/src/compenents/tree-category/index.tsx index bc01a50..bbaf0d3 100644 --- a/src/compenents/tree-category/index.tsx +++ b/src/compenents/tree-category/index.tsx @@ -1,6 +1,6 @@ import { Tree } from "antd"; import { useState, useEffect } from "react"; -import { resourceCategory } from "../../api/index"; +import { useSelector } from "react-redux"; interface Option { key: string | number; @@ -19,6 +19,9 @@ export const TreeCategory = (props: PropInterface) => { const [treeData, setTreeData] = useState([]); const [loading, setLoading] = useState(true); const [selectKey, setSelectKey] = useState([]); + const resourceCategories = useSelector( + (state: any) => state.systemConfig.value.resourceCategories + ); useEffect(() => { if (props.selected && props.selected.length > 0) { @@ -27,21 +30,17 @@ export const TreeCategory = (props: PropInterface) => { }, [props.selected]); useEffect(() => { - resourceCategory.resourceCategoryList().then((res: any) => { - const categories: CategoriesBoxModel = res.data.categories; - if (JSON.stringify(categories) !== "{}") { - const new_arr: Option[] = checkArr(categories, 0); - if (props.type === "no-cate") { - new_arr.unshift({ - key: 0, - title: 未分类, - }); - } - - setTreeData(new_arr); + if (JSON.stringify(resourceCategories) !== "{}") { + const new_arr: Option[] = checkArr(resourceCategories, 0); + if (props.type === "no-cate") { + new_arr.unshift({ + key: 0, + title: 未分类, + }); } - }); - }, []); + setTreeData(new_arr); + } + }, [resourceCategories]); const checkArr = (categories: CategoriesBoxModel, id: number) => { const arr = []; diff --git a/src/compenents/tree-department/index.tsx b/src/compenents/tree-department/index.tsx index 3bba14d..098bb4f 100644 --- a/src/compenents/tree-department/index.tsx +++ b/src/compenents/tree-department/index.tsx @@ -1,6 +1,7 @@ import { Button, Input, message, Tree } from "antd"; import { useState, useEffect } from "react"; import { department } from "../../api/index"; +import { useSelector } from "react-redux"; interface Option { key: string | number; @@ -22,6 +23,9 @@ export const TreeDepartment = (props: PropInterface) => { const [loading, setLoading] = useState(true); const [selectKey, setSelectKey] = useState([]); const [userTotal, setUserTotal] = useState(0); + const localDepartments = useSelector( + (state: any) => state.systemConfig.value.departments + ); useEffect(() => { if (props.selected && props.selected.length > 0) { @@ -31,18 +35,30 @@ export const TreeDepartment = (props: PropInterface) => { useEffect(() => { setLoading(true); - department.departmentList().then((res: any) => { - const departments: DepartmentsBoxModel = res.data.departments; - const departCount: DepIdsModel = res.data.dep_user_count; - setUserTotal(res.data.user_total); - if (JSON.stringify(departments) !== "{}") { - if (props.showNum) { + if (props.showNum) { + department.departmentList().then((res: any) => { + const departments: DepartmentsBoxModel = res.data.departments; + const departCount: DepIdsModel = res.data.dep_user_count; + setUserTotal(res.data.user_total); + if (JSON.stringify(departments) !== "{}") { const new_arr: any[] = checkNewArr(departments, 0, departCount); setTreeData(new_arr); } else { - const new_arr: any[] = checkArr(departments, 0); + const new_arr: Option[] = [ + { + key: "", + title: "全部", + children: [], + }, + ]; setTreeData(new_arr); } + setLoading(false); + }); + } else { + if (JSON.stringify(localDepartments) !== "{}") { + const new_arr: any[] = checkArr(localDepartments, 0); + setTreeData(new_arr); } else { const new_arr: Option[] = [ { @@ -54,8 +70,8 @@ export const TreeDepartment = (props: PropInterface) => { setTreeData(new_arr); } setLoading(false); - }); - }, [props.refresh]); + } + }, [props.refresh, localDepartments]); const checkNewArr = ( departments: DepartmentsBoxModel, diff --git a/src/pages/init/index.tsx b/src/pages/init/index.tsx index b71db95..8bdbf1d 100644 --- a/src/pages/init/index.tsx +++ b/src/pages/init/index.tsx @@ -27,6 +27,8 @@ const InitPage = (props: Props) => { systemH5Url: props.configData["system.h5_url"], memberDefaultAvatar: props.configData["member.default_avatar"], courseDefaultThumbs: props.configData["default.course_thumbs"], + departments: props.configData["departments"], + resourceCategories: props.configData["resource_categories"], }; dispatch(saveConfigAction(config)); } diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index a7918bd..d687553 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -67,6 +67,8 @@ const LoginPage = () => { systemH5Url: res.data["system.h5_url"], memberDefaultAvatar: res.data["member.default_avatar"], courseDefaultThumbs: res.data["default.course_thumbs"], + departments: res.data["departments"], + resourceCategories: res.data["resource_categories"], }; dispatch(saveConfigAction(data)); }; diff --git a/src/pages/system/config/index.tsx b/src/pages/system/config/index.tsx index 6aa754a..4145c82 100644 --- a/src/pages/system/config/index.tsx +++ b/src/pages/system/config/index.tsx @@ -259,6 +259,8 @@ const SystemConfigPage = () => { systemH5Url: res.data["system.h5_url"], memberDefaultAvatar: res.data["member.default_avatar"], courseDefaultThumbs: res.data["default.course_thumbs"], + departments: res.data["departments"], + resourceCategories: res.data["resource_categories"], }; dispatch(saveConfigAction(data)); }); diff --git a/src/store/system/systemConfigSlice.ts b/src/store/system/systemConfigSlice.ts index f4a37c7..ce4facb 100644 --- a/src/store/system/systemConfigSlice.ts +++ b/src/store/system/systemConfigSlice.ts @@ -9,6 +9,8 @@ type SystemConfigStoreInterface = { systemName?: string; memberDefaultAvatar?: string; courseDefaultThumbs?: string[]; + departments: any; + resourceCategories: any; }; const systemConfigSlice = createSlice({