You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

package live
import (
"encoding/json"
"errors"
"fmt"
"live/app/lib/config"
"live/app/lib/recook"
"live/app/lib/tencent"
live2 "live/app/model/live"
"live/app/model/user"
)
type im struct {
}
const (
LiveGroupMsgTypeExplain = "Explain"
LiveGroupMsgTypeUnExplain = "UnExplain"
LiveGroupMsgTypeLiveStop = "LiveStop"
LiveGroupMsgTypeBuyGoods = "BuyGoods"
LiveGroupMsgTypeNotice = "Notice"
LiveGroupMsgTypePraise = "Praise"
LiveGroupMsgTypePlay = "Play"
)
var ImLogic = &im{}
type UserInfo struct {
Identifier string `json:"identifier"`
Sign string `json:"sign"`
}
// @Title 登录账号信息
func (i *im) LoginUserInfo(userId uint) (err error, userInfo UserInfo) {
userImModel := &user.UserIm{}
userIm := userImModel.GetByUserId(userId)
users, _ := recook.User.GetUsers([]uint{userId})
if len(*users) < 0 {
return errors.New("网络异常"), userInfo
}
identifier := fmt.Sprintf("recook_%v", userId)
err = tencent.Im.AddUser(identifier, (*users)[0].Nickname, config.Config.Section("recook").Key("cdn").String()+(*users)[0].HeadImgUrl)
if err != nil {
return
}
if userIm.Id <= 0 {
userIm = user.UserIm{
UserId: userId,
Identifier: identifier,
}
userImModel.Create(&userIm)
}
userInfo.Identifier = userIm.Identifier
userInfo.Sign = tencent.Im.GetSign(userIm.Identifier)
return
}
// @Title 未登录临时账号
func (i *im) NoLoginUserInfo() (err error, userInfo UserInfo) {
userImModel := &live2.LiveImProvisional{}
for {
userIm := userImModel.GetByUserId()
if userIm.Id <= 0 {
return errors.New("网络异常"), userInfo
}
userIm.Count++
count := userImModel.UpdateCount("id = ? and updated_at = ?", userIm.Id, userIm.UpdatedAt)
if count > 0 {
userInfo.Identifier = userIm.Identifier
userInfo.Sign = tencent.Im.GetSign(userIm.Identifier)
return
}
}
}
type LiveGroupMessage struct {
Type string `json:"type"`
Data interface{} `json:"data"`
}
// @Title 直播群消息
func (i *im) SendLiveGroupMessage(liveRoomId uint, messageType string, data interface{}) {
liveRoom := (&live2.LiveRoom{}).GetLiveById(liveRoomId)
strData, _ := json.Marshal(LiveGroupMessage{Type: messageType, Data: data})
tencent.Im.SendLiveGroupMessage(liveRoom.GroupId, string(strData))
}