package upgrade import ( "io/ioutil" "os" "strconv" "strings" "github.com/astaxie/beego" ) type VersionController struct { beego.Controller } func init() { } //检查是否有新的客户端版本.客户端本地检查接口 // @Summary 检查是否有新的客户端版本 // @router /version/isnew [get] func (c *VersionController) IsNewClientVersion() { if IsNewVersion == 0 { c.Data["json"] = c.warpOK("") } else { c.Data["json"] = c.warpOK(GetNewVersionInfo()) } c.ServeJSON() } //检查是否有新的客户端版本.服务端接口 // @Summary 获取最新客户端版本下载地址 // @router /version/check_client [get] func (c *VersionController) CheckClientVersion() { clientVersion := c.GetString("version") if clientVersion == "" { c.Data["json"] = c.warpError("无效的客户端版本号") c.ServeJSON() return } clientVersion = strings.Replace(clientVersion, "-", "", 1) dir := "./static/client_version" _, err := os.Stat(dir) if err != nil { c.Data["json"] = c.warpError("检查版本信息时异常:" + err.Error()) c.ServeJSON() return } newVersion := map[string]string{"url": "", "version": "", "desc": ""} if os.IsNotExist(err) { os.MkdirAll(dir, 0777) c.Data["json"] = c.warpOK(newVersion) c.ServeJSON() return } //获取目录 下最新的更新包 //包为zip格式,包名规则为打包时的年月日时,如2022011413表示该包在2022年01月14日13点完成打包,将以此为版本号与客户端版本号进行对比 flist, err := ioutil.ReadDir(dir) if err != nil { c.Data["json"] = c.warpError("检查版本信息时异常:" + err.Error()) c.ServeJSON() return } maxVersion := 0 filename := "" for _, fp := range flist { if fp.IsDir() { continue } f2 := strings.Split(fp.Name(), ".") if len(f2) == 1 || f2[1] != "zip" { continue } vvv, err := strconv.Atoi(strings.ReplaceAll(f2[0], "-", "")) if err != nil { continue } if vvv > maxVersion { maxVersion = vvv filename = f2[0] } } clientVersionInt, _ := strconv.Atoi(clientVersion) if maxVersion > clientVersionInt { newVersion["version"] = strconv.Itoa(maxVersion) newVersion["url"] = "static/client_version/" + filename + ".zip" } c.Data["json"] = c.warpOK(newVersion) c.ServeJSON() } func (c *VersionController) warpOK(data interface{}) interface{} { user := make(map[string]interface{}) user["data"] = data user["returncode"] = 200 user["msg"] = "" return user } func (c *VersionController) warpError(msg string) interface{} { user := make(map[string]interface{}) user["data"] = "" user["returncode"] = 500 user["msg"] = msg return user }