const.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-common/app/service/main/archive/api"
  6. "go-common/app/service/main/archive/model/archive"
  7. "net/url"
  8. "strings"
  9. )
  10. const (
  11. // PlatAndroid is int8 for android.
  12. PlatAndroid = int8(0)
  13. // PlatIPhone is int8 for iphone.
  14. PlatIPhone = int8(1)
  15. // PlatIPad is int8 for ipad.
  16. PlatIPad = int8(2)
  17. // PlatWPhone is int8 for wphone.
  18. PlatWPhone = int8(3)
  19. // PlatAndroidG is int8 for Android Global.
  20. PlatAndroidG = int8(4)
  21. // PlatIPhoneI is int8 for Iphone Global.
  22. PlatIPhoneI = int8(5)
  23. // PlatIPadI is int8 for IPAD Global.
  24. PlatIPadI = int8(6)
  25. // PlatAndroidTV is int8 for AndroidTV Global.
  26. PlatAndroidTV = int8(7)
  27. // PlatAndroidI is int8 for Android Global.
  28. PlatAndroidI = int8(8)
  29. GotoAv = "av"
  30. GotoWeb = "web"
  31. GotoBangumi = "bangumi"
  32. GotoPGC = "pgc"
  33. GotoLive = "live"
  34. GotoGame = "game"
  35. GotoAdAv = "ad_av"
  36. GotoAdWeb = "ad_web"
  37. GotoRank = "rank"
  38. GotoBangumiRcmd = "bangumi_rcmd"
  39. GotoLogin = "login"
  40. GotoBanner = "banner"
  41. GotoAdWebS = "ad_web_s"
  42. GotoConverge = "converge"
  43. GotoSpecial = "special"
  44. GotoArticle = "article"
  45. GotoArticleS = "article_s"
  46. GotoGameDownloadS = "game_download_s"
  47. GotoShoppingS = "shopping_s"
  48. GotoAudio = "audio"
  49. GotoPlayer = "player"
  50. GotoAdLarge = "ad_large"
  51. GotoSpecialS = "special_s"
  52. GotoPlayerLive = "player_live"
  53. GotoSong = "song"
  54. GotoLiveUpRcmd = "live_up_rcmd"
  55. GotoUpRcmdAv = "up_rcmd_av"
  56. GotoSubscribe = "subscribe"
  57. GotoSearchSubscribe = "search_subscribe"
  58. GotoChannelRcmd = "channel_rcmd"
  59. GotoMoe = "moe"
  60. // GotoAuthor is search
  61. GotoAuthor = "author"
  62. GotoSp = "sp"
  63. GotoMovie = "movie"
  64. GotoEP = "ep"
  65. GotoSuggestKeyWord = "suggest_keyword"
  66. GotoRecommendWord = "recommend_word"
  67. GotoTwitter = "twitter"
  68. GotoChannel = "channel"
  69. FromOrder = "order"
  70. FromOperation = "operation"
  71. FromRcmd = "recommend"
  72. CoverIng = "即将上映"
  73. CoverPay = "付费观看"
  74. CoverFree = "免费观看"
  75. CoverVipFree = "付费观看"
  76. CoverVipOnly = "专享"
  77. CoverVipFirst = "抢先"
  78. Hans = "hans"
  79. Hant = "hant"
  80. // ForbidCode is forbid by law
  81. ForbidCode = -110
  82. NoResultCode = -111
  83. )
  84. var (
  85. // AvHandler is handler
  86. AvHandler = func(a *api.Arc, trackid string, ap *archive.PlayerInfo) func(uri string) string {
  87. var player string
  88. if ap != nil {
  89. bs, _ := json.Marshal(ap)
  90. player = url.QueryEscape(string(bs))
  91. if strings.IndexByte(player, '+') > -1 {
  92. player = strings.Replace(player, "+", "%20", -1)
  93. }
  94. }
  95. return func(uri string) string {
  96. if a == nil {
  97. return uri
  98. }
  99. var uriStr string
  100. if player != "" && (a.Dimension.Height != 0 || a.Dimension.Width != 0) {
  101. uriStr = 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)
  102. } else if player != "" {
  103. uriStr = fmt.Sprintf("%s?page=1&player_preload=%s", uri, player)
  104. } else if a.Dimension.Height != 0 || a.Dimension.Width != 0 {
  105. uriStr = fmt.Sprintf("%s?player_width=%d&player_height=%d&player_rotate=%d", uri, a.Dimension.Width, a.Dimension.Height, a.Dimension.Rotate)
  106. }
  107. if trackid != "" {
  108. if uriStr == "" {
  109. uriStr = fmt.Sprintf("%s?trackid=%s", uri, trackid)
  110. } else {
  111. uriStr = fmt.Sprintf("%s&trackid=%s", uriStr, trackid)
  112. }
  113. }
  114. if uriStr != "" {
  115. return uriStr
  116. }
  117. return uri
  118. }
  119. }
  120. )
  121. // TWLocale is taiwan locale
  122. func TWLocale(locale string) bool {
  123. var twLocalem = map[string]struct{}{
  124. "zh_hk": struct{}{},
  125. "zh_mo": struct{}{},
  126. "zh_tw": struct{}{},
  127. }
  128. _, ok := twLocalem[strings.ToLower(locale)]
  129. return ok
  130. }
  131. // Plat return plat by platStr or mobiApp
  132. func Plat(mobiApp, device string) int8 {
  133. switch mobiApp {
  134. case "iphone":
  135. if device == "pad" {
  136. return PlatIPad
  137. }
  138. return PlatIPhone
  139. case "white":
  140. return PlatIPhone
  141. case "ipad":
  142. return PlatIPad
  143. case "android", "android_b":
  144. return PlatAndroid
  145. case "win":
  146. return PlatWPhone
  147. case "android_G":
  148. return PlatAndroidG
  149. case "android_i":
  150. return PlatAndroidI
  151. case "iphone_i":
  152. if device == "pad" {
  153. return PlatIPadI
  154. }
  155. return PlatIPhoneI
  156. case "ipad_i":
  157. return PlatIPadI
  158. case "android_tv":
  159. return PlatAndroidTV
  160. }
  161. return PlatIPhone
  162. }
  163. // IsAndroid check plat is android or ipad.
  164. func IsAndroid(plat int8) bool {
  165. return plat == PlatAndroid || plat == PlatAndroidG
  166. }
  167. // IsIOS check plat is iphone or ipad.
  168. func IsIOS(plat int8) bool {
  169. return plat == PlatIPad || plat == PlatIPhone || plat == PlatIPadI || plat == PlatIPhoneI
  170. }
  171. // IsIPhone check plat is iphone.
  172. func IsIPhone(plat int8) bool {
  173. return plat == PlatIPhone || plat == PlatIPhoneI
  174. }
  175. // IsIPad check plat is pad.
  176. func IsIPad(plat int8) bool {
  177. return plat == PlatIPad || plat == PlatIPadI
  178. }
  179. // IsOverseas is overseas
  180. func IsOverseas(plat int8) bool {
  181. return plat == PlatAndroidI || plat == PlatIPhoneI || plat == PlatIPadI
  182. }
  183. // FillURI deal app schema.
  184. func FillURI(gt, param string, f func(uri string) string) (uri string) {
  185. if param == "" {
  186. return
  187. }
  188. switch gt {
  189. case GotoAv, "":
  190. uri = "bilibili://video/" + param
  191. case GotoLive:
  192. uri = "bilibili://live/" + param
  193. case GotoBangumi:
  194. uri = "bilibili://bangumi/season/" + param
  195. case GotoGame:
  196. uri = "bilibili://game_center/detail?id=" + param + "&sourceType=adPut"
  197. case GotoSp:
  198. uri = "bilibili://splist/" + param
  199. case GotoAuthor:
  200. uri = "bilibili://author/" + param
  201. case GotoArticle:
  202. uri = "bilibili://article/" + param
  203. case GotoWeb:
  204. uri = param
  205. case GotoPGC:
  206. uri = "https://www.bilibili.com/bangumi/play/ss" + param
  207. case GotoChannel:
  208. uri = "bilibili://pegasus/channel/" + param + "/"
  209. case GotoEP:
  210. uri = "https://www.bilibili.com/bangumi/play/ep" + param
  211. case GotoTwitter:
  212. uri = "bilibili://pictureshow/detail/" + param
  213. default:
  214. return
  215. }
  216. if f != nil {
  217. uri = f(uri)
  218. }
  219. return
  220. }
  221. // InvalidBuild check source build is not allow by config build and condition.
  222. // eg: when condition is gt, means srcBuild must gt cfgBuild, otherwise is invalid srcBuild.
  223. func InvalidBuild(srcBuild, cfgBuild int, cfgCond string) bool {
  224. if cfgBuild != 0 && cfgCond != "" {
  225. switch cfgCond {
  226. case "gt":
  227. if cfgBuild >= srcBuild {
  228. return true
  229. }
  230. case "lt":
  231. if cfgBuild <= srcBuild {
  232. return true
  233. }
  234. case "eq":
  235. if cfgBuild != srcBuild {
  236. return true
  237. }
  238. case "ne":
  239. if cfgBuild == srcBuild {
  240. return true
  241. }
  242. }
  243. }
  244. return false
  245. }
  246. // StatusMark cover status mark
  247. func StatusMark(status int) string {
  248. if status == 0 {
  249. return CoverIng
  250. } else if status == 1 {
  251. return CoverPay
  252. } else if status == 2 {
  253. return CoverFree
  254. } else if status == 3 {
  255. return CoverVipFree
  256. } else if status == 4 {
  257. return CoverVipOnly
  258. } else if status == 5 {
  259. return CoverVipFirst
  260. }
  261. return ""
  262. }