package youchu import ( "bytes" "encoding/json" "github.com/pkg/errors" "io/ioutil" "net/http" "qteam/config" "time" ) type YouChuClient struct { cfg config.YouChuConfig } type YouChuSendRequest struct { CustNo string `json:"custNo"` ReqTransTime string `json:"reqTransTime"` Code string `json:"code"` } func (this *YouChuSendRequest) toMap() (resultMap map[string]interface{}) { // Marshal the struct to JSON, ignoring omitempty fields. jsonBytes, err := json.Marshal(this) if err != nil { return } // Unmarshal the JSON into a map to get the final result. err = json.Unmarshal(jsonBytes, &resultMap) if err != nil { return } return resultMap } func (this *YouChuClient) doPost(url string, partnerTxSriNo string, method string, jsonBytes []byte) (body []byte, err error) { // 创建POST请求 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes)) if err != nil { return } // 设置Content-Type头 req.Header.Set("Content-Type", "application/json;charset=utf-8") req.Header.Set("partnerTxSriNo", partnerTxSriNo) req.Header.Set("method", method) req.Header.Set("version", "1") req.Header.Set("appID", this.cfg.AppID) req.Header.Set("merchantId", this.cfg.MerchantId) req.Header.Set("reqTime", time.Now().Format("20060102150405")) req.Header.Set("accessType", "API") // 创建HTTP客户端 client := &http.Client{ Timeout: 3 * time.Second, // 设置请求超时时间为5秒 } // 发送请求并处理响应 resp, err := client.Do(req) if err != nil { return } defer resp.Body.Close() // 读取响应体 if resp.StatusCode != http.StatusOK { err = errors.New("HTTP request failed: " + resp.Status) return } body, err = ioutil.ReadAll(resp.Body) if err != nil { return } return }