report.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "go-common/app/interface/main/push/model"
  9. pushmdl "go-common/app/service/main/push/model"
  10. "go-common/library/log"
  11. )
  12. // PubReport pub report.
  13. func (s *Service) PubReport(c context.Context, r *pushmdl.Report) (err error) {
  14. err = s.dao.PubReport(c, r)
  15. return
  16. }
  17. // ReportOld report old version app
  18. func (s *Service) ReportOld(ctx context.Context, token, buvid, version string, mid int64, pid, timezone int) (err error) {
  19. platform := translatePlatform(pid)
  20. build := ver2build(version, platform)
  21. if build == 0 {
  22. return
  23. }
  24. // 接新上报后的版本不再使用老的上报数据了
  25. switch platform {
  26. case pushmdl.PlatformXiaomi:
  27. // version 5.16
  28. if build >= 516000 {
  29. return
  30. }
  31. case pushmdl.PlatformIPhone:
  32. // version 5.16
  33. if build >= 6140 {
  34. return
  35. }
  36. case pushmdl.PlatformIPad:
  37. // version 1.50
  38. if build >= 12040 {
  39. return
  40. }
  41. default:
  42. // 未识别的平台
  43. return
  44. }
  45. if platform == pushmdl.PlatformIPad && build < 10000 {
  46. platform = pushmdl.PlatformIPhone
  47. }
  48. r := &pushmdl.Report{
  49. APPID: pushmdl.APPIDBBPhone,
  50. PlatformID: platform,
  51. Mid: mid,
  52. Buvid: buvid,
  53. Build: build,
  54. DeviceToken: token,
  55. TimeZone: timezone,
  56. NotifySwitch: 1,
  57. }
  58. err = s.dao.PubReport(ctx, r)
  59. log.Info("pub old report(%+v)", r)
  60. return
  61. }
  62. func translatePlatform(platformID int) int {
  63. switch platformID {
  64. case model.OldPlatformIPhone, model.OldPlatformIPad:
  65. return pushmdl.PlatformIPhone
  66. case model.OldPlatformAndroid, model.OldPlatformAndroidNow:
  67. return pushmdl.PlatformXiaomi
  68. case model.OldPlatformIPadHD:
  69. return pushmdl.PlatformIPad
  70. }
  71. return pushmdl.PlatformUnknown
  72. }
  73. var buildRegex, _ = regexp.Compile(`\((\d+)\)`)
  74. func ver2build(versionAndBuild string, platform int) (res int) {
  75. version := versionAndBuild
  76. // example: 5.12.1(6050) remove '(' suffix
  77. i := strings.Index(version, "(")
  78. if i != -1 {
  79. version = version[0:i]
  80. }
  81. // example: 5.14.0-preview remove '-' suffix
  82. i = strings.Index(version, "-")
  83. if i != -1 {
  84. version = version[0:i]
  85. }
  86. switch platform {
  87. case pushmdl.PlatformIPhone:
  88. res = model.VersionsIPhone[version]
  89. case pushmdl.PlatformIPad:
  90. res = model.VersionsIPad[version]
  91. default:
  92. p := strings.Split(version, ".")
  93. if len(p) < 3 {
  94. return
  95. }
  96. res, _ = strconv.Atoi(p[0] + p[1] + fmt.Sprintf("%03s", p[2]))
  97. }
  98. if res == 0 {
  99. // match as 2_5.10(5960)
  100. matches := buildRegex.FindSubmatch([]byte(versionAndBuild))
  101. if len(matches) > 1 {
  102. res, _ = strconv.Atoi(string(matches[1]))
  103. }
  104. }
  105. return
  106. }