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

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
        })
      }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app是一种基于Vue.js的跨平台开发框架,可以用于开发微信小程序、H5、App等多个平台的应用。《优购商城项目》是一个基于uni-app开发的微信小程序商城项目。 要实现《优购商城项目》,首先需要配置uni-app的开发环境。可以使用HBuilderX作为开发工具,它提供了丰富的功能和插件来辅助开发。然后需要获取微信小程序的APPID,这是小程序的身份证明,是唯一的。可以在微信开放平台上注册一个小程序并获取到APPID。 接下来,可以在HBuilderX中创建一个新的uni-app项目。在菜单栏中选择文件 -> 项目 -> 新建选择uni-app,填写项目名称并指定项目创建的目录。然后选择微信小程序作为目标平台,并填写小程序的APPID。这样就可以开始在uni-app上开发《优购商城项目》了。 在开发过程中,可以使用uni-app提供的丰富组件和API来实现商城功能,比如商品展示、购物车、订单管理等。可以使用uni-app提供的跨平台能力,一次开发即可在多个平台上运行。在开发完成后,可以将项目打包成微信小程序并在微信开发者工具中进行调试和发布。 通过以上步骤,就可以利用uni-app开发微信小程序实现《优购商城项目》了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [微信小程序--优购商城项目(1)](https://blog.csdn.net/ljn1046016768/article/details/124043924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uniapp微信小程序实现《优购商城项目》](https://blog.csdn.net/qq_64102392/article/details/131212823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值