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.
170 lines
4.4 KiB
170 lines
4.4 KiB
2 years ago
|
package serviceGame
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"github.com/gogf/gf/v2/errors/gerror"
|
||
|
"github.com/gogf/gf/v2/frame/g"
|
||
|
"tyj_admin/api/v1/game"
|
||
|
"tyj_admin/internal/dao"
|
||
|
"tyj_admin/internal/model/do"
|
||
|
"tyj_admin/internal/model/entity"
|
||
|
"tyj_admin/internal/serviceGame/internal"
|
||
|
"tyj_admin/library/libUtils"
|
||
|
"tyj_admin/library/liberr"
|
||
|
)
|
||
|
|
||
|
type IGameNotice interface {
|
||
|
GetNotice(ctx context.Context, req *game.GetNoticeReq) (res *game.GetNoticeRes, err error)
|
||
|
AddNotice(ctx context.Context, req *game.AddNoticeReq) (res *game.AddNoticeRes, err error)
|
||
|
PopNotice(ctx context.Context, req *game.PopReq) (res *game.PopRes, err error)
|
||
|
GetGameNotice(ctx context.Context, req *game.GetGameNoticeReq) (res *game.GetGameNoticeRes, err error)
|
||
|
}
|
||
|
|
||
|
type gameNoticeImpl struct {
|
||
|
}
|
||
|
|
||
|
var gameNoticeService = gameNoticeImpl{}
|
||
|
|
||
|
func GameNotice() IGameNotice {
|
||
|
return &gameNoticeService
|
||
|
}
|
||
|
func (c *gameNoticeImpl) GetNotice(ctx context.Context, req *game.GetNoticeReq) (res *game.GetNoticeRes, err error) {
|
||
|
internal.GetWhiteList(ctx)
|
||
|
res = new(game.GetNoticeRes)
|
||
|
g.Try(ctx, func(ctx context.Context) {
|
||
|
model := dao.GameNotice.Ctx(ctx)
|
||
|
model = model.Where("notice_type = ?", req.NoticeType)
|
||
|
model = model.Where("channel=? ", req.Channel)
|
||
|
var noticeList []*entity.GameNotice
|
||
|
|
||
|
err = model.Scan(¬iceList)
|
||
|
if len(noticeList) >= 1 {
|
||
|
res.Content = noticeList[0].Content
|
||
|
res.Status = noticeList[0].Status
|
||
|
res.Id = noticeList[0].Id
|
||
|
} else {
|
||
|
return
|
||
|
}
|
||
|
if res.Status == 2 {
|
||
|
//ip := libUtils.GetClientIp(ctx)
|
||
|
//whiteIps := internal.GetWhiteList(ctx)
|
||
|
//res.Status = 3
|
||
|
//for _, v := range whiteIps {
|
||
|
// if v == ip {
|
||
|
// res.Status = 1
|
||
|
// break
|
||
|
// }
|
||
|
//}
|
||
|
}
|
||
|
|
||
|
})
|
||
|
return
|
||
|
|
||
|
}
|
||
|
|
||
|
func (c *gameNoticeImpl) GetGameNotice(ctx context.Context, req *game.GetGameNoticeReq) (res *game.GetGameNoticeRes, err error) {
|
||
|
res = new(game.GetGameNoticeRes)
|
||
|
g.Try(ctx, func(ctx context.Context) {
|
||
|
model := dao.GameNotice.Ctx(ctx)
|
||
|
model = model.Where("notice_type = ?", req.NoticeType)
|
||
|
model = model.Where("channel=? ", req.Channel)
|
||
|
var noticeList []*entity.GameNotice
|
||
|
var count int
|
||
|
count, err = model.Count()
|
||
|
if count == 0 {
|
||
|
model = dao.GameNotice.Ctx(ctx)
|
||
|
model = model.Where("notice_type = ?", req.NoticeType)
|
||
|
model = model.Where("channel=? ", 0)
|
||
|
}
|
||
|
err = model.Scan(¬iceList)
|
||
|
if len(noticeList) > 1 {
|
||
|
for _, v := range noticeList {
|
||
|
if v.Channel == req.Channel {
|
||
|
res.Content = v.Content
|
||
|
res.Status = v.Status
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
} else if len(noticeList) == 1 {
|
||
|
res.Content = noticeList[0].Content
|
||
|
res.Status = noticeList[0].Status
|
||
|
res.Id = noticeList[0].Id
|
||
|
} else if len(noticeList) == 0 {
|
||
|
res.Content = ""
|
||
|
res.Status = 1
|
||
|
res.Id = 0
|
||
|
}
|
||
|
if res.Status == 2 {
|
||
|
ip := libUtils.GetClientIp(ctx)
|
||
|
whiteIps, e := internal.GetWhiteList(ctx)
|
||
|
g.Log().Debugf(ctx, "ip"+ip, len(whiteIps), e)
|
||
|
for _, v := range whiteIps {
|
||
|
g.Log().Debugf(ctx, "whiteIps"+v)
|
||
|
}
|
||
|
res.Status = 3
|
||
|
for _, v := range whiteIps {
|
||
|
if v == ip {
|
||
|
res.Status = 1
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
})
|
||
|
return
|
||
|
|
||
|
}
|
||
|
|
||
|
func (c *gameNoticeImpl) AddNotice(ctx context.Context, req *game.AddNoticeReq) (res *game.AddNoticeRes, err error) {
|
||
|
if req.Id > 0 {
|
||
|
err = g.Try(ctx, func(ctx context.Context) {
|
||
|
_, e := dao.GameNotice.Ctx(ctx).Where("id=", req.Id).Data(&do.GameNotice{
|
||
|
Status: req.Status,
|
||
|
Content: req.Content,
|
||
|
}).Update()
|
||
|
liberr.ErrIsNil(ctx, e, "修改通知失败")
|
||
|
})
|
||
|
|
||
|
} else {
|
||
|
err = g.Try(ctx, func(ctx context.Context) {
|
||
|
_, e := dao.GameNotice.Ctx(ctx).Where("id=", req.Id).Insert(&do.GameNotice{
|
||
|
Status: req.Status,
|
||
|
Content: req.Content,
|
||
|
NoticeType: req.NoticeType,
|
||
|
Channel: req.Channel,
|
||
|
})
|
||
|
liberr.ErrIsNil(ctx, e, "插入通知失败")
|
||
|
})
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
func (c *gameNoticeImpl) PopNotice(ctx context.Context, req *game.PopReq) (res *game.PopRes, err error) {
|
||
|
mqttNotice := internal.MqttNotice{}
|
||
|
mqttNotice.Body.Uid = req.Uids
|
||
|
mqttNotice.Body.ModuleType = "broadcastMessage"
|
||
|
mqttNotice.Body.Content = req.Content
|
||
|
mqttNotice.Body.Time = 0
|
||
|
mqttNotice.Body.Type = req.Type
|
||
|
mqttNotice.ModuleId = "webadmin"
|
||
|
mqttNotice.ReqId = 1
|
||
|
var js []byte
|
||
|
js, err = json.Marshal(mqttNotice)
|
||
|
connectCh := make(chan bool)
|
||
|
server := "192.168.2.100:3005"
|
||
|
defer close(connectCh)
|
||
|
internal.SendMqtt(js, connectCh, server)
|
||
|
var result bool
|
||
|
for {
|
||
|
select {
|
||
|
case result = <-connectCh:
|
||
|
if result == false {
|
||
|
err = gerror.New("失败")
|
||
|
return
|
||
|
} else {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|