bullet_chat.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package bullet_chat
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "hongze/hongze_yb/models/tables/yb_bullet_chat"
  6. "hongze/hongze_yb/models/tables/yb_community_video"
  7. "hongze/hongze_yb/models/tables/yb_config"
  8. "hongze/hongze_yb/models/tables/yb_road_video"
  9. "math/rand"
  10. "time"
  11. )
  12. const (
  13. ConfBulletChatColor = "bullet_chat_color"
  14. )
  15. var (
  16. SourceBulletChatVideo int
  17. SourceBulletChatRoadVideo int
  18. )
  19. func init() {
  20. SourceBulletChatVideo = yb_bullet_chat.SourceBulletVideo
  21. SourceBulletChatRoadVideo = yb_bullet_chat.SourceBulletChatVideo
  22. }
  23. // BulletChatColor 弹幕颜色
  24. type BulletChatColor struct {
  25. Id int `json:"id" description:"颜色ID"`
  26. Color string `json:"color" description:"颜色#"`
  27. Remark string `json:"remark" description:"备注"`
  28. }
  29. // GetRandomColor 随机获取弹幕颜色
  30. func GetRandomColor() (color *BulletChatColor, err error) {
  31. // 读取配置
  32. var cond string
  33. var pars []interface{}
  34. cond += `config_code = ?`
  35. pars = append(pars, ConfBulletChatColor)
  36. confDao := new(yb_config.YbConfig)
  37. conf, e := confDao.Fetch(cond, pars)
  38. if e != nil {
  39. err = errors.New("获取弹幕颜色配置失败, Err: " + e.Error())
  40. return
  41. }
  42. if conf.ConfigID <= 0 {
  43. err = errors.New("获取弹幕颜色配置失败, ID为空")
  44. return
  45. }
  46. if conf.ConfigValue == "" {
  47. err = errors.New("弹幕颜色配置有误")
  48. return
  49. }
  50. colors := make([]*BulletChatColor, 0)
  51. if e = json.Unmarshal([]byte(conf.ConfigValue), &colors); e != nil {
  52. err = errors.New("弹幕颜色配置格式有误, Err: " + e.Error())
  53. return
  54. }
  55. defaultColor := &BulletChatColor{
  56. Id: 1,
  57. Color: "#FFFFFF",
  58. Remark: "白色",
  59. }
  60. colorLen := len(colors)
  61. if colorLen == 0 {
  62. color = defaultColor
  63. return
  64. }
  65. // 随机获取
  66. rand.Seed(time.Now().UnixNano())
  67. x := rand.Intn(colorLen)
  68. color = colors[x]
  69. return
  70. }
  71. // GetListMapBySourceAndIds 通过来源及IDs获取弹幕列表Map
  72. func GetListMapBySourceAndIds(source int, primaryIds []int) (idBulletsMap map[int][]*yb_bullet_chat.YbBulletChat, err error) {
  73. var cond string
  74. var pars []interface{}
  75. cond += `source = ? AND primary_id IN ?`
  76. pars = append(pars, source, primaryIds)
  77. list, e := yb_bullet_chat.GetList(cond, pars)
  78. if e != nil {
  79. err = errors.New("获取弹幕列表失败, Err: " + e.Error())
  80. return
  81. }
  82. idBulletsMap = make(map[int][]*yb_bullet_chat.YbBulletChat, 0)
  83. for i := range primaryIds {
  84. idBulletsMap[primaryIds[i]] = make([]*yb_bullet_chat.YbBulletChat, 0)
  85. }
  86. for i := range list {
  87. idBulletsMap[list[i].PrimaryID] = append(idBulletsMap[list[i].PrimaryID], list[i])
  88. }
  89. return
  90. }
  91. // CreateBulletChat 新增弹幕
  92. func CreateBulletChat(userId, primaryId, source, sourceAgent int, seconds float64, content string) (item *yb_bullet_chat.YbBulletChat, err error) {
  93. // 随机颜色
  94. color, e := GetRandomColor()
  95. if e != nil {
  96. err = errors.New("获取弹幕随机颜色失败, Err: " + e.Error())
  97. return
  98. }
  99. title := ""
  100. if source == 1 {
  101. vd, e := yb_community_video.GetItemById(primaryId)
  102. if e != nil {
  103. err = errors.New("获取视频失败, Err: " + e.Error())
  104. return
  105. }
  106. title = vd.Title
  107. } else {
  108. rv, e := yb_road_video.GetItemById(primaryId)
  109. if e != nil {
  110. err = errors.New("获取路演视频失败, Err: " + e.Error())
  111. return
  112. }
  113. title = rv.Title
  114. }
  115. nowTime := time.Now().Local()
  116. bc := new(yb_bullet_chat.YbBulletChat)
  117. bc.UserID = userId
  118. bc.PrimaryID = primaryId
  119. bc.Content = content
  120. bc.Seconds = seconds
  121. bc.Color = color.Color
  122. bc.Title = title
  123. bc.Source = source
  124. bc.SourceAgent = sourceAgent
  125. bc.CreateTime = nowTime
  126. bc.ModifyTime = nowTime
  127. if e := bc.Create(); e != nil {
  128. err = errors.New("新增弹幕失败, Err: " + e.Error())
  129. return
  130. }
  131. item = bc
  132. return
  133. }