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