123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- <template>
- <view class="cart" :class="{ hasPaddingTop: cartTopFixed }">
- <tui-skeleton v-if="isRequest" loadingType="2"></tui-skeleton>
- <template v-if="!isEmpty">
- <template v-if="!isshowDelbtn">
- <!-- 商品统计 / 批量删除商品 -->
- <view class="cart-top" :class="{ fixed: cartTopFixed }">
- <view class="count">共{{ kindCount }}件商品</view>
- <view class="btn" @click="showDelManager">删除</view>
- </view>
- <!-- TODO -->
- <view class="receive-coupon">
- <view class="tip-text">{{ couponTipText }}</view>
- <view class="btn" @click="receiveCoupon">领券</view>
- </view>
- </template>
- <!-- 购物车列表 -->
- <template v-for="(cart, index) in goodsList">
- <cm-cart-product
- class="product-list"
- :listType="listType"
- :key="index"
- :data="cart"
- vkey="productList"
- @chooseAll="chooseAllProduct"
- @chooseOne="chooseOneProduct"
- ></cm-cart-product>
- </template>
- <!-- 失效商品列表 -->
- <cm-cart-product
- class="product-list"
- :key="index"
- :data="failureList"
- :listType="failureListType"
- @chooseOne="chooseFailureProduct"
- v-if="failureList.length > 0"
- ></cm-cart-product>
- <!-- 底部按钮 -->
- <view class="footer">
- <view class="radio" @click="chooseAll">
- <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" v-if="currentCouponIndex > -1">
- <text>共减</text>
- <text class="discounted-price">¥{{ currentCoupon.couponAmount | NumFormat }}</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"
- :couponList="couponList"
- :visible="couponVisible"
- :btnUseType="2"
- @close="closeCouponList"
- @tabClick="couponListTabClick"
- @couponClick="couponClick"
- :tabs="couponListTabs"
- ></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>¥{{ allPrice | NumFormat }}</text>
- </view>
- <view class="row">
- <text>促销满减</text> <text class="red">-¥{{ discountedPrice | NumFormat }}</text>
- </view>
- <view class="row" v-if="currentCouponIndex > -1">
- <text>优惠券</text> <text class="red">-¥{{ currentCoupon.couponAmount | NumFormat }}</text>
- </view>
- <view class="row total">
- <text>总计</text> <text>¥{{ finallyPrice | NumFormat }}</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 { isIntersect } from '@/common/util.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'
- import { fetchSelectedProducts, findCouponBySelected, canUseCoupon, getCountPrice, couponSort } from '@/common/couponUtil.js'
- 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, //满减
- isshowDelbtn: false,
- scrollHeight: 'auto',
- modal: false,
- contentModalText: '',
- couponVisible: false,
- showDitail: false,
- listType: 'normal',
- failureListType: 'expired',
- scrollTop: 0,
- isRequest: true,
- currentCouponIndex: -1,
- selectedPoducts: [], //已勾选商品列表
- receiveCouponList: [], // 已领取优惠券
- ableCouponList: [], // 可领取优惠券
- canUseCouponList: [], // 当前选中商品可使用的优惠券
- currentTabIndex: 0
- }
- },
- computed: {
- ...mapGetters([
- 'hasLogin',
- 'isIphoneX',
- 'isEmpty',
- 'goodsList',
- 'failureList',
- 'kindCount',
- 'userId',
- 'cartIds',
- 'productIds'
- ]),
- //被选中的产品数量
- allCount() {
- return this.cartIds.length
- },
- cartTopFixed() {
- return this.scrollTop > 0 && !this.isshowDelbtn
- },
- currentCoupon() {
- return this.canUseCouponList[this.currentCouponIndex]
- },
- nextCoupon() {
- if (this.currentCouponIndex <= 0) {
- return this.currentCoupon
- } else {
- return this.canUseCouponList[this.currentCouponIndex - 1]
- }
- },
- discountedPrice() {
- return 0
- },
- finallyPrice() {
- if (this.currentCoupon) {
- const finallyPrice = this.allPrice - this.discountedPrice - this.currentCoupon.couponAmount
- return finallyPrice < 0 ? 0 : finallyPrice
- }
- return this.allPrice - this.discountedPrice
- },
- couponTipText() {
- if (this.currentCouponIndex === -1) return ''
- if (this.currentCoupon === this.nextCoupon) {
- if (this.nextCoupon.noThresholdFlag === 1) {
- return `已享“减${this.currentCoupon.couponAmount}元”优惠券`
- }
- return `已享“满${this.currentCoupon.touchPrice}元减${this.currentCoupon.couponAmount}元”优惠券`
- }
- return `还差¥${this.nextCoupon.touchPrice - getCountPrice(this.selectedPoducts, this.nextCoupon)}元可用“满${this.nextCoupon.touchPrice}元减${
- this.nextCoupon.couponAmount
- }元”优惠券`
- },
- couponList() {
- if (this.currentTabIndex === 0) {
- return couponSort(this.receiveCouponList)
- } else {
- return couponSort(this.ableCouponList)
- }
- },
- couponListTabs() {
- const tabs = []
- const receiveCount = this.receiveCouponList.length
- const ableCouponCount = this.ableCouponList.length
- if (receiveCount > 0) {
- tabs.push({ name: `已领取优惠券(${receiveCount})` })
- } else {
- tabs.push({ name: '已领取优惠券' })
- }
- if (ableCouponCount > 0) {
- tabs.push({ name: `可领取优惠券(${ableCouponCount})` })
- } else {
- tabs.push({ name: '可领取优惠券' })
- }
- return tabs
- }
- },
- 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()
- this.resetCouponUtil()
- }
- },
- isshowDelbtn(val) {
- if (val) {
- this.failureListType = 'expired delete'
- this.listType = 'normal delete'
- } else {
- this.failureListType = 'expired'
- this.listType = 'normal'
- }
- }
- },
- //下拉刷新
- onPullDownRefresh() {
- this.initCart().finally(() => {
- setTimeout(() => {
- uni.stopPullDownRefresh()
- }, 2000)
- })
- },
- onPageScroll(e) {
- this.scrollTop = e.scrollTop
- },
- onShow() {
- if (this.hasLogin) {
- this.initData()
- } else {
- this.$api.redirectTo('/pages/login/login')
- }
- },
- onLoad() {
- this.setScrollHeight()
- },
- methods: {
- ...mapActions('cart', ['initCart', 'removeFromCart']),
- ...mapMutations('cart', ['selectAllProduct', 'saveCartIds', 'selectAllFailure', 'saveProductIds']),
- initData() {
- this.saveCartIds([]) // 清空已选项
- this.saveProductIds([]) // 清空已选项
- this.isCheckAll = false
- this.isshowDelbtn = false
- // 初始化购物车后也要获取商品下的优惠券列表
- this.initCart()
- .then(() => {
- this.getCouponList()
- })
- .finally(() => {
- this.isRequest = false
- })
- },
- // 获取优惠券列表
- getCouponList() {
- if (this.isEmpty) return
- const productIds = []
- this.goodsList.forEach(shop => shop.productList.forEach(product => productIds.push(product.productId)))
- this.CouponService.GetCouponByProductIds({ userId: this.userId, productIds: productIds.join(',') }).then(
- res => {
- this.receiveCouponList = res.data.receiveCouponList
- this.ableCouponList = res.data.ableCouponList
- this.resetCouponUtil()
- }
- )
- },
- resetCouponUtil() {
- if(this.receiveCouponList.length <= 0) return
- this.selectedPoducts = fetchSelectedProducts(this.goodsList)
- this.canUseCouponList = findCouponBySelected(this.productIds, this.receiveCouponList)
- this.currentCouponIndex = canUseCoupon(0, this.productIds, this.canUseCouponList, this.selectedPoducts)
- },
- couponListTabClick(index) {
- this.currentTabIndex = index
- },
- couponClick() {
- // 领取优惠券
- if (this.currentTabIndex === 1) {
- this.getCouponList()
- }else{
- this.couponVisible = false
- }
- },
- // 领取优惠券弹窗
- receiveCoupon(){
- this.couponVisible = true
- this.getCouponList()
- },
- // 勾选单个失效商品
- chooseFailureProduct() {
- this.isSelectAll()
- },
- // 勾选供应商下所有商品
- chooseAllProduct() {
- this.isSelectAll()
- },
- // 勾选单核商品
- chooseOneProduct() {
- this.isSelectAll()
- },
- // 勾选全部商品
- chooseAll() {
- 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)
- if (this.isshowDelbtn) {
- this.isCheckAll = this.isCheckAll && this.failureList.every(product => product.productsChecked)
- }
- this.getCheckedProductId()
- },
- // 获取勾选商品的id
- getCheckedProductId() {
- const cartIds = []
- const productIds = []
- // 获取所有有效商品cartId
- this.goodsList.forEach(shop => {
- const ids = shop.productList.reduce((cartIds, prod) => {
- if (prod.productsChecked) {
- cartIds.push(prod.cartId)
- productIds.push(prod.productId)
- }
- return cartIds
- }, [])
- cartIds.push(...ids)
- })
- // 获取所有失效商品cartId
- if (this.isshowDelbtn) {
- const ids = this.failureList.reduce((cartIds, prod) => {
- if (prod.productsChecked) cartIds.push(prod.cartId)
- return cartIds
- }, [])
- cartIds.push(...ids)
- }
- this.saveProductIds(productIds) // 保存已选商品
- this.saveCartIds(cartIds)
- this.resetCouponUtil()
- },
- //计算总价格,每次调用此方法,将初始值为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
- },
- //显示删除商品管理
- showDelManager() {
- this.isshowDelbtn = true
- },
- //隐藏删除商品管理
- hideDelManage() {
- this.selectAllFailure(false)
- this.isSelectAll()
- this.isshowDelbtn = false
- },
- //删除购物车商品
- deleteList() {
- if (this.cartIds.length <= 0) {
- return this.$util.msg('请选择要删除的商品~', 2000)
- } else {
- this.modal = true
- this.contentModalText = '确定删除选中的商品吗?'
- }
- },
- //从购物车移除商品
- handleClick(e) {
- if (e.index !== 1) return (this.modal = false)
- // 删除所有勾选的商品
- this.removeFromCart().finally(() => {
- this.isshowDelbtn = false
- })
- this.modal = false
- },
- // 关闭优惠券弹窗
- closeCouponList() {
- this.couponVisible = false
- },
- // 活动价弹窗
- clickPopupShow(pros, type) {
- if (pros.ladderList.length > 0) {
- this.popupShow = true
- this.handlerPros = pros
- }
- },
- // 优惠明细
- showDiscountedDetail() {
- this.showDitail = true
- },
- //跳转确认订单页面
- toConfirmation() {
- if (this.cartIds.length <= 0) {
- return this.$util.msg('请先选择结算商品~', 2000)
- }
- let cartPramsData = {
- allPrice: this.allPrice,
- allCount: this.allCount,
- cartIds: this.cartIds.join(','),
- productIds: this.productIds.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: 100rpx;
- background: #f7f7f7;
- min-height: 100%;
- box-sizing: border-box;
- &.hasPaddingTop {
- padding-top: 80rpx;
- }
- .cart-top {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 0 24rpx;
- width: 750rpx;
- height: 80rpx;
- background: #f7f7f7;
- box-sizing: border-box;
- &.fixed {
- position: fixed;
- top: 0;
- flex: 0;
- z-index: 9999;
- }
- .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 {
- width: 560rpx;
- font-size: 26rpx;
- color: #ff457b;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .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>
|