bullet_chat.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package bullet_chat
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "hongze/hongze_yb/models/request"
  7. responseModel "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/services/bullet_chat"
  9. "hongze/hongze_yb/services/user"
  10. "hongze/hongze_yb/services/wx_app"
  11. "strconv"
  12. )
  13. // Add
  14. // @Description 新增弹幕
  15. // @Success 200 {string} string "操作成功"
  16. // @Router /bullet_chat/add [post]
  17. func Add(c *gin.Context) {
  18. userInfo := user.GetInfoByClaims(c)
  19. if userInfo.UserID <= 0 {
  20. response.Fail("请登录后操作", c)
  21. return
  22. }
  23. // 是否为备用小程序
  24. copyYb := c.Request.Header.Get("CopyYb")
  25. var req request.BulletChatAddReq
  26. if c.ShouldBind(&req) != nil {
  27. response.Fail("参数有误", c)
  28. return
  29. }
  30. if req.PrimaryId <= 0 {
  31. response.Fail("参数有误", c)
  32. return
  33. }
  34. if req.Content == "" {
  35. response.Fail("内容不可为空", c)
  36. return
  37. }
  38. if len([]rune(req.Content)) > 50 {
  39. response.Fail("内容不可超过50个字", c)
  40. return
  41. }
  42. // 敏感词校验
  43. // TODO:如果备用小程序提上master的话,此处要有调整(2022/11/08)
  44. if userInfo.RecordInfo.OpenID != "" && userInfo.RecordInfo.CreatePlatform == 6 {
  45. checkResult, e := wx_app.MsgSecCheck(userInfo.RecordInfo.OpenID, req.Content, copyYb)
  46. if e == nil {
  47. if checkResult.Result != nil && checkResult.Result.Suggest != "pass" {
  48. errMsg := "含有违禁词, 不允许发布: " + checkResult.Result.Suggest + ", 命中标签: " + strconv.Itoa(checkResult.Result.Label)
  49. response.FailMsg("内容含有违禁词, 不允许发布", errMsg, c)
  50. return
  51. }
  52. }
  53. }
  54. bc, e := bullet_chat.CreateBulletChat(int(userInfo.UserID), req.PrimaryId, req.Source, req.SourceAgent, req.Seconds, req.Content)
  55. if e != nil {
  56. response.FailMsg("发布失败", "新增弹幕失败, Err: "+e.Error(), c)
  57. return
  58. }
  59. resp := new(responseModel.BulletChatItem)
  60. resp.Id = bc.ID
  61. resp.UserId = bc.UserID
  62. resp.Content = bc.Content
  63. resp.Seconds = fmt.Sprint(bc.Seconds)
  64. resp.Color = bc.Color
  65. response.OkData("操作成功", resp, c)
  66. }