-
Notifications
You must be signed in to change notification settings - Fork 157
/
client.go
77 lines (55 loc) · 2.28 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
package image
import (
"context"
"errors"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/image/response"
)
type Client struct {
BaseClient *kernel.BaseClient
}
// 本接口提供基于小程序的图片智能裁剪能力
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/img/img.aiCrop.html
func (comp *Client) AICrop(ctx context.Context, imgURL string, img *power.HashMap) (*response.ResponseIMGAICrop, error) {
result := &response.ResponseIMGAICrop{}
_, err := comp.UploadImage(ctx, "cv/img/aicrop", imgURL, img, result)
return result, err
}
// 本接口提供基于小程序的条码/二维码识别的API
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/img/img.scanQRCode.html
func (comp *Client) ScanQRCode(ctx context.Context, imgURL string, img *power.HashMap) (*response.ResponseIMGScanQRCode, error) {
result := &response.ResponseIMGScanQRCode{}
_, err := comp.UploadImage(ctx, "cv/img/aicrop", imgURL, img, result)
return result, err
}
// 本接口提供基于小程序的图片高清化能力
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/img/img.superresolution.html
func (comp *Client) SuperResolution(ctx context.Context, imgURL string, img *power.HashMap) (*response.ResponseIMGSuperResolution, error) {
result := &response.ResponseIMGSuperResolution{}
_, err := comp.UploadImage(ctx, "cv/img/superresolution", imgURL, img, result)
return result, err
}
func (comp *Client) UploadImage(ctx context.Context, entryPoint string, imgURL string, img *power.HashMap, result interface{}) (interface{}, error) {
params := &object.StringMap{}
var formData *kernel.UploadForm
if imgURL != "" {
params = &object.StringMap{
"img_url": imgURL,
}
} else if img != nil {
formData = &kernel.UploadForm{
Contents: []*kernel.UploadContent{
&kernel.UploadContent{
Name: (*img)["name"].(string),
Value: (*img)["value"],
},
},
}
} else {
return nil, errors.New("Please send image url or image form data ")
}
rs, err := comp.BaseClient.HttpUpload(ctx, entryPoint, nil, formData, params, nil, result)
return rs, err
}