elkers
2025-01-25 659d9d31ab3420b5fde4ae76973c89279fd48834
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * @Author: Liuyi candymxq888@outlook.com
 * @Date: 2024-08-06 14:47:41
 * @LastEditors: Liuyi candymxq888@outlook.com
 * @LastEditTime: 2024-08-15 11:15:57
 * @FilePath: \water-qinghe-web\src\utils\methods.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//将没有层级的对象列表传为树形,树形节点标识为parentId
export const listToTree = (data) => {
    const tree = {};
    
    // 遍历数据数组
    data.forEach((item) => {
      const { id, parentId } = item;
      
      if (!tree[id]) {
        tree[id] = { ...item, children: [] }; // 创建节点
      } else {
        tree[id] = { ...tree[id], ...item }; // 更新节点
      }
      
      if (parentId !== id) {
        if (!tree[parentId]) {
          tree[parentId] = { children: [] }; // 创建父节点
        }
        tree[parentId].children.push(tree[id]); // 将子节点添加到父节点的 children 中
      }
    });
    // 找到根节点
    const rootIds = Object.keys(tree).filter(idx => tree[idx].parentId === 0 || tree[idx].parentId === 40);
    const root = rootIds.map(idx => tree[idx]);
    console.log('tree', tree, rootIds, root)
    return root;
  }
  //设置图片路径
import { PREURL } from '@/config/index'
export const getImage = (imgList) =>{
    if(imgList != null){
        return imgList.split(',').map((image) => {
            if(image.indexOf('http') == -1 && image != ''){
                return PREURL + image
            }else {
                return image
            }
        })
    }
}
export const setImage = (imgList) =>{
  return imgList.map((image) => { 
      if(image.indexOf(PREURL) != -1){
        return image.replace(PREURL,'') 
      }else {
          return image
      }
  })
}