微信小程序(黑马优购:商品列表)

1.渲染商品列表并美化布局

复制代码
  <view class="goods-list">
      <block v-for="(goods,i) in goodsList" :key="i">
          <view class="goods-item">
            <!-- 左侧的盒子 -->
            <view class="goods-item-left">
              <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
            </view>
            <!-- 右侧的盒子 -->
            <view class="goods-item-right">
              <!-- 商品的名字 -->
              <view class="goods-name">{{goods.goods_name}}</view>
              <view class="goods-info-box">
                  <view class="goods-price">¥{{goods.goods_price}}</view>
              </view>
            </view>
          </view>
      </block>
  </view>

export default {
    data() {
      return {
        //请求参数对象
        queryObj: {
          //查询关键词
          query: '',
          //商品分类id
          cid: '',
          //页码值
          pagenum: 1,
          //每页显示多少条数据
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        defaultPic: 'https://img1.baidu.com/it/u=347909717,3772510335&fm=253&fmt=auto&app=138&f=JPEG?w=609&h=500'
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      
      this.getGoodsList()
      
    },
    methods: {
      //获取商品列表数据的方法
      async getGoodsList(){
        const{data: res} = await  uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        if(res.meta.status !== 200){
          return uni.$showMsg()
        }
        this.goodsList = res.message.goods
        this.total = res.message.total
      }
    }
    

.goods-item{
  display: flex;
  padding: 10px 5px;
  
  border-bottom: 1px solid #f0f0f0;
  
  .goods-item-left{
    margin-left: 5px;
    .goods-pic{
      width: 100px;
      height: 100px;
      display: block;
    }
  }
  
  .goods-item-right{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    
    .goods-name{
      font-size: 13px;
    }
    .goods-info-box{
      .goods-price{
        color: #C00000;
        font-size: 16px;
      }
    }
    
    
  }

2.封装商品列表

复制代码
   <view class="goods-list">
      <view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>

<view class="goods-item">
    <!-- 左侧的盒子 -->
    <view class="goods-item-left">
      <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
    </view>
    <!-- 右侧的盒子 -->
    <view class="goods-item-right">
      <!-- 商品的名字 -->
      <view class="goods-name">{{goods.goods_name}}</view>
      <view class="goods-info-box">
        <view class="goods-price">¥{{goods.goods_price | tofixed}}</view>
      </view>
    </view>
  </view>

<script>
  export default {
    props: {
      goods: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        // 默认的图片
        defaultPic: 'https://img0.baidu.com/it/u=596425167,3189264920&fm=253&fmt=auto&app=138&f=JPEG?w=361&h=500'
      };
    }

  }
</script>

3.过滤器处理商品价格

复制代码
 <view class="goods-info-box">
        <view class="goods-price">¥{{goods.goods_price | tofixed}}</view>
      </view>

filters: {
      tofixed(num) {
        //保留两位小数
        return  Number(num).toFixed(2)
      }
    }

4.上拉加载更多

pages.json

复制代码
{
          "path" : "goods_list/goods_list",
          "style" : 
          {
            //上拉触底
            "onReachBottomDistance": 150
          }
        },

async getGoodsList(cb) {
     
        const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)

        if (res.meta.status !== 200) return uni.$showMsg()
          
        //拼接数据
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      },

 onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕!')


      // 让页码值自增+1
      this.queryObj.pagenum++
      this.getGoodsList()
    },

5.节流阀

复制代码
 async getGoodsList(cb) {
        // 打开节流阀
        this.isloading = true}

 onReachBottom() {
      //防止发起额外的请求
      if (this.isloading) return

数据加载完毕不会发起额外请求

复制代码
 //判断是否还有下一页数据
      //当前页码值 * 每页显示多少条数据 >= 总数条数
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
        return uni.$showMsg('数据加载完毕')
      }

6.下拉刷新

复制代码
 {
          "path" : "goods_list/goods_list",
          "style" : 
          {
            //上拉触底
            "onReachBottomDistance": 150,
            //开启下拉刷新
            "enablePullDownRefresh": true,
            "backgroundColor": "#F8F8F8"
          }
        },

onPullDownRefresh() {
      // 重置关键数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []

      // 重新发起数据请求
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }

7.跳转到商品详情页

复制代码
  gotoDetail(goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
相关推荐
2501_915918414 分钟前
Web 前端可视化开发工具对比 低代码平台、可视化搭建工具、前端可视化编辑器与在线可视化开发环境的实战分析
前端·低代码·ios·小程序·uni-app·编辑器·iphone
2501_9151063230 分钟前
iOS 使用记录和能耗监控实战,如何查看电池电量消耗、App 使用时长与性能数据(uni-app 开发调试必备指南)
android·ios·小程序·uni-app·cocoa·iphone·webview
じòぴé南冸じょうげん9 小时前
小程序的project.private.config.json是无依赖文件,那可以删除吗?
前端·小程序·json
2501_916013749 小时前
HTTPS 抓包难点分析,从端口到工具的实战应对
网络协议·http·ios·小程序·https·uni-app·iphone
2501_9159184112 小时前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张12 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
微三云-轩13 小时前
区块链:重构企业数字化的信任核心与创新动力
人工智能·小程序·区块链·生活·我店
阿隆_趣编程15 小时前
为了方便相亲,我用AI写了一款小程序
前端·javascript·微信小程序
2501_915918411 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
黑马源码库miui520861 天前
JAVA同城打车小程序APP打车顺风车滴滴车跑腿源码微信小程序打车源码
java·微信·微信小程序·小程序·uni-app