diff --git a/src/api/department.ts b/src/api/department.ts index 2850091..9a462c0 100644 --- a/src/api/department.ts +++ b/src/api/department.ts @@ -36,3 +36,17 @@ export function updateDepartment( export function destroyDepartment(id: number) { return client.destroy(`/backend/v1/department/${id}`); } + +export function dropSameClass(ids: number[]) { + return client.put(`/backend/v1/department/update/sort`, { + ids: ids, + }); +} + +export function dropDiffClass(id: number, parent_id: number, ids: number[]) { + return client.put(`/backend/v1/department/update/parent`, { + id: id, + parent_id: parent_id, + ids: ids, + }); +} diff --git a/src/api/resource-category.ts b/src/api/resource-category.ts index e52694a..8802f5b 100644 --- a/src/api/resource-category.ts +++ b/src/api/resource-category.ts @@ -40,3 +40,17 @@ export function updateResourceCategory( export function destroyResourceCategory(id: number) { return client.destroy(`/backend/v1/resource-category/${id}`); } + +export function dropSameClass(ids: number[]) { + return client.put(`/backend/v1/resource-category/update/sort`, { + ids: ids, + }); +} + +export function dropDiffClass(id: number, parent_id: number, ids: number[]) { + return client.put(`/backend/v1/resource-category/update/parent`, { + id: id, + parent_id: parent_id, + ids: ids, + }); +} diff --git a/src/pages/department/index.tsx b/src/pages/department/index.tsx index 5ba4437..a0c5a60 100644 --- a/src/pages/department/index.tsx +++ b/src/pages/department/index.tsx @@ -192,6 +192,13 @@ export const DepartmentPage: React.FC = () => { } }; const data = [...treeData]; + let isTop = false; + + for (let i = 0; i < data.length; i++) { + if (data[i].key === dragKey) { + isTop = true; + } + } // Find dragObject let dragObj: DataNode; @@ -233,6 +240,46 @@ export const DepartmentPage: React.FC = () => { } } setTreeData(data); + submitDrop(isTop, data, dragKey); + }; + + const submitDrop = (isTop: boolean, data: any, key: any) => { + let result = checkDropArr(data, key); + if (result) { + if (isTop) { + department.dropSameClass(result.ids).then((res: any) => { + console.log("ok"); + }); + } else { + submitChildDrop(key, 0, result); + } + } + }; + + const submitChildDrop = (key: any, pid: any, ids: any) => { + department.dropDiffClass(key, pid, ids.ids).then((res: any) => { + console.log("ok"); + }); + }; + + const checkDropArr = (data: any, key: any) => { + let ids = []; + let isSame = false; + for (let i = 0; i < data.length; i++) { + ids.push(data[i].key); + if (data[i].key === key) { + isSame = true; + } + if (data[i].children) { + let res: any = checkDropArr(data[i].children, key); + if (res) { + submitChildDrop(key, data[i].key, res); + } + } + } + if (isSame) { + return { key, ids }; + } }; return ( diff --git a/src/pages/resource/resource-category/index.tsx b/src/pages/resource/resource-category/index.tsx index ca6b926..643a821 100644 --- a/src/pages/resource/resource-category/index.tsx +++ b/src/pages/resource/resource-category/index.tsx @@ -172,7 +172,6 @@ export const ResourceCategoryPage: React.FC = () => { const dropPos = info.node.pos.split("-"); const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]); - const loop = ( data: DataNode[], key: React.Key, @@ -188,6 +187,13 @@ export const ResourceCategoryPage: React.FC = () => { } }; const data = [...treeData]; + let isTop = false; + + for (let i = 0; i < data.length; i++) { + if (data[i].key === dragKey) { + isTop = true; + } + } // Find dragObject let dragObj: DataNode; @@ -229,6 +235,46 @@ export const ResourceCategoryPage: React.FC = () => { } } setTreeData(data); + submitDrop(isTop, data, dragKey); + }; + + const submitDrop = (isTop: boolean, data: any, key: any) => { + let result = checkDropArr(data, key); + if (result) { + if (isTop) { + resourceCategory.dropSameClass(result.ids).then((res: any) => { + console.log("ok"); + }); + } else { + submitChildDrop(key, 0, result); + } + } + }; + + const submitChildDrop = (key: any, pid: any, ids: any) => { + resourceCategory.dropDiffClass(key, pid, ids.ids).then((res: any) => { + console.log("ok"); + }); + }; + + const checkDropArr = (data: any, key: any) => { + let ids = []; + let isSame = false; + for (let i = 0; i < data.length; i++) { + ids.push(data[i].key); + if (data[i].key === key) { + isSame = true; + } + if (data[i].children) { + let res: any = checkDropArr(data[i].children, key); + if (res) { + submitChildDrop(key, data[i].key, res); + } + } + } + if (isSame) { + return { key, ids }; + } }; return (