github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/bizwifi/device/list.go (about) 1 package device 2 3 import ( 4 "github.com/chanxuehong/wechat/internal/util" 5 "github.com/chanxuehong/wechat/mp/core" 6 ) 7 8 type SearchQuery struct { 9 ShopId *int64 `json:"shop_id,omitempty"` // 根据门店id查询 10 PageIndex int `json:"pageindex,omitempty"` // 分页下标,默认从1开始 11 PageSize int `json:"pagesize,omitempty"` // 每页的个数,默认10个,最大20个 12 } 13 14 func NewSearchQuery1(PageIndex, PageSize int) *SearchQuery { 15 return &SearchQuery{ 16 PageIndex: PageIndex, 17 PageSize: PageSize, 18 } 19 } 20 21 func NewSearchQuery2(ShopId int64, PageIndex, PageSize int) *SearchQuery { 22 return &SearchQuery{ 23 ShopId: util.Int64(ShopId), 24 PageIndex: PageIndex, 25 PageSize: PageSize, 26 } 27 } 28 29 type ListResult struct { 30 PageIndex int `json:"pageindex"` // 分页下标 31 PageCount int `json:"pagecount"` // 分页页数 32 33 TotalCount int `json:"totalcount"` // 总数 34 ItemCount int `json:"itemcount"` // 当前页列表大小 35 36 Records []Device `json:"records"` // 当前页列表数组 37 } 38 39 type Device struct { 40 ShopId int64 `json:"shop_id"` // 门店ID 41 SSID string `json:"ssid"` // 连网设备ssid 42 BSSID string `json:"bssid"` // 无线MAC地址 43 } 44 45 // 查询设备. 46 func List(clt *core.Client, query *SearchQuery) (rslt *ListResult, err error) { 47 var result struct { 48 core.Error 49 ListResult `json:"data"` 50 } 51 52 incompleteURL := "https://api.weixin.qq.com/bizwifi/device/list?access_token=" 53 if err = clt.PostJSON(incompleteURL, query, &result); err != nil { 54 return 55 } 56 57 if result.ErrCode != core.ErrCodeOK { 58 err = &result.Error 59 return 60 } 61 62 result.ListResult.ItemCount = len(result.ListResult.Records) 63 rslt = &result.ListResult 64 return 65 } 66 67 // DeviceIterator 68 // 69 // iter, err := NewDeviceIterator(*core.Client, *SearchQuery) 70 // if err != nil { 71 // // TODO: 增加你的代码 72 // } 73 // 74 // for iter.HasNext() { 75 // items, err := iter.NextPage() 76 // if err != nil { 77 // // TODO: 增加你的代码 78 // } 79 // // TODO: 增加你的代码 80 // } 81 type DeviceIterator struct { 82 clt *core.Client 83 84 nextQuery *SearchQuery 85 86 lastListResult *ListResult // 最近一次获取的数据 87 nextPageCalled bool // NextPage() 是否调用过 88 } 89 90 func (iter *DeviceIterator) TotalCount() int { 91 return iter.lastListResult.TotalCount 92 } 93 94 func (iter *DeviceIterator) HasNext() bool { 95 if !iter.nextPageCalled { // 第一次调用需要特殊对待 96 return iter.lastListResult.ItemCount > 0 || 97 iter.nextQuery.PageIndex <= iter.lastListResult.PageCount 98 } 99 100 return iter.nextQuery.PageIndex <= iter.lastListResult.PageCount 101 } 102 103 func (iter *DeviceIterator) NextPage() (records []Device, err error) { 104 if !iter.nextPageCalled { // 第一次调用需要特殊对待 105 iter.nextPageCalled = true 106 107 records = iter.lastListResult.Records 108 return 109 } 110 111 rslt, err := List(iter.clt, iter.nextQuery) 112 if err != nil { 113 return 114 } 115 116 iter.nextQuery.PageIndex++ 117 iter.lastListResult = rslt 118 119 records = rslt.Records 120 return 121 } 122 123 func NewDeviceIterator(clt *core.Client, query *SearchQuery) (iter *DeviceIterator, err error) { 124 // 逻辑上相当于第一次调用 DeviceIterator.NextPage, 因为第一次调用 DeviceIterator.HasNext 需要数据支撑, 所以提前获取了数据 125 126 rslt, err := List(clt, query) 127 if err != nil { 128 return 129 } 130 131 query.PageIndex++ 132 133 iter = &DeviceIterator{ 134 clt: clt, 135 136 nextQuery: query, 137 138 lastListResult: rslt, 139 nextPageCalled: false, 140 } 141 return 142 }