---
url: "https://pay.weixin.qq.com/doc/brand/4018381151-go.md"
title: "分页查询投放计划列表-请求示例-go"
doc_id: 4018381151
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"
)

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 := &ListDeliveryPlansRequest{
		PageSize:   wxpay_brand_utility.Int64(5),
		Offset:     wxpay_brand_utility.Int64(10),
		PlanState:  PLANSTATE_CREATED.Ptr(),
		AuditState: PLANAUDITSTATE_AUDIT_INITIAL.Ptr(),
		PlanId:     wxpay_brand_utility.String("12000"),
	}

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

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

func ListDeliveryPlans(config *wxpay_brand_utility.BrandConfig, request *ListDeliveryPlansRequest) (response *ListDeliveryPlansResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/brand/marketing/delivery-plan/delivery-plans"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	query := reqUrl.Query()
	if request.PageSize != nil {
		query.Add("page_size", fmt.Sprintf("%v", *request.PageSize))
	}
	if request.Offset != nil {
		query.Add("offset", fmt.Sprintf("%v", *request.Offset))
	}
	if request.PlanState != nil {
		query.Add("plan_state", fmt.Sprintf("%v", *request.PlanState))
	}
	if request.AuditState != nil {
		query.Add("audit_state", fmt.Sprintf("%v", *request.AuditState))
	}
	if request.PlanId != nil {
		query.Add("plan_id", *request.PlanId)
	}
	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 := &ListDeliveryPlansResponse{}
		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 ListDeliveryPlansRequest struct {
	PageSize   *int64          `json:"page_size,omitempty"`
	Offset     *int64          `json:"offset,omitempty"`
	PlanId     *string         `json:"plan_id,omitempty"`
	PlanState  *PlanState      `json:"plan_state,omitempty"`
	AuditState *PlanAuditState `json:"audit_state,omitempty"`
}

func (o *ListDeliveryPlansRequest) MarshalJSON() ([]byte, error) {
	type Alias ListDeliveryPlansRequest
	a := &struct {
		PageSize   *int64          `json:"page_size,omitempty"`
		Offset     *int64          `json:"offset,omitempty"`
		PlanId     *string         `json:"plan_id,omitempty"`
		PlanState  *PlanState      `json:"plan_state,omitempty"`
		AuditState *PlanAuditState `json:"audit_state,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		PageSize:   nil,
		Offset:     nil,
		PlanId:     nil,
		PlanState:  nil,
		AuditState: nil,
		Alias:      (*Alias)(o),
	}
	return json.Marshal(a)
}

type ListDeliveryPlansResponse struct {
	TotalCount *int64         `json:"total_count,omitempty"`
	PlanList   []DeliveryPlan `json:"plan_list,omitempty"`
}

type PlanState string

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

const (
	PLANSTATE_CREATED    PlanState = "CREATED"
	PLANSTATE_TERMINATED PlanState = "TERMINATED"
	PLANSTATE_EXPIRED    PlanState = "EXPIRED"
	PLANSTATE_DELIVERING PlanState = "DELIVERING"
	PLANSTATE_PAUSED     PlanState = "PAUSED"
)

type PlanAuditState string

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

const (
	PLANAUDITSTATE_AUDIT_INITIAL    PlanAuditState = "AUDIT_INITIAL"
	PLANAUDITSTATE_AUDIT_PROCESSING PlanAuditState = "AUDIT_PROCESSING"
	PLANAUDITSTATE_AUDIT_PASSED     PlanAuditState = "AUDIT_PASSED"
	PLANAUDITSTATE_AUDIT_REJECTED   PlanAuditState = "AUDIT_REJECTED"
)

type DeliveryPlan struct {
	PlanId            *string           `json:"plan_id,omitempty"`
	PlanName          *string           `json:"plan_name,omitempty"`
	PlanState         *PlanState        `json:"plan_state,omitempty"`
	DeliveryStartTime *string           `json:"delivery_start_time,omitempty"`
	DeliveryEndTime   *string           `json:"delivery_end_time,omitempty"`
	ProductCouponId   *string           `json:"product_coupon_id,omitempty"`
	UsageMode         *ProductUsageMode `json:"usage_mode,omitempty"`
	StockId           *string           `json:"stock_id,omitempty"`
	StockBundleId     *string           `json:"stock_bundle_id,omitempty"`
	RecommendWord     *string           `json:"recommend_word,omitempty"`
	TotalCount        *int64            `json:"total_count,omitempty"`
	UserLimit         *int64            `json:"user_limit,omitempty"`
	DailyLimit        *int64            `json:"daily_limit,omitempty"`
	ReuseCouponConfig *bool             `json:"reuse_coupon_config,omitempty"`
	BrandId           *string           `json:"brand_id,omitempty"`
}

type ProductUsageMode string

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

const (
	PRODUCTUSAGEMODE_SINGLE             ProductUsageMode = "SINGLE"
	PRODUCTUSAGEMODE_PROGRESSIVE_BUNDLE ProductUsageMode = "PROGRESSIVE_BUNDLE"
)

```
