versionController.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package upgrade
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "github.com/astaxie/beego"
  8. )
  9. type VersionController struct {
  10. beego.Controller
  11. }
  12. func init() {
  13. }
  14. //检查是否有新的客户端版本.客户端本地检查接口
  15. // @router /api/version/isnew [get]
  16. func (c *VersionController) IsNewClientVersion() {
  17. if IsNewVersion == 0 {
  18. c.Data["json"] = c.warpOK("")
  19. } else {
  20. c.Data["json"] = c.warpOK(GetNewVersionInfo())
  21. }
  22. c.ServeJSON()
  23. }
  24. //检查是否有新的客户端版本.服务端接口
  25. // @router /api/version/check_client [get]
  26. func (c *VersionController) CheckClientVersion() {
  27. clientVersion := c.GetString("version")
  28. if clientVersion == "" {
  29. c.Data["json"] = c.warpError("无效的客户端版本号")
  30. c.ServeJSON()
  31. return
  32. }
  33. clientVersion = strings.Replace(clientVersion, "-", "", 1)
  34. dir := "./static/client_version"
  35. _, err := os.Stat(dir)
  36. if err != nil {
  37. c.Data["json"] = c.warpError("检查版本信息时异常:" + err.Error())
  38. c.ServeJSON()
  39. return
  40. }
  41. newVersion := map[string]string{"url": "", "version": "", "desc": ""}
  42. if os.IsNotExist(err) {
  43. os.MkdirAll(dir, 0777)
  44. c.Data["json"] = c.warpOK(newVersion)
  45. c.ServeJSON()
  46. return
  47. }
  48. //获取目录 下最新的更新包
  49. //包为zip格式,包名规则为打包时的年月日时,如2022011413表示该包在2022年01月14日13点完成打包,将以此为版本号与客户端版本号进行对比
  50. flist, err := ioutil.ReadDir(dir)
  51. if err != nil {
  52. c.Data["json"] = c.warpError("检查版本信息时异常:" + err.Error())
  53. c.ServeJSON()
  54. return
  55. }
  56. maxVersion := 0
  57. filename := ""
  58. for _, fp := range flist {
  59. if fp.IsDir() {
  60. continue
  61. }
  62. f2 := strings.Split(fp.Name(), ".")
  63. if len(f2) == 1 || f2[1] != "zip" {
  64. continue
  65. }
  66. vvv, err := strconv.Atoi(strings.ReplaceAll(f2[0], "-", ""))
  67. if err != nil {
  68. continue
  69. }
  70. if vvv > maxVersion {
  71. maxVersion = vvv
  72. filename = f2[0]
  73. }
  74. }
  75. clientVersionInt, _ := strconv.Atoi(clientVersion)
  76. if maxVersion > clientVersionInt {
  77. newVersion["version"] = strconv.Itoa(maxVersion)
  78. newVersion["url"] = "static/client_version/" + filename + ".zip"
  79. }
  80. c.Data["json"] = c.warpOK(newVersion)
  81. c.ServeJSON()
  82. }
  83. func (c *VersionController) warpOK(data interface{}) interface{} {
  84. user := make(map[string]interface{})
  85. user["data"] = data
  86. user["returncode"] = 200
  87. user["msg"] = ""
  88. return user
  89. }
  90. func (c *VersionController) warpError(msg string) interface{} {
  91. user := make(map[string]interface{})
  92. user["data"] = ""
  93. user["returncode"] = 500
  94. user["msg"] = msg
  95. return user
  96. }