client.go 2.1 KB
package k3cloud

import (
	"errors"
	"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()
}