sms.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package dao
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _maxAIDPath = "http://api.bilibili.co/x/internal/v2/archive/maxAid"
  16. )
  17. // MaxAID return max aid
  18. func (d *Dao) MaxAID(c context.Context) (id int64, err error) {
  19. var res struct {
  20. Code int `json:"code"`
  21. Data int64 `json:"data"`
  22. }
  23. if err = d.smsClient.Get(c, _maxAIDPath, "", nil, &res); err != nil {
  24. return
  25. }
  26. if res.Code != 0 {
  27. log.Error("d.client.MaxAid Code(%d)", res.Code)
  28. return
  29. }
  30. log.Info("got MaxAid(%d)", res.Data)
  31. id = res.Data
  32. return
  33. }
  34. // SendQiyeWX send qiye wx
  35. func (d *Dao) SendQiyeWX(msg string) {
  36. type wxParams struct {
  37. Username string `json:"username"`
  38. Content string `json:"content"`
  39. Token string `json:"token"`
  40. Timestamp int64 `json:"timestamp"`
  41. Sign string `json:"signature"`
  42. }
  43. var resp struct {
  44. Status int64 `json:"status"`
  45. Msg string `json:"msg"`
  46. }
  47. params := url.Values{}
  48. params.Set("username", d.c.Monitor.Users)
  49. params.Set("content", msg)
  50. params.Set("token", d.c.Monitor.Token)
  51. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  52. mh := md5.Sum([]byte(params.Encode() + d.c.Monitor.Secret))
  53. params.Set("signature", hex.EncodeToString(mh[:]))
  54. p := &wxParams{
  55. Username: params.Get("username"),
  56. Content: params.Get("content"),
  57. Token: params.Get("token"),
  58. Sign: params.Get("signature"),
  59. }
  60. p.Timestamp, _ = strconv.ParseInt(params.Get("timestamp"), 10, 64)
  61. bs, _ := json.Marshal(p)
  62. payload := strings.NewReader(string(bs))
  63. req, _ := http.NewRequest("POST", d.c.Monitor.URL, payload)
  64. req.Header.Add("content-type", "application/json; charset=utf-8")
  65. if err := d.smsClient.Do(context.TODO(), req, &resp); err != nil {
  66. log.Error("d.smsClient.Do() error(%v)", err)
  67. }
  68. }