From f27d89f7c21029925cab1e3e3eb65e11affa632c Mon Sep 17 00:00:00 2001 From: JNG <365252428@qq.com> Date: Wed, 25 Mar 2026 14:22:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0app=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/router/router.go | 2 + pkg/service/version/version.go | 100 ++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/pkg/router/router.go b/pkg/router/router.go index 9fb1c2ff..d332b47e 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -65,6 +65,8 @@ func NewRouter() *gin.Engine { SupplierRouter(privateGroup) { v1.POST("version", version.Version) //版本号公共 + v1.POST("store_versions", version.StoreBrandsVersions) + v1.POST("check_version", version.CheckBrandVersion) } //账号模块 { diff --git a/pkg/service/version/version.go b/pkg/service/version/version.go index 08261b2e..b3e12841 100644 --- a/pkg/service/version/version.go +++ b/pkg/service/version/version.go @@ -1,16 +1,46 @@ package version import ( + "encoding/json" + "errors" + "fmt" "fonchain-fiee/pkg/cache" "fonchain-fiee/pkg/model/query" "fonchain-fiee/pkg/model/vo" "fonchain-fiee/pkg/service" + "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" + "github.com/go-redis/redis" ) -func Version(c *gin.Context) { +// handlePanic 统一处理 panic 恢复 +func handlePanic(c *gin.Context) { + if r := recover(); r != nil { + var err error + switch v := r.(type) { + case error: + err = v + case string: + err = fmt.Errorf("panic: %s", v) + default: + err = fmt.Errorf("panic: %v", v) + } + service.Error(c, err) + } +} +// containsVersion 检查版本是否存在于版本列表中 +func containsVersion(versions []string, targetVersion string) bool { + for _, version := range versions { + if version == targetVersion { + return true + } + } + return false +} + +func Version(c *gin.Context) { var req query.VersionQuery if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil { @@ -25,6 +55,70 @@ func Version(c *gin.Context) { } service.Success(c, vo.VersionVo{Version: val}) - return - +} + +func StoreBrandsVersions(c *gin.Context) { + defer handlePanic(c) + + var brands []struct { + Brand string `json:"brand"` + Version []string `json:"version"` + } + + if err := c.ShouldBindBodyWith(&brands, binding.JSON); err != nil { + service.Error(c, err) + return + } + + for _, brand := range brands { + versionData, err := json.Marshal(brand.Version) + if err != nil { + service.Error(c, err) + return + } + + if err = cache.RedisClient.HSet("brands_versions", brand.Brand, versionData).Err(); err != nil { + service.Error(c, err) + return + } + } + + service.Success(c, true) +} + +func CheckBrandVersion(c *gin.Context) { + defer handlePanic(c) + + var brand struct { + Brand string `json:"brand"` + Version string `json:"version"` + } + + if err := c.ShouldBindBodyWith(&brand, binding.JSON); err != nil { + service.Error(c, err) + return + } + + storedVersionsStr, err := cache.RedisClient.HGet("brands_versions", brand.Brand).Result() + + // 品牌不存在,返回 false + if errors.Is(err, redis.Nil) { + service.Success(c, false) + return + } + + if err != nil { + service.Error(c, err) + return + } + + var storedVersions []string + if err = json.Unmarshal([]byte(storedVersionsStr), &storedVersions); err != nil { + service.Error(c, err) + return + } + + // 检查版本是否存在 + versionFound := containsVersion(storedVersions, brand.Version) + service.Success(c, versionFound) }