12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package notice
- import (
- "bp.jydev.jianyu360.cn/BP/jynsq/gonsq"
- "encoding/json"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "log"
- )
- type NoticeConfig struct {
- IsOpen bool //是否开启提示
- IsJsonEncode bool
- Address, TopPic string
- Id, Title, Text string
- }
- type Notice struct {
- p *gonsq.Producer
- c NoticeConfig
- }
- func NewNotice(c NoticeConfig) (n *Notice, err error) {
- if c.IsOpen {
- n = &Notice{
- c: c,
- }
- var producer *gonsq.Producer
- producer, err = gonsq.NewProducer(c.Address, c.TopPic, c.IsJsonEncode)
- if err != nil {
- return
- }
- n.p = producer
- }
- return nil, nil
- }
- // SendWarnMsg 发送异常通知
- func (n *Notice) SendWarnMsg(detail map[string]interface{}) error {
- if n.p == nil {
- return nil
- }
- m := &gonsq.Msg{n.c.Id, n.c.Title, n.c.Text, detail}
- bs, err := json.Marshal(m)
- if err != nil {
- return err
- }
- g.Log().Debugf(gctx.New(), "模拟发送信息----->", string(bs))
- if err := n.p.Publish(bs); err != nil {
- log.Println("nsq连接失败", err)
- }
- return nil
- }
|