数据结构重构优化

This commit is contained in:
unknown 2023-08-07 09:23:49 +08:00
parent cf7feffe27
commit 8c52461558
3 changed files with 30 additions and 4 deletions

View File

@ -51,7 +51,7 @@ const DepartmentPage = () => {
const getData = () => { const getData = () => {
setLoading(true); setLoading(true);
department.departmentList().then((res: any) => { department.departmentList().then((res: any) => {
const departments = res.data.departments; const departments: DepartmentsBoxModel = res.data.departments;
if (JSON.stringify(departments) !== "{}") { if (JSON.stringify(departments) !== "{}") {
const new_arr: Option[] = checkArr(departments, 0); const new_arr: Option[] = checkArr(departments, 0);
setTreeData(new_arr); setTreeData(new_arr);
@ -61,7 +61,7 @@ const DepartmentPage = () => {
}); });
}; };
const checkArr = (departments: any[], id: number) => { const checkArr = (departments: DepartmentsBoxModel, id: number) => {
const arr = []; const arr = [];
for (let i = 0; i < departments[id].length; i++) { for (let i = 0; i < departments[id].length; i++) {
if (!departments[departments[id][i].id]) { if (!departments[departments[id][i].id]) {

View File

@ -50,7 +50,7 @@ const ResourceCategoryPage = () => {
const getData = () => { const getData = () => {
setLoading(true); setLoading(true);
resourceCategory.resourceCategoryList().then((res: any) => { resourceCategory.resourceCategoryList().then((res: any) => {
const categories = res.data.categories; const categories: CategoriesBoxModel = res.data.categories;
if (JSON.stringify(categories) !== "{}") { if (JSON.stringify(categories) !== "{}") {
const new_arr: Option[] = checkArr(categories, 0); const new_arr: Option[] = checkArr(categories, 0);
setTreeData(new_arr); setTreeData(new_arr);
@ -59,7 +59,7 @@ const ResourceCategoryPage = () => {
}); });
}; };
const checkArr = (categories: any[], id: number) => { const checkArr = (categories: CategoriesBoxModel, id: number) => {
const arr = []; const arr = [];
for (let i = 0; i < categories[id].length; i++) { for (let i = 0; i < categories[id].length; i++) {
if (!categories[categories[id][i].id]) { if (!categories[categories[id][i].id]) {

26
src/playedu.d.ts vendored
View File

@ -47,6 +47,18 @@ declare global {
title: string; title: string;
} }
interface CategoriesBoxModel {
[key: number]: CategoriesItemModel[];
}
interface CategoriesItemModel {
id: number;
name: string;
parent_chain: string;
parent_id: number;
sort: number;
}
interface CategoriesModel { interface CategoriesModel {
[key: number]: string; [key: number]: string;
} }
@ -62,6 +74,20 @@ declare global {
interface CategoryIdsModel { interface CategoryIdsModel {
[key: number]: number[]; [key: number]: number[];
} }
interface DepartmentsBoxModel {
[key: number]: DepartmentsItemModel[];
}
interface DepartmentsItemModel {
created_at: string;
id: number;
name: string;
parent_chain: string;
parent_id: number;
sort: number;
updated_at: string;
}
} }
export {}; export {};