timeSliceQueryPush

This commit is contained in:
ziming 2025-06-12 10:17:55 +08:00
parent d348f247fe
commit b366cadb9d
2 changed files with 68 additions and 43 deletions

View File

@ -2,6 +2,7 @@ package script
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -10,25 +11,42 @@ import (
const ( const (
URL = "http://127.0.0.1:15000/voucher/timeSliceQueryPush" URL = "http://127.0.0.1:15000/voucher/timeSliceQueryPush"
DEV_URL = "http://open.cszfan.com/voucher/cmb/timeSliceQueryPush" DEV_URL = "http://open.cszfan.com/voucher/timeSliceQueryPush"
PRO_URL = "https://voucher.86698.cn/voucher/cmb/timeSliceQueryPush" PRO_URL = "https://voucher.86698.cn/voucher/timeSliceQueryPush"
) )
const ( const (
SINGLE_URL = "http://127.0.0.1:15000/voucher/pushWechatQuery" SINGLE_URL = "http://127.0.0.1:15000//voucher/pushWechatQuery"
DEV_SINGLE_URL = "http://open.cszfan.com/voucher/cmb/pushWechatQuery" DEV_SINGLE_URL = "http://open.cszfan.com//voucher/pushWechatQuery"
PRO_SINGLE_URL = "https://voucher.86698.cn/voucher/cmb/pushWechatQuery" PRO_SINGLE_URL = "https://voucher.86698.cn//voucher/pushWechatQuery"
) )
func script(startTime, endTime time.Time, duration time.Duration, body []byte, URL string) error { func timeSliceQueryPush(startTime, endTime time.Time, duration time.Duration, requestURL string) error {
// 每指定间隔时间发送一次请求 // 每指定间隔时间发送一次请求
for t := startTime; t.Before(endTime); t = t.Add(duration) { for t := startTime; t.Before(endTime); t = t.Add(duration) {
end := t.Add(duration) // 计算每次请求的结束时间 end := t.Add(duration) // 计算每次请求的结束时间
// 创建请求体
requestBody := map[string]any{
"go_num": 2, // 并发数量
"time_slice_hours": 1, // 时间间隔
"product_no": "",
"start_time": t.Format(time.DateTime),
"end_time": end.Format(time.DateTime),
}
// 将请求体转换为 JSON 格式
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return err
}
fmt.Printf("body:%s\n", string(bodyBytes))
// 发送请求 // 发送请求
if err := sendRequest(t, end, body, URL); err != nil { if err2 := sendRequest(bodyBytes, requestURL); err2 != nil {
fmt.Printf("Error sending request: %v\n", err) fmt.Printf("Error sending request: %v\n", err2)
} }
// 等待一段时间后再发送下一个请求 // 等待一段时间后再发送下一个请求
@ -38,9 +56,43 @@ func script(startTime, endTime time.Time, duration time.Duration, body []byte, U
return nil return nil
} }
func sendRequest(startTime, endTime time.Time, body []byte, URL string) error { func pushWechatQuery(startTime, endTime time.Time, duration time.Duration, requestURL string) error {
resp, err := http.Post(URL, "application/json", bytes.NewBuffer(body)) // 每指定间隔时间发送一次请求
for t := startTime; t.Before(endTime); t = t.Add(duration) {
end := t.Add(duration) // 计算每次请求的结束时间
// 创建请求体
requestBody := map[string]any{
"order_no": "",
"product_no": "",
"start_time": t.Format(time.DateTime),
"end_time": end.Format(time.DateTime),
}
// 将请求体转换为 JSON 格式
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return err
}
fmt.Printf("body:%s\n", string(bodyBytes))
// 发送请求
if err2 := sendRequest(bodyBytes, requestURL); err2 != nil {
fmt.Printf("Error sending request: %v\n", err2)
}
// 等待一段时间后再发送下一个请求
time.Sleep(1 * time.Second) // 可以根据需要调整间隔时间
}
return nil
}
func sendRequest(body []byte, requestURL string) error {
resp, err := http.Post(requestURL, "application/json", bytes.NewBuffer(body))
if err != nil { if err != nil {
return fmt.Errorf("failed to send POST request: %v", err) return fmt.Errorf("failed to send POST request: %v", err)
} }

View File

@ -1,7 +1,6 @@
package script package script
import ( import (
"encoding/json"
"testing" "testing"
"time" "time"
) )
@ -14,7 +13,7 @@ func Test_script(t *testing.T) {
return return
} }
endTime, err := time.Parse(time.DateTime, "2025-05-01 5:00:00") endTime, err := time.Parse(time.DateTime, "2025-05-01 2:00:03")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -22,23 +21,10 @@ func Test_script(t *testing.T) {
duration := 5 * time.Hour duration := 5 * time.Hour
// 创建请求体 //requestUrl := URL
requestBody := map[string]any{ requestUrl := DEV_URL
"go_num": 2, // 并发数量
"time_slice_hours": 2, // 时间间隔
"product_no": "",
"start_time": startTime.Format(time.DateTime),
"end_time": endTime.Format(time.DateTime),
}
// 将请求体转换为 JSON 格式 if err = timeSliceQueryPush(startTime, endTime, duration, requestUrl); err != nil {
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
t.Error(err)
return
}
if err = script(startTime, endTime, duration, bodyBytes, URL); err != nil {
t.Error(err) t.Error(err)
} }
} }
@ -59,22 +45,9 @@ func Test_script2(t *testing.T) {
duration := 50 * time.Hour duration := 50 * time.Hour
// 创建请求体 requestUrl := SINGLE_URL
requestBody := map[string]any{
"order_no": "",
"product_no": "",
"start_time": startTime.Format(time.DateTime),
"end_time": endTime.Format(time.DateTime),
}
// 将请求体转换为 JSON 格式 if err = pushWechatQuery(startTime, endTime, duration, requestUrl); err != nil {
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
t.Error(err)
return
}
if err = script(startTime, endTime, duration, bodyBytes, SINGLE_URL); err != nil {
t.Error(err) t.Error(err)
} }
} }