1.0.1 • Published 1 month ago

@tanzhenxing/zx-cart-bar v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

CartBar 购物车底部栏

购物车底部栏组件,用于展示购物车商品信息、价格统计和结算功能。支持全选操作、商品数量显示、价格计算等功能。

引入

import zxCartBar from '@/components/zx-cart-bar/zx-cart-bar.vue'

代码演示

基础用法

<template>
  <zx-cart-bar
    :total-price="totalPrice"
    :checked-count="checkedCount"
    :total-count="totalCount"
    :all-checked="allChecked"
    @submit="handleSubmit"
    @check-all="handleCheckAll"
  />
</template>

<script setup>
import { ref } from 'vue'

const totalPrice = ref(12800) // 单位:分
const checkedCount = ref(2)
const totalCount = ref(5)
const allChecked = ref(false)

const handleSubmit = (data) => {
  console.log('结算', data)
}

const handleCheckAll = (checked) => {
  console.log('全选状态', checked)
  allChecked.value = checked
}
</script>

显示原价和优惠信息

<template>
  <zx-cart-bar
    :total-price="totalPrice"
    :original-price="originalPrice"
    :checked-count="checkedCount"
    :show-original-price="true"
    discount-text="已优惠 ¥20.00"
    @submit="handleSubmit"
  />
</template>

<script setup>
const totalPrice = ref(12800) // ¥128.00
const originalPrice = ref(14800) // ¥148.00
const checkedCount = ref(2)
</script>

自定义按钮

<template>
  <zx-cart-bar
    :total-price="totalPrice"
    :checked-count="checkedCount"
    button-text="立即购买"
    button-type="primary"
    :button-round="false"
  >
    <template #button>
      <view class="custom-buttons">
        <zx-button type="warning" size="small" @click="addToFavorites">收藏</zx-button>
        <zx-button type="danger" @click="buyNow">立即购买</zx-button>
      </view>
    </template>
  </zx-cart-bar>
</template>

添加提示信息

<template>
  <zx-cart-bar
    :total-price="totalPrice"
    :checked-count="checkedCount"
    tip="满99元免运费,还差21元"
    tip-icon="info-circle"
  />
</template>

隐藏部分功能

<template>
  <zx-cart-bar
    :total-price="totalPrice"
    :checked-count="checkedCount"
    :show-check-all="false"
    :show-count="false"
    price-label="总计:"
  />
</template>

自定义左侧内容

<template>
  <zx-cart-bar
    :total-price="totalPrice"
    :checked-count="checkedCount"
  >
    <template #left>
      <view class="custom-left">
        <zx-icon name="gift" color="#ff6900" />
        <text>有礼品</text>
      </view>
    </template>
  </zx-cart-bar>
</template>

API

Props

参数说明类型默认值
total-price总价格,单位为分number | string0
original-price原价格,单位为分number | string0
checked-count已选商品数量number | string0
total-count商品总数量number | string0
all-checked是否全选booleanfalse
show-check-all是否显示全选booleantrue
show-count是否显示商品数量booleantrue
show-price是否显示价格booleantrue
show-original-price是否显示原价booleanfalse
price-label价格标签文字string'合计:'
currency货币符号string'¥'
button-text按钮文字string''
discount-text优惠信息文字string''
tip顶部提示文字string''
tip-icon提示图标名称string''
button-type按钮类型string'danger'
button-color按钮颜色string''
button-round按钮是否圆角booleantrue
checkbox-color复选框颜色string'#ff6900'
disabled是否禁用booleanfalse
loading是否加载中booleanfalse
safe-area-inset-bottom是否开启底部安全区适配booleantrue
decimal-length价格小数位数number | string2
custom-class自定义类名string''
custom-style自定义样式object{}

Events

事件名说明回调参数
submit点击结算按钮时触发{ checkedCount, totalPrice, originalPrice }
check-all点击全选时触发checked: boolean
update:all-checked全选状态更新时触发checked: boolean

Slots

名称说明
tip自定义顶部提示内容
left自定义左侧内容
button自定义按钮区域
bottom自定义底部内容

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式。

名称默认值描述
--zx-cart-bar-height100rpx主体高度
--zx-cart-bar-padding20rpx 32rpx内边距
--zx-cart-bar-background#fff背景色
--zx-cart-bar-border-color#ebedf0边框颜色
--zx-cart-bar-price-color#ee0a24价格颜色
--zx-cart-bar-text-color#323233文字颜色
--zx-cart-bar-tip-background#fff7e8提示背景色
--zx-cart-bar-tip-color#ff6900提示文字颜色

注意事项

  1. 价格单位统一使用分,组件内部会自动转换为元显示
  2. 组件默认固定在页面底部,使用时需要给页面内容添加底部边距
  3. 在小程序中使用时,建议开启 safe-area-inset-bottom 以适配底部安全区域
  4. 全选状态需要通过 v-model:all-checked 或监听 check-all 事件来同步

常见问题

如何处理价格计算?

// 价格以分为单位传入
const totalPrice = ref(12800) // 表示 ¥128.00

// 如果后端返回的是元,需要转换
const priceInYuan = 128.00
const totalPrice = ref(Math.round(priceInYuan * 100))

如何实现全选功能?

<template>
  <zx-cart-bar
    v-model:all-checked="allChecked"
    @check-all="handleCheckAll"
  />
</template>

<script setup>
const allChecked = ref(false)

const handleCheckAll = (checked) => {
  // 更新商品列表的选中状态
  cartList.value.forEach(item => {
    item.checked = checked
  })
  
  // 重新计算价格和数量
  calculateTotal()
}
</script>

如何添加页面底部边距?

.page-content {
  padding-bottom: 120rpx; /* 购物车栏高度 + 安全区域 */
}