screenshot.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package lxrod
  2. import (
  3. "context"
  4. "net/http"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. "git.listensoft.net/lx/taxrobot/common/clients"
  9. "git.listensoft.net/lx/taxrobot/common/path"
  10. "git.listensoft.net/lx/taxrobot/model"
  11. "github.com/go-rod/rod"
  12. "go.uber.org/zap"
  13. )
  14. // 截图并上传
  15. func PostScreenshot(l *zap.Logger, p *rod.Page, task *model.TaxTask) error {
  16. l.Debug("尝试保存截图")
  17. imgPath := path.GetErrImgPath(task.TaxNo, task.Period)
  18. bin, err := p.Timeout(10*time.Second).Screenshot(false, nil)
  19. if err != nil {
  20. l.Error("截图创建失败", zap.Error(err))
  21. return err
  22. }
  23. err = os.WriteFile(imgPath, bin, 0666)
  24. if err != nil {
  25. l.Error("截图保存失败", zap.Error(err))
  26. return err
  27. }
  28. l.Info("截图保存成功", zap.String("path", imgPath))
  29. l.Debug("尝试上传截图")
  30. ctx, cancel := context.WithTimeout(p.GetContext(), 30*time.Second)
  31. defer cancel()
  32. fc := clients.FileClient{
  33. Client: http.DefaultClient,
  34. Logger: l,
  35. }
  36. businessImg, err := fc.PostScreenShotBytes(ctx, bin, clients.PostScreenShotReq{
  37. Name: filepath.Base(imgPath),
  38. TaxNo: task.TaxNo,
  39. Period: task.Period,
  40. })
  41. if err != nil {
  42. l.Error("截图上传失败", zap.Error(err))
  43. return err
  44. }
  45. l.Info("截图上传成功", zap.String("businessImg", businessImg))
  46. task.Result.BusinessImg = businessImg
  47. return nil
  48. }