1 пре 1 месец
родитељ
комит
3977f640b4
2 измењених фајлова са 54 додато и 0 уклоњено
  1. 24 0
      common/period.go
  2. 30 0
      common/rod_utils.go

+ 24 - 0
common/period.go

@@ -3,6 +3,7 @@ package common
 import (
 	"errors"
 	"git.listensoft.net/tool/jspkit/logger"
+	"math"
 	"strconv"
 	"strings"
 	"time"
@@ -219,3 +220,26 @@ func GetFirstDay(period string) time.Time {
 	logger.Info(firstOfMonth, lastOfMonth)
 	return firstOfMonth
 }
+
+func GetPrevQuarterStartTimeAndEndTime(period string) (string, string) {
+	quarterStart := ""
+	quarterEnd := ""
+	quarter := math.Ceil(StrToFloat(period[4:6])/3) - 1
+	if quarter == 1 { //第一季度
+		quarterStart = period[0:4] + "-01-01"
+		quarterEnd = period[0:4] + "-03-31"
+	}
+	if quarter == 2 { //第二季度
+		quarterStart = period[0:4] + "-04-01"
+		quarterEnd = period[0:4] + "-06-30"
+	}
+	if quarter == 3 { //第三季度
+		quarterStart = period[0:4] + "-07-01"
+		quarterEnd = period[0:4] + "-09-30"
+	}
+	if quarter == 0 { //第四季度
+		quarterStart = IntToStr(StrToInt(period[0:4])-1) + "-10-01"
+		quarterEnd = IntToStr(StrToInt(period[0:4])-1) + "-12-31"
+	}
+	return quarterStart, quarterEnd
+}

+ 30 - 0
common/rod_utils.go

@@ -310,3 +310,33 @@ func GetHijackResp(ch chan []byte, t float64) ([]byte, error) {
 		return nil, errors.New("请求网页超时,请稍后重试!")
 	}
 }
+
+// WaitForPageLoad 等待页面完全加载,selector 为等待某个元素加载的选择器,可选
+func WaitForPageLoad(page *rod.Page, timeoutDuration time.Duration, selector ...string) {
+	start := time.Now()
+	// 1. 等待页面 readyState 为 "complete",加上超时控制
+	for {
+		readyState := page.MustEval(`() => document.readyState`).String()
+		if readyState == "complete" {
+			break // 页面加载完成,退出循环
+		}
+		// 检查是否超时
+		if time.Since(start) > timeoutDuration {
+			break
+		}
+		time.Sleep(500 * time.Millisecond) // 每次检查之间等待一段时间
+	}
+	// 2. 等待页面元素稳定
+	page.MustWaitStable()
+	// 3. 如果传入了 selector,等待特定元素加载完成
+	if len(selector) > 0 {
+		rod.Try(func() {
+			page.Timeout(timeoutDuration).MustElement(selector[0]) // 使用传入的选择器等待元素
+		})
+	}
+	// 4. 等待所有网络请求结束
+	rod.Try(func() {
+		page.Timeout(timeoutDuration).MustWaitIdle()
+	})
+	utils.Sleep(3)
+}