fix(api): 统一用户ID参数及禁止删除公共模板

- 将模板相关接口中用户ID参数"userId"改为"current_user_id"
- 修改前端模板列表删除按钮的显示逻辑
- API新增检查删除模板时是否为公共模板,owner_id为0时禁止删除
- 删除模板前检查模板存在性,返回404错误
- 修改调用构建查询字符串时使用"current_user_id"参数
- 更新请求URL构建逻辑,确保传递正确的用户ID参数
This commit is contained in:
zhouyonggao 2025-12-18 18:27:25 +08:00
parent a275e71933
commit 5020b6bc61
3 changed files with 19 additions and 6 deletions

View File

@ -109,8 +109,8 @@ func (api *TemplatesAPI) createTemplate(w http.ResponseWriter, r *http.Request)
r = WithPayload(r, payload) r = WithPayload(r, payload)
// 介URL参数获取用户ID // 从 URL 参数获取用户ID
if userIDStr := r.URL.Query().Get("userId"); userIDStr != "" { if userIDStr := r.URL.Query().Get("current_user_id"); userIDStr != "" {
var userID uint64 var userID uint64
if _, scanErr := fmt.Sscan(userIDStr, &userID); scanErr == nil && userID > 0 { if _, scanErr := fmt.Sscan(userIDStr, &userID); scanErr == nil && userID > 0 {
payload.OwnerID = userID payload.OwnerID = userID
@ -152,7 +152,7 @@ func (api *TemplatesAPI) createTemplate(w http.ResponseWriter, r *http.Request)
// listTemplates 获取模板列表 // listTemplates 获取模板列表
func (api *TemplatesAPI) listTemplates(w http.ResponseWriter, r *http.Request) { 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 // 构建查询SQL
querySQL := `SELECT id, name, datasource, main_table, file_format, visibility, 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 删除模板 // deleteTemplate 删除模板
func (api *TemplatesAPI) deleteTemplate(w http.ResponseWriter, r *http.Request, templateID string) { 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 var jobCount int64
row := api.metaDB.QueryRow("SELECT COUNT(1) FROM export_jobs WHERE template_id=?", templateID) row := api.metaDB.QueryRow("SELECT COUNT(1) FROM export_jobs WHERE template_id=?", templateID)

View File

@ -45,7 +45,7 @@
size="small" size="small"
@click="openEdit(scope.row)">编辑</el-button> @click="openEdit(scope.row)">编辑</el-button>
<el-button <el-button
v-if="(!hasUserId) || (Number(scope.row.owner_id)!==0 && Number(scope.row.owner_id)===currentUserId)" v-if="Number(scope.row.owner_id)!==0 && ((!hasUserId) || Number(scope.row.owner_id)===currentUserId)"
size="small" size="small"
type="danger" type="danger"
@click="removeTemplate(scope.row.id)">删除</el-button> @click="removeTemplate(scope.row.id)">删除</el-button>

View File

@ -41,13 +41,13 @@ const getMerchantId = () => {
/** /**
* 构建用户相关的查询字符串 * 构建用户相关的查询字符串
* @returns {string} 查询字符串 '?userId=1&merchantId=2' * @returns {string} 查询字符串 '?current_user_id=1&merchantId=2'
*/ */
const buildUserQueryString = () => { const buildUserQueryString = () => {
const userId = getUserId(); const userId = getUserId();
const merchantId = getMerchantId(); const merchantId = getMerchantId();
const parts = []; 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)); if (merchantId) parts.push('merchantId=' + encodeURIComponent(merchantId));
return parts.length ? ('?' + parts.join('&')) : ''; return parts.length ? ('?' + parts.join('&')) : '';
}; };
@ -95,6 +95,7 @@ const get = async (endpoint, options = {}) => {
const userId = getUserId(); const userId = getUserId();
const merchantId = getMerchantId(); const merchantId = getMerchantId();
if (userId) queryParams.set('userId', userId); if (userId) queryParams.set('userId', userId);
if (userId) queryParams.set('current_user_id', userId);
if (merchantId) queryParams.set('merchantId', merchantId); if (merchantId) queryParams.set('merchantId', merchantId);
} }