github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/shakearound/statistics/pagelist.go (about)

     1  package statistics
     2  
     3  import (
     4  	"github.com/chanxuehong/wechat/mp/core"
     5  )
     6  
     7  const PageListPageSize = 50
     8  
     9  type PageListResult struct {
    10  	PageIndex int   `json:"page_index"`
    11  	Date      int64 `json:"date"`
    12  
    13  	TotalCount int `json:"total_count"`
    14  	ItemCount  int `json:"item_count"`
    15  
    16  	Data struct {
    17  		PageStatisticsList []PageStatistics `json:"pages"`
    18  	} `json:"data"`
    19  }
    20  
    21  // 批量查询设备统计数据接口
    22  func PageList(clt *core.Client, date int64, pageIndex int) (rslt *PageListResult, err error) {
    23  	request := struct {
    24  		Date      int64 `json:"date"`
    25  		PageIndex int   `json:"page_index"`
    26  	}{
    27  		Date:      date,
    28  		PageIndex: pageIndex,
    29  	}
    30  
    31  	var result struct {
    32  		core.Error
    33  		PageListResult
    34  	}
    35  
    36  	incompleteURL := "https://api.weixin.qq.com/shakearound/statistics/pagelist?access_token="
    37  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    38  		return
    39  	}
    40  
    41  	if result.ErrCode != core.ErrCodeOK {
    42  		err = &result.Error
    43  		return
    44  	}
    45  
    46  	pages := result.PageListResult.Data.PageStatisticsList
    47  	for i := 0; i < len(pages); i++ {
    48  		pages[i].Ftime = result.PageListResult.Date
    49  	}
    50  	result.PageListResult.ItemCount = len(pages)
    51  	rslt = &result.PageListResult
    52  	return
    53  }
    54  
    55  // PageStatisticsIterator
    56  //
    57  //	iter, err := NewPageStatisticsIterator(clt, date, pageIndex)
    58  //	if err != nil {
    59  //	    // TODO: 增加你的代码
    60  //	}
    61  //
    62  //	for iter.HasNext() {
    63  //	    items, err := iter.NextPage()
    64  //	    if err != nil {
    65  //	        // TODO: 增加你的代码
    66  //	    }
    67  //	    // TODO: 增加你的代码
    68  //	}
    69  type PageStatisticsIterator struct {
    70  	clt *core.Client
    71  
    72  	date          int64
    73  	nextPageIndex int
    74  
    75  	lastPageListResult *PageListResult // 最近一次获取的数据
    76  	nextPageCalled     bool            // NextPage() 是否调用过
    77  }
    78  
    79  func (iter *PageStatisticsIterator) TotalCount() int {
    80  	return iter.lastPageListResult.TotalCount
    81  }
    82  
    83  func (iter *PageStatisticsIterator) HasNext() bool {
    84  	if !iter.nextPageCalled { // 第一次调用需要特殊对待
    85  		return iter.lastPageListResult.ItemCount > 0
    86  	}
    87  
    88  	return iter.lastPageListResult.ItemCount >= PageListPageSize
    89  }
    90  
    91  func (iter *PageStatisticsIterator) NextPage() (statisticsList []PageStatistics, err error) {
    92  	if !iter.nextPageCalled { // 第一次调用需要特殊对待
    93  		iter.nextPageCalled = true
    94  
    95  		statisticsList = iter.lastPageListResult.Data.PageStatisticsList
    96  		return
    97  	}
    98  
    99  	rslt, err := PageList(iter.clt, iter.date, iter.nextPageIndex)
   100  	if err != nil {
   101  		return
   102  	}
   103  
   104  	iter.nextPageIndex++
   105  	iter.lastPageListResult = rslt
   106  
   107  	statisticsList = rslt.Data.PageStatisticsList
   108  	return
   109  }
   110  
   111  func NewPageStatisticsIterator(clt *core.Client, date int64, pageIndex int) (iter *PageStatisticsIterator, err error) {
   112  	// 逻辑上相当于第一次调用 PageStatisticsIterator.NextPage, 因为第一次调用 PageStatisticsIterator.HasNext 需要数据支撑, 所以提前获取了数据
   113  
   114  	rslt, err := PageList(clt, date, pageIndex)
   115  	if err != nil {
   116  		return
   117  	}
   118  
   119  	iter = &PageStatisticsIterator{
   120  		clt: clt,
   121  
   122  		date:          date,
   123  		nextPageIndex: pageIndex + 1,
   124  
   125  		lastPageListResult: rslt,
   126  		nextPageCalled:     false,
   127  	}
   128  	return
   129  }