|
@@ -35,74 +35,59 @@ export const postListTreeWithUser = () => {
|
|
export const treeDept = ref([]);
|
|
export const treeDept = ref([]);
|
|
export const postListTreeWithUserApi = async () => {
|
|
export const postListTreeWithUserApi = async () => {
|
|
const res = await postListTreeWithUser();
|
|
const res = await postListTreeWithUser();
|
|
- treeDept.value = transformData(res.data);
|
|
|
|
- console.log("treeDept.value", transformData(res.data));
|
|
|
|
- console.log("treeDept", res.data);
|
|
|
|
|
|
+ let treeList = res.data.map(item => {
|
|
|
|
+ return {
|
|
|
|
+ childrenRes: item.childrenRes,
|
|
|
|
+ childrenUserRes: item.childrenUserRes,
|
|
|
|
+ userCodeNew: item.deptCode,
|
|
|
|
+ userNameNew: item.deptName
|
|
|
|
+ };
|
|
|
|
+ });
|
|
|
|
+ treeDept.value = transformData(treeList);
|
|
|
|
+ // console.log("treeDept.value", transformData(treeList));
|
|
|
|
+ // console.log("treeDept", res.data);
|
|
};
|
|
};
|
|
-// const transformData = data => {
|
|
|
|
-// return data.map(item => {
|
|
|
|
-// // 新的结构
|
|
|
|
-// const newItem = {
|
|
|
|
-// userName: item.deptName || item.realName, // 使用deptName或userName
|
|
|
|
-// userCode: item.deptCode || item.userCode, // 使用deptCode或userCode
|
|
|
|
-// children: [] // 初始化children数组
|
|
|
|
-// };
|
|
|
|
|
|
+function transformData(
|
|
|
|
+ data,
|
|
|
|
+ newDeptCodeKey = "userCodeNew",
|
|
|
|
+ newDeptNameKey = "userNameNew"
|
|
|
|
+) {
|
|
|
|
+ return data.map(item => {
|
|
|
|
+ const newItem = { ...item };
|
|
|
|
|
|
-// // 合并childrenRes和childrenUserRes
|
|
|
|
-// const childrenRes = item.childrenRes ? transformData(item.childrenRes) : [];
|
|
|
|
-// const childrenUserRes = item.childrenUserRes
|
|
|
|
-// ? item.childrenUserRes.map(user => ({
|
|
|
|
-// userName: user.userName,
|
|
|
|
-// userCode: user.userCode
|
|
|
|
-// }))
|
|
|
|
-// : [];
|
|
|
|
|
|
+ // 合并 childrenUserRes 和 childrenRes,并添加类型标识
|
|
|
|
+ newItem.children = [
|
|
|
|
+ ...(newItem.childrenUserRes || []).map(child => ({
|
|
|
|
+ ...child,
|
|
|
|
+ type: "user"
|
|
|
|
+ })),
|
|
|
|
+ ...(newItem.childrenRes || []).map(child => ({ ...child, type: "dept" }))
|
|
|
|
+ ];
|
|
|
|
+ delete newItem.childrenUserRes;
|
|
|
|
+ delete newItem.childrenRes;
|
|
|
|
|
|
-// newItem.children = [...childrenRes, ...childrenUserRes]; // 合并两个子项
|
|
|
|
-
|
|
|
|
-// return newItem; // 返回处理后的新对象
|
|
|
|
-// });
|
|
|
|
-// };
|
|
|
|
-const transformData = data => {
|
|
|
|
- // 检查data是否存在且是数组且长度大于0
|
|
|
|
- if (Array.isArray(data) && data.length > 0) {
|
|
|
|
- return data.map(item => {
|
|
|
|
- let newItem = {
|
|
|
|
- userNameNew: item.realName || item.deptName || "", // 优先使用realName,其次使用deptName
|
|
|
|
- userCodeNew: item.userCode || item.deptCode || "", // 优先使用userCode,其次使用deptCode
|
|
|
|
- children: [] // 初始化children为一个空数组
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- // 确保childrenUserRes和childrenRes存在
|
|
|
|
- const childrenUserRes = Array.isArray(item.childrenUserRes)
|
|
|
|
- ? item.childrenUserRes
|
|
|
|
- : [];
|
|
|
|
- const childrenRes = Array.isArray(item.childrenRes)
|
|
|
|
- ? item.childrenRes
|
|
|
|
- : [];
|
|
|
|
-
|
|
|
|
- // 处理子部门
|
|
|
|
- const renRes = childrenRes.map(itEes => ({
|
|
|
|
- userNameNew: itEes.deptName || "", // 使用deptName
|
|
|
|
- userCodeNew: itEes.deptCode || "", // 使用deptCode
|
|
|
|
- children: transformData(itEes.childrenRes) // 递归处理子部门
|
|
|
|
- }));
|
|
|
|
-
|
|
|
|
- // 处理用户
|
|
|
|
- const userRes = childrenUserRes.map(itEes => ({
|
|
|
|
- userNameNew: itEes.realName || "", // 使用realName
|
|
|
|
- userCodeNew: itEes.userCode || "", // 使用userCode
|
|
|
|
- jobTitle: itEes.jobTitle || "", // 包含职位信息
|
|
|
|
- hospitalCode: itEes.hospitalCode || "", // 包含医院代码
|
|
|
|
- children: transformData(itEes.childrenUserRes) // 递归处理用户子项
|
|
|
|
- }));
|
|
|
|
-
|
|
|
|
- // 合并两个子项
|
|
|
|
- newItem.children = [...renRes, ...userRes];
|
|
|
|
-
|
|
|
|
- return newItem; // 返回新的结构
|
|
|
|
|
|
+ // 重命名属性
|
|
|
|
+ newItem.children = newItem.children.map(child => {
|
|
|
|
+ const newChild = { ...child };
|
|
|
|
+ if (child.type === "user") {
|
|
|
|
+ newChild.userNameNew = child.realName;
|
|
|
|
+ newChild.userCodeNew = child.userCode;
|
|
|
|
+ } else if (child.type === "dept") {
|
|
|
|
+ newChild[newDeptCodeKey] = child.deptCode;
|
|
|
|
+ newChild[newDeptNameKey] = child.deptName;
|
|
|
|
+ }
|
|
|
|
+ return newChild;
|
|
});
|
|
});
|
|
- }
|
|
|
|
|
|
|
|
- // 如果数据无效,返回空数组
|
|
|
|
- return [];
|
|
|
|
-};
|
|
|
|
|
|
+ // 递归处理子节点
|
|
|
|
+ if (newItem.children && newItem.children.length) {
|
|
|
|
+ newItem.children = transformData(
|
|
|
|
+ newItem.children,
|
|
|
|
+ newDeptCodeKey,
|
|
|
|
+ newDeptNameKey
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return newItem;
|
|
|
|
+ });
|
|
|
|
+}
|