在电商平台的APP中,用户往往希望快速找到附近的店铺,并且只对有商品的店铺感兴趣。本文将分享如何在uni-app中实现这样的功能:在店铺街显示店铺与用户的距离,并隐藏那些没有商品的店铺。
问题背景:想象一下,你正在寻找附近的电子产品店,但列表中却混杂着一些没有商品的店铺,这无疑增加了你的选择成本。为了让用户有更好的体验,我们需要在店铺街中显示店铺与用户的距离,并过滤掉那些没有商品的店铺。
解决方案:
获取用户位置:
请求店铺数据:
过滤无商品店铺:
计算并显示距离:
示例代码:
javascript// 查找店铺storeMerchantList() { if (this.loading) return; this.loading = true; let rqData = { keyword: this.sotreParam.keyword, page: this.sotreParam.page, limit: 10, order: this.sotreParam.order, category_id: this.sotreParam.category_id, type_id: this.sotreParam.type_id }; if (this.latitude) { rqData.location = this.latitude + ',' + this.longitude; } storeMerchantList(rqData).then(res => { this.count = res.data.count; let tempList = res.data.list; // 过滤掉没有商品的店铺 for (var i = tempList.length - 1; i >= 0; i--) { if (tempList[i].recommend.length == 0) { tempList.splice(i, 1); } } // 将过滤后的店铺列表添加到总列表中 this.storeList = this.storeList.concat(tempList); this.loading = false; this.loadingIcon = false; // 修复去除空白店铺数据导致加载不了下一页的问题 if(this.storeList.length <= 4) { this.sotreParam.page += 1; this.storeMerchantList(); } });},
实施步骤:
总结:通过这个小技巧,我们可以在uni-app的店铺街中显示店铺与用户的距离,并隐藏那些没有商品的店铺,从而提升用户的购物体验。
关于码斯阁:在码斯阁,我们专注于提供实用的技术解决方案,帮助开发者解决实际问题。如果你在开发过程中遇到任何难题,欢迎来找我们聊聊。
扫一扫,关注我们