Merge branch 'jng' into dev
This commit is contained in:
commit
e2d6150bcc
@ -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)
|
||||
}
|
||||
//账号模块
|
||||
{
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user