mock数据分组管理

This commit is contained in:
2026-06-04 15:24:11 +08:00
parent b1e4d2d7e7
commit 31095c967e
5 changed files with 521 additions and 25 deletions
+311 -22
View File
@@ -123,16 +123,27 @@
</el-card>
</el-tab-pane>
<el-tab-pane label="Mock 数据配置" name="mocks">
<el-tab-pane label="数据配置" name="mocks">
<el-card class="section-card">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;">
<strong>Mock 数据配置</strong>
<strong>数据配置(mock)</strong>
<div style="display:flex;gap:8px;align-items:center;">
<el-select size="small" v-model="mockGroupFilter" clearable placeholder="按分组筛选" style="width:160px;">
<el-option label="全部" :value="null"></el-option>
<el-option label="未分组" :value="-1"></el-option>
<el-option v-for="g in mockGroups" :key="g.id" :label="g.name" :value="g.id"></el-option>
</el-select>
<el-input size="small" v-model="mockFileSearch" placeholder="搜索别名或路径" clearable style="width:220px;" prefix-icon="el-icon-search"></el-input>
<el-button size="mini" type="primary" @click="openMockFileDialogForCreate">新增 Mock 文件</el-button>
<el-button size="mini" @click="openMockGroupDialog">分组管理</el-button>
</div>
</div>
<el-table :data="pagedMockFiles" border>
<el-table-column label="分组" width="140">
<template slot-scope="scope">
<span>{{ getGroupName(scope.row.groupId) }}</span>
</template>
</el-table-column>
<el-table-column label="别名" min-width="180">
<template slot-scope="scope">
<span>{{ scope.row.alias || "-" }}</span>
@@ -313,19 +324,37 @@
></el-input>
</el-form-item>
<el-form-item label="Mock 文件">
<el-select
v-model="routeDialog.form.filePath"
filterable
placeholder="请选择 mock 文件"
style="width: 100%;"
>
<el-option
v-for="item in mockFiles"
:key="item.filePath"
:label="item.alias ? item.alias + ' (' + item.filePath + ')' : item.filePath"
:value="item.filePath"
></el-option>
</el-select>
<div style="display:flex;gap:8px;">
<el-select
v-model="routeDialog.form.groupFilter"
clearable
placeholder="选择分组"
style="width: 180px;"
@change="onRouteDialogGroupChange"
>
<el-option label="全部" :value="null"></el-option>
<el-option label="无分组" :value="-1"></el-option>
<el-option
v-for="g in mockGroups"
:key="g.id"
:label="g.name"
:value="g.id"
></el-option>
</el-select>
<el-select
v-model="routeDialog.form.filePath"
filterable
placeholder="请选择 mock 文件"
style="flex: 1;"
>
<el-option
v-for="item in routeDialogMockFiles"
:key="item.filePath"
:label="item.alias ? item.alias + ' (' + item.filePath + ')' : item.filePath"
:value="item.filePath"
></el-option>
</el-select>
</div>
</el-form-item>
<el-form-item label="返回状态码">
<el-select
@@ -369,6 +398,12 @@
placeholder="可选:用于展示,支持任意文本"
></el-input>
</el-form-item>
<el-form-item label="分组">
<el-select v-model="mockFileDialog.form.groupId" clearable placeholder="选择分组" style="width: 100%;">
<el-option label="无分组" :value="null"></el-option>
<el-option v-for="g in mockGroups" :key="g.id" :label="g.name" :value="g.id"></el-option>
</el-select>
</el-form-item>
<el-form-item label="Mock 文件路径">
<el-input
v-model="mockFileDialog.form.fileName"
@@ -435,6 +470,67 @@
<el-button type="primary" @click="submitBatchImport">导入</el-button>
</span>
</el-dialog>
<el-dialog
title="分组管理"
:visible.sync="mockGroupDialog.visible"
width="600px"
>
<div style="margin-bottom:12px;">
<el-button size="mini" type="primary" @click="addMockGroup">新增分组</el-button>
</div>
<el-table :data="mockGroups" border size="small" style="width: 100%;">
<el-table-column prop="id" label="ID" width="60"></el-table-column>
<el-table-column label="分组名称" min-width="200">
<template slot-scope="scope">
<el-input
v-if="mockGroupDialog.editingId === scope.row.id"
v-model="mockGroupDialog.form.name"
size="small"
@blur="saveMockGroupEdit(scope.row)"
@keyup.enter.native="saveMockGroupEdit(scope.row)"
></el-input>
<span v-else>{{ scope.row.name }}</span>
</template>
</el-table-column>
<el-table-column label="文件数" width="80" align="center">
<template slot-scope="scope">
<span>{{ getGroupFileCount(scope.row.id) }}</span>
</template>
</el-table-column>
<el-table-column label="操作" width="160">
<template slot-scope="scope">
<el-button size="mini" type="primary" plain @click="startEditMockGroup(scope.row)">修改</el-button>
<el-button size="mini" type="danger" @click="confirmDeleteGroup(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-dialog>
<el-dialog
title="删除分组确认"
:visible.sync="deleteGroupDialog.visible"
width="500px"
>
<div style="margin-bottom:16px;">
<p>分组「{{ deleteGroupDialog.groupName }}」下有 <strong>{{ deleteGroupDialog.fileCount }}</strong> 个 mock 文件。</p>
<p>请选择对这些文件的处理方式:</p>
</div>
<el-radio-group v-model="deleteGroupDialog.action" style="display:flex;flex-direction:column;gap:12px;">
<el-radio label="reassign">转移到其他分组</el-radio>
<el-radio label="unassign">取消分组(设为未分组)</el-radio>
<el-radio label="delete">删除全部文件</el-radio>
</el-radio-group>
<div v-if="deleteGroupDialog.action === 'reassign'" style="margin-top:12px;">
<el-select v-model="deleteGroupDialog.targetGroupId" placeholder="选择目标分组" style="width:100%;">
<el-option v-for="g in mockGroups.filter(function(g){return g.id !== deleteGroupDialog.groupId})" :key="g.id" :label="g.name" :value="g.id"></el-option>
</el-select>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="deleteGroupDialog.visible = false">取消</el-button>
<el-button type="danger" @click="submitDeleteGroup">确认删除</el-button>
</span>
</el-dialog>
</div>
</div>
@@ -459,6 +555,22 @@
input: "",
},
mockFiles: [],
mockGroups: [],
mockGroupFilter: null,
mockGroupDialog: {
visible: false,
mode: "create",
editingId: null,
form: { name: "" },
},
deleteGroupDialog: {
visible: false,
groupId: null,
groupName: "",
fileCount: 0,
action: "",
targetGroupId: null,
},
contentTypeOptions: [
"application/json",
"application/x-www-form-urlencoded",
@@ -515,6 +627,7 @@
filePath: "",
statusCode: 200,
enabled: true,
groupFilter: null,
},
},
mockFileDialog: {
@@ -531,11 +644,13 @@
watch: {
routeSearch: function () { this.routePagination.currentPage = 1; },
mockFileSearch: function () { this.mockFilePagination.currentPage = 1; },
mockGroupFilter: function () { this.mockFilePagination.currentPage = 1; },
apiSearch: function () { this.apiPagination.currentPage = 1; },
},
created: async function () {
await this.loadApiList();
await this.loadMockFiles();
await this.loadMockGroups();
await this.loadConfig();
},
methods: {
@@ -648,6 +763,7 @@
filePath: "",
statusCode: 200,
enabled: true,
groupFilter: null,
};
},
isApiListLocked: function () {
@@ -663,6 +779,10 @@
this.routeDialog.form.route = selected.route;
this.routeDialog.form.apiName = selected.name;
},
onRouteDialogGroupChange: function () {
// 切换分组时清空已选文件
this.routeDialog.form.filePath = "";
},
openRouteDialogForCreate: function () {
this.routeDialog.mode = "create";
this.routeDialog.editingIndex = -1;
@@ -678,6 +798,8 @@
this.routeDialog.mode = "edit";
this.routeDialog.editingIndex = actualIndex;
var matched = this.findApiByRoute(item.route || "");
var mockFile = this.mockFiles.find(function (f) { return f.filePath === item.filePath; });
var groupFilter = mockFile && mockFile.groupId ? mockFile.groupId : null;
this.routeDialog.form = {
apiName: matched ? matched.name : item.apiName || "",
selectedApiRoute: matched ? matched.route : "",
@@ -686,6 +808,7 @@
filePath: item.filePath || "mock/",
statusCode: this.normalizeStatusCode(item.statusCode),
enabled: item.enabled !== false,
groupFilter: groupFilter,
};
this.routeDialog.visible = true;
},
@@ -694,6 +817,7 @@
fileName: "",
alias: "",
content: "",
groupId: null,
};
},
openMockFileDialogForCreate: function () {
@@ -709,6 +833,7 @@
fileName: name,
alias: String(item.alias || ""),
content: String(item.content || ""),
groupId: item.groupId || null,
};
this.mockFileDialog.visible = true;
},
@@ -745,6 +870,7 @@
filePath: filePath,
alias: this.mockFileDialog.form.alias,
content: this.mockFileDialog.form.content,
groupId: this.mockFileDialog.form.groupId,
}),
});
var data = await resp.json();
@@ -1010,6 +1136,148 @@
this.batchImportDialog.visible = false;
await this.loadApiList();
},
getGroupName: function (groupId) {
if (!groupId) return "未分组";
var group = this.mockGroups.find(function (g) { return g.id === groupId; });
return group ? group.name : "未分组";
},
getGroupFileCount: function (groupId) {
return this.mockFiles.filter(function (f) { return f.groupId === groupId; }).length;
},
loadMockGroups: async function () {
try {
var resp = await fetch("/__mock-groups");
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "加载分组失败");
}
this.mockGroups = Array.isArray(data.list) ? data.list : [];
} catch (err) {
this.mockGroups = [];
this.$message.error("加载分组失败: " + err.message);
}
},
openMockGroupDialog: function () {
this.mockGroupDialog.editingId = null;
this.mockGroupDialog.form.name = "";
this.mockGroupDialog.visible = true;
},
addMockGroup: async function () {
var self = this;
this.$prompt("请输入分组名称", "新增分组", {
confirmButtonText: "确定",
cancelButtonText: "取消",
inputPattern: /\S+/,
inputErrorMessage: "分组名称不能为空",
}).then(async function (res) {
var name = (res.value || "").trim();
try {
var resp = await fetch("/__mock-groups", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: name }),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "新增分组失败");
}
self.$message.success("分组已创建");
await self.loadMockGroups();
} catch (err) {
self.$message.error("新增分组失败: " + err.message);
}
}).catch(function () {});
},
startEditMockGroup: function (group) {
this.mockGroupDialog.editingId = group.id;
this.mockGroupDialog.form.name = group.name;
},
saveMockGroupEdit: async function (group) {
var newName = (this.mockGroupDialog.form.name || "").trim();
this.mockGroupDialog.editingId = null;
if (!newName || newName === group.name) return;
try {
var resp = await fetch("/__mock-groups", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: group.id, name: newName }),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "修改分组失败");
}
this.$message.success("分组已修改");
await this.loadMockGroups();
} catch (err) {
this.$message.error("修改分组失败: " + err.message);
}
},
confirmDeleteGroup: async function (group) {
var fileCount = this.getGroupFileCount(group.id);
if (fileCount === 0) {
// 无文件,直接删除
try {
var resp = await fetch("/__mock-groups", {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: group.id }),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "删除分组失败");
}
this.$message.success("分组已删除");
await this.loadMockGroups();
} catch (err) {
this.$message.error("删除分组失败: " + err.message);
}
return;
}
// 有文件,弹出确认对话框
this.deleteGroupDialog.groupId = group.id;
this.deleteGroupDialog.groupName = group.name;
this.deleteGroupDialog.fileCount = fileCount;
this.deleteGroupDialog.action = "";
this.deleteGroupDialog.targetGroupId = null;
this.deleteGroupDialog.visible = true;
},
submitDeleteGroup: async function () {
var action = this.deleteGroupDialog.action;
if (!action) {
this.$message.error("请选择处理方式");
return;
}
if (action === "reassign" && !this.deleteGroupDialog.targetGroupId) {
this.$message.error("请选择目标分组");
return;
}
var payload = {
id: this.deleteGroupDialog.groupId,
action: action === "unassign" ? "reassign" : action,
};
if (action === "reassign") {
payload.targetGroupId = this.deleteGroupDialog.targetGroupId;
} else if (action === "unassign") {
payload.targetGroupId = null;
}
try {
var resp = await fetch("/__mock-groups", {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "删除分组失败");
}
this.$message.success("分组已删除");
this.deleteGroupDialog.visible = false;
await this.loadMockGroups();
await this.loadMockFiles();
} catch (err) {
this.$message.error("删除分组失败: " + err.message);
}
},
},
computed: {
filteredRoutes: function () {
@@ -1030,13 +1298,24 @@
},
filteredMockFiles: function () {
var q = (this.mockFileSearch || "").toLowerCase().trim();
if (!q) return this.mockFiles;
return this.mockFiles.filter(function (item) {
return (
(item.filePath || "").toLowerCase().indexOf(q) !== -1 ||
(item.alias || "").toLowerCase().indexOf(q) !== -1
);
});
var groupFilter = this.mockGroupFilter;
var list = this.mockFiles;
if (groupFilter !== null && groupFilter !== undefined) {
if (groupFilter === -1) {
list = list.filter(function (item) { return !item.groupId; });
} else {
list = list.filter(function (item) { return item.groupId === groupFilter; });
}
}
if (q) {
list = list.filter(function (item) {
return (
(item.filePath || "").toLowerCase().indexOf(q) !== -1 ||
(item.alias || "").toLowerCase().indexOf(q) !== -1
);
});
}
return list;
},
filteredApiList: function () {
var q = (this.apiSearch || "").toLowerCase().trim();
@@ -1057,6 +1336,16 @@
pagedApiList: function () {
return this.getPagedData(this.filteredApiList, this.apiPagination);
},
routeDialogMockFiles: function () {
var groupFilter = this.routeDialog.form.groupFilter;
if (groupFilter === null || groupFilter === undefined) {
return this.mockFiles;
}
if (groupFilter === -1) {
return this.mockFiles.filter(function (f) { return !f.groupId; });
}
return this.mockFiles.filter(function (f) { return f.groupId === groupFilter; });
},
},
});
</script>