重构数据库版本

This commit is contained in:
2026-06-04 12:13:28 +08:00
parent 600c8b2e30
commit cd9e75790f
18 changed files with 1795 additions and 1371 deletions
+300 -71
View File
@@ -30,18 +30,15 @@
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}
.top-nav-inner {
max-width: 1200px;
max-width: 1320px;
margin: 0 auto;
padding: 10px 16px;
padding: 14px 16px;
}
.actions {
display: flex;
gap: 8px;
}
@media (max-width: 1200px) {
.actions {
flex-wrap: wrap;
}
.top-nav-title {
font-size: 18px;
font-weight: 600;
color: #303133;
letter-spacing: 1px;
}
.small-text {
color: #909399;
@@ -60,11 +57,7 @@
<div id="app">
<div class="top-nav">
<div class="top-nav-inner">
<div class="actions">
<el-button type="primary" @click="saveConfig">保存配置到 config.json</el-button>
<el-button @click="reloadConfig">重载服务内存配置</el-button>
<el-button @click="loadConfig">刷新页面数据</el-button>
</div>
<span class="top-nav-title">企业级数据Mock管理系统</span>
</div>
</div>
<div class="container">
@@ -73,7 +66,10 @@
<el-card class="section-card">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;">
<strong>路由配置(routes)</strong>
<el-button size="mini" type="primary" @click="openRouteDialogForCreate">新增路由</el-button>
<div style="display:flex;gap:8px;align-items:center;">
<el-input size="small" v-model="routeSearch" placeholder="搜索接口名称或路径" clearable style="width:220px;" prefix-icon="el-icon-search"></el-input>
<el-button size="mini" type="primary" @click="openRouteDialogForCreate">新增路由</el-button>
</div>
</div>
<el-table :data="pagedRoutes" border class="route-table" style="width: 100%;">
<el-table-column label="接口名称" min-width="180" show-overflow-tooltip>
@@ -98,7 +94,7 @@
</el-table-column>
<el-table-column label="启用" width="90" align="center">
<template slot-scope="scope">
<el-switch v-model="scope.row.enabled"></el-switch>
<el-switch v-model="scope.row.enabled" @change="saveRoutes"></el-switch>
</template>
</el-table-column>
<el-table-column label="操作" width="160">
@@ -121,11 +117,11 @@
layout="total, prev, pager, next"
:current-page="routePagination.currentPage"
:page-size="routePagination.pageSize"
:total="form.routes.length"
:total="filteredRoutes.length"
@current-change="handleRoutePageChange"
></el-pagination>
<div class="small-text" style="margin-top:8px;">
说明:关闭“启用”后会以 # 注释路由,不参与 mock 命中;切换后请点击“保存配置到 config.json”生效到文件。
<div class=”small-text” style=”margin-top:8px;”>
说明:关闭”启用”后该路由不参与 mock 命中,切换后自动生效。
</div>
</el-card>
</el-tab-pane>
@@ -134,7 +130,10 @@
<el-card class="section-card">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;">
<strong>Mock 数据配置</strong>
<el-button size="mini" type="primary" @click="openMockFileDialogForCreate">新增 Mock 文件</el-button>
<div style="display:flex;gap:8px;align-items:center;">
<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>
</div>
</div>
<el-table :data="pagedMockFiles" border>
<el-table-column label="别名" min-width="180">
@@ -165,29 +164,78 @@
layout="total, prev, pager, next"
:current-page="mockFilePagination.currentPage"
:page-size="mockFilePagination.pageSize"
:total="mockFiles.length"
:total="filteredMockFiles.length"
@current-change="handleMockFilePageChange"
></el-pagination>
</el-card>
</el-tab-pane>
<el-tab-pane label="接口管理" name="api">
<el-card class="section-card">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;">
<strong>接口列表</strong>
<div style="display:flex;gap:8px;align-items:center;">
<el-input size="small" v-model="apiSearch" placeholder="搜索接口名称或路径" clearable style="width:220px;" prefix-icon="el-icon-search"></el-input>
<el-button size="mini" type="primary" @click="openApiDialogForCreate">新增接口</el-button>
</div>
</div>
<el-table :data="pagedApiList" border style="width: 100%;">
<el-table-column label="接口名称" min-width="200">
<template slot-scope="scope">
<span>{{ scope.row.name }}</span>
</template>
</el-table-column>
<el-table-column label="请求路径" min-width="300">
<template slot-scope="scope">
<span>{{ scope.row.route }}</span>
</template>
</el-table-column>
<el-table-column label="操作" width="160">
<template slot-scope="scope">
<el-button size="mini" type="primary" plain @click="openApiDialogForEdit(scope.$index)">修改</el-button>
<el-button size="mini" type="danger" @click="removeApiItem(scope.$index)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
class="table-pagination"
background
layout="total, prev, pager, next"
:current-page="apiPagination.currentPage"
:page-size="apiPagination.pageSize"
:total="filteredApiList.length"
@current-change="handleApiPageChange"
></el-pagination>
</el-card>
</el-tab-pane>
<el-tab-pane label="基础配置" name="basic">
<el-card class="section-card">
<el-form :model="form.config" label-width="180px">
<el-row :gutter="16">
<el-col :span="12">
<el-form-item label="Mock 开关">
<el-switch v-model="form.config.mockEnabled"></el-switch>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="自动重载配置文件">
<el-switch v-model="form.config.reloadOnChange"></el-switch>
<el-switch v-model="form.config.mockEnabled" @change="saveServerConfig"></el-switch>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="默认响应 Content-Type">
<el-input v-model="form.config.defaultContentType"></el-input>
<el-select
v-model="form.config.defaultContentType"
filterable
allow-create
default-first-option
placeholder="请选择"
@change="saveServerConfig"
style="width: 100%;"
>
<el-option
v-for="item in contentTypeOptions"
:key="item"
:label="item"
:value="item"
></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
@@ -196,12 +244,13 @@
v-model="form.config.proxyPort"
:min="1"
:max="65535"
@change="saveServerConfig"
></el-input-number>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="目标主机">
<el-input v-model="form.config.targetHost"></el-input>
<el-input v-model="form.config.targetHost" @blur="saveServerConfig"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
@@ -210,12 +259,13 @@
v-model="form.config.targetPort"
:min="1"
:max="65535"
@change="saveServerConfig"
></el-input-number>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="目标 HTTPS">
<el-switch v-model="form.config.targetHttps"></el-switch>
<el-switch v-model="form.config.targetHttps" @change="saveServerConfig"></el-switch>
</el-form-item>
</el-col>
</el-row>
@@ -320,10 +370,12 @@
</el-form-item>
<el-form-item label="Mock 文件路径">
<el-input
v-model="mockFileDialog.form.filePath"
placeholder="mock/test123.json(路径仅英文和数字)"
v-model="mockFileDialog.form.fileName"
placeholder="请输入文件名,如 test123.json"
:disabled="mockFileDialog.mode === 'edit'"
></el-input>
>
<template slot="prepend">mock/</template>
</el-input>
</el-form-item>
<el-form-item label="文件内容">
<el-input
@@ -339,6 +391,25 @@
<el-button type="primary" @click="submitMockFileDialog">保存 Mock 文件</el-button>
</span>
</el-dialog>
<el-dialog
:title="apiDialog.mode === 'edit' ? '修改接口' : '新增接口'"
:visible.sync="apiDialog.visible"
width="500px"
>
<el-form :model="apiDialog.form" label-width="80px">
<el-form-item label="接口名称">
<el-input v-model="apiDialog.form.name" placeholder="如:获取用户信息"></el-input>
</el-form-item>
<el-form-item label="请求路径">
<el-input v-model="apiDialog.form.route" placeholder="如:/api2/user/info"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="apiDialog.visible = false">取消</el-button>
<el-button type="primary" @click="submitApiDialog">确定</el-button>
</span>
</el-dialog>
</div>
</div>
@@ -351,7 +422,18 @@
return {
activeMainTab: "routes",
apiList: [],
apiPagination: { currentPage: 1, pageSize: 10 },
apiDialog: {
visible: false,
mode: "create",
editingIndex: -1,
form: { name: "", route: "", originalRoute: "" },
},
mockFiles: [],
contentTypeOptions: [
"application/json",
"application/x-www-form-urlencoded",
],
commonStatusCodes: [
{ value: 200, label: "200 OK" },
{ value: 201, label: "201 Created" },
@@ -368,6 +450,9 @@
{ value: 502, label: "502 Bad Gateway" },
{ value: 503, label: "503 Service Unavailable" },
],
routeSearch: "",
mockFileSearch: "",
apiSearch: "",
routePagination: {
currentPage: 1,
pageSize: 10,
@@ -397,7 +482,6 @@
apiName: "",
selectedApiRoute: "",
originalRoute: "",
originalRawRoute: "",
route: "",
filePath: "",
statusCode: 200,
@@ -408,13 +492,18 @@
visible: false,
mode: "create",
form: {
filePath: "mock/",
fileName: "",
alias: "",
content: "",
},
},
};
},
watch: {
routeSearch: function () { this.routePagination.currentPage = 1; },
mockFileSearch: function () { this.mockFilePagination.currentPage = 1; },
apiSearch: function () { this.apiPagination.currentPage = 1; },
},
created: async function () {
await this.loadApiList();
await this.loadMockFiles();
@@ -456,22 +545,20 @@
return item.route === route;
});
},
toRouteArray: function (routesObj, routeStatusesObj) {
toRouteArray: function (routesObj, routeStatusesObj, routeEnabledMap, routeApiNameMap) {
var self = this;
return Object.keys(routesObj || {}).map(function (rawRoute) {
var enabled = !rawRoute.startsWith("#");
var route = enabled ? rawRoute : rawRoute.replace(/^#+/, "");
var matched = self.findApiByRoute(route);
return Object.keys(routesObj || {}).map(function (route) {
var storedName = (routeApiNameMap || {})[route] || "";
var matched = storedName ? null : self.findApiByRoute(route);
return {
rawRoute: rawRoute,
route: route,
filePath: routesObj[rawRoute],
filePath: routesObj[route],
statusCode: self.normalizeStatusCode(
routeStatusesObj && routeStatusesObj[rawRoute],
routeStatusesObj && routeStatusesObj[route],
),
apiName: matched ? matched.name : "",
apiName: storedName || (matched ? matched.name : ""),
selectedApiRoute: matched ? matched.route : "",
enabled: enabled,
enabled: (routeEnabledMap || {})[route] !== false,
};
});
},
@@ -481,8 +568,7 @@
var route = (item.route || "").trim();
var filePath = (item.filePath || "").trim();
if (route && filePath) {
var routeKey = item.enabled === false ? "#" + route : route;
obj[routeKey] = filePath;
obj[route] = filePath;
}
});
return obj;
@@ -493,24 +579,42 @@
(routeArray || []).forEach(function (item) {
var route = (item.route || "").trim();
if (!route) return;
var routeKey = item.enabled === false ? "#" + route : route;
var statusCode = self.normalizeStatusCode(item.statusCode);
obj[routeKey] = statusCode;
obj[route] = self.normalizeStatusCode(item.statusCode);
});
return obj;
},
removeRoute: function (index) {
toRouteEnabledMap: function (routeArray) {
var obj = {};
(routeArray || []).forEach(function (item) {
var route = (item.route || "").trim();
if (route) {
obj[route] = item.enabled !== false;
}
});
return obj;
},
toRouteApiNameMap: function (routeArray) {
var obj = {};
(routeArray || []).forEach(function (item) {
var route = (item.route || "").trim();
if (route) {
obj[route] = item.apiName || "";
}
});
return obj;
},
removeRoute: async function (index) {
var actualIndex =
(this.routePagination.currentPage - 1) * this.routePagination.pageSize +
index;
this.form.routes.splice(actualIndex, 1);
await this.saveRoutes();
},
getDefaultRouteForm: function () {
return {
apiName: "",
selectedApiRoute: "",
originalRoute: "",
originalRawRoute: "",
route: "",
filePath: "",
statusCode: 200,
@@ -549,7 +653,6 @@
apiName: matched ? matched.name : item.apiName || "",
selectedApiRoute: matched ? matched.route : "",
originalRoute: item.route || "",
originalRawRoute: item.rawRoute || item.route || "",
route: item.route || "",
filePath: item.filePath || "mock/",
statusCode: this.normalizeStatusCode(item.statusCode),
@@ -559,7 +662,7 @@
},
getDefaultMockFileForm: function () {
return {
filePath: "mock/",
fileName: "",
alias: "",
content: "",
};
@@ -570,9 +673,11 @@
this.mockFileDialog.visible = true;
},
openMockFileDialogForEdit: function (item) {
var full = item.filePath || "";
var name = full.indexOf("mock/") === 0 ? full.slice(5) : full;
this.mockFileDialog.mode = "edit";
this.mockFileDialog.form = {
filePath: item.filePath || "mock/",
fileName: name,
alias: String(item.alias || ""),
content: String(item.content || ""),
};
@@ -593,16 +698,22 @@
}
},
submitMockFileDialog: async function () {
if (!this.mockFileDialog.form.filePath) {
this.$message.error("Mock 文件路径不能为空");
var fileName = (this.mockFileDialog.form.fileName || "").trim();
if (!fileName) {
this.$message.error("文件名不能为空");
return;
}
if (fileName.indexOf("/") !== -1 || fileName.indexOf("\\") !== -1) {
this.$message.error("文件名不能包含路径分隔符");
return;
}
var filePath = "mock/" + fileName;
try {
var resp = await fetch("/__mock-files", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
filePath: this.mockFileDialog.form.filePath,
filePath: filePath,
alias: this.mockFileDialog.form.alias,
content: this.mockFileDialog.form.content,
}),
@@ -663,17 +774,20 @@
this.form.routes = this.toRouteArray(
data.routes || {},
data.routeStatuses || {},
data.routeEnabledMap || {},
data.routeApiNameMap || {},
);
this.routePagination.currentPage = 1;
} catch (err) {
this.$message.error("加载配置失败: " + err.message);
}
},
saveConfig: async function () {
saveRoutes: async function () {
var payload = {
config: this.form.config,
routes: this.toRouteObject(this.form.routes),
routeStatuses: this.toRouteStatusObject(this.form.routes),
routeEnabledMap: this.toRouteEnabledMap(this.form.routes),
routeApiNameMap: this.toRouteApiNameMap(this.form.routes),
};
try {
var resp = await fetch("/__config", {
@@ -685,22 +799,23 @@
if (!resp.ok || data.success === false) {
throw new Error(data.error || "保存失败");
}
this.$message.success("配置已保存");
} catch (err) {
this.$message.error("保存失败: " + err.message);
this.$message.error("保存路由失败: " + err.message);
}
},
reloadConfig: async function () {
saveServerConfig: async function () {
try {
var resp = await fetch("/__reload-config", { method: "POST" });
var resp = await fetch("/__config", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ config: this.form.config }),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "重载失败");
throw new Error(data.error || "保存失败");
}
this.$message.success("配置已重载");
this.loadConfig();
} catch (err) {
this.$message.error("重载失败: " + err.message);
this.$message.error("保存配置失败: " + err.message);
}
},
submitRouteDialog: async function () {
@@ -738,13 +853,127 @@
this.$message.error("创建失败: " + err.message);
}
},
handleApiPageChange: function (page) {
this.apiPagination.currentPage = page;
},
openApiDialogForCreate: function () {
this.apiDialog.mode = "create";
this.apiDialog.editingIndex = -1;
this.apiDialog.form = { name: "", route: "", originalRoute: "" };
this.apiDialog.visible = true;
},
openApiDialogForEdit: function (index) {
var actualIndex =
(this.apiPagination.currentPage - 1) * this.apiPagination.pageSize + index;
var item = this.apiList[actualIndex];
this.apiDialog.mode = "edit";
this.apiDialog.editingIndex = actualIndex;
this.apiDialog.form = {
name: item.name,
route: item.route,
originalRoute: item.route,
};
this.apiDialog.visible = true;
},
submitApiDialog: async function () {
if (!this.apiDialog.form.name) {
this.$message.error("接口名称不能为空");
return;
}
if (!this.apiDialog.form.route || !this.apiDialog.form.route.startsWith("/")) {
this.$message.error("请求路径必须以 / 开头");
return;
}
try {
var resp = await fetch("/__api-list", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(this.apiDialog.form),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "操作失败");
}
this.$message.success(this.apiDialog.mode === "edit" ? "接口已修改" : "接口已添加");
this.apiDialog.visible = false;
await this.loadApiList();
} catch (err) {
this.$message.error("操作失败: " + err.message);
}
},
removeApiItem: async function (index) {
var actualIndex =
(this.apiPagination.currentPage - 1) * this.apiPagination.pageSize + index;
var item = this.apiList[actualIndex];
try {
await this.$confirm("确定删除接口「" + item.name + "」?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
});
var resp = await fetch("/__api-list", {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ route: item.route }),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "删除失败");
}
this.$message.success("接口已删除");
await this.loadApiList();
} catch (err) {
if (err !== "cancel") {
this.$message.error("删除失败: " + err.message);
}
}
},
},
computed: {
filteredRoutes: function () {
var q = (this.routeSearch || "").toLowerCase().trim();
var list = this.form.routes;
if (q) {
list = list.filter(function (item) {
return (
(item.route || "").toLowerCase().indexOf(q) !== -1 ||
(item.apiName || "").toLowerCase().indexOf(q) !== -1 ||
(item.filePath || "").toLowerCase().indexOf(q) !== -1
);
});
}
return list.slice().sort(function (a, b) {
return (b.enabled !== false ? 1 : 0) - (a.enabled !== false ? 1 : 0);
});
},
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
);
});
},
filteredApiList: function () {
var q = (this.apiSearch || "").toLowerCase().trim();
if (!q) return this.apiList;
return this.apiList.filter(function (item) {
return (
(item.route || "").toLowerCase().indexOf(q) !== -1 ||
(item.name || "").toLowerCase().indexOf(q) !== -1
);
});
},
pagedRoutes: function () {
return this.getPagedData(this.form.routes, this.routePagination);
return this.getPagedData(this.filteredRoutes, this.routePagination);
},
pagedMockFiles: function () {
return this.getPagedData(this.mockFiles, this.mockFilePagination);
return this.getPagedData(this.filteredMockFiles, this.mockFilePagination);
},
pagedApiList: function () {
return this.getPagedData(this.filteredApiList, this.apiPagination);
},
},
});