hyb
2026-01-30 15bc7727b58bf9ca0c8f21702fa893daac232b8d
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<template>
    <el-container>
        <el-header style="background: #fff; padding: 0; height: 50px;">
            <div class="nav-api-header">
                <div style="display: flex; padding: 10px; align-items: center;">
                    <el-button
                        type="primary"
                        size="small"
                        icon="el-icon-circle-plus-outline"
                        @click="buttonActivate = false"
                        :disabled="buttonActivate"
                        >添加用例</el-button
                    >
                    <span style="margin-left: 10px">配置</span>
                    <el-select
                        placeholder="请选择配置"
                        size="small"
                        v-model="currentConfig"
                        :disabled="addTestActivate"
                        style="margin-left: 10px;"
                        value-key="id"
                    >
                        <el-option
                            v-for="item in configOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.name"
                        ></el-option>
                    </el-select>
                    <el-button
                        style="margin-left: 10px"
                        v-if="addTestActivate"
                        type="primary"
                        size="mini"
                        title="批量运行用例"
                        icon="el-icon-caret-right"
                        @click="run = !run"
                        >批量运行</el-button
                    >
                    <el-button
                        v-if="addTestActivate"
                        :disabled="!isSelectCase"
                        type="success"
                        size="mini"
                        icon="el-icon-right"
                        title="移动用例到指定目录"
                        @click="move = !move"
                        >移动用例</el-button
                    >
                    <el-button
                        v-if="addTestActivate"
                        :disabled="!isSelectCase"
                        type="danger"
                        icon="el-icon-delete"
                        size="mini"
                        title="批量删除用例"
                        @click="del = !del"
                        >批量删除</el-button
                    >
                    <el-button
                        v-show="isShowListBtn"
                        type="text"
                        style="position: absolute; right: 30px;"
                        @click="handleBackList"
                        >返回列表</el-button
                    >
                </div>
            </div>
            <el-dialog
                title="新建目录"
                width="30%"
                :visible.sync="dialogVisible"
                :close-on-click-modal="false"
                :style="{ 'text-align': 'center' }"
            >
                <el-form
                    :model="nodeForm"
                    :rules="rules"
                    ref="nodeForm"
                    label-width="100px"
                    class="project"
                >
                    <el-form-item label="目录名称" prop="name">
                        <el-input
                            v-model="nodeForm.name"
                            placeholder="请输入目录名称"
                        ></el-input>
                    </el-form-item>
                </el-form>
 
                <el-radio-group v-model="radio" size="small">
                    <el-radio-button label="根目录"></el-radio-button>
                    <el-radio-button label="子目录"></el-radio-button>
                </el-radio-group>
 
                <span
                    slot="footer"
                    class="dialog-footer"
                    style="display: flex; justify-content: flex-end;"
                >
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handleConfirm('nodeForm')"
                        >确 定</el-button
                    >
                </span>
            </el-dialog>
        </el-header>
 
        <el-container>
            <el-aside
                style="width: 250px; margin-top: 10px"
                v-show="addTestActivate"
            >
                <div class="nav-api-side">
                    <div class="api-tree">
                        <el-input
                            placeholder="请输入关键字进行过滤"
                            v-model="filterText"
                            size="small"
                            clearable
                            prefix-icon="el-icon-search"
                        ></el-input>
 
                        <el-tree
                            :data="dataTree"
                            @node-drag-start="handleDragStart"
                            @node-click="handleNodeClick"
                            @node-drag-end="handleDragEnd"
                            @node-expand="handleNodeExpand"
                            @node-collapse="handleNodeCollapse"
                            node-key="id"
                            draggable
                            highlight-current
                            ref="tree2"
                            :filter-node-method="filterNode"
                            :default-expand-all="false"
                            :expand-on-click-node="false"
                            :default-expanded-keys="expandedNodeIds"
                        >
                            <span
                                class="custom-tree-node"
                                slot-scope="{ node, data }"
                                @mouseenter="mouseenter(node)"
                                @mouseleave="mouseleave"
                            >
                                <span class="custom-tree-node-span">
                                    <i
                                        v-if="node.childNodes.length > 0"
                                        class="el-icon-folder-opened"
                                    ></i>
                                    <i v-else class="el-icon-folder"></i>
                                    &nbsp;&nbsp;{{ node.label }}
                                </span>
 
                                <span style="flex-shrink: 0; margin-left: 5px;">
                                    <el-badge
                                        :value="data.data_count"
                                        :max="99"
                                        type="primary"
                                    ></el-badge>
                                </span>
 
                                <span
                                    class="icon-group"
                                    v-show="node.id === mouseNodeId"
                                >
                                    <i
                                        class="el-icon-folder-add"
                                        @click="dialogVisible = true"
                                    ></i>
                                    <i
                                        class="el-icon-edit"
                                        @click="renameNode(node)"
                                    ></i>
                                    <i
                                        class="el-icon-delete"
                                        @click="deleteNode(node)"
                                    ></i>
                                </span>
                            </span>
                        </el-tree>
                    </div>
                </div>
            </el-aside>
 
            <el-main style="padding: 0;">
                <edit-test
                    v-show="!addTestActivate"
                    :project="$route.params.id"
                    :editBack="editBack"
                    :resetEditTestStepActivate="resetEditTestStepActivate"
                    :node="currentNode.id"
                    :testStepResp="testStepResp"
                    :config="currentConfig"
                    :rigEnv.sync="rigEnv"
                    :tag.sync="tag"
                    :search.sync="search"
                    :addTestActivate="addTestActivate"
                    @addSuccess="handleAddSuccess"
                    @dataChanged="handleContentChange"
                    @showListBtn="ShowListBtnHandle"
                ></edit-test>
                <test-list
                    v-show="addTestActivate"
                    :project="$route.params.id"
                    :pNode="currentNode !== '' ? currentNode.id : ''"
                    :del="del"
                    @testStep="handleTestStep"
                    @resetNode="resetNodeStatus"
                    @refreshTree="getCaseTree"
                    @showListBtn="ShowListBtnHandle"
                    :back="back"
                    :run="run"
                    :move="move"
                    :isSelectCase.sync="isSelectCase"
                    :pageSize.sync="pageSize"
                    :currentPage.sync="currentPage"
                ></test-list>
            </el-main>
        </el-container>
    </el-container>
