From c976b01e8ab26ff30a9e0c743907cbe62a204977 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Thu, 16 May 2024 23:33:37 +0800 Subject: [PATCH] vercmp --- app/mix/controller/config_op.go | 2 +- app/mix/service/configservice.go | 29 +++++++++++++ library/sutil/sutil.go | 70 ++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 library/sutil/sutil.go diff --git a/app/mix/controller/config_op.go b/app/mix/controller/config_op.go index 01daf725..6ca3a051 100644 --- a/app/mix/controller/config_op.go +++ b/app/mix/controller/config_op.go @@ -45,7 +45,7 @@ func OpGetSupportWxIdList(ctx *gin.Context) { func OpIsThereANewVersionAvailable(ctx *gin.Context) { req := ctx.MustGet("client_req").(*base.BaseRequest) - result, version, ec := service.DefaultConfigService.OpIsThereANewVersionAvailable(ctx, req) + result, version, ec := service.DefaultConfigService.OpIsThereANewVersionAvailableV2(ctx, req) if ec != errcode.ErrCodeOk { logger.Error("OpIsThereANewVersionAvailable fail, ec: %v", ec) ReplyErrCodeMsg(ctx, ec) diff --git a/app/mix/service/configservice.go b/app/mix/service/configservice.go index c2f7974d..415a5a62 100644 --- a/app/mix/service/configservice.go +++ b/app/mix/service/configservice.go @@ -6,8 +6,10 @@ import ( "service/api/consts" "service/api/errcode" "service/apollostruct" + "service/bizcommon/util" "service/library/apollo" "service/library/logger" + "service/library/sutil" "strconv" "strings" @@ -149,6 +151,33 @@ func (s *ConfigService) OpIsThereANewVersionAvailable(ctx *gin.Context, baseRequ return } +func (s *ConfigService) OpIsThereANewVersionAvailableV2(ctx *gin.Context, baseRequest *base.BaseRequest) (result bool, version *apollostruct.Version, ec errcode.ErrCode) { + ec = errcode.ErrCodeOk + result = false + + var key string + if baseRequest.DevType == consts.DevType_Android { + key = consts.AndroidKey + } else if baseRequest.DevType == consts.DevType_Ios { + key = consts.IosKey + } + ver := &apollostruct.Version{} + err := apollo.GetJson(key, ver, apollo.ApolloOpts().SetNamespace("version")) + if err != nil { + logger.Error("Apollo read failed : %v", err) + ec = errcode.ErrCodeApolloReadFail + return + } + logger.Info("apollo_ver, %v", util.ToJson(ver)) + + verCmp := sutil.NewVersionCmp(baseRequest.Version) + if verCmp.Lt(ver.Version) { + result = true + version = ver + } + return +} + func (s *ConfigService) OpGetUploadMediaFailConfigList(ctx *gin.Context) (uploadMediaFailConfigMap map[string]int64, ec errcode.ErrCode) { ec = errcode.ErrCodeOk diff --git a/library/sutil/sutil.go b/library/sutil/sutil.go new file mode 100644 index 00000000..d919d841 --- /dev/null +++ b/library/sutil/sutil.go @@ -0,0 +1,70 @@ +// Copyright 2014 The sutil Author. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package sutil + +import ( + "fmt" + "strings" + //"crypto/sha1" +) + +type VersionCmp struct { + ver string +} + +func NewVersionCmp(ver string) *VersionCmp { + v := &VersionCmp{} + + v.ver = v.fmtver(ver) + return v +} + +func (m *VersionCmp) fmtver(ver string) string { + pvs := strings.Split(ver, ".") + + rv := "" + for _, pv := range pvs { + rv += fmt.Sprintf("%020s", pv) + } + + return rv + +} + +func (m *VersionCmp) Min() string { + return m.fmtver("0") +} + +func (m *VersionCmp) Max() string { + return m.fmtver("99999999999999999999") +} + +func (m *VersionCmp) Lt(ver string) bool { + return m.ver < m.fmtver(ver) +} + +func (m *VersionCmp) Lte(ver string) bool { + return m.ver <= m.fmtver(ver) +} + +func (m *VersionCmp) Gt(ver string) bool { + return m.ver > m.fmtver(ver) +} + +func (m *VersionCmp) Gte(ver string) bool { + return m.ver >= m.fmtver(ver) +} + +func (m *VersionCmp) Eq(ver string) bool { + return m.ver == m.fmtver(ver) +} + +func (m *VersionCmp) Ne(ver string) bool { + return m.ver != m.fmtver(ver) +} + +func (m *VersionCmp) GetFormatVersion() string { + return m.ver +}