forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pagelist.go
129 lines (103 loc) · 3.02 KB
/
pagelist.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package statistics
import (
"github.com/charsunny/wechat/mp/core"
)
const PageListPageSize = 50
type PageListResult struct {
PageIndex int `json:"page_index"`
Date int64 `json:"date"`
TotalCount int `json:"total_count"`
ItemCount int `json:"item_count"`
Data struct {
PageStatisticsList []PageStatistics `json:"pages"`
} `json:"data"`
}
// 批量查询设备统计数据接口
func PageList(clt *core.Client, date int64, pageIndex int) (rslt *PageListResult, err error) {
request := struct {
Date int64 `json:"date"`
PageIndex int `json:"page_index"`
}{
Date: date,
PageIndex: pageIndex,
}
var result struct {
core.Error
PageListResult
}
incompleteURL := "https://api.weixin.qq.com/shakearound/statistics/pagelist?access_token="
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
pages := result.PageListResult.Data.PageStatisticsList
for i := 0; i < len(pages); i++ {
pages[i].Ftime = result.PageListResult.Date
}
result.PageListResult.ItemCount = len(pages)
rslt = &result.PageListResult
return
}
// PageStatisticsIterator
//
// iter, err := NewPageStatisticsIterator(clt, date, pageIndex)
// if err != nil {
// // TODO: 增加你的代码
// }
//
// for iter.HasNext() {
// items, err := iter.NextPage()
// if err != nil {
// // TODO: 增加你的代码
// }
// // TODO: 增加你的代码
// }
type PageStatisticsIterator struct {
clt *core.Client
date int64
nextPageIndex int
lastPageListResult *PageListResult // 最近一次获取的数据
nextPageCalled bool // NextPage() 是否调用过
}
func (iter *PageStatisticsIterator) TotalCount() int {
return iter.lastPageListResult.TotalCount
}
func (iter *PageStatisticsIterator) HasNext() bool {
if !iter.nextPageCalled { // 第一次调用需要特殊对待
return iter.lastPageListResult.ItemCount > 0
}
return iter.lastPageListResult.ItemCount >= PageListPageSize
}
func (iter *PageStatisticsIterator) NextPage() (statisticsList []PageStatistics, err error) {
if !iter.nextPageCalled { // 第一次调用需要特殊对待
iter.nextPageCalled = true
statisticsList = iter.lastPageListResult.Data.PageStatisticsList
return
}
rslt, err := PageList(iter.clt, iter.date, iter.nextPageIndex)
if err != nil {
return
}
iter.nextPageIndex++
iter.lastPageListResult = rslt
statisticsList = rslt.Data.PageStatisticsList
return
}
func NewPageStatisticsIterator(clt *core.Client, date int64, pageIndex int) (iter *PageStatisticsIterator, err error) {
// 逻辑上相当于第一次调用 PageStatisticsIterator.NextPage, 因为第一次调用 PageStatisticsIterator.HasNext 需要数据支撑, 所以提前获取了数据
rslt, err := PageList(clt, date, pageIndex)
if err != nil {
return
}
iter = &PageStatisticsIterator{
clt: clt,
date: date,
nextPageIndex: pageIndex + 1,
lastPageListResult: rslt,
nextPageCalled: false,
}
return
}