-
Notifications
You must be signed in to change notification settings - Fork 156
/
client.go
115 lines (90 loc) · 3.22 KB
/
client.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package wxaCode
import (
"context"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
response4 "github.com/ArtisanCloud/PowerWeChat/v3/src/work/media/response"
"io/fs"
"io/ioutil"
"net/http"
)
type Client struct {
BaseClient *kernel.BaseClient
}
// 获取小程序二维码,适用于需要的码数量较少的业务场景
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html
func (comp *Client) CreateQRCode(ctx context.Context, path string, width int64) (*http.Response, error) {
var header = &response4.ResponseHeaderMedia{}
if width <= 0 {
width = 430
}
data := &object.HashMap{
"form_params": &object.HashMap{
"path": path,
"width": width,
},
}
rs, err := comp.BaseClient.RequestRaw(ctx, "cgi-bin/wxaapp/createwxaqrcode", http.MethodPost, data, &header, nil)
return rs, err
}
// 获取小程序码,适用于需要的码数量较少的业务场景
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html
func (comp *Client) Get(ctx context.Context, path string, width int64,
autoColor bool, lineColor *power.HashMap, isHyaline bool, envVersion string) (*http.Response, error) {
var header = &response4.ResponseHeaderMedia{}
if width <= 0 {
width = 430
}
data := &object.HashMap{
"form_params": &object.HashMap{
"path": path,
"width": width,
"auto_color": autoColor,
"line_color": lineColor.ToHashMap(),
"is_hyaline": isHyaline,
"env_version": envVersion,
},
}
rs, err := comp.BaseClient.RequestRaw(ctx, "wxa/getwxacode", http.MethodPost, data, &header, nil)
return rs, err
}
// 获取小程序码,适用于需要的码数量较少的业务场景
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html
func (comp *Client) SaveAs(ctx context.Context, savedPath string, perm fs.FileMode,
path string, width int64,
autoColor bool, lineColor *power.HashMap, isHyaline bool, envVersion string) error {
rs, err := comp.Get(ctx, path, width, autoColor, lineColor, isHyaline, envVersion)
if err != nil {
return err
}
body, err := ioutil.ReadAll(rs.Body)
err = ioutil.WriteFile(savedPath, body, perm) //保存图片地址
return err
}
// 获取小程序码,适用于需要的码数量极多的业务场景
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
func (comp *Client) GetUnlimited(
ctx context.Context,
scene string, page string,
checkPath bool, envVersion string, width int64,
autoColor bool, lineColor *power.HashMap, isHyaline bool) (*http.Response, error) {
var header = &response4.ResponseHeaderMedia{}
if width <= 0 {
width = 430
}
data := &object.HashMap{
"form_params": &object.HashMap{
"scene": scene,
"page": page,
"width": width,
"check_path": checkPath,
"env_version": envVersion,
"auto_color": autoColor,
"line_color": lineColor.ToHashMap(),
"is_hyaline": isHyaline,
},
}
rs, err := comp.BaseClient.RequestRaw(ctx, "wxa/getwxacodeunlimit", http.MethodPost, data, &header, nil)
return rs, err
}