基于廊坊系统为基础,国防科技大学系统演示Demo
surprise
2024-04-28 c310c155e3e91e6b285b3fb3519a3ef7c6690d66
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<template>
  <div class="wrap">
    <div class="bread">
      <el-breadcrumb separator="el-icon-arrow-right">
        <el-breadcrumb-item>{{ breadLabel }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="treeBox">
      <el-tree
        ref="tree"
        :props="defaultProps"
        node-key="id"
        :data="dirData"
        :expand-on-click-node="false"
        :default-expanded-keys="[1, 2, 3, 4, 5, 6, 7, 8, 9]"
        draggable
        @node-click="handleNodeClick"
        @node-drop="handleDrop"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span class="btnBox" v-show="showCata">
            <el-button
              type="text"
              size="mini"
              @click="() => append(data, node)"
            >
              <i class="el-icon-circle-plus"></i>
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="() => remove(node, data)"
            >
              <i class="el-icon-delete-solid"></i>
            </el-button>
          </span>
        </span>
      </el-tree>
      <div style="margin-left: 130px">
        <el-button @click="sendChange">保存</el-button>
      </div>
    </div>
  </div>
</template>
 
<script>
import { queryDirTree, updateDirTree } from "../api/api";
export default {
  name: "catalogueTree",
  props: ["showBtn"],
  data() {
    return {
      showCata: false,
      showBread: false,
      oriData: [], //原始树数据
      dirData: [], //el树数据
      newData: [], //拖动后数据
      breadList: [], //面包屑数组
      breadLabel: "", //面包屑文字
      filterText: "",
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
 
  methods: {
    // 请求目录树
    getDirTree() {
      queryDirTree().then((res) => {
        if (res.status == 200) {
          this.oriData = res.data;
          this.newData = res.data;
          this.dirData = this.treeData(res.data);
        } else {
          console.log("接口报错");
        }
      });
    },
    treeData(source) {
      let cloneData = JSON.parse(JSON.stringify(source)); // 对源数据深度克隆
      return cloneData.filter((father) => {
        // 循环所有项
        let branchArr = cloneData.filter((child) => father.id == child.pid); // 对比ID,分别上下级菜单,并返回数据
        branchArr.length > 0 ? (father.children = branchArr) : ""; // 给父级添加一个children属性,并赋值
        // 属于同一对象问题,例如:令 a=b、c=1 ,然后再令 b.c=c , 那么 a.c=b.c=c=1 ;同理,后续令 c.d=2 ,那么 a.c.d 也是=2;
        // 由此循环多次后,就能形成相应的树形数据结构
        return father.pid == -1; // 返回一级菜单
      });
    },
    append(data, parentNode) {
      this.$prompt("请输入名称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          const newChild = {
            id: value,
            name: value,
            pid: data.id,
            // children: [],
            oid: data.children ? data.children.length + 1 : 1,
          };
          if (!data.children) {
            this.$set(data, "children", []);
          }
          data.children.push(newChild);
          this.newData.push(newChild);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    remove(node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex((d) => d.id === data.id);
      let res = children.splice(index, 1);
 
    },
    flaten(arr) {
      return arr.reduce((p, v, i) => {
        for (let i = 0; i < p.length; i++) {
          if (p[i].children) {
            delete p[i].children;
          }
        }
        return p.concat(v.children ? this.flaten(v.children).concat(v) : v);
      }, []);
    },
    handleNodeClick(data) {
      // console.log(data);
      this.$store.commit("changeNode", data); //传递点击的节点
      this.breadList = [];
      this.getTreeNode(this.$refs.tree.getNode(data.id));
    },
    getTreeNode(node) {
      if (node && node.label) {
        this.breadList.unshift(node.label);
        this.getTreeNode(node.parent); //递归
        this.breadLabel = this.breadList.join(">");
        this.$store.commit("changeCata", this.breadLabel); //传递面包屑路径
      }
    },
    handleDrop(draggingNode, dropNode, dropType, ev) {
      console.log("被拖拽节点", draggingNode);
      // console.log("进入的节点", dropNode);
      // console.log("放置位置", dropType);
      // console.log("事件", ev);
      // if (dropType !== "inner") {
      //   // 1.修改父节点pid
      //   draggingNode.data.pid = dropNode.data.pid;
      //   dropNode.parent.childNodes.forEach((item, index) => {
      //     // 2.修改自身顺序oid
      //     item.data.oid = index + 1;
      //   });
      // }
      // // console.log(draggingNode.data.id);
      // let res = this.oriData.filter((item) => item.id == draggingNode.data.id);
      // console.log(res);
 
      let paramData = [];
      let data = dropType != "inner" ? dropNode.parent.data : dropNode.data;
      let nodeData =
        dropNode.level == 1 && dropType != "inner" ? data : data.children;
      let pid = "";
      nodeData.forEach((item, i) => {
        if (dropType != "inner") {
          if (draggingNode.data.pid === dropNode.data.pid) {
            pid = item.pid;
          } else {
            pid = dropNode.data.pid;
          }
        } else {
          pid = data.id;
        }
        let collection = {
          id: item.id,
          name: item.name,
          pid,
          oid: i + 1,
        };
        paramData.push(collection);
      });
      // console.log(paramData);
      let arr = [];
      this.oriData.forEach((e) => {
        paramData.forEach((item) => {
          if (item.id === e.id) {
            e = item;
          }
        });
        arr.push(e);
      });
      this.newData = arr;
    },
 
    sendChange() {
      updateDirTree(this.newData).then((res) => {
        console.log(res);
        if (res.status == 200) {
          this.$message({
            type: "success",
            message: "更新成功!",
          });
        }
      });
    },
  },
  mounted() {
    this.getDirTree();
  },
  watch: {
    showBtn: {
      immediate: true,
      handler(val) {
        if (val) this.showCata = val;
      },
    },
    showBread: {
      immediate: true,
      handler(val) {
        if (val) this.showBread = val;
      },
    },
  },
};
</script>
 
<style lang="less" scoped>
.wrap {
  width: 98.5%;
  height: 100%;
  .bread {
    width: 100%;
    height: 5%;
    margin: 0 auto;
    overflow: auto;
  }
  .treeBox {
    margin-top: 1%;
    width: 100%;
    height: 94%;
    overflow: auto;
 
  }
}
</style>