const.go 11 KB

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