</template>
 
<script>
import TestList from "./components/TestList";
import EditTest from "./components/EditTest";
 
export default {
    name: "AutoTest",
    components: {
        EditTest,
        TestList
    },
    data() {
        return {
            isShowListBtn: false,
            isContentChanged: false,
            expandedNodeIds: [],
            originalTreeData: [],
            mouseNodeId: -1,
            testStepResp: {},
            nodeForm: {
                name: ""
            },
            rules: {
                name: [
                    {
                        required: true,
                        message: "请输入目录名称",
                        trigger: "blur"
                    },
                    {
                        min: 1,
                        max: 50,
                        message: "最多不超过50个字符",
                        trigger: "blur"
                    }
                ]
            },
            back: false,
            editBack: false,
            resetEditTestStepActivate: false,
            del: false,
            run: false,
            move: false,
            radio: "根目录",
            addTestActivate: true,
            currentConfig: "",
            treeId: "",
            maxId: "",
            dialogVisible: false,
            currentNode: "",
            data: "",
            filterText: "",
            expand: "&#xe65f;",
            dataTree: [],
            configOptions: [],
            rigEnv: "",
            tag: "",
            search: "",
            pageSize: 10,
            currentPage: 1,
            isSelectCase: false
        };
    },
    computed: {
        buttonActivate: {
            get() {
                return !this.addTestActivate || this.currentNode === "";
            },
            set(value) {
                this.addTestActivate = value;
                this.isShowListBtn = !value;
                this.testStepResp = {};
            }
        }
    },
    watch: {
        filterText(val) {
            this.$refs.tree2.filter(val);
        }
    },
    methods: {
        ShowListBtnHandle(val) {
            this.isShowListBtn = val;
        },
        handleContentChange(hasChanged) {
            this.isContentChanged = hasChanged;
        },
        handleDragStart() {
            // 保存当前树的状态
            this.originalTreeData = JSON.parse(JSON.stringify(this.dataTree));
        },
        handleNodeExpand(node) {
            // 当一个目录被展开时,将它的 ID 添加到 expandedNodeIds 中
            if (!this.expandedNodeIds.includes(node.id)) {
                this.expandedNodeIds.push(node.id);
            }
        },
        handleNodeCollapse(node) {
            // 当一个目录被收起时,将它的 ID 从 expandedNodeIds 中移除并且移除所有子节点
            // 移除当前节点 ID
            const index = this.expandedNodeIds.indexOf(node.id);
            if (index !== -1) {
                this.expandedNodeIds.splice(index, 1);
            }
 
            // 递归移除所有子节点 ID
            if (node.children && node.children.length > 0) {
                node.children.forEach(childNode => {
                    this.handleNodeCollapse(childNode);
                });
            }
        },
        // 将目录的状态恢复到未选中状态
        resetNodeStatus() {
            this.$refs.tree2.setCurrentKey(null);
            this.currentNode = "";
        },
        handleDragEnd() {
            this.$confirm("确定要移动这个目录吗?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    // 用户确认拖拽操作,更新树形结构
                    this.updateTree();
                })
                .catch(() => {
                    // 用户取消操作,恢复原始树形结构
                    this.dataTree = JSON.parse(
                        JSON.stringify(this.originalTreeData)
                    );
                });
        },
        getConfig() {
            this.$api.getAllConfig(this.$route.params.id).then(resp => {
                this.configOptions = resp;
                const _config = this.configOptions.filter(
                    item => item.is_default === true
                );
                if (_config.length) {
                    this.currentConfig = _config[0].name;
                }
            });
        },
        handleAddSuccess() {
            this.addTestActivate = true;
            this.search = "";
            this.pageSize = 10;
            this.currentPage = 1;
            this.testStepResp = {};
            this.back = !this.back;
        },
        closeEditor() {
            this.addTestActivate = true;
            this.isShowListBtn = false;
            this.resetEditTestStepActivate = !this.resetEditTestStepActivate;
            this.editBack = !this.editBack;
        },
        handleBackList() {
            if (this.isContentChanged) {
                this.$confirm("内容未保存,确定关闭?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        this.closeEditor();
                    })
                    .catch(() => {});
            } else {
                this.closeEditor();
            }
        },
        handleTestStep(resp) {
            this.testStepResp = resp;
            this.addTestActivate = false;
        },
        getCaseTree() {
            this.$api
                .getTree(this.$route.params.id, { params: { type: 2 } })
                .then(resp => {
                    this.dataTree = resp["tree"];
                    this.treeId = resp["id"];
                    this.maxId = resp["max"];
                });
        },
        // 准备树形数据以供更新
        prepareTreeDataForUpdate(treeData) {
            return treeData.map(node => {
                // 创建一个不包含 data_count 字段的新对象
                const { data_count, ...nodeWithoutDataCount } = node;
 
                // 如果有 children 字段,递归处理子目录
                if (node.children && node.children.length > 0) {
                    return {
                        ...nodeWithoutDataCount,
                        children: this.prepareTreeDataForUpdate(node.children)
                    };
                }
 
                // 返回处理后的目录
                return nodeWithoutDataCount;
            });
        },
        updateTree() {
            const preparedData = this.prepareTreeDataForUpdate(this.dataTree);
            this.$api
                .updateTree(this.treeId, {
                    body: preparedData,
                    type: 2
                })
                .then(resp => {
                    if (resp.success) {
                        this.$message({
                            message: "目录更新成功",
                            type: "success"
                        });
                        this.dataTree = resp.data.tree;
                        this.maxId = resp.data.max;
                    } else {
                        this.$message({
                            message: resp.msg,
                            type: "error"
                        });
                    }
                });
        },
        renameNode(nodeObj) {
            this.$prompt("请输入目录名", "编辑", {
                closeOnClickModal: false,
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                inputPattern: /\S/,
                inputErrorMessage: "目录名不能为空",
                inputValue: nodeObj.label
            }).then(({ value }) => {
                const parent = this.data.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(
                    childNode => childNode.id === this.currentNode.id
                );
                children[index]["label"] = value;
                this.updateTree();
            });
        },
        deleteNode(node) {
            this.$confirm(`删除 ${node.label}, 是否继续?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    if (this.currentNode === "") {
                        this.$message.info("请选择一个目录");
                    } else {
                        let dataCopy = JSON.parse(
                            JSON.stringify(this.dataTree)
                        ); // 创建树形数据的拷贝
                        this.removeNodeFromData(this.currentNode.id, dataCopy); // 从拷贝的数据中移除目录
                        this.$api
                            .updateTree(this.treeId, {
                                body: dataCopy, // 发送已经移除目录的数据
                                type: 2
                            })
                            .then(resp => {
                                if (resp["success"]) {
                                    this.dataTree = resp["data"]["tree"]; // 更新前端的树形数据
                                    this.maxId = resp["data"]["max"];
                                    this.$message.success("目录更新成功");
                                } else {
                                    this.$message.error(resp.msg);
                                }
                            });
                    }
                })
                .catch(err => {
                    if (err !== "cancel") {
                        this.$message.error(err);
                    }
                });
        },
        removeNodeFromData(nodeId, data) {
            for (let i = 0; i < data.length; i++) {
                if (data[i].id === nodeId) {
                    data.splice(i, 1);
                    break;
                } else if (data[i].children) {
                    this.removeNodeFromData(nodeId, data[i].children);
                }
            }
        },
        handleConfirm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.append(this.currentNode);
                    this.updateTree();
                    this.dialogVisible = false;
                    this.nodeForm.name = "";
                }
            });
        },
        handleNodeClick(node, data) {
            this.currentNode = node;
            this.data = data;
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        remove(data, node) {
            const parent = node.parent;
            const children = parent.data.children || parent.data;
            const index = children.findIndex(
                childNode => childNode.id === data.id
            );
            children.splice(index, 1);
        },
        append(data) {
            const newChild = {
                id: ++this.maxId,
                label: this.nodeForm.name,
                children: [],
                expand: false // 添加一个 expand 字段到每个目录对象
            };
            if (
                data === "" ||
                this.dataTree.length === 0 ||
                this.radio === "根目录"
            ) {
                this.dataTree.push(newChild);
                return;
            }
            if (!data.children) {
                this.$set(data, "children", []);
            }
            data.children.push(newChild);
            // 新添加的代码,只有当添加的是子目录时,才把父目录的 id 添加到 expandedKeys 中
            if (this.radio === "子目录") {
                this.expandedNodeIds.push(data.id); // 添加当前父目录的 id
            }
        },
        mouseenter(node) {
            this.mouseNodeId = node.id;
        },
        mouseleave() {
            this.mouseNodeId = -1;
        }
    },
    mounted() {
        this.getCaseTree();
        this.getConfig();
    }
};
</script>
 
<style scoped>
.icon-group {
    margin-right: 6px;
}
 
.icon-group i {
    margin-right: 4px;
    padding: 2px;
}
</style>