wxbroadcastv2.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package wxbroadcastv2
  2. import (
  3. "active/internal/model/goldbrick"
  4. "active/tools"
  5. "crypto/aes"
  6. "crypto/cipher"
  7. "encoding/base64"
  8. "encoding/json"
  9. "fmt"
  10. "runtime/debug"
  11. "strings"
  12. "time"
  13. "git.jiaxianghudong.com/go/logs"
  14. "git.jiaxianghudong.com/webs/pkg/rds"
  15. "git.jiaxianghudong.com/webs/pkg/xgorm"
  16. "gorm.io/gorm/clause"
  17. )
  18. // 直播数据的一些参数
  19. const (
  20. LiveWatcherMin = 1 // 场观人数小于此数,算不合格开播
  21. LiveOpenBelowStandardMax = 3 //开播多少次不合格,关闭一键直播推荐
  22. )
  23. type WxBroadcastInfo struct {
  24. TaskMap map[int64]bool `json:"task_map"`
  25. StartTime int64 `json:"start_time"`
  26. StartNumber int64 `json:"start_number"`
  27. Time int64 `json:"time"`
  28. Number int64 `json:"number"` //游戏局数
  29. First bool `json:"first"`
  30. WatchCount int64 `json:"watch_count"` //观看人数
  31. }
  32. type Wxbroadcast struct {
  33. Id int64 `json:"id" gorm:"column:id"`
  34. Daily3 int64 `json:"daily_3" gorm:"column:daily_3"`
  35. Daily7 int64 `json:"daily_7" gorm:"column:daily_7"`
  36. UpdateTime int64 `json:"update_time" gorm:"column:update_time"`
  37. FirstBroadcast int64 `json:"first_broadcast" gorm:"column:first_broadcast"`
  38. LiveLessCount int64 `json:"live_less_count" gorm:"column:live_less_count"` //场观人数小于等于1的开播次数
  39. }
  40. // 直播数据
  41. type GameLiveDetails struct {
  42. FeedId int64 `json:"feedId"` // 直播id
  43. Description string `json:"description"` // 直播主题
  44. StartTime int64 `json:"startTime"` // 开播时间戳
  45. EndTime int64 `json:"endTime"` // 关播时间戳
  46. TotalCheerCount int64 `json:"totalCheerCount"` // 主播收到的喝彩总数
  47. TotalAudienceCount int64 `json:"totalAudienceCount"` // 直播间总观众人数
  48. LiveDurationInSeconds int64 `json:"liveDurationInSeconds"` // 直播总时长
  49. Watermark struct {
  50. Timestamp int64 `json:"timestamp"`
  51. AppID string `json:"appid"`
  52. } `json:"watermark"`
  53. }
  54. func (w Wxbroadcast) TableName() string {
  55. return "wxbroadcast"
  56. }
  57. func (w *Wxbroadcast) Save() error {
  58. xdb := xgorm.NewConn(goldbrick.GoldBrickGorm)
  59. return xdb.Clauses(clause.OnConflict{
  60. Columns: []clause.Column{{Name: "id"}},
  61. UpdateAll: true,
  62. }).Save(w).Error
  63. }
  64. func (w *Wxbroadcast) One() error {
  65. id := w.Id
  66. xdb := xgorm.NewConn(goldbrick.GoldBrickGorm)
  67. err := xdb.Where("id = ?", w.Id).Find(w).Error
  68. w.Id = id
  69. return err
  70. }
  71. func (w *WxBroadcastInfo) Get(userId string, tm string) {
  72. data := rds.Redis.HGet(fmt.Sprintf(WXBROADCASTDAY, tm), userId).Val()
  73. if data != "" {
  74. err := json.Unmarshal([]byte(data), w)
  75. if err != nil {
  76. logs.Errorf("redpacket[%s] WxBroadcastInfo json err:%v", userId, err)
  77. }
  78. }
  79. if w.TaskMap == nil {
  80. w.TaskMap = make(map[int64]bool)
  81. }
  82. }
  83. func (w *WxBroadcastInfo) Set(userId string, tm string) bool {
  84. data, _ := json.Marshal(w)
  85. rds.Redis.HSet(fmt.Sprintf(WXBROADCASTDAY, tm), userId, string(data)).Err()
  86. end, _ := time.Parse("2006-01-02", tm)
  87. expireAt := end.Add(48 * time.Hour)
  88. rds.Redis.ExpireAt(fmt.Sprintf(WXBROADCASTDAY, tm), expireAt)
  89. return true
  90. }
  91. func Init() {
  92. tools.ReloadYaml("wxbroadcastv2.yaml", &confv2)
  93. for i := 0; i < len(confv2.AwardList); i++ {
  94. fmt.Printf("AwardList %+v \n", confv2.AwardList[i])
  95. }
  96. for i := 0; i < len(confv2.AwardListV1); i++ {
  97. fmt.Printf("AwardListV1 %+v \n", confv2.AwardListV1[i])
  98. }
  99. tools.ReloadYaml("wxbroadcastv2.yaml", &upListConf)
  100. fmt.Printf("upddd %v \n", upListConf)
  101. }
  102. // 获取小游戏用户的已结束的直播数据
  103. func EncryptedGameLiveDetails(userid uint32, encryptedData, iv string) *GameLiveDetails {
  104. sessionKey := rds.Redis2.HGet(fmt.Sprintf("user_%d", userid), "min_session_key").Val()
  105. if sessionKey == "" {
  106. logs.Errorf("userid %d min_session_key is not find:", userid)
  107. return nil
  108. }
  109. aesKey, _ := base64.StdEncoding.DecodeString(fmt.Sprintf("%s", sessionKey))
  110. encryptedStr, _ := base64.StdEncoding.DecodeString(strings.Replace(encryptedData, " ", "+", -1))
  111. IV, err := base64.StdEncoding.DecodeString(strings.Replace(iv, " ", "+", -1))
  112. if err != nil {
  113. logs.Errorf("iv decodeString[%v] err=%v", encryptedStr, iv)
  114. return nil
  115. }
  116. liveDetail, err := WxAESDecode(encryptedStr, aesKey, IV, userid)
  117. if err != nil {
  118. logs.Errorf("WxAESDecode|wechat_data decrypt err:userid:%v,encryptedStr=%v,iv=%v,err=%v", userid, encryptedStr, IV, err)
  119. return nil
  120. }
  121. logs.Infof("encryptedData=%v,iv=%v,userid:%v,GameLiveDetails=%+v", encryptedData, iv, userid, liveDetail)
  122. return liveDetail
  123. }
  124. // WxAESDecode 微信aes解码
  125. func WxAESDecode(encryptedData []byte, sessionKey []byte, iv []byte, userid uint32) (*GameLiveDetails, error) {
  126. var err error
  127. var info GameLiveDetails
  128. defer func() { // 必须要先声明defer,否则不能捕获到panic异常
  129. if err1 := recover(); err1 != nil {
  130. logs.Error(fmt.Sprintf("[WxAESDecode] | sessionKey: %s | iv: %s | err: %v | userid: %d", string(sessionKey), string(iv), err1, userid))
  131. debug.PrintStack()
  132. }
  133. }()
  134. var aesBlockDecrypter cipher.Block
  135. aesBlockDecrypter, err = aes.NewCipher(sessionKey)
  136. if err != nil {
  137. return &info, err
  138. }
  139. decrypted := make([]byte, len(encryptedData))
  140. aesDecrypter := cipher.NewCBCDecrypter(aesBlockDecrypter, iv)
  141. aesDecrypter.CryptBlocks(decrypted, encryptedData)
  142. aesPlantText := PKCS7UnPadding(decrypted)
  143. err = json.Unmarshal(aesPlantText, &info)
  144. return &info, err
  145. }
  146. func PKCS7UnPadding(plantText []byte) []byte {
  147. length := len(plantText)
  148. if length > 0 {
  149. unPadding := int(plantText[length-1])
  150. return plantText[:(length - unPadding)]
  151. }
  152. return plantText
  153. }