DESKTOP-HN5QP3V\Administrator 3 недель назад
Родитель
Сommit
c7e266b78e
2 измененных файлов с 44 добавлено и 18 удалено
  1. 19 6
      app/api/v1/version.go
  2. 25 12
      app/task/version.go

+ 19 - 6
app/api/v1/version.go

@@ -5,22 +5,35 @@ import (
 	"dsbqj-admin/pkg/app"
 	"dsbqj-admin/pkg/e"
 	"dsbqj-admin/pkg/serializer"
-	"github.com/gin-gonic/gin"
 	"net/http"
+
+	"github.com/gin-gonic/gin"
 )
 
+func isOldVersion(v string) bool {
+	// 比如 < 1.2.0 都算老版本
+	return v <= "1.0.67"
+}
+
 func CheckVersion(c *gin.Context) {
 	var appG = app.Gin{C: c}
 	service := service.VersionCheckService{}
-	if err := c.ShouldBind(&service); err == nil {
-		if res, err := service.Check(); err != nil {
-			appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
+	err := c.ShouldBind(&service)
+	if err != nil {
+		if isOldVersion(service.Version) {
+			service.Channel = "wx"
 		} else {
-			appG.Response(http.StatusOK, e.SUCCESS, serializer.BuildVersion(res))
+			appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
+			return
 		}
-	} else {
+	}
+
+	if res, err := service.Check(); err != nil {
 		appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error())
+	} else {
+		appG.Response(http.StatusOK, e.SUCCESS, serializer.BuildVersion(res))
 	}
+
 }
 
 func ServerVersion(c *gin.Context) {

+ 25 - 12
app/task/version.go

@@ -13,6 +13,10 @@ var VersionTask *Versions
 
 type Versions struct {
 	sync.Mutex
+	VChannels util.SafeMap[string, *ChannelVersions]
+}
+
+type ChannelVersions struct {
 	VList util.SafeArray[*version.Version]
 	VMap  util.SafeMap[string, *version.Version]
 }
@@ -26,21 +30,29 @@ func (this *Versions) Exec() {
 	var versions = make([]*version.Version, 0)
 	err := mgm.Coll(&version.Version{}).SimpleFind(&versions, bson.M{})
 	if err == nil {
-		// 重置
-		this.VList.Flush()
-		this.VList.PushMany(versions)
-		this.VMap.Clear()
+		this.VChannels.Clear()
 		for _, v := range versions {
-			this.VMap.Set(v.Version, v)
+			if _, ok := this.VChannels.Get(v.Channel); !ok {
+				this.VChannels.Set(v.Channel, new(ChannelVersions))
+			}
+
+			channelVersion, _ := this.VChannels.Get(v.Channel)
+			channelVersion.VList.Push(v)
+			channelVersion.VMap.Set(v.Version, v)
 		}
 	}
 }
 
 func (this *Versions) Check(version string, channel string) *version.Version {
-	data, ok := this.VMap.Get(version)
+	channelVersions, ok := this.VChannels.Get(channel)
 	if !ok { // 遍历列表
-		for _, v := range this.VList.View() {
-			if v.Default && v.Channel == channel {
+		return nil
+	}
+
+	data, ok := channelVersions.VMap.Get(version)
+	if !ok {
+		for _, v := range channelVersions.VList.View() {
+			if v.Default && v.Version == version && v.Channel == channel {
 				data = v
 				break
 			}
@@ -52,11 +64,12 @@ func (this *Versions) Check(version string, channel string) *version.Version {
 
 func (this *Versions) Servers(channel string) []*version.Version {
 	var res = make([]*version.Version, 0)
-	for _, v := range this.VList.View() {
-		if v.Channel == channel {
-			res = append(res, v)
+	if channelVersions, ok := this.VChannels.Get(channel); ok {
+		for _, v := range channelVersions.VList.View() {
+			if v.Channel == channel {
+				res = append(res, v)
+			}
 		}
 	}
-	  
 	return res
 }