client.go
2.6 KB
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
package k3cloud
import (
"errors"
"fmt"
"io"
"time"
"github.com/google/uuid"
)
//初始化默认的客户端
type Client struct {
HttpClient
}
/*NewClient 新建客户端,
默认执行登录操作,填充cookie
*/
func NewClient(apihost string, acctID string, username string, password string, lcid ...int) (*Client, error) {
client := &Client{
HttpClient{
HostUrl: apihost,
},
}
_, err := client.Login(acctID, username, password, lcid...)
if err != nil {
return nil, err
}
return client, nil
}
/*
Login 登陆验证接口
acctID 账套Id,
username 密码
password 用户登陆名
lcid 语言id,选择哪种语言访问,参考:中文2052,英文1033,繁体307
*/
func (c *Client) Login(acctID string, username string, password string, lcid ...int) (*ReponseValidateUser, error) {
api := "/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc"
lcidTemp := 2052
if len(lcid) > 0 {
lcidTemp = lcid[0]
}
param := RequestValidateUser{
Format: 1,
Useragent: "ApiClient",
Rid: uuid.NewString(),
Timestamp: time.Now().Format("2006-01-02"),
V: "1.0",
Parameters: []interface{}{acctID, username, password, lcidTemp},
}
resp, err := c.PostRequest(api, param)
if err != nil {
return nil, err
}
defer resp.Body.Close()
cookies := resp.Cookies()
//填充调用接口时需要的cookie
c.HttpClient.Cookie = cookies
var loginReturn ReponseValidateUser
err = c.DecodeJSON(resp.Body, &loginReturn)
if err != nil {
return nil, err
}
if loginReturn.LoginResultType != 1 {
return &loginReturn, errors.New(loginReturn.Message)
}
return &loginReturn, nil
}
// ExecuteBillQuery 单据查询
func (c *Client) ExecuteBillQuery(param RequestExecuteBillQuery) (*BillQueryResult, error) {
api := "/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc"
resp, err := c.PostRequest(api, param)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var dataByte []byte
dataByte, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
queryResult := newBillQueryResult(dataByte, param.Data.FieldKeys)
return queryResult, queryResult.Error()
}
// ExecuteMetaDataQuery 元数据查询
func (c *Client) ExecuteMetaDataQuery(param RequestMetaDataQuery) ([]byte, error) {
api := "/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.QueryBusinessInfo.common.kdsvc"
resp, err := c.PostRequest(api, param)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var dataByte []byte
dataByte, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
fmt.Println("data ", string(dataByte))
return dataByte, nil
}