-
Notifications
You must be signed in to change notification settings - Fork 7
/
account_book_api.go
81 lines (66 loc) · 1.68 KB
/
account_book_api.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
package feidee
import (
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/PuerkitoBio/goquery"
)
// 刷新账本列表
func (cli *Client) SyncAccountBookList() error {
resp, err := cli.Get(BaseUrl + "/report_index.do")
if err != nil {
return fmt.Errorf("请求出错: %s", err)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return fmt.Errorf("读取出错: %s", err)
}
idNames := []IdName{}
lists := doc.Find("ul.s-accountbook-all li")
if len(lists.Nodes) == 0 {
return fmt.Errorf("未找到账本列表")
}
for i := range lists.Nodes {
list := lists.Eq(i)
name, _ := list.Attr("title")
idStr, _ := list.Attr("data-bookid")
id, _ := strconv.Atoi(idStr)
if id != 0 && name != "" {
idNames = append(idNames, IdName{Id: id, Name: name})
}
}
cli.AccountBookList = idNames
return nil
}
// 切换账本
func (cli *Client) SwitchAccountBook(name string) error {
var accountBookId int
for _, accountBook := range cli.AccountBookList {
if accountBook.Name == name {
accountBookId = accountBook.Id
break
}
}
if accountBookId == 0 {
return fmt.Errorf("未找到账本")
}
data := url.Values{}
data.Set("opt", "switch")
data.Set("switchId", strconv.Itoa(accountBookId))
data.Set("return", "xxx") //该参数必须提供但值无所谓
resp, err := cli.Get(BaseUrl + "/systemSet/book.do?" + data.Encode())
if err != nil {
return fmt.Errorf("请求错误: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("响应出错: %s", resp.Status)
}
err = cli.SyncMetaInfo()
if err != nil {
return fmt.Errorf("获取账基础信息错误: %s", err)
}
return nil
}