forked from baidu/smartapp-openapi-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addCouponBanner.go
76 lines (67 loc) · 2.16 KB
/
addCouponBanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package openapi
import (
"encoding/json"
)
// AddCouponBannerRequest 请求结构体
type AddCouponBannerRequest struct {
AccessToken string // 接口调用凭证
CouponID string // 卡券 ID
PicURL string // 卡券 banner 图片
Title string // 卡券 banner 图标题
AppRedirectPath string // banner 图跳转的小程序页面路径
}
// 响应结构体
type AddCouponBannerResponsedata struct {
BannerID int64 `json:"bannerId"` // 卡券 banner 记录 id
}
type AddCouponBannerResponse struct {
Data AddCouponBannerResponsedata `json:"data"` // 响应对象
Errno int64 `json:"errno"` // 错误码
ErrMsg string `json:"msg"` // 错误信息
ErrorCode int64 `json:"error_code"` // openapi 错误码
ErrorMsg string `json:"error_msg"` // openapi 错误信息
}
// AddCouponBanner
func AddCouponBanner(params *AddCouponBannerRequest) (*AddCouponBannerResponsedata, error) {
var (
err error
defaultRet *AddCouponBannerResponsedata
)
respData := &AddCouponBannerResponse{}
client := NewHTTPClient().
SetContentType(ContentTypeJSON).
SetConverterType(ConverterTypeJSON).
SetMethod("POST").
SetScheme(SCHEME).
SetHost(OPENAPIHOST).
SetPath("/rest/2.0/smartapp/v1.0/coupon/banner/add")
client.AddGetParam("access_token", params.AccessToken)
client.AddGetParam("sp_sdk_ver", SDKVERSION)
client.AddGetParam("sp_sdk_lang", SDKLANG)
postData := map[string]interface{}{
"couponId": params.CouponID,
"picUrl": params.PicURL,
"title": params.Title,
"appRedirectPath": params.AppRedirectPath,
}
bts, err := json.Marshal(postData)
if err != nil {
return defaultRet, err
}
client.SetBody(bts)
err = client.Do()
if err != nil {
return defaultRet, err
}
err = client.Convert(respData)
if err != nil {
return defaultRet, err
}
if respData.ErrorCode != 0 {
return defaultRet, &OpenAPIError{respData.ErrorCode, respData.ErrorMsg, respData}
}
if respData.Errno != 0 {
return defaultRet, &APIError{respData.Errno, respData.ErrMsg, respData}
}
return &respData.Data, nil
}