8. 案例
data: {
options: {},
shopList: [],
page: 1,
pageSize:10,
total:0,
isLoading:false
},
onLoad(options) {
console.log(options)
this.setData({
options: options
})
this.getShopList()
},
onReady() {
wx.setNavigationBarTitle({
title: this.data.options.name,
})
},
onPullDownRefresh() {
this.setData({
page:1,
shopList:[],
total:0
})
this.getShopList(() => {
wx.stopPullDownRefresh()
})
},
onReachBottom() {
if (this.data.page * this.data.pageSize >= this.data.total) {
return wx.showToast({
title: '数据加载完毕',
icon: 'none'
})
}
if(this.data.isLoading)return
this.setData({
page:this.data.page+1
})
this.getShopList()
},
getShopList(cb){
this.setData({
isLoading:true
})
wx.showLoading({
title: '数据加载中...',
})
wx.request({
url: 'https://www.escook/cn/categories/${this.data.options.id}/shops',
method:'GET',
data:{
_page:this.data.page,
_limit:this.data.pageSize
},
success:(res)=>{
this.setData({
shopList:[...this.data.shopList,...res.data],
total:res.header['X-Total-Count']-0
})
},
complete:()=>{
wx.hideLoading()
cb && cb()
this.setData({
isLoading:false
})
}
})
},
<text>pages/shoplist/shoplist.wxml</text>
<view class="shop-item" wx:for="{{shopList}}" wx:key="id">
<view class="thumb">
<image src="{{item.image[0]}}" mode="widthFix"/>
</view>
<view class="info">
<text class="shop-title">{{item.name}}</text>
<text>电话:{{tools.splitPhone(item.phone)}}</text>
<text>地址:{{item.address}}</text>
<text>营业时间:{{item.businessHors}}</text>
</view>
</view>
<wxs src="tools.wxs" module="tools"/>
.shop-item{
display: flex;
padding: 15rpx;
border: 1prx solid #efefef;
border-radius: 8rpx;
margin: 15rpx;
box-shadow: 1rpx 1rpx 15prx #ddd;
}
.thumb image{
width: 250rpx;
height: 250rpx;
display: block;
margin-right: 15rpx;
}
.info{
display: flex;
flex-direction: column;
justify-content: space-around;
font-size: 24rpx;
}
.shop-title{
font-weight: bold;
}
function splitPhone(str) {
if (str.length != 11) return str
var arr = str.split('')
arr.splice(3,0,'-')
arr.splice(8,0,'-')
return str.join('')
}
module.exports = {
splitPhone: splitPhone
}