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

     1  package statistics
     2  
     3  import (
     4  	"github.com/chanxuehong/wechat/mp/core"
     5  )
     6  
     7  const DeviceListPageSize = 50
     8  
     9  type DeviceListResult 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  		DeviceStatisticsList []DeviceStatistics `json:"devices"`
    18  	} `json:"data"`
    19  }
    20  
    21  // 批量查询设备统计数据接口
    22  func DeviceList(clt *core.Client, date int64, pageIndex int) (rslt *DeviceListResult, 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  		DeviceListResult
    34  	}
    35  
    36  	incompleteURL := "https://api.weixin.qq.com/shakearound/statistics/devicelist?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  	devices := result.DeviceListResult.Data.DeviceStatisticsList
    47  	for i := 0; i < len(devices); i++ {
    48  		devices[i].Ftime = result.DeviceListResult.Date
    49  	}
    50  	result.DeviceListResult.ItemCount = len(devices)
    51  	rslt = &result.DeviceListResult
    52  	return
    53  }
    54  
    55  // DeviceStatisticsIterator
    56  //
    57  //	iter, err := NewDeviceStatisticsIterator(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 DeviceStatisticsIterator struct {
    70  	clt *core.Client
    71  
    72  	date          int64
    73  	nextPageIndex int
    74  
    75  	lastDeviceListResult *DeviceListResult // 最近一次获取的数据
    76  	nextPageCalled       bool              // NextPage() 是否调用过
    77  }
    78  
    79  func (iter *DeviceStatisticsIterator) TotalCount() int {
    80  	return iter.lastDeviceListResult.TotalCount
    81  }
    82  
    83  func (iter *DeviceStatisticsIterator) HasNext() bool {
    84  	if !iter.nextPageCalled { // 第一次调用需要特殊对待
    85  		return iter.lastDeviceListResult.ItemCount > 0
    86  	}
    87  
    88  	return iter.lastDeviceListResult.ItemCount >= DeviceListPageSize
    89  }
    90  
    91  func (iter *DeviceStatisticsIterator) NextPage() (statisticsList []DeviceStatistics, err error) {
    92  	if !iter.nextPageCalled { // 第一次调用需要特殊对待
    93  		iter.nextPageCalled = true
    94  
    95  		statisticsList = iter.lastDeviceListResult.Data.DeviceStatisticsList
    96  		return
    97  	}
    98  
    99  	rslt, err := DeviceList(iter.clt, iter.date, iter.nextPageIndex)
   100  	if err != nil {
   101  		return
   102  	}
   103  
   104  	iter.nextPageIndex++
   105  	iter.lastDeviceListResult = rslt
   106  
   107  	statisticsList = rslt.Data.DeviceStatisticsList
   108  	return
   109  }
   110  
   111  func NewDeviceStatisticsIterator(clt *core.Client, date int64, pageIndex int) (iter *DeviceStatisticsIterator, err error) {
   112  	// 逻辑上相当于第一次调用 DeviceStatisticsIterator.NextPage, 因为第一次调用 DeviceStatisticsIterator.HasNext 需要数据支撑, 所以提前获取了数据
   113  
   114  	rslt, err := DeviceList(clt, date, pageIndex)
   115  	if err != nil {
   116  		return
   117  	}
   118  
   119  	iter = &DeviceStatisticsIterator{
   120  		clt: clt,
   121  
   122  		date:          date,
   123  		nextPageIndex: pageIndex + 1,
   124  
   125  		lastDeviceListResult: rslt,
   126  		nextPageCalled:       false,
   127  	}
   128  	return
   129  }