staff.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package staff
  2. import (
  3. "context"
  4. "bytes"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "encoding/json"
  8. "go-common/app/admin/main/videoup/model/archive"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. "net/http"
  13. "net/url"
  14. "strconv"
  15. "time"
  16. )
  17. const ()
  18. // Staffs fn
  19. func (d *Dao) Staffs(c context.Context, aid int64) (data []*archive.Staff, err error) {
  20. params := url.Values{}
  21. params.Set("aid", strconv.FormatInt(aid, 10))
  22. var res struct {
  23. Code int `json:"code"`
  24. Message string `json:"message"`
  25. Data []*archive.Staff `json:"data"`
  26. }
  27. if err = d.httpClient.Get(c, d.staffURI, "", params, &res); err != nil {
  28. log.Error("archive.Staffs url(%s) error(%v)", d.staffURI+"?"+params.Encode(), err)
  29. return
  30. }
  31. if res.Code != 0 {
  32. log.Error("Staffs api url(%s) res(%v) code(%d)", d.staffURI, res, res.Code)
  33. err = ecode.Int(res.Code)
  34. return
  35. }
  36. data = res.Data
  37. return
  38. }
  39. // StaffApplyBatchSubmit add .
  40. func (d *Dao) StaffApplyBatchSubmit(c context.Context, ap *archive.StaffBatchParam) (err error) {
  41. params := url.Values{}
  42. params.Set("appkey", d.c.App.Key)
  43. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  44. mh := md5.Sum([]byte(params.Encode() + d.c.App.Secret))
  45. params.Set("sign", hex.EncodeToString(mh[:]))
  46. var (
  47. uri = d.submitUrl + "?" + params.Encode()
  48. )
  49. bs, err := json.Marshal(ap)
  50. if err != nil {
  51. log.Error("json.Marshal error(%v) | ap(%v) ", err, ap)
  52. return
  53. }
  54. req, err := http.NewRequest("POST", uri, bytes.NewReader(bs))
  55. if err != nil {
  56. log.Error("http.NewRequest error(%v) | uri(%s)", err, uri)
  57. return
  58. }
  59. ip := metadata.String(c, metadata.RemoteIP)
  60. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  61. var res struct {
  62. Code int `json:"code"`
  63. Message string `json:"message"`
  64. }
  65. if err = d.httpClient.Do(c, req, &res); err != nil {
  66. log.Error("d.StaffApplyBatchSubmit error(%v) | uri(%s) ap(%+v)", err, uri, ap)
  67. err = ecode.CreativeArchiveAPIErr
  68. return
  69. }
  70. if res.Code != 0 {
  71. err = ecode.Error(ecode.Int(res.Code), res.Message)
  72. log.Error("d.StaffApplyBatchSubmit nq zero (%v)|(%v)|(%v)|(%v)|uri(%s),ap(%+v)", res.Code, res.Message, res, err, uri, ap)
  73. return
  74. }
  75. log.Info("d.StaffApplyBatchSubmit (%s)|res.Data.Aid aid(%d) res(%+v) ip(%s) ", string(bs), ap.AID, res, ip)
  76. return
  77. }