
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9)
10
11func main() {
12
13 config, err := wxpay_utility.CreateMchConfig(
14 "19xxxxxxxx",
15 "1DDE55AD98Exxxxxxxxxx",
16 "/path/to/apiclient_key.pem",
17 "PUB_KEY_ID_xxxxxxxxxxxxx",
18 "/path/to/wxp_pub.pem",
19 )
20 if err != nil {
21 fmt.Println(err)
22 return
23 }
24
25 request := &ListGuidesRequest{
26 SubMchid: wxpay_utility.String("1234567890"),
27 StoreId: wxpay_utility.Int64(1234),
28 Userid: wxpay_utility.String("robert"),
29 Mobile: wxpay_utility.String("str2WoWy8uUiWM7xahvNv0lV3C+nn3t4QlKNnyr+iwlk2FoMcn/lxrR6YdKKFI6NFJkC5oyvzNwc1MDzIgjLR0W6bJKiwWeOV3Fp0x+VIoRDONal1Mgb6VTYg7lvACEqdwmVkvbt4/oEWaWP62njMGGe0fMiBSAvao3LrcsOwDvN3E9kiaw5XQZMP/rUdWTFfy5THuohcQobGMrxclHKAnwAHU8CWfkziW5crUc3Z83uMVcFQu38y9EcWR12FJ3jip5nyVKiqCF4iDSN+JRXjWsWlqTZ0Y8Q+piBCS5ACusK6nz7mKQeypi9fKjAggRfvNFPf/bNxPvork4mMVgZkA=="),
30 WorkId: wxpay_utility.String("robert"),
31 Limit: wxpay_utility.Int64(5),
32 Offset: wxpay_utility.Int64(0),
33 }
34
35 response, err := ListGuides(config, request)
36 if err != nil {
37 fmt.Printf("请求失败: %+v\n", err)
38
39 return
40 }
41
42
43 fmt.Printf("请求成功: %+v\n", response)
44}
45
46func ListGuides(config *wxpay_utility.MchConfig, request *ListGuidesRequest) (response *ListGuidesResponse, err error) {
47 const (
48 host = "https://api.mch.weixin.qq.com"
49 method = "GET"
50 path = "/v3/smartguide/guides"
51 )
52
53 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
54 if err != nil {
55 return nil, err
56 }
57 query := reqUrl.Query()
58 if request.SubMchid != nil {
59 query.Add("sub_mchid", *request.SubMchid)
60 }
61 if request.StoreId != nil {
62 query.Add("store_id", fmt.Sprintf("%v", *request.StoreId))
63 }
64 if request.Userid != nil {
65 query.Add("userid", *request.Userid)
66 }
67 if request.Mobile != nil {
68 query.Add("mobile", *request.Mobile)
69 }
70 if request.WorkId != nil {
71 query.Add("work_id", *request.WorkId)
72 }
73 if request.Limit != nil {
74 query.Add("limit", fmt.Sprintf("%v", *request.Limit))
75 }
76 if request.Offset != nil {
77 query.Add("offset", fmt.Sprintf("%v", *request.Offset))
78 }
79 reqUrl.RawQuery = query.Encode()
80 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
81 if err != nil {
82 return nil, err
83 }
84 httpRequest.Header.Set("Accept", "application/json")
85 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
86 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
87 if err != nil {
88 return nil, err
89 }
90 httpRequest.Header.Set("Authorization", authorization)
91
92 client := &http.Client{}
93 httpResponse, err := client.Do(httpRequest)
94 if err != nil {
95 return nil, err
96 }
97 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
98 if err != nil {
99 return nil, err
100 }
101 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
102
103 err = wxpay_utility.ValidateResponse(
104 config.WechatPayPublicKeyId(),
105 config.WechatPayPublicKey(),
106 &httpResponse.Header,
107 respBody,
108 )
109 if err != nil {
110 return nil, err
111 }
112 response := &ListGuidesResponse{}
113 if err := json.Unmarshal(respBody, response); err != nil {
114 return nil, err
115 }
116
117 return response, nil
118 } else {
119 return nil, wxpay_utility.NewApiException(
120 httpResponse.StatusCode,
121 httpResponse.Header,
122 respBody,
123 )
124 }
125}
126
127type ListGuidesRequest struct {
128 SubMchid *string `json:"sub_mchid,omitempty"`
129 StoreId *int64 `json:"store_id,omitempty"`
130 Userid *string `json:"userid,omitempty"`
131 Mobile *string `json:"mobile,omitempty"`
132 WorkId *string `json:"work_id,omitempty"`
133 Limit *int64 `json:"limit,omitempty"`
134 Offset *int64 `json:"offset,omitempty"`
135}
136
137func (o *ListGuidesRequest) MarshalJSON() ([]byte, error) {
138 type Alias ListGuidesRequest
139 a := &struct {
140 SubMchid *string `json:"sub_mchid,omitempty"`
141 StoreId *int64 `json:"store_id,omitempty"`
142 Userid *string `json:"userid,omitempty"`
143 Mobile *string `json:"mobile,omitempty"`
144 WorkId *string `json:"work_id,omitempty"`
145 Limit *int64 `json:"limit,omitempty"`
146 Offset *int64 `json:"offset,omitempty"`
147 *Alias
148 }{
149
150 SubMchid: nil,
151 StoreId: nil,
152 Userid: nil,
153 Mobile: nil,
154 WorkId: nil,
155 Limit: nil,
156 Offset: nil,
157 Alias: (*Alias)(o),
158 }
159 return json.Marshal(a)
160}
161
162type ListGuidesResponse struct {
163 Data []Guide `json:"data,omitempty"`
164 TotalCount *int64 `json:"total_count,omitempty"`
165 Limit *int64 `json:"limit,omitempty"`
166 Offset *int64 `json:"offset,omitempty"`
167}
168
169type Guide struct {
170 GuideId *string `json:"guide_id,omitempty"`
171 StoreId *int64 `json:"store_id,omitempty"`
172 Name *string `json:"name,omitempty"`
173 Mobile *string `json:"mobile,omitempty"`
174 Userid *string `json:"userid,omitempty"`
175 WorkId *string `json:"work_id,omitempty"`
176}
177