device.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package blademaster
  2. import (
  3. "strconv"
  4. "go-common/library/net/metadata"
  5. )
  6. const (
  7. // PlatAndroid is int8 for android.
  8. PlatAndroid = int8(0)
  9. // PlatIPhone is int8 for iphone.
  10. PlatIPhone = int8(1)
  11. // PlatIPad is int8 for ipad.
  12. PlatIPad = int8(2)
  13. // PlatWPhone is int8 for wphone.
  14. PlatWPhone = int8(3)
  15. // PlatAndroidG is int8 for Android Global.
  16. PlatAndroidG = int8(4)
  17. // PlatIPhoneI is int8 for Iphone Global.
  18. PlatIPhoneI = int8(5)
  19. // PlatIPadI is int8 for IPAD Global.
  20. PlatIPadI = int8(6)
  21. // PlatAndroidTV is int8 for AndroidTV Global.
  22. PlatAndroidTV = int8(7)
  23. // PlatAndroidI is int8 for Android Global.
  24. PlatAndroidI = int8(8)
  25. // PlatAndroidB is int8 for Android Blue.
  26. PlatAndroidB = int8(9)
  27. // PlatIPhoneB is int8 for Ios Blue
  28. PlatIPhoneB = int8(10)
  29. // PlatBilistudio is int8 for bilistudio
  30. PlatBilistudio = int8(11)
  31. // PlatAndroidTVYST is int8 for AndroidTV_YST Global.
  32. PlatAndroidTVYST = int8(12)
  33. )
  34. // Device is the mobile device model
  35. type Device struct {
  36. Build int64
  37. Buvid string
  38. Buvid3 string
  39. Channel string
  40. Device string
  41. Sid string
  42. RawPlatform string
  43. RawMobiApp string
  44. }
  45. // Mobile is the default handler
  46. func Mobile() HandlerFunc {
  47. return func(ctx *Context) {
  48. req := ctx.Request
  49. dev := new(Device)
  50. dev.Buvid = req.Header.Get("Buvid")
  51. if buvid3, err := req.Cookie("buvid3"); err == nil && buvid3 != nil {
  52. dev.Buvid3 = buvid3.Value
  53. }
  54. if sid, err := req.Cookie("sid"); err == nil && sid != nil {
  55. dev.Sid = sid.Value
  56. }
  57. if build, err := strconv.ParseInt(req.Form.Get("build"), 10, 64); err == nil {
  58. dev.Build = build
  59. }
  60. dev.Channel = req.Form.Get("channel")
  61. dev.Device = req.Form.Get("device")
  62. dev.RawMobiApp = req.Form.Get("mobi_app")
  63. dev.RawPlatform = req.Form.Get("platform")
  64. ctx.Set("device", dev)
  65. if md, ok := metadata.FromContext(ctx); ok {
  66. md[metadata.Device] = dev
  67. }
  68. }
  69. }
  70. // Plat return platform from raw platform and mobiApp
  71. func (d *Device) Plat() int8 {
  72. switch d.RawMobiApp {
  73. case "iphone":
  74. if d.Device == "pad" {
  75. return PlatIPad
  76. }
  77. return PlatIPhone
  78. case "white":
  79. return PlatIPhone
  80. case "ipad":
  81. return PlatIPad
  82. case "android":
  83. return PlatAndroid
  84. case "win":
  85. return PlatWPhone
  86. case "android_G":
  87. return PlatAndroidG
  88. case "android_i":
  89. return PlatAndroidI
  90. case "android_b":
  91. return PlatAndroidB
  92. case "iphone_i":
  93. if d.Device == "pad" {
  94. return PlatIPadI
  95. }
  96. return PlatIPhoneI
  97. case "ipad_i":
  98. return PlatIPadI
  99. case "iphone_b":
  100. return PlatIPhoneB
  101. case "android_tv":
  102. return PlatAndroidTV
  103. case "android_tv_yst":
  104. return PlatAndroidTVYST
  105. case "bilistudio":
  106. return PlatBilistudio
  107. }
  108. return PlatIPhone
  109. }
  110. // IsAndroid check plat is android or ipad.
  111. func (d *Device) IsAndroid() bool {
  112. plat := d.Plat()
  113. return plat == PlatAndroid ||
  114. plat == PlatAndroidG ||
  115. plat == PlatAndroidB ||
  116. plat == PlatAndroidI ||
  117. plat == PlatBilistudio ||
  118. plat == PlatAndroidTV ||
  119. plat == PlatAndroidTVYST
  120. }
  121. // IsIOS check plat is iphone or ipad.
  122. func (d *Device) IsIOS() bool {
  123. plat := d.Plat()
  124. return plat == PlatIPad ||
  125. plat == PlatIPhone ||
  126. plat == PlatIPadI ||
  127. plat == PlatIPhoneI ||
  128. plat == PlatIPhoneB
  129. }
  130. // IsOverseas is overseas
  131. func (d *Device) IsOverseas() bool {
  132. plat := d.Plat()
  133. return plat == PlatAndroidI || plat == PlatIPhoneI || plat == PlatIPadI
  134. }
  135. // InvalidChannel check source channel is not allow by config channel.
  136. func (d *Device) InvalidChannel(cfgCh string) bool {
  137. plat := d.Plat()
  138. return plat == PlatAndroid && cfgCh != "*" && cfgCh != d.Channel
  139. }
  140. // MobiApp by plat
  141. func (d *Device) MobiApp() string {
  142. plat := d.Plat()
  143. switch plat {
  144. case PlatAndroid:
  145. return "android"
  146. case PlatIPhone:
  147. return "iphone"
  148. case PlatIPad:
  149. return "ipad"
  150. case PlatAndroidI:
  151. return "android_i"
  152. case PlatIPhoneI:
  153. return "iphone_i"
  154. case PlatIPadI:
  155. return "ipad_i"
  156. case PlatAndroidG:
  157. return "android_G"
  158. }
  159. return "iphone"
  160. }
  161. // MobiAPPBuleChange is app blue change.
  162. func (d *Device) MobiAPPBuleChange() string {
  163. switch d.RawMobiApp {
  164. case "android_b":
  165. return "android"
  166. case "iphone_b":
  167. return "iphone"
  168. }
  169. return d.RawMobiApp
  170. }