package script import ( "bytes" "encoding/json" "fmt" "io" "net/http" "time" ) func script(startTime, endTime time.Time, duration time.Duration) error { // 每指定间隔时间发送一次请求 for t := startTime; t.Before(endTime); t = t.Add(duration) { end := t.Add(duration) // 计算每次请求的结束时间 // 发送请求 if err := sendRequest(t, end); err != nil { fmt.Printf("Error sending request: %v\n", err) } // 等待一段时间后再发送下一个请求 time.Sleep(1 * time.Second) // 可以根据需要调整间隔时间 } return nil } func sendRequest(startTime, endTime time.Time) error { url := "https://gateway.dev.cdlsxd.cn/voucher/cmb/v1/orderQuery" // 创建请求体 requestBody := map[string]interface{}{ "product_no": "", "start_time": startTime.Format(time.DateTime), "end_time": endTime.Format(time.DateTime), } // 将请求体转换为 JSON 格式 jsonData, err := json.Marshal(requestBody) if err != nil { return fmt.Errorf("failed to marshal JSON: %v", err) } // 发送 POST 请求 resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData)) if err != nil { return fmt.Errorf("failed to send POST request: %v", err) } defer resp.Body.Close() bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("读取响应体失败: %w", err) } if resp.StatusCode == http.StatusOK { fmt.Printf("Request sent successfully,body:%s", string(bodyBytes)) } else { return fmt.Errorf("failed with status code: %d", resp.StatusCode) } return nil }