const.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "go-common/app/interface/main/app-card/model/card/live"
  9. "go-common/app/service/main/archive/model/archive"
  10. )
  11. const (
  12. // PlatAndroid is int8 for android.
  13. PlatAndroid = int8(0)
  14. // PlatIPhone is int8 for iphone.
  15. PlatIPhone = int8(1)
  16. // PlatIPad is int8 for ipad.
  17. PlatIPad = int8(2)
  18. // PlatWPhone is int8 for wphone.
  19. PlatWPhone = int8(3)
  20. // PlatAndroidG is int8 for Android Global.
  21. PlatAndroidG = int8(4)
  22. // PlatIPhoneI is int8 for Iphone Global.
  23. PlatIPhoneI = int8(5)
  24. // PlatIPadI is int8 for IPAD Global.
  25. PlatIPadI = int8(6)
  26. // PlatAndroidTV is int8 for AndroidTV Global.
  27. PlatAndroidTV = int8(7)
  28. // PlatAndroidI is int8 for Android Global.
  29. PlatAndroidI = int8(8)
  30. // PlatAndroidB is int8 for android_b
  31. PlatAndroidB = int8(10)
  32. // PlatIPhoneB is int8 for iphone_b
  33. PlatIPhoneB = int8(11)
  34. GotoAv = "av"
  35. GotoWeb = "web"
  36. GotoBangumi = "bangumi"
  37. GotoPGC = "pgc"
  38. GotoLive = "live"
  39. GotoGame = "game"
  40. GotoAdAv = "ad_av"
  41. GotoAdWeb = "ad_web"
  42. GotoRank = "rank"
  43. GotoBangumiRcmd = "bangumi_rcmd"
  44. GotoLogin = "login"
  45. GotoUpBangumi = "up_bangumi"
  46. GotoBanner = "banner"
  47. GotoAdWebS = "ad_web_s"
  48. GotoUpArticle = "up_article"
  49. GotoConverge = "converge"
  50. GotoSpecial = "special"
  51. GotoArticle = "article"
  52. GotoArticleS = "article_s"
  53. GotoGameDownloadS = "game_download_s"
  54. GotoShoppingS = "shopping_s"
  55. GotoAudio = "audio"
  56. GotoPlayer = "player"
  57. GotoAdLarge = "ad_large"
  58. GotoSpecialS = "special_s"
  59. GotoPlayerLive = "player_live"
  60. GotoSong = "song"
  61. GotoLiveUpRcmd = "live_up_rcmd"
  62. GotoUpRcmdAv = "up_rcmd_av"
  63. GotoSubscribe = "subscribe"
  64. GotoSearchSubscribe = "search_subscribe"
  65. GotoChannelRcmd = "channel_rcmd"
  66. GotoMoe = "moe"
  67. GotoPicture = "picture"
  68. GotoInterest = "interest"
  69. GotoFollowMode = "follow_mode"
  70. // for fill uri
  71. GotoAudioTag = "audio_tag"
  72. GotoAlbum = "album"
  73. GotoClip = "clip"
  74. GotoDaily = "daily"
  75. // extra tab
  76. GotoTabBackground = "background"
  77. GotoTabEntrance = "entrance"
  78. GotoTabContentRcmd = "content_rcmd"
  79. GotoTabTagRcmd = "tag_rcmd"
  80. GotoTabSignIn = "sign_in"
  81. GotoTabNews = "news"
  82. )
  83. var (
  84. OperateType = map[int]string{
  85. 0: GotoWeb,
  86. 1: GotoGame,
  87. 2: GotoAv,
  88. 3: GotoBangumi,
  89. 4: GotoLive,
  90. 6: GotoArticleS,
  91. 7: GotoDaily,
  92. 8: GotoAudio,
  93. 9: GotoSong,
  94. 10: GotoAlbum,
  95. 11: GotoClip,
  96. }
  97. AudioHandler = func(uri string) string {
  98. return uri + "?from=tianma"
  99. }
  100. AvPlayHandler = func(a *archive.Archive3, ap *archive.PlayerInfo) func(uri string) string {
  101. var player string
  102. if ap != nil {
  103. bs, _ := json.Marshal(ap)
  104. player = url.QueryEscape(string(bs))
  105. if strings.IndexByte(player, '+') > -1 {
  106. player = strings.Replace(player, "+", "%20", -1)
  107. }
  108. }
  109. return func(uri string) string {
  110. if player != "" && (a.Dimension.Height != 0 || a.Dimension.Width != 0) {
  111. return fmt.Sprintf("%s?page=1&player_preload=%s&player_width=%d&player_height=%d&player_rotate=%d", uri, player, a.Dimension.Width, a.Dimension.Height, a.Dimension.Rotate)
  112. } else if player != "" {
  113. return fmt.Sprintf("%s?page=1&player_preload=%s", uri, player)
  114. } else if a.Dimension.Height != 0 || a.Dimension.Width != 0 {
  115. return fmt.Sprintf("%s?player_width=%d&player_height=%d&player_rotate=%d", uri, a.Dimension.Width, a.Dimension.Height, a.Dimension.Rotate)
  116. }
  117. return uri
  118. }
  119. }
  120. AvGameHandler = func(uri string) string {
  121. return uri + "&sourceType=adPut"
  122. }
  123. LiveUpHandler = func(l *live.Card) func(uri string) string {
  124. return func(uri string) string {
  125. if l == nil {
  126. return uri
  127. }
  128. return fmt.Sprintf("%s?broadcast_type=%d", uri, l.BroadcastType)
  129. }
  130. }
  131. LiveRoomHandler = func(l *live.Room) func(uri string) string {
  132. return func(uri string) string {
  133. if l == nil {
  134. return uri
  135. }
  136. return fmt.Sprintf("%s?broadcast_type=%d", uri, l.BroadcastType)
  137. }
  138. }
  139. )
  140. // IsAndroid check plat is android or ipad.
  141. func IsAndroid(plat int8) bool {
  142. return plat == PlatAndroid || plat == PlatAndroidG
  143. }
  144. // IsIOS check plat is iphone or ipad.
  145. func IsIOS(plat int8) bool {
  146. return plat == PlatIPad || plat == PlatIPhone || plat == PlatIPadI || plat == PlatIPhoneI || plat == PlatIPhoneB
  147. }
  148. // IsIPad check plat is pad.
  149. func IsIPad(plat int8) bool {
  150. return plat == PlatIPad || plat == PlatIPadI
  151. }
  152. // IsOverseas is overseas
  153. func IsOverseas(plat int8) bool {
  154. return plat == PlatAndroidI || plat == PlatIPhoneI || plat == PlatIPadI
  155. }
  156. // IsIPhoneB check plat is ios but not iphone_b.
  157. func IsIOSNormal(plat int8) bool {
  158. return plat == PlatIPad || plat == PlatIPhone || plat == PlatIPadI || plat == PlatIPhoneI
  159. }
  160. // FillURI deal app schema.
  161. func FillURI(gt, param string, plat int8, build int, f func(uri string) string) (uri string) {
  162. if param == "" {
  163. return
  164. }
  165. switch gt {
  166. case GotoAv, GotoAdAv, GotoChannelRcmd, "":
  167. uri = "bilibili://video/" + param
  168. case GotoLive:
  169. uri = "bilibili://live/" + param
  170. case GotoBangumi:
  171. uri = "https://www.bilibili.com/bangumi/play/ep" + param
  172. case GotoUpBangumi:
  173. uri = "https://www.bilibili.com/bangumi/play/ss" + param
  174. case GotoUpArticle, GotoArticle, GotoArticleS:
  175. uri = "bilibili://article/" + param
  176. case GotoGame:
  177. const (
  178. _iPhoneGameCenter = 6500
  179. _androidGameCenter = 519010
  180. )
  181. // TODO FUCK GAME
  182. if (plat == PlatAndroid && build >= _androidGameCenter) || (plat == PlatIPhone && build >= _iPhoneGameCenter) || plat == PlatIPhoneB {
  183. uri = "bilibili://game_center/detail?id=" + param + "&sourceType=adPut"
  184. } else {
  185. uri = "bilibili://game/" + param
  186. }
  187. case GotoAudio:
  188. uri = "bilibili://music/menu/detail/" + param
  189. case GotoSong:
  190. uri = "bilibili://music/detail/" + param
  191. case GotoAudioTag:
  192. uri = "bilibili://music/categorydetail/" + param
  193. case GotoDaily:
  194. uri = "bilibili://pegasus/list/daily/" + param
  195. case GotoAlbum:
  196. uri = "bilibili://album/" + param
  197. case GotoClip:
  198. uri = "bilibili://clip/" + param
  199. case GotoWeb, GotoAdWeb, GotoRank, GotoAdWebS, GotoShoppingS, GotoAdLarge:
  200. uri = param
  201. }
  202. if f != nil {
  203. uri = f(uri)
  204. }
  205. return
  206. }
  207. func FillRedirect(gt string, typ int) (redirect string) {
  208. switch gt {
  209. case GotoSpecial, GotoConverge, GotoGameDownloadS, GotoSpecialS:
  210. switch typ {
  211. case 7:
  212. redirect = "daily"
  213. case 6:
  214. redirect = "article"
  215. case 5:
  216. redirect = "category/65541"
  217. case 4:
  218. redirect = "live"
  219. case 3:
  220. redirect = ""
  221. case 2:
  222. redirect = "video"
  223. case 1:
  224. redirect = "game"
  225. case 0:
  226. redirect = ""
  227. }
  228. }
  229. return
  230. }
  231. // CoverURL convert cover url to full url.
  232. func CoverURL(uri string) (cover string) {
  233. if uri == "" {
  234. cover = "http://static.hdslb.com/images/transparent.gif"
  235. return
  236. }
  237. if strings.HasPrefix(uri, "http://i0.hdslb.com") || strings.HasPrefix(uri, "http://i1.hdslb.com") || strings.HasPrefix(uri, "http://i2.hdslb.com") {
  238. uri = uri[19:]
  239. } else if strings.HasPrefix(uri, "https://i0.hdslb.com") || strings.HasPrefix(uri, "https://i1.hdslb.com") || strings.HasPrefix(uri, "https://i2.hdslb.com") {
  240. uri = uri[20:]
  241. }
  242. cover = uri
  243. if strings.HasPrefix(uri, "/bfs") {
  244. cover = "http://i0.hdslb.com" + uri
  245. return
  246. }
  247. if strings.Index(uri, "http://") == 0 {
  248. return
  249. }
  250. if len(uri) >= 10 && uri[:10] == "/templets/" {
  251. return
  252. }
  253. if strings.HasPrefix(uri, "group1") || strings.HasPrefix(uri, "/group1") {
  254. cover = "http://i0.hdslb.com/" + uri
  255. return
  256. }
  257. if pos := strings.Index(uri, "/uploads/"); pos != -1 && (pos == 0 || pos == 3) {
  258. cover = uri[pos+8:]
  259. }
  260. cover = strings.Replace(cover, "{IMG}", "", -1)
  261. cover = "http://i0.hdslb.com" + cover
  262. return
  263. }
  264. func CoverURLHTTPS(uri string) (cover string) {
  265. if strings.HasPrefix(uri, "http://") {
  266. cover = "https://" + uri[7:]
  267. } else {
  268. cover = uri
  269. }
  270. return
  271. }
  272. // InvalidBuild check source build is not allow by config build and condition.
  273. // eg: when condition is gt, means srcBuild must gt cfgBuild, otherwise is invalid srcBuild.
  274. func InvalidBuild(srcBuild, cfgBuild int, cfgCond string) bool {
  275. if cfgBuild != 0 && cfgCond != "" {
  276. switch cfgCond {
  277. case "gt":
  278. if cfgBuild >= srcBuild {
  279. return true
  280. }
  281. case "lt":
  282. if cfgBuild <= srcBuild {
  283. return true
  284. }
  285. case "eq":
  286. if cfgBuild != srcBuild {
  287. return true
  288. }
  289. case "ne":
  290. if cfgBuild == srcBuild {
  291. return true
  292. }
  293. }
  294. }
  295. return false
  296. }
  297. // InvalidChannel check source channel is not allow by config channel.
  298. func InvalidChannel(plat int8, srcCh, cfgCh string) bool {
  299. return plat == PlatAndroid && cfgCh != "*" && cfgCh != srcCh
  300. }
  301. // Plat return plat by platStr or mobiApp
  302. func Plat(mobiApp, device string) int8 {
  303. switch mobiApp {
  304. case "iphone":
  305. if device == "pad" {
  306. return PlatIPad
  307. }
  308. return PlatIPhone
  309. case "white":
  310. return PlatIPhone
  311. case "ipad":
  312. return PlatIPad
  313. case "android", "android_b":
  314. return PlatAndroid
  315. case "win":
  316. return PlatWPhone
  317. case "android_G":
  318. return PlatAndroidG
  319. case "android_i":
  320. return PlatAndroidI
  321. case "iphone_i":
  322. if device == "pad" {
  323. return PlatIPadI
  324. }
  325. return PlatIPhoneI
  326. case "ipad_i":
  327. return PlatIPadI
  328. case "android_tv":
  329. return PlatAndroidTV
  330. case "iphone_b":
  331. return PlatIPhoneB
  332. }
  333. return PlatIPhone
  334. }
  335. type SortInt64 []int64
  336. func (is SortInt64) Len() int { return len(is) }
  337. func (is SortInt64) Less(i, j int) bool { return is[i] > is[j] }
  338. func (is SortInt64) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
  339. // AdAvIsNormal check advert archive normal.
  340. func AdAvIsNormal(a *archive.ArchiveWithPlayer) bool {
  341. if a == nil || a.Archive3 == nil {
  342. return false
  343. }
  344. return a.State >= 0 || a.State == -6 || a.State == -40
  345. }
  346. func Rounding(number, divisor int64) string {
  347. if divisor > 0 {
  348. tmp := float64(number) / float64(divisor)
  349. tmpStr := fmt.Sprintf("%0.1f", tmp)
  350. parts := strings.Split(tmpStr, ".")
  351. if len(parts) > 1 && parts[1] == "0" {
  352. return parts[0]
  353. }
  354. return tmpStr
  355. }
  356. return strconv.FormatInt(number, 10)
  357. }
  358. func PlatAPPBuleChange(plat int8) int8 {
  359. switch plat {
  360. case PlatAndroidB:
  361. return PlatAndroid
  362. case PlatIPhoneB:
  363. return PlatIPhone
  364. }
  365. return plat
  366. }