From 6300ce4835fd7e24472c0fb6ed0bce6b369f821b Mon Sep 17 00:00:00 2001 From: Martial BE Date: Mon, 1 Apr 2024 09:47:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Fix=20model=20name=20not?= =?UTF-8?q?=20URL=20encoded=20in=20pricing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/pricing.go | 9 +++++++-- router/api-router.go | 4 ++-- web/src/views/Pricing/single.js | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/controller/pricing.go b/controller/pricing.go index 8174832c..d2d96e81 100644 --- a/controller/pricing.go +++ b/controller/pricing.go @@ -3,6 +3,7 @@ package controller import ( "errors" "net/http" + "net/url" "one-api/common" "one-api/model" "one-api/relay/util" @@ -80,10 +81,12 @@ func AddPrice(c *gin.Context) { func UpdatePrice(c *gin.Context) { modelName := c.Param("model") - if modelName == "" { + if modelName == "" || len(modelName) < 2 { common.APIRespondWithError(c, http.StatusOK, errors.New("model name is required")) return } + modelName = modelName[1:] + modelName, _ = url.PathUnescape(modelName) var price model.Price if err := c.ShouldBindJSON(&price); err != nil { @@ -104,10 +107,12 @@ func UpdatePrice(c *gin.Context) { func DeletePrice(c *gin.Context) { modelName := c.Param("model") - if modelName == "" { + if modelName == "" || len(modelName) < 2 { common.APIRespondWithError(c, http.StatusOK, errors.New("model name is required")) return } + modelName = modelName[1:] + modelName, _ = url.PathUnescape(modelName) if err := util.PricingInstance.DeletePrice(modelName); err != nil { common.APIRespondWithError(c, http.StatusOK, err) diff --git a/router/api-router.go b/router/api-router.go index 548e1326..b8742c16 100644 --- a/router/api-router.go +++ b/router/api-router.go @@ -137,8 +137,8 @@ func SetApiRouter(router *gin.Engine) { { pricesRoute.GET("/model_list", controller.GetAllModelList) pricesRoute.POST("/single", controller.AddPrice) - pricesRoute.PUT("/single/:model", controller.UpdatePrice) - pricesRoute.DELETE("/single/:model", controller.DeletePrice) + pricesRoute.PUT("/single/*model", controller.UpdatePrice) + pricesRoute.DELETE("/single/*model", controller.DeletePrice) pricesRoute.POST("/multiple", controller.BatchSetPrices) pricesRoute.PUT("/multiple/delete", controller.BatchDeletePrices) pricesRoute.POST("/sync", controller.SyncPricing) diff --git a/web/src/views/Pricing/single.js b/web/src/views/Pricing/single.js index ef37348a..1b9fee40 100644 --- a/web/src/views/Pricing/single.js +++ b/web/src/views/Pricing/single.js @@ -79,7 +79,8 @@ const Single = ({ ownedby, prices, reloadData }) => { if (oldRow.model == '') { res = await API.post('/api/prices/single', newRow); } else { - res = await API.put('/api/prices/single/' + oldRow.model, newRow); + let modelEncode = encodeURIComponent(oldRow.model); + res = await API.put('/api/prices/single/' + modelEncode, newRow); } const { success, message } = res.data; if (success) { @@ -282,7 +283,8 @@ const Single = ({ ownedby, prices, reloadData }) => { const deletePirces = async (modelName) => { try { - const res = await API.delete('/api/prices/single/' + modelName); + let modelEncode = encodeURIComponent(modelName); + const res = await API.delete('/api/prices/single/' + modelEncode); const { success, message } = res.data; if (success) { showSuccess('保存成功');