online.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/library/ecode"
  7. "go-common/library/net/metadata"
  8. )
  9. const _onlineCountURI = "/x/internal/chat/num/ol"
  10. // OnlineCount get online count by aid and cid
  11. func (d *Dao) OnlineCount(c context.Context, aid, cid int64) (count int64, err error) {
  12. var (
  13. params = url.Values{}
  14. ip = metadata.String(c, metadata.RemoteIP)
  15. )
  16. params.Set("aid", strconv.FormatInt(aid, 10))
  17. params.Set("cids", strconv.FormatInt(cid, 10))
  18. var res struct {
  19. Code int `json:"code"`
  20. Data []struct {
  21. Cid int64 `json:"cid"`
  22. Count int64 `json:"count"`
  23. } `json:"data"`
  24. }
  25. if err = d.client.Get(c, d.onlineCountURL, ip, params, &res); err != nil {
  26. PromError("OnlineNum接口错误", "d.client.Get(%s) error(%v)", d.onlineCountURL, err)
  27. return
  28. }
  29. if res.Code != ecode.OK.Code() {
  30. PromError("OnlineNum接口Code错误", "d.client.Get(%s) code(%d) error", d.onlineCountURL, res.Code)
  31. return
  32. }
  33. if len(res.Data) == 0 || res.Data[0].Cid != cid {
  34. PromError("OnlineNum接口数据错误", "d.client.Get(%s) data(%v) error", d.onlineCountURL, res.Data)
  35. }
  36. count = res.Data[0].Count
  37. return
  38. }