bullet_chat.go 3.1 KB

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