123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- <template>
- <view class="cart">
- <template v-if="!isEmpty">
- <template v-if="listType !== 'delete'">
- <!-- 商品统计 / 批量删除商品 -->
- <view class="cart-top">
- <view class="count">共{{ kindCount }}件商品</view>
- <view class="btn" @click="showDelManager">删除</view>
- </view>
- <!-- TODO -->
- <view class="receive-coupon">
- <view class="tip-text">还差¥200可用“满200元减50元”优惠券</view>
- <view class="btn" @click="couponVisible = true">领券</view>
- </view>
- </template>
- <view>
- <!-- 购物车列表 -->
- <template v-for="(cart, index) in goodsList">
- <cm-cart-product
- class="product-list"
- :listType="listType"
- :key="index"
- :data="cart"
- vkey="productList"
- @chooseAll="chooseAll"
- @chooseOne="chooseOne"
- ></cm-cart-product>
- </template>
- <!-- 失效商品列表 -->
- <cm-cart-product
- class="product-list"
- :key="index"
- :data="failureList"
- listType="expired"
- ></cm-cart-product>
- </view>
- <!-- 底部按钮 -->
- <view class="footer">
- <view class="radio" @click="checkAll">
- <text class="iconfont" :class="[isCheckAll ? 'icon-xuanze' : 'icon-weixuanze']"></text>
- <text class="tip">全选</text>
- </view>
- <template v-if="!isshowDelbtn">
- <view class="center">
- <view class="row">
- <text>总价:</text> <text class="total-price">¥{{ allPrice | NumFormat }}</text>
- </view>
- <!-- TODO -->
- <view class="row">
- <text>共减</text> <text class="discounted-price">¥200.00</text>
- <text @click="showDiscountedDetail">优惠明细</text>
- <text
- class="iconfont"
- :class="showDitail ? 'tui-icon-arrowdown' : 'tui-icon-arrowup'"
- ></text>
- </view>
- </view>
- <view class="submit" @click="toConfirmation">去结算({{ allCount }})</view>
- </template>
- <view v-else class="footer-del">
- <view class="btn btn-cancel" @tap.stop="hideDelManage">取消</view>
- <view class="btn btn-confirm" @tap.stop="deleteList">删除</view>
- </view>
- </view>
- </template>
- <view class="empty" v-else>
- <cm-empty :image="StaticUrl + 'icon-empty-cart.png'" message="购物车空空的,快去逛逛吧~"></cm-empty>
- </view>
- <!-- 优惠券列表 TODO-->
- <cm-coupon-list
- title="优惠券"
- listType="search"
- :visible="couponVisible"
- @close="closeCouponList"
- ></cm-coupon-list>
- <!-- 优惠明细 -->
- <cm-drawer
- title="优惠明细"
- :visible="showDitail"
- position="bottom"
- :offset="100"
- @close="showDitail = false"
- zIndex="99"
- >
- <template>
- <view class="discounted-ditail">
- <view class="row"> <text>商品总额</text> <text>¥450.00</text> </view>
- <view class="row"> <text>促销满减</text> <text class="red">-¥180.00</text> </view>
- <view class="row"> <text>优惠券</text> <text class="red">-¥20.00</text> </view>
- <view class="row total"> <text>总计</text> <text>¥250.00</text> </view>
- <view class="tip"> 实际订单金额以结算页为准 </view>
- </view>
- </template>
- </cm-drawer>
- <!-- 操作弹窗 -->
- <tui-modal
- :show="modal"
- @click="handleClick"
- @cancel="hideModal"
- :content="contentModalText"
- color="#333"
- :size="32"
- shape="circle"
- :maskClosable="false"
- ></tui-modal>
- </view>
- </template>
- <script>
- import authorize from '@/common/authorize.js'
- import CmCouponList from '@/components/cm-module/cm-coupon-list/cm-coupon-list'
- import CmDrawer from '@/components/cm-module/cm-drawer/cm-drawer.vue'
- import CmCartProduct from '@/components/cm-module/cm-cart-product/cm-cart-product.vue'
- import CmEmpty from '@/components/cm-module/cm-empty/cm-empty.vue'
- import { mapGetters, mapActions, mapMutations } from 'vuex'
- export default {
- components: {
- CmCouponList,
- CmDrawer,
- CmCartProduct,
- CmEmpty
- },
- data() {
- return {
- StaticUrl: this.$Static,
- CustomBar: this.CustomBar, // 顶部导航栏高度
- popupShow: false,
- handlerPros: {}, //监听单挑促销商品
- isCheckAll: false, //是否全选
- allPrice: 0, //所有价格
- totalOriginalPrice: 0, //所有原价价
- reducedPrice: 0, //满减
- skeletonShow: true,
- isshowDelbtn: false,
- scrollHeight: 'auto',
- hasNextPage: false,
- modal: false,
- contentModalText: '',
- deleteType: 0,
- couponVisible: false,
- showDitail: false,
- listType: 'list'
- }
- },
- onLoad() {
- this.setScrollHeight()
- },
- computed: {
- ...mapGetters([
- 'hasLogin',
- 'isIphoneX',
- 'isEmpty',
- 'goodsList',
- 'failureList',
- 'kindCount',
- 'userId',
- 'cartIds'
- ]),
- //被选中的产品数量
- allCount() {
- return this.cartIds.length
- }
- },
- filters: {
- NumFormat(value) {
- //处理金额
- return Number(value).toFixed(2)
- },
- totalprice(val, count) {
- //单件商品的价格 × 数量
- return (val * count).toFixed(2)
- }
- },
- watch: {
- //深度监听所有数据,每次改变重新计算总价和总数
- goodsList: {
- deep: true,
- handler(val, oldval) {
- this.totalPeice()
- }
- }
- },
- //下拉刷新
- onPullDownRefresh() {
- this.initCart().finally(() => {
- setTimeout(() => {
- uni.stopPullDownRefresh()
- }, 2000)
- })
- },
- onShow() {
- if (this.hasLogin) {
- this.initData()
- } else {
- this.$api.redirectTo('/pages/login/login')
- }
- },
- onHide() {
- this.saveCartIds([])
- },
- methods: {
- ...mapActions('cart', ['initCart', 'updateShoppogCount', 'removeFromCart', 'removeFailureFromCart']),
- ...mapMutations('cart', [
- 'selectProduct',
- 'selectAllShopProduct',
- 'selectAllProduct',
- 'saveCartIds',
- 'selectFailure',
- 'selectAllFailure'
- ]),
- initData() {
- this.initCart().finally(() => {
- this.skeletonShow = false
- })
- },
- // 关闭优惠券弹窗
- closeCouponList() {
- this.couponVisible = false
- },
- // 活动价弹窗
- clickPopupShow(pros, type) {
- if (pros.ladderList.length > 0) {
- this.popupShow = true
- this.handlerPros = pros
- }
- },
- // 优惠明细 TODO
- showDiscountedDetail() {
- this.showDitail = true
- },
- // 勾选单个失效商品
- ischeckFailure(failure) {
- this.selectFailure({
- productId: failure.productId,
- checked: !failure.productsChecked
- })
- this.getCheckedProductId()
- },
- // 勾选供应商下所有商品
- chooseAll() {
- this.isSelectAll()
- },
- // 勾选单核商品
- chooseOne() {
- this.isSelectAll()
- },
- // 勾选全部商品
- checkAll() {
- this.isCheckAll = !this.isCheckAll
- this.selectAllProduct(this.isCheckAll)
- // 删除商品的全选也要选中失效商品
- if (this.isshowDelbtn) {
- this.selectAllFailure(this.isCheckAll)
- }
- this.isSelectAll()
- },
- // 判断是否全选商品
- isSelectAll() {
- this.isCheckAll = this.goodsList.every(shop => shop.checked)
- this.getCheckedProductId()
- },
- // 获取勾选商品的id
- getCheckedProductId() {
- const cartIds = []
- this.goodsList.forEach(shop => {
- const ids = shop.productList.reduce((cartIds, prod) => {
- if (prod.productsChecked) cartIds.push(prod.cartId)
- return cartIds
- }, [])
- cartIds.push(...ids)
- })
- if (this.isshowDelbtn) {
- const ids = this.failureList.reduce((cartIds, prod) => {
- if (prod.productsChecked) cartIds.push(prod.cartId)
- return cartIds
- }, [])
- cartIds.push(...ids)
- }
- console.log(cartIds)
- this.saveCartIds(cartIds)
- },
- //计算总价格,每次调用此方法,将初始值为0,遍历价格并累加
- totalPeice() {
- const priceObj = this.goodsList.reduce(
- (priceObj, shop) => {
- shop.productList.forEach(prod => {
- if (prod.productsChecked) {
- priceObj.totalOriginalPrice += prod.price * prod.productCount
- // 单品满减
- if (prod.promotion && prod.promotion.type * 1 === 1 && prod.promotion.mode * 1 === 2) {
- // 单品满减-重新计算供应商总价/满减金额
- if (prod.price * prod.productCount >= prod.promotion.touchPrice) {
- priceObj.reducedPrice += prod.promotion.reducedPrice
- }
- }
- }
- })
- priceObj.allPrice = priceObj.totalOriginalPrice - priceObj.reducedPrice
- return priceObj
- },
- {
- totalOriginalPrice: 0,
- reducedPrice: 0,
- allPrice: 0
- }
- )
- this.allPrice = priceObj.allPrice
- },
- //商品数量加加
- changeCountAdd(item, pros) {
- const productCount = pros.productCount + 1
- this.updateShoppogCount({
- cartId: pros.cartId,
- productCount
- })
- },
- //商品数量减减
- changeCountSub(item, pros) {
- let productCount = pros.productCount
- if (productCount <= 1) {
- productCount = 1
- this.$util.msg('购买数量不能少于1', 2000)
- return
- } else {
- productCount--
- }
- this.updateShoppogCount({
- cartId: pros.cartId,
- productCount
- })
- },
- //输入商品数量更新
- changeInput(pros) {},
- //输入商品数量更新
- changeNumber(e, item, pros) {
- let _value = Math.abs(Number(e.detail.value))
- let productCount = pros.productCount
- if (_value <= 1) {
- this.$util.msg('购买数量不能少于1', 2000)
- productCount = 1
- } else {
- productCount = _value
- }
- this.updateShoppogCount({
- cartId: pros.cartId,
- productCount
- })
- },
- //显示删除商品管理
- showDelManager() {
- this.isshowDelbtn = true
- this.listType = 'delete'
- },
- //隐藏删除商品管理
- hideDelManage() {
- this.selectAllFailure(false)
- this.getCheckedProductId()
- this.isshowDelbtn = false
- this.listType = 'list'
- },
- //删除购物车商品
- deleteList() {
- if (this.cartIds.length <= 0) {
- return this.$util.msg('请选择要删除的商品~', 2000)
- } else {
- this.modal = true
- this.contentModalText = '确定删除选中的商品吗?'
- this.deleteType = 1
- }
- },
- // 清空失效商品
- deletefailureList() {
- this.modal = true
- this.contentModalText = '确定清除所有失效商品吗?'
- this.deleteType = 2
- },
- //从购物车移除商品
- handleClick(e) {
- if (e.index !== 1) return (this.modal = false)
- if (this.deleteType === 1) {
- // 删除所有勾选的商品
- this.removeFromCart().finally(() => {
- this.isshowDelbtn = false
- })
- } else {
- //一键删除失效商品
- this.removeFailureFromCart().finally(() => {
- this.isshowDelbtn = false
- this.listType = 'list'
- })
- }
- this.modal = false
- },
- //跳转确认订单页面
- toConfirmation() {
- if (this.cartIds.length <= 0) {
- return this.$util.msg('请先选择结算商品~', 2000)
- }
- let cartPramsData = {
- allPrice: this.allPrice,
- allCount: this.allCount,
- cartIds: this.cartIds.join(','),
- productCount: ''
- }
- this.$api.navigateTo(`/pages/user/order/create-order?data=${JSON.stringify({ data: cartPramsData })}`)
- },
- hideModal() {
- this.modal = false
- },
- navToListPage(item) {
- this.$api.navigateTo(`/pages/goods/product?productId=${item.productId}`)
- },
- // 窗口高度-footer高度
- setScrollHeight() {
- const { windowHeight, pixelRatio } = uni.getSystemInfoSync()
- setTimeout(() => {
- const query = uni.createSelectorQuery().in(this)
- query.selectAll('.footer').boundingClientRect()
- query.exec(res => {
- this.windowHeight = windowHeight
- if (res[0][0]) {
- this.scrollHeight = windowHeight - res[0][0].height
- }
- })
- }, 500)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .cart {
- padding-bottom: 124rpx;
- background: #f7f7f7;
- min-height: 100%;
- box-sizing: border-box;
- .cart-top {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 0 24rpx;
- width: 750rpx;
- height: 80rpx;
- background: #f7f7f7;
- box-sizing: border-box;
- .count {
- font-size: 30rpx;
- color: #333333;
- }
- .btn {
- display: flex;
- justify-content: center;
- align-items: center;
- width: 88rpx;
- height: 42rpx;
- background: #fff8fd;
- border: 1rpx solid #ff457b;
- border-radius: 24rpx;
- font-size: 26rpx;
- color: #ff457b;
- }
- }
- .receive-coupon {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 24rpx;
- background: #fff;
- .tip-text {
- font-size: 26rpx;
- color: #ff457b;
- }
- .btn {
- width: 88rpx;
- height: 42rpx;
- background: linear-gradient(270deg, #f83c6c 0%, #fc32b4 100%);
- border-radius: 28rpx;
- font-size: 26rpx;
- color: #ffffff;
- text-align: center;
- line-height: 42rpx;
- }
- }
- .footer {
- position: fixed;
- bottom: 0;
- left: 0;
- z-index: 899;
- display: flex;
- justify-content: space-between;
- align-items: center;
- width: 100%;
- height: 100rpx;
- box-sizing: border-box;
- padding: 0 24rpx;
- border-top: 1rpx solid #eee;
- background: #ffffff;
- .radio {
- font-size: 36rpx;
- color: #b2b2b2;
- .icon-xuanze {
- color: #f83c6c;
- }
- .tip {
- font-size: 30rpx;
- color: #333333;
- margin-left: 6rpx;
- }
- }
- .row {
- &:nth-child(1) {
- font-size: 26rpx;
- color: #333333;
- .total-price {
- color: #ff457b;
- }
- }
- &:nth-child(2) {
- margin-top: 6rpx;
- font-size: 24rpx;
- color: #ff457b;
- }
- .discounted-price {
- margin-right: 32rpx;
- }
- }
- .submit {
- width: 210rpx;
- height: 80rpx;
- background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
- border-radius: 40rpx;
- font-size: 30rpx;
- color: #ffffff;
- text-align: center;
- line-height: 80rpx;
- }
- .footer-del {
- width: 420rpx;
- height: 100rpx;
- position: absolute;
- padding-left: 200rpx;
- background: #ffffff;
- right: 0;
- top: 0;
- z-index: 1000;
- box-sizing: border-box;
- padding: 10rpx 0;
- display: flex;
- &.show {
- animation: showDelbtn 0s linear both;
- }
- &.none {
- animation: hideDelbtn 0s linear both;
- }
- .btn {
- flex: 1;
- margin: 0 8rpx;
- height: 100%;
- line-height: 80rpx;
- font-size: $font-size-28;
- color: #ffffff;
- text-align: center;
- float: left;
- border-radius: 40rpx;
- }
- .btn.btn-cancel {
- background: #f7f7f7;
- color: #b2b2b2;
- }
- .btn.btn-confirm {
- background: $btn-confirm;
- color: #ffffff;
- }
- @keyframes showDelbtn {
- 0% {
- transform: translateX(0);
- }
- 100% {
- transform: translateX(-100%);
- }
- }
- @keyframes hideDelbtn {
- 0% {
- transform: translateX(-100%);
- }
- 100% {
- transform: translateX(0);
- }
- }
- }
- }
- .discounted-ditail {
- padding: 52rpx 8rpx 0;
- .row {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 12rpx 0;
- text {
- font-size: 30rpx;
- color: #333333;
- &.red {
- color: #f94b4b;
- }
- }
- &.total {
- font-size: 30rpx;
- font-weight: 600;
- }
- }
- .tip {
- padding: 12rpx 0;
- font-size: 26rpx;
- color: #999999;
- }
- }
- .empty{
- height: 80vh;
- }
- }
- </style>
|