12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package service
- import (
- "encoding/json"
- "fmt"
- "github.com/pkg/errors"
- "github.com/robfig/cron"
- "kpt.notice/apiserver/model"
- "kpt.notice/middleware/notice"
- "kpt.notice/pkg/log"
- )
- func JobInit() {
- c := cron.New()
- c.AddFunc("30 * * * * *", ReadMsgs)
- c.Start()
- }
- func ReadMsgs() {
- var msgs []model.Message
- var err error
- if msgs, err = svc.QueryMsg(); err != nil {
- log.Errorf("%v", err)
- return
- }
- fmt.Println("msg len=", len(msgs))
- if len(msgs) == 0 {
- return
- }
- // fmt.Printf("%v\n", msgs)
- for _, val := range msgs {
- e := SendMsg(&val)
- log.Error(e)
- }
- }
- func SendMsg(val *model.Message) (err error) {
- // if val.SysName != "tmrWatch_test" {
- // return
- // } else {
- // val.Target = `["oLd1b56PwpexCa0QK4NCkza9TKyY"]`
- // }
- data := make(map[string]interface{})
- targets := make([]string, 10)
- if e := json.Unmarshal([]byte(val.MsgContent), &data); e != nil {
- log.Errorf("%v", err)
- return
- }
- if e := json.Unmarshal([]byte(val.Target), &targets); e != nil {
- log.Errorf("%v", err)
- return
- }
- for _, openid := range targets {
- data["touser"] = openid
- if jsonstr, e := json.Marshal(data); e != nil {
- err = errors.Wrap(e, "SendMessage{json marshal error of data}=%v")
- log.Errorf("%v", err)
- continue
- } else {
- if resp, tmperr := notice.MsgTemplate(jsonstr); tmperr != nil {
- err = errors.Wrap(tmperr, "SendMessage{send message error}==%v")
- log.Errorf("%v", err)
- } else {
- log.Infof("send msg success%v", string(resp))
- if e := svc.UpdateMessage(val); e != nil {
- err = errors.Wrap(e, "SendMessage{update message error}=%v")
- log.Errorf("%v", err)
- }
- }
- }
- }
- return
- }
|