From 5020b6bc61ae1eeb77bebcc77fc11f6890e00726 Mon Sep 17 00:00:00 2001 From: zhouyonggao <1971162852@qq.com> Date: Thu, 18 Dec 2025 18:27:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=E7=BB=9F=E4=B8=80=E7=94=A8?= =?UTF-8?q?=E6=88=B7ID=E5=8F=82=E6=95=B0=E5=8F=8A=E7=A6=81=E6=AD=A2?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=85=AC=E5=85=B1=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将模板相关接口中用户ID参数"userId"改为"current_user_id" - 修改前端模板列表删除按钮的显示逻辑 - API新增检查删除模板时是否为公共模板,owner_id为0时禁止删除 - 删除模板前检查模板存在性,返回404错误 - 修改调用构建查询字符串时使用"current_user_id"参数 - 更新请求URL构建逻辑,确保传递正确的用户ID参数 --- server/internal/api/templates.go | 18 +++++++++++++++--- web/index.html | 2 +- web/modules/api.js | 5 +++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/server/internal/api/templates.go b/server/internal/api/templates.go index d8c6beb..645ccaa 100644 --- a/server/internal/api/templates.go +++ b/server/internal/api/templates.go @@ -109,8 +109,8 @@ func (api *TemplatesAPI) createTemplate(w http.ResponseWriter, r *http.Request) r = WithPayload(r, payload) - // 介URL参数获取用户ID - if userIDStr := r.URL.Query().Get("userId"); userIDStr != "" { + // 从 URL 参数获取用户ID + if userIDStr := r.URL.Query().Get("current_user_id"); userIDStr != "" { var userID uint64 if _, scanErr := fmt.Sscan(userIDStr, &userID); scanErr == nil && userID > 0 { payload.OwnerID = userID @@ -152,7 +152,7 @@ func (api *TemplatesAPI) createTemplate(w http.ResponseWriter, r *http.Request) // listTemplates 获取模板列表 func (api *TemplatesAPI) listTemplates(w http.ResponseWriter, r *http.Request) { - userIDStr := r.URL.Query().Get("userId") + userIDStr := r.URL.Query().Get("current_user_id") // 构建查询SQL querySQL := `SELECT id, name, datasource, main_table, file_format, visibility, @@ -387,6 +387,18 @@ func (api *TemplatesAPI) patchTemplate(w http.ResponseWriter, r *http.Request, t // deleteTemplate 删除模板 func (api *TemplatesAPI) deleteTemplate(w http.ResponseWriter, r *http.Request, templateID string) { + // 检查是否为公共模板(owner_id=0) + var ownerID uint64 + rowOwner := api.metaDB.QueryRow("SELECT owner_id FROM export_templates WHERE id=?", templateID) + if err := rowOwner.Scan(&ownerID); err != nil { + fail(w, r, http.StatusNotFound, "template not found") + return + } + if ownerID == 0 { + fail(w, r, http.StatusForbidden, "公共模板不允许删除") + return + } + // 检查是否有关联的导出任务 var jobCount int64 row := api.metaDB.QueryRow("SELECT COUNT(1) FROM export_jobs WHERE template_id=?", templateID) diff --git a/web/index.html b/web/index.html index 59194e7..18e3e37 100644 --- a/web/index.html +++ b/web/index.html @@ -45,7 +45,7 @@ size="small" @click="openEdit(scope.row)">编辑 删除 diff --git a/web/modules/api.js b/web/modules/api.js index d524e7d..3970792 100644 --- a/web/modules/api.js +++ b/web/modules/api.js @@ -41,13 +41,13 @@ const getMerchantId = () => { /** * 构建用户相关的查询字符串 - * @returns {string} 查询字符串,如 '?userId=1&merchantId=2' + * @returns {string} 查询字符串,如 '?current_user_id=1&merchantId=2' */ const buildUserQueryString = () => { const userId = getUserId(); const merchantId = getMerchantId(); const parts = []; - if (userId) parts.push('userId=' + encodeURIComponent(userId)); + if (userId) parts.push('current_user_id=' + encodeURIComponent(userId)); if (merchantId) parts.push('merchantId=' + encodeURIComponent(merchantId)); return parts.length ? ('?' + parts.join('&')) : ''; }; @@ -95,6 +95,7 @@ const get = async (endpoint, options = {}) => { const userId = getUserId(); const merchantId = getMerchantId(); if (userId) queryParams.set('userId', userId); + if (userId) queryParams.set('current_user_id', userId); if (merchantId) queryParams.set('merchantId', merchantId); }