package market import ( "bytes" "encoding/json" "github.com/pkg/errors" "io/ioutil" "net/http" "qteam/config" ) type MarketClient struct { cfg config.MarketConfig } type MarketSendRequest struct { AppId string `json:"app_id"` //APP ID Sign string `json:"sign"` //签名 ReqCode string `json:"req_code"` //固定值:voucher.create MemId string `json:"mem_id"` //商户号 ReqSerialNo string `json:"req_serial_no"` //请求唯一流水号 最大32位 TimeTamp string `json:"timestamp"` //时间戳 yyyyMMddHHmmss PosId string `json:"pos_id"` //商户方平台号 VoucherId string `json:"voucher_id"` //制码批次号 VoucherNum int `json:"voucher_num"` //请券数量,默认是 1 MobileNo string `json:"mobile_no"` //11 手机号,可传空字符串 SendMsg string `json:"send_msg"` //是否发送短信:2- 发送 1-不发送 } type MarketSenResponse struct { VoucherId string `json:"voucher_id"` //制码批次号 VoucherCode string `json:"voucher_code"` //券码 ShortUrl string `json:"short_url"` //含二维码、条码的短链接 VoucherSdate string `json:"voucher_sdate"` //有效期起 VoucherEdate string `json:"voucher_edate"` //有效期止 CodeType string `json:"code_type"` //码类型: 00- 代金券 01- 满减券 } type MarketResponse struct { ErrCode string `json:"errCode"` //00-成功 其他:失败 Msg string `json:"msg"` //描 述 (失败时必填) Data MarketSenResponse `json:"data"` } func (this *MarketSendRequest) 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 *MarketClient) doPost(url string, jsonBytes []byte) (body []byte, err error) { // 创建POST请求 url = this.cfg.Host + url req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes)) if err != nil { return } // 设置Content-Type头 req.Header.Set("Content-Type", "application/json") // 创建HTTP客户端 client := &http.Client{} // 发送请求并处理响应 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 }