---
url: "https://pay.weixin.qq.com/doc/brand/4017327728-go.md"
title: "查询图片生成结果-请求示例-go"
doc_id: 4017327728
example_lang: "go"
---

## Go


需配合微信支付工具库 wxpay\_utility 使用，请参考 [Go](https://pay.weixin.qq.com/doc/brand/4015826866.md)

```
package main

import (
	"demo/wxpay_brand_utility" // 引用微信支付工具库，参考 https://pay.weixin.qq.com/doc/brand/4015826866
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"strings"
)

func main() {
	// TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/brand/4015415289
	config, err := wxpay_brand_utility.CreateBrandConfig(
		"xxxxxxxx",                   // 品牌ID，是由微信支付系统生成并分配给每个品牌方的唯一标识符，品牌ID获取方式参考 https://pay.weixin.qq.com/doc/brand/4015415289
		"1DDE55AD98Exxxxxxxxxx",      // 品牌API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/brand/4015407570
		"/path/to/apiclient_key.pem", // 品牌API证书私钥文件路径，本地文件路径
		"PUB_KEY_ID_xxxxxxxxxxxxx",   // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/brand/4015453439
		"/path/to/wxp_pub.pem",       // 微信支付公钥文件路径，本地文件路径
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	request := &GetImageGenerationTaskRequest{
		TaskId:              wxpay_brand_utility.String("image_generation_task_1"),
		ImageGenerationType: IMAGEGENERATIONTYPE_COMBINE_IMAGE.Ptr(),
	}

	response, err := GetImageGenerationTask(config, request)
	if err != nil {
		fmt.Printf("请求失败: %+v\n", err)
		// TODO: 请求失败，根据状态码执行不同的处理
		return
	}

	// TODO: 请求成功，继续业务逻辑
	fmt.Printf("请求成功: %+v\n", response)
}

func GetImageGenerationTask(config *wxpay_brand_utility.BrandConfig, request *GetImageGenerationTaskRequest) (response *ImageGenerationTask, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/brand/marketing/product-coupon/image-generation-tasks/{task_id}"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqUrl.Path = strings.Replace(reqUrl.Path, "{task_id}", url.PathEscape(*request.TaskId), -1)
	query := reqUrl.Query()
	if request.ImageGenerationType != nil {
		query.Add("image_generation_type", fmt.Sprintf("%v", *request.ImageGenerationType))
	}
	reqUrl.RawQuery = query.Encode()
	httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Accept", "application/json")
	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
	authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Authorization", authorization)

	client := &http.Client{}
	httpResponse, err := client.Do(httpRequest)
	if err != nil {
		return nil, err
	}
	respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
	if err != nil {
		return nil, err
	}
	if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
		// 2XX 成功，验证应答签名
		err = wxpay_brand_utility.ValidateResponse(
			config.WechatPayPublicKeyId(),
			config.WechatPayPublicKey(),
			&httpResponse.Header,
			respBody,
		)
		if err != nil {
			return nil, err
		}
		response := &ImageGenerationTask{}
		if err := json.Unmarshal(respBody, response); err != nil {
			return nil, err
		}

		return response, nil
	} else {
		return nil, wxpay_brand_utility.NewApiException(
			httpResponse.StatusCode,
			httpResponse.Header,
			respBody,
		)
	}
}

type GetImageGenerationTaskRequest struct {
	TaskId              *string              `json:"task_id,omitempty"`
	ImageGenerationType *ImageGenerationType `json:"image_generation_type,omitempty"`
}

func (o *GetImageGenerationTaskRequest) MarshalJSON() ([]byte, error) {
	type Alias GetImageGenerationTaskRequest
	a := &struct {
		TaskId              *string              `json:"task_id,omitempty"`
		ImageGenerationType *ImageGenerationType `json:"image_generation_type,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		TaskId:              nil,
		ImageGenerationType: nil,
		Alias:               (*Alias)(o),
	}
	return json.Marshal(a)
}

type ImageGenerationTask struct {
	TaskId              *string              `json:"task_id,omitempty"`
	ImageGenerationType *ImageGenerationType `json:"image_generation_type,omitempty"`
	TaskState           *TaskState           `json:"task_state,omitempty"`
	CombineImageResult  *CombineImageResult  `json:"combine_image_result,omitempty"`
	CutOutResult        *CutOutResult        `json:"cut_out_result,omitempty"`
}

type ImageGenerationType string

func (e ImageGenerationType) Ptr() *ImageGenerationType {
	return &e
}

const (
	IMAGEGENERATIONTYPE_COMBINE_IMAGE ImageGenerationType = "COMBINE_IMAGE"
	IMAGEGENERATIONTYPE_CUT_OUT       ImageGenerationType = "CUT_OUT"
)

type TaskState string

func (e TaskState) Ptr() *TaskState {
	return &e
}

const (
	TASKSTATE_PROCESSING TaskState = "PROCESSING"
	TASKSTATE_FAIL       TaskState = "FAIL"
	TASKSTATE_SUCCESS    TaskState = "SUCCESS"
)

type CombineImageResult struct {
	ImageUrl *string `json:"image_url,omitempty"`
}

type CutOutResult struct {
	ImageUrl *string `json:"image_url,omitempty"`
}

```